| OLD | NEW |
| 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.sync.signin; | 5 package org.chromium.sync.signin; |
| 6 | 6 |
| 7 | 7 |
| 8 import android.accounts.Account; | 8 import android.accounts.Account; |
| 9 import android.accounts.AccountManager; | 9 import android.accounts.AccountManager; |
| 10 import android.accounts.AccountManagerFuture; | 10 import android.accounts.AccountManagerFuture; |
| 11 import android.accounts.AuthenticatorDescription; | 11 import android.accounts.AuthenticatorDescription; |
| 12 import android.accounts.AuthenticatorException; | 12 import android.accounts.AuthenticatorException; |
| 13 import android.accounts.OperationCanceledException; | 13 import android.accounts.OperationCanceledException; |
| 14 import android.app.Activity; | 14 import android.app.Activity; |
| 15 import android.content.Context; | 15 import android.content.Context; |
| 16 import android.content.Intent; | 16 import android.content.Intent; |
| 17 import android.os.AsyncTask; | 17 import android.os.AsyncTask; |
| 18 import android.os.Bundle; | 18 import android.os.Bundle; |
| 19 import android.util.Log; | 19 import android.util.Log; |
| 20 | 20 |
| 21 import org.chromium.base.ThreadUtils; | 21 import org.chromium.base.ThreadUtils; |
| 22 import org.chromium.base.VisibleForTesting; | 22 import org.chromium.base.VisibleForTesting; |
| 23 import org.chromium.net.NetworkChangeNotifier; | 23 import org.chromium.net.NetworkChangeNotifier; |
| 24 | 24 |
| 25 import java.io.IOException; | 25 import java.io.IOException; |
| 26 import java.util.ArrayList; | 26 import java.util.ArrayList; |
| 27 import java.util.List; | 27 import java.util.List; |
| 28 import java.util.Locale; |
| 28 import java.util.concurrent.atomic.AtomicBoolean; | 29 import java.util.concurrent.atomic.AtomicBoolean; |
| 29 import java.util.concurrent.atomic.AtomicInteger; | 30 import java.util.concurrent.atomic.AtomicInteger; |
| 31 import java.util.regex.Pattern; |
| 30 | 32 |
| 31 import javax.annotation.Nullable; | 33 import javax.annotation.Nullable; |
| 32 | 34 |
| 33 /** | 35 /** |
| 34 * AccountManagerHelper wraps our access of AccountManager in Android. | 36 * AccountManagerHelper wraps our access of AccountManager in Android. |
| 35 * | 37 * |
| 36 * Use the AccountManagerHelper.get(someContext) to instantiate it | 38 * Use the AccountManagerHelper.get(someContext) to instantiate it |
| 37 */ | 39 */ |
| 38 public class AccountManagerHelper { | 40 public class AccountManagerHelper { |
| 39 | 41 |
| 40 private static final String TAG = "AccountManagerHelper"; | 42 private static final String TAG = "AccountManagerHelper"; |
| 41 | 43 |
| 44 private static final Pattern AT_SYMBOL = Pattern.compile("@"); |
| 45 |
| 46 private static final String GMAIL_COM = "gmail.com"; |
| 47 |
| 48 private static final String GOOGLEMAIL_COM = "googlemail.com"; |
| 49 |
| 42 public static final String GOOGLE_ACCOUNT_TYPE = "com.google"; | 50 public static final String GOOGLE_ACCOUNT_TYPE = "com.google"; |
| 43 | 51 |
| 44 private static final Object sLock = new Object(); | 52 private static final Object sLock = new Object(); |
| 45 | 53 |
| 46 private static final int MAX_TRIES = 3; | 54 private static final int MAX_TRIES = 3; |
| 47 | 55 |
| 48 private static AccountManagerHelper sAccountManagerHelper; | 56 private static AccountManagerHelper sAccountManagerHelper; |
| 49 | 57 |
| 50 private final AccountManagerDelegate mAccountManager; | 58 private final AccountManagerDelegate mAccountManager; |
| 51 | 59 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 123 } |
| 116 | 124 |
| 117 public Account[] getGoogleAccounts() { | 125 public Account[] getGoogleAccounts() { |
| 118 return mAccountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE); | 126 return mAccountManager.getAccountsByType(GOOGLE_ACCOUNT_TYPE); |
| 119 } | 127 } |
| 120 | 128 |
| 121 public boolean hasGoogleAccounts() { | 129 public boolean hasGoogleAccounts() { |
| 122 return getGoogleAccounts().length > 0; | 130 return getGoogleAccounts().length > 0; |
| 123 } | 131 } |
| 124 | 132 |
| 133 private String canonicalizeName(String name) { |
| 134 String[] parts = AT_SYMBOL.split(name); |
| 135 if (parts.length != 2) return name; |
| 136 |
| 137 if (GOOGLEMAIL_COM.equalsIgnoreCase(parts[1])) { |
| 138 parts[1] = GMAIL_COM; |
| 139 } |
| 140 if (GMAIL_COM.equalsIgnoreCase(parts[1])) { |
| 141 parts[0] = parts[0].replace(".", ""); |
| 142 } |
| 143 return (parts[0] + "@" + parts[1]).toLowerCase(Locale.US); |
| 144 } |
| 145 |
| 125 /** | 146 /** |
| 126 * Returns the account if it exists, null otherwise. | 147 * Returns the account if it exists, null otherwise. |
| 127 */ | 148 */ |
| 128 public Account getAccountFromName(String accountName) { | 149 public Account getAccountFromName(String accountName) { |
| 129 Account[] accounts = mAccountManager.getAccountsByType(GOOGLE_ACCOUNT_TY
PE); | 150 String canonicalName = canonicalizeName(accountName); |
| 151 Account[] accounts = getGoogleAccounts(); |
| 130 for (Account account : accounts) { | 152 for (Account account : accounts) { |
| 131 if (account.name.equals(accountName)) { | 153 if (canonicalizeName(account.name).equals(canonicalName)) { |
| 132 return account; | 154 return account; |
| 133 } | 155 } |
| 134 } | 156 } |
| 135 return null; | 157 return null; |
| 136 } | 158 } |
| 137 | 159 |
| 138 /** | 160 /** |
| 139 * Returns whether the accounts exists. | 161 * Returns whether the accounts exists. |
| 140 */ | 162 */ |
| 141 public boolean hasAccountForName(String accountName) { | 163 public boolean hasAccountForName(String accountName) { |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 null, account, authTokenType, callback, numTries, errorEncountered,
null); | 365 null, account, authTokenType, callback, numTries, errorEncountered,
null); |
| 344 } | 366 } |
| 345 | 367 |
| 346 /** | 368 /** |
| 347 * Removes an auth token from the AccountManager's cache. | 369 * Removes an auth token from the AccountManager's cache. |
| 348 */ | 370 */ |
| 349 public void invalidateAuthToken(String authToken) { | 371 public void invalidateAuthToken(String authToken) { |
| 350 mAccountManager.invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken); | 372 mAccountManager.invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, authToken); |
| 351 } | 373 } |
| 352 } | 374 } |
| OLD | NEW |