| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.components.signin; | 5 package org.chromium.components.signin; |
| 6 | 6 |
| 7 import android.Manifest; | 7 import android.Manifest; |
| 8 import android.accounts.Account; | 8 import android.accounts.Account; |
| 9 import android.accounts.AccountManager; | 9 import android.accounts.AccountManager; |
| 10 import android.accounts.AccountManagerCallback; | 10 import android.accounts.AccountManagerCallback; |
| 11 import android.accounts.AccountManagerFuture; | 11 import android.accounts.AccountManagerFuture; |
| 12 import android.accounts.AuthenticatorDescription; | 12 import android.accounts.AuthenticatorDescription; |
| 13 import android.accounts.AuthenticatorException; | 13 import android.accounts.AuthenticatorException; |
| 14 import android.accounts.OperationCanceledException; | 14 import android.accounts.OperationCanceledException; |
| 15 import android.app.Activity; | 15 import android.app.Activity; |
| 16 import android.content.Context; |
| 16 import android.content.pm.PackageManager; | 17 import android.content.pm.PackageManager; |
| 17 import android.os.Build; | 18 import android.os.Build; |
| 18 import android.os.Bundle; | 19 import android.os.Bundle; |
| 19 import android.os.Process; | 20 import android.os.Process; |
| 20 import android.os.SystemClock; | 21 import android.os.SystemClock; |
| 21 | 22 |
| 22 import com.google.android.gms.auth.GoogleAuthException; | 23 import com.google.android.gms.auth.GoogleAuthException; |
| 23 import com.google.android.gms.auth.GoogleAuthUtil; | 24 import com.google.android.gms.auth.GoogleAuthUtil; |
| 24 import com.google.android.gms.auth.GooglePlayServicesAvailabilityException; | 25 import com.google.android.gms.auth.GooglePlayServicesAvailabilityException; |
| 25 | 26 |
| 26 import org.chromium.base.ApiCompatibilityUtils; | 27 import org.chromium.base.ApiCompatibilityUtils; |
| 27 import org.chromium.base.Callback; | 28 import org.chromium.base.Callback; |
| 28 import org.chromium.base.ContextUtils; | |
| 29 import org.chromium.base.Log; | 29 import org.chromium.base.Log; |
| 30 import org.chromium.base.ThreadUtils; | 30 import org.chromium.base.ThreadUtils; |
| 31 import org.chromium.base.annotations.MainDex; | 31 import org.chromium.base.annotations.MainDex; |
| 32 import org.chromium.base.library_loader.LibraryLoader; | 32 import org.chromium.base.library_loader.LibraryLoader; |
| 33 import org.chromium.base.metrics.RecordHistogram; | 33 import org.chromium.base.metrics.RecordHistogram; |
| 34 | 34 |
| 35 import java.io.IOException; | 35 import java.io.IOException; |
| 36 import java.util.concurrent.TimeUnit; | 36 import java.util.concurrent.TimeUnit; |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Default implementation of {@link AccountManagerDelegate} which delegates all
calls to the | 39 * Default implementation of {@link AccountManagerDelegate} which delegates all
calls to the |
| 40 * Android account manager. | 40 * Android account manager. |
| 41 */ | 41 */ |
| 42 @MainDex | 42 @MainDex |
| 43 public class SystemAccountManagerDelegate implements AccountManagerDelegate { | 43 public class SystemAccountManagerDelegate implements AccountManagerDelegate { |
| 44 private final AccountManager mAccountManager; | 44 private final AccountManager mAccountManager; |
| 45 private final Context mApplicationContext; |
| 45 private static final String TAG = "Auth"; | 46 private static final String TAG = "Auth"; |
| 46 | 47 |
| 47 public SystemAccountManagerDelegate() { | 48 public SystemAccountManagerDelegate(Context context) { |
| 48 mAccountManager = AccountManager.get(ContextUtils.getApplicationContext(
)); | 49 mApplicationContext = context.getApplicationContext(); |
| 50 mAccountManager = AccountManager.get(context.getApplicationContext()); |
| 49 } | 51 } |
| 50 | 52 |
| 51 @Override | 53 @Override |
| 52 public Account[] getAccountsByType(String type) { | 54 public Account[] getAccountsByType(String type) { |
| 53 if (!hasGetAccountsPermission()) { | 55 if (!hasGetAccountsPermission()) { |
| 54 return new Account[] {}; | 56 return new Account[] {}; |
| 55 } | 57 } |
| 56 long now = SystemClock.elapsedRealtime(); | 58 long now = SystemClock.elapsedRealtime(); |
| 57 Account[] accounts = mAccountManager.getAccountsByType(type); | 59 Account[] accounts = mAccountManager.getAccountsByType(type); |
| 58 long elapsed = SystemClock.elapsedRealtime() - now; | 60 long elapsed = SystemClock.elapsedRealtime() - now; |
| 59 recordElapsedTimeHistogram("Signin.AndroidGetAccountsTime_AccountManager
", elapsed); | 61 recordElapsedTimeHistogram("Signin.AndroidGetAccountsTime_AccountManager
", elapsed); |
| 60 return accounts; | 62 return accounts; |
| 61 } | 63 } |
| 62 | 64 |
| 63 @Override | 65 @Override |
| 64 public String getAuthToken(Account account, String authTokenScope) throws Au
thException { | 66 public String getAuthToken(Account account, String authTokenScope) throws Au
thException { |
| 65 assert !ThreadUtils.runningOnUiThread(); | 67 assert !ThreadUtils.runningOnUiThread(); |
| 66 assert AccountManagerHelper.GOOGLE_ACCOUNT_TYPE.equals(account.type); | 68 assert AccountManagerHelper.GOOGLE_ACCOUNT_TYPE.equals(account.type); |
| 67 try { | 69 try { |
| 68 return GoogleAuthUtil.getTokenWithNotification( | 70 return GoogleAuthUtil.getTokenWithNotification( |
| 69 ContextUtils.getApplicationContext(), account, authTokenScop
e, null); | 71 mApplicationContext, account, authTokenScope, null); |
| 70 } catch (GoogleAuthException ex) { | 72 } catch (GoogleAuthException ex) { |
| 71 // This case includes a UserRecoverableNotifiedException, but most c
lients will have | 73 // This case includes a UserRecoverableNotifiedException, but most c
lients will have |
| 72 // their own retry mechanism anyway. | 74 // their own retry mechanism anyway. |
| 73 // TODO(bauerb): Investigate integrating the callback with Connectio
nRetry. | 75 // TODO(bauerb): Investigate integrating the callback with Connectio
nRetry. |
| 74 throw new AuthException(false /* isTransientError */, ex); | 76 throw new AuthException(false /* isTransientError */, ex); |
| 75 } catch (IOException ex) { | 77 } catch (IOException ex) { |
| 76 throw new AuthException(true /* isTransientError */, ex); | 78 throw new AuthException(true /* isTransientError */, ex); |
| 77 } | 79 } |
| 78 } | 80 } |
| 79 | 81 |
| 80 @Override | 82 @Override |
| 81 public void invalidateAuthToken(String authToken) throws AuthException { | 83 public void invalidateAuthToken(String authToken) throws AuthException { |
| 82 try { | 84 try { |
| 83 GoogleAuthUtil.clearToken(ContextUtils.getApplicationContext(), auth
Token); | 85 GoogleAuthUtil.clearToken(mApplicationContext, authToken); |
| 84 } catch (GooglePlayServicesAvailabilityException ex) { | 86 } catch (GooglePlayServicesAvailabilityException ex) { |
| 85 throw new AuthException(false /* isTransientError */, ex); | 87 throw new AuthException(false /* isTransientError */, ex); |
| 86 } catch (GoogleAuthException ex) { | 88 } catch (GoogleAuthException ex) { |
| 87 throw new AuthException(false /* isTransientError */, ex); | 89 throw new AuthException(false /* isTransientError */, ex); |
| 88 } catch (IOException ex) { | 90 } catch (IOException ex) { |
| 89 throw new AuthException(true /* isTransientError */, ex); | 91 throw new AuthException(true /* isTransientError */, ex); |
| 90 } | 92 } |
| 91 } | 93 } |
| 92 | 94 |
| 93 @Override | 95 @Override |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 159 } |
| 158 } | 160 } |
| 159 }; | 161 }; |
| 160 // Android 4.4 throws NullPointerException if null is passed | 162 // Android 4.4 throws NullPointerException if null is passed |
| 161 Bundle emptyOptions = new Bundle(); | 163 Bundle emptyOptions = new Bundle(); |
| 162 mAccountManager.updateCredentials( | 164 mAccountManager.updateCredentials( |
| 163 account, "android", emptyOptions, activity, realCallback, null); | 165 account, "android", emptyOptions, activity, realCallback, null); |
| 164 } | 166 } |
| 165 | 167 |
| 166 protected boolean hasGetAccountsPermission() { | 168 protected boolean hasGetAccountsPermission() { |
| 167 return ApiCompatibilityUtils.checkPermission(ContextUtils.getApplication
Context(), | 169 return ApiCompatibilityUtils.checkPermission(mApplicationContext, |
| 168 Manifest.permission.GET_ACCOUNTS, Process.myPid(), Proces
s.myUid()) | 170 Manifest.permission.GET_ACCOUNTS, Process.myPid(), Proces
s.myUid()) |
| 169 == PackageManager.PERMISSION_GRANTED; | 171 == PackageManager.PERMISSION_GRANTED; |
| 170 } | 172 } |
| 171 | 173 |
| 172 protected boolean hasManageAccountsPermission() { | 174 protected boolean hasManageAccountsPermission() { |
| 173 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | 175 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 174 return true; | 176 return true; |
| 175 } | 177 } |
| 176 return ApiCompatibilityUtils.checkPermission(ContextUtils.getApplication
Context(), | 178 return ApiCompatibilityUtils.checkPermission(mApplicationContext, |
| 177 "android.permission.MANAGE_ACCOUNTS", Process.myPid(), Pr
ocess.myUid()) | 179 "android.permission.MANAGE_ACCOUNTS", Process.myPid(), Pr
ocess.myUid()) |
| 178 == PackageManager.PERMISSION_GRANTED; | 180 == PackageManager.PERMISSION_GRANTED; |
| 179 } | 181 } |
| 180 } | 182 } |
| OLD | NEW |