OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.chrome.browser.signin; | 5 package org.chromium.chrome.browser.signin; |
6 | 6 |
| 7 import android.content.Context; |
| 8 |
7 import com.google.android.gms.auth.GoogleAuthException; | 9 import com.google.android.gms.auth.GoogleAuthException; |
8 import com.google.android.gms.auth.GoogleAuthUtil; | 10 import com.google.android.gms.auth.GoogleAuthUtil; |
9 | 11 |
10 import org.chromium.base.ContextUtils; | |
11 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
12 import org.chromium.base.ThreadUtils; | 13 import org.chromium.base.ThreadUtils; |
13 import org.chromium.base.VisibleForTesting; | 14 import org.chromium.base.VisibleForTesting; |
14 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; | 15 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; |
15 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; | 16 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; |
16 | 17 |
17 import java.io.IOException; | 18 import java.io.IOException; |
18 | 19 |
19 /** | 20 /** |
20 * Returns a stable id that can be used to identify a Google Account. This | 21 * Returns a stable id that can be used to identify a Google Account. This |
(...skipping 10 matching lines...) Expand all Loading... |
31 | 32 |
32 /** | 33 /** |
33 * Returns a stable id for the account associated with the given email addre
ss. | 34 * Returns a stable id for the account associated with the given email addre
ss. |
34 * If an account with the given email address is not installed on the device | 35 * If an account with the given email address is not installed on the device |
35 * then null is returned. | 36 * then null is returned. |
36 * | 37 * |
37 * This method will throw IllegalStateException if called on the main thread
. | 38 * This method will throw IllegalStateException if called on the main thread
. |
38 * | 39 * |
39 * @param accountName The email address of a Google account. | 40 * @param accountName The email address of a Google account. |
40 */ | 41 */ |
41 public String getAccountId(String accountName) { | 42 public String getAccountId(Context ctx, String accountName) { |
42 try { | 43 try { |
43 return GoogleAuthUtil.getAccountId(ContextUtils.getApplicationContex
t(), accountName); | 44 return GoogleAuthUtil.getAccountId(ctx, accountName); |
44 } catch (IOException | GoogleAuthException ex) { | 45 } catch (IOException | GoogleAuthException ex) { |
45 Log.e("cr.AccountIdProvider", "AccountIdProvider.getAccountId", ex); | 46 Log.e("cr.AccountIdProvider", "AccountIdProvider.getAccountId", ex); |
46 return null; | 47 return null; |
47 } | 48 } |
48 } | 49 } |
49 | 50 |
50 /** | 51 /** |
51 * Returns whether the AccountIdProvider can be used. | 52 * Returns whether the AccountIdProvider can be used. |
52 * Since the AccountIdProvider queries Google Play services, this basically
checks whether | 53 * Since the AccountIdProvider queries Google Play services, this basically
checks whether |
53 * Google Play services is available. | 54 * Google Play services is available. |
54 */ | 55 */ |
55 public boolean canBeUsed() { | 56 public boolean canBeUsed(Context ctx) { |
56 return ExternalAuthUtils.getInstance().canUseGooglePlayServices( | 57 return ExternalAuthUtils.getInstance().canUseGooglePlayServices( |
57 ContextUtils.getApplicationContext(), new UserRecoverableErrorHa
ndler.Silent()); | 58 ctx, new UserRecoverableErrorHandler.Silent()); |
58 } | 59 } |
59 | 60 |
60 /** | 61 /** |
61 * Gets the global account Id provider. | 62 * Gets the global account Id provider. |
62 */ | 63 */ |
63 public static AccountIdProvider getInstance() { | 64 public static AccountIdProvider getInstance() { |
64 ThreadUtils.assertOnUiThread(); | 65 ThreadUtils.assertOnUiThread(); |
65 if (sProvider == null) sProvider = new AccountIdProvider(); | 66 if (sProvider == null) sProvider = new AccountIdProvider(); |
66 return sProvider; | 67 return sProvider; |
67 } | 68 } |
68 | 69 |
69 /** | 70 /** |
70 * For testing purposes only, allows to set the provider even if it has alre
ady been | 71 * For testing purposes only, allows to set the provider even if it has alre
ady been |
71 * initialized. | 72 * initialized. |
72 */ | 73 */ |
73 @VisibleForTesting | 74 @VisibleForTesting |
74 public static void setInstanceForTest(AccountIdProvider provider) { | 75 public static void setInstanceForTest(AccountIdProvider provider) { |
75 ThreadUtils.assertOnUiThread(); | 76 ThreadUtils.assertOnUiThread(); |
76 sProvider = provider; | 77 sProvider = provider; |
77 } | 78 } |
78 } | 79 } |
79 | 80 |
OLD | NEW |