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

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

Issue 2801033002: Revert of Android: Remove GetApplicationContext part 2 (Closed)
Patch Set: 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 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2011 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.accounts.Account; 7 import android.accounts.Account;
8 import android.accounts.AuthenticatorDescription; 8 import android.accounts.AuthenticatorDescription;
9 import android.app.Activity; 9 import android.app.Activity;
10 import android.content.Context; 10 import android.content.Context;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 */ 45 */
46 @VisibleForTesting 46 @VisibleForTesting
47 public static final String FEATURE_IS_CHILD_ACCOUNT_KEY = "service_uca"; 47 public static final String FEATURE_IS_CHILD_ACCOUNT_KEY = "service_uca";
48 48
49 private static final Object sLock = new Object(); 49 private static final Object sLock = new Object();
50 50
51 private static AccountManagerHelper sAccountManagerHelper; 51 private static AccountManagerHelper sAccountManagerHelper;
52 52
53 private final AccountManagerDelegate mAccountManager; 53 private final AccountManagerDelegate mAccountManager;
54 54
55 private Context mApplicationContext;
56
55 /** 57 /**
56 * A simple callback for getAuthToken. 58 * A simple callback for getAuthToken.
57 */ 59 */
58 public interface GetAuthTokenCallback { 60 public interface GetAuthTokenCallback {
59 /** 61 /**
60 * Invoked on the UI thread if a token is provided by the AccountManager . 62 * Invoked on the UI thread if a token is provided by the AccountManager .
61 * 63 *
62 * @param token Auth token, guaranteed not to be null. 64 * @param token Auth token, guaranteed not to be null.
63 */ 65 */
64 void tokenAvailable(String token); 66 void tokenAvailable(String token);
65 67
66 /** 68 /**
67 * Invoked on the UI thread if no token is available. 69 * Invoked on the UI thread if no token is available.
68 * 70 *
69 * @param isTransientError Indicates if the error is transient (network timeout or 71 * @param isTransientError Indicates if the error is transient (network timeout or
70 * unavailable, etc) or persistent (bad credentials, permission denied, etc). 72 * unavailable, etc) or persistent (bad credentials, permission denied, etc).
71 */ 73 */
72 void tokenUnavailable(boolean isTransientError); 74 void tokenUnavailable(boolean isTransientError);
73 } 75 }
74 76
75 /** 77 /**
78 * @param context the Android context
76 * @param accountManager the account manager to use as a backend service 79 * @param accountManager the account manager to use as a backend service
77 */ 80 */
78 private AccountManagerHelper(AccountManagerDelegate accountManager) { 81 private AccountManagerHelper(Context context, AccountManagerDelegate account Manager) {
82 mApplicationContext = context.getApplicationContext();
79 mAccountManager = accountManager; 83 mAccountManager = accountManager;
80 } 84 }
81 85
82 /** 86 /**
83 * Initialize AccountManagerHelper with a custom AccountManagerDelegate. 87 * Initialize AccountManagerHelper with a custom AccountManagerDelegate.
84 * Ensures that the singleton AccountManagerHelper hasn't been created yet. 88 * Ensures that the singleton AccountManagerHelper hasn't been created yet.
85 * This can be overriden in tests using the overrideAccountManagerHelperForT ests method. 89 * This can be overriden in tests using the overrideAccountManagerHelperForT ests method.
86 * 90 *
91 * @param context the applicationContext is retrieved from the context used as an argument.
87 * @param delegate the custom AccountManagerDelegate to use. 92 * @param delegate the custom AccountManagerDelegate to use.
88 */ 93 */
89 public static void initializeAccountManagerHelper(AccountManagerDelegate del egate) { 94 public static void initializeAccountManagerHelper(
95 Context context, AccountManagerDelegate delegate) {
90 synchronized (sLock) { 96 synchronized (sLock) {
91 assert sAccountManagerHelper == null; 97 assert sAccountManagerHelper == null;
92 sAccountManagerHelper = new AccountManagerHelper(delegate); 98 sAccountManagerHelper = new AccountManagerHelper(context, delegate);
93 } 99 }
94 } 100 }
95 101
96 /** 102 /**
97 * A getter method for AccountManagerHelper singleton which also initializes it if not wasn't 103 * A getter method for AccountManagerHelper singleton which also initializes it if not wasn't
98 * already initialized. 104 * already initialized.
99 * 105 *
106 * @param context the applicationContext is retrieved from the context used as an argument.
100 * @return a singleton instance of the AccountManagerHelper 107 * @return a singleton instance of the AccountManagerHelper
101 */ 108 */
102 public static AccountManagerHelper get() { 109 public static AccountManagerHelper get(Context context) {
103 synchronized (sLock) { 110 synchronized (sLock) {
104 if (sAccountManagerHelper == null) { 111 if (sAccountManagerHelper == null) {
105 sAccountManagerHelper = 112 sAccountManagerHelper = new AccountManagerHelper(
106 new AccountManagerHelper(new SystemAccountManagerDelegat e()); 113 context, new SystemAccountManagerDelegate(context));
107 } 114 }
108 } 115 }
109 return sAccountManagerHelper; 116 return sAccountManagerHelper;
110 } 117 }
111 118
112 /** 119 /**
113 * Override AccountManagerHelper with a custom AccountManagerDelegate in tes ts. 120 * Override AccountManagerHelper with a custom AccountManagerDelegate in tes ts.
114 * Unlike initializeAccountManagerHelper, this will override the existing in stance of 121 * Unlike initializeAccountManagerHelper, this will override the existing in stance of
115 * AccountManagerHelper if any. Only for use in Tests. 122 * AccountManagerHelper if any. Only for use in Tests.
116 * 123 *
117 * @param context the applicationContext is retrieved from the context used as an argument. 124 * @param context the applicationContext is retrieved from the context used as an argument.
118 * @param delegate the custom AccountManagerDelegate to use. 125 * @param delegate the custom AccountManagerDelegate to use.
119 */ 126 */
120 @VisibleForTesting 127 @VisibleForTesting
121 public static void overrideAccountManagerHelperForTests( 128 public static void overrideAccountManagerHelperForTests(
122 Context context, AccountManagerDelegate delegate) { 129 Context context, AccountManagerDelegate delegate) {
123 synchronized (sLock) { 130 synchronized (sLock) {
124 sAccountManagerHelper = new AccountManagerHelper(delegate); 131 sAccountManagerHelper = new AccountManagerHelper(context, delegate);
125 } 132 }
126 } 133 }
127 134
128 /** 135 /**
129 * Creates an Account object for the given name. 136 * Creates an Account object for the given name.
130 */ 137 */
131 public static Account createAccountFromName(String name) { 138 public static Account createAccountFromName(String name) {
132 return new Account(name, GOOGLE_ACCOUNT_TYPE); 139 return new Account(name, GOOGLE_ACCOUNT_TYPE);
133 } 140 }
134 141
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 public void onConnectionTypeChanged(int connectionType) { 457 public void onConnectionTypeChanged(int connectionType) {
451 assert mNumTries.get() < MAX_TRIES; 458 assert mNumTries.get() < MAX_TRIES;
452 if (NetworkChangeNotifier.isOnline()) { 459 if (NetworkChangeNotifier.isOnline()) {
453 // The network is back; stop listening and try again. 460 // The network is back; stop listening and try again.
454 NetworkChangeNotifier.removeConnectionTypeObserver(this); 461 NetworkChangeNotifier.removeConnectionTypeObserver(this);
455 attempt(); 462 attempt();
456 } 463 }
457 } 464 }
458 } 465 }
459 } 466 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698