Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: components/signin/core/browser/android/java/src/org/chromium/components/signin/SystemAccountManagerDelegate.java

Issue 2784353002: Android: Remove GetApplicationContext part 2 (Closed)
Patch Set: Fix tests Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
17 import android.content.pm.PackageManager; 16 import android.content.pm.PackageManager;
18 import android.os.Build; 17 import android.os.Build;
19 import android.os.Bundle; 18 import android.os.Bundle;
20 import android.os.Process; 19 import android.os.Process;
21 import android.os.SystemClock; 20 import android.os.SystemClock;
22 21
23 import com.google.android.gms.auth.GoogleAuthException; 22 import com.google.android.gms.auth.GoogleAuthException;
24 import com.google.android.gms.auth.GoogleAuthUtil; 23 import com.google.android.gms.auth.GoogleAuthUtil;
25 import com.google.android.gms.auth.GooglePlayServicesAvailabilityException; 24 import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
26 25
27 import org.chromium.base.ApiCompatibilityUtils; 26 import org.chromium.base.ApiCompatibilityUtils;
28 import org.chromium.base.Callback; 27 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;
46 private static final String TAG = "Auth"; 45 private static final String TAG = "Auth";
47 46
48 public SystemAccountManagerDelegate(Context context) { 47 public SystemAccountManagerDelegate() {
49 mApplicationContext = context.getApplicationContext(); 48 mAccountManager = AccountManager.get(ContextUtils.getApplicationContext( ));
50 mAccountManager = AccountManager.get(context.getApplicationContext());
51 } 49 }
52 50
53 @Override 51 @Override
54 public Account[] getAccountsByType(String type) { 52 public Account[] getAccountsByType(String type) {
55 if (!hasGetAccountsPermission()) { 53 if (!hasGetAccountsPermission()) {
56 return new Account[] {}; 54 return new Account[] {};
57 } 55 }
58 long now = SystemClock.elapsedRealtime(); 56 long now = SystemClock.elapsedRealtime();
59 Account[] accounts = mAccountManager.getAccountsByType(type); 57 Account[] accounts = mAccountManager.getAccountsByType(type);
60 long elapsed = SystemClock.elapsedRealtime() - now; 58 long elapsed = SystemClock.elapsedRealtime() - now;
61 recordElapsedTimeHistogram("Signin.AndroidGetAccountsTime_AccountManager ", elapsed); 59 recordElapsedTimeHistogram("Signin.AndroidGetAccountsTime_AccountManager ", elapsed);
62 return accounts; 60 return accounts;
63 } 61 }
64 62
65 @Override 63 @Override
66 public String getAuthToken(Account account, String authTokenScope) throws Au thException { 64 public String getAuthToken(Account account, String authTokenScope) throws Au thException {
67 assert !ThreadUtils.runningOnUiThread(); 65 assert !ThreadUtils.runningOnUiThread();
68 assert AccountManagerHelper.GOOGLE_ACCOUNT_TYPE.equals(account.type); 66 assert AccountManagerHelper.GOOGLE_ACCOUNT_TYPE.equals(account.type);
69 try { 67 try {
70 return GoogleAuthUtil.getTokenWithNotification( 68 return GoogleAuthUtil.getTokenWithNotification(
71 mApplicationContext, account, authTokenScope, null); 69 ContextUtils.getApplicationContext(), account, authTokenScop e, null);
72 } catch (GoogleAuthException ex) { 70 } catch (GoogleAuthException ex) {
73 // This case includes a UserRecoverableNotifiedException, but most c lients will have 71 // This case includes a UserRecoverableNotifiedException, but most c lients will have
74 // their own retry mechanism anyway. 72 // their own retry mechanism anyway.
75 // TODO(bauerb): Investigate integrating the callback with Connectio nRetry. 73 // TODO(bauerb): Investigate integrating the callback with Connectio nRetry.
76 throw new AuthException(false /* isTransientError */, ex); 74 throw new AuthException(false /* isTransientError */, ex);
77 } catch (IOException ex) { 75 } catch (IOException ex) {
78 throw new AuthException(true /* isTransientError */, ex); 76 throw new AuthException(true /* isTransientError */, ex);
79 } 77 }
80 } 78 }
81 79
82 @Override 80 @Override
83 public void invalidateAuthToken(String authToken) throws AuthException { 81 public void invalidateAuthToken(String authToken) throws AuthException {
84 try { 82 try {
85 GoogleAuthUtil.clearToken(mApplicationContext, authToken); 83 GoogleAuthUtil.clearToken(ContextUtils.getApplicationContext(), auth Token);
86 } catch (GooglePlayServicesAvailabilityException ex) { 84 } catch (GooglePlayServicesAvailabilityException ex) {
87 throw new AuthException(false /* isTransientError */, ex); 85 throw new AuthException(false /* isTransientError */, ex);
88 } catch (GoogleAuthException ex) { 86 } catch (GoogleAuthException ex) {
89 throw new AuthException(false /* isTransientError */, ex); 87 throw new AuthException(false /* isTransientError */, ex);
90 } catch (IOException ex) { 88 } catch (IOException ex) {
91 throw new AuthException(true /* isTransientError */, ex); 89 throw new AuthException(true /* isTransientError */, ex);
92 } 90 }
93 } 91 }
94 92
95 @Override 93 @Override
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 } 157 }
160 } 158 }
161 }; 159 };
162 // Android 4.4 throws NullPointerException if null is passed 160 // Android 4.4 throws NullPointerException if null is passed
163 Bundle emptyOptions = new Bundle(); 161 Bundle emptyOptions = new Bundle();
164 mAccountManager.updateCredentials( 162 mAccountManager.updateCredentials(
165 account, "android", emptyOptions, activity, realCallback, null); 163 account, "android", emptyOptions, activity, realCallback, null);
166 } 164 }
167 165
168 protected boolean hasGetAccountsPermission() { 166 protected boolean hasGetAccountsPermission() {
169 return ApiCompatibilityUtils.checkPermission(mApplicationContext, 167 return ApiCompatibilityUtils.checkPermission(ContextUtils.getApplication Context(),
170 Manifest.permission.GET_ACCOUNTS, Process.myPid(), Proces s.myUid()) 168 Manifest.permission.GET_ACCOUNTS, Process.myPid(), Proces s.myUid())
171 == PackageManager.PERMISSION_GRANTED; 169 == PackageManager.PERMISSION_GRANTED;
172 } 170 }
173 171
174 protected boolean hasManageAccountsPermission() { 172 protected boolean hasManageAccountsPermission() {
175 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 173 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
176 return true; 174 return true;
177 } 175 }
178 return ApiCompatibilityUtils.checkPermission(mApplicationContext, 176 return ApiCompatibilityUtils.checkPermission(ContextUtils.getApplication Context(),
179 "android.permission.MANAGE_ACCOUNTS", Process.myPid(), Pr ocess.myUid()) 177 "android.permission.MANAGE_ACCOUNTS", Process.myPid(), Pr ocess.myUid())
180 == PackageManager.PERMISSION_GRANTED; 178 == PackageManager.PERMISSION_GRANTED;
181 } 179 }
182 } 180 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698