| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.accounts.Account; | 7 import android.accounts.Account; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.SharedPreferences; | |
| 10 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
| 11 | 10 |
| 12 import com.google.android.gms.auth.AccountChangeEvent; | 11 import com.google.android.gms.auth.AccountChangeEvent; |
| 13 import com.google.android.gms.auth.GoogleAuthException; | 12 import com.google.android.gms.auth.GoogleAuthException; |
| 14 import com.google.android.gms.auth.GoogleAuthUtil; | 13 import com.google.android.gms.auth.GoogleAuthUtil; |
| 15 | 14 |
| 16 import org.chromium.base.ContextUtils; | 15 import org.chromium.base.ContextUtils; |
| 17 import org.chromium.base.Log; | 16 import org.chromium.base.Log; |
| 18 import org.chromium.base.VisibleForTesting; | 17 import org.chromium.base.VisibleForTesting; |
| 19 import org.chromium.chrome.browser.invalidation.InvalidationServiceFactory; | 18 import org.chromium.chrome.browser.invalidation.InvalidationServiceFactory; |
| 20 import org.chromium.chrome.browser.preferences.ChromePreferenceManager; | 19 import org.chromium.chrome.browser.preferences.ChromePreferenceManager; |
| 21 import org.chromium.chrome.browser.preferences.PrefServiceBridge; | 20 import org.chromium.chrome.browser.preferences.PrefServiceBridge; |
| 22 import org.chromium.chrome.browser.profiles.Profile; | 21 import org.chromium.chrome.browser.profiles.Profile; |
| 23 import org.chromium.chrome.browser.signin.SigninManager.SignInCallback; | 22 import org.chromium.chrome.browser.signin.SigninManager.SignInCallback; |
| 24 import org.chromium.chrome.browser.sync.ProfileSyncService; | 23 import org.chromium.chrome.browser.sync.ProfileSyncService; |
| 25 import org.chromium.components.signin.AccountManagerHelper; | 24 import org.chromium.components.signin.AccountManagerHelper; |
| 26 import org.chromium.components.signin.ChromeSigninController; | 25 import org.chromium.components.signin.ChromeSigninController; |
| 27 import org.chromium.components.sync.AndroidSyncSettings; | 26 import org.chromium.components.sync.AndroidSyncSettings; |
| 28 | 27 |
| 29 import java.io.IOException; | 28 import java.io.IOException; |
| 30 import java.util.ArrayList; | 29 import java.util.ArrayList; |
| 31 import java.util.HashSet; | |
| 32 import java.util.List; | 30 import java.util.List; |
| 33 import java.util.Set; | |
| 34 | 31 |
| 35 import javax.annotation.Nullable; | 32 import javax.annotation.Nullable; |
| 36 | 33 |
| 37 /** | 34 /** |
| 38 * A helper for tasks like re-signin. | 35 * A helper for tasks like re-signin. |
| 39 * | 36 * |
| 40 * This should be merged into SigninManager when it is upstreamed. | 37 * This should be merged into SigninManager when it is upstreamed. |
| 41 */ | 38 */ |
| 42 public class SigninHelper { | 39 public class SigninHelper { |
| 43 | 40 |
| 44 private static final String TAG = "SigninHelper"; | 41 private static final String TAG = "SigninHelper"; |
| 45 | 42 |
| 46 private static final Object LOCK = new Object(); | 43 private static final Object LOCK = new Object(); |
| 47 | 44 |
| 48 private static final String ACCOUNTS_CHANGED_PREFS_KEY = "prefs_sync_account
s_changed"; | 45 private static final String ACCOUNTS_CHANGED_PREFS_KEY = "prefs_sync_account
s_changed"; |
| 49 | 46 |
| 50 // Key to the shared pref that holds the new account's name if the currently
signed | 47 // Key to the shared pref that holds the new account's name if the currently
signed |
| 51 // in account has been renamed. | 48 // in account has been renamed. |
| 52 private static final String ACCOUNT_RENAMED_PREFS_KEY = "prefs_sync_account_
renamed"; | 49 private static final String ACCOUNT_RENAMED_PREFS_KEY = "prefs_sync_account_
renamed"; |
| 53 | 50 |
| 54 // Key to the shared pref that holds the last read index of all the account
changed | 51 // Key to the shared pref that holds the last read index of all the account
changed |
| 55 // events of the current signed in account. | 52 // events of the current signed in account. |
| 56 private static final String ACCOUNT_RENAME_EVENT_INDEX_PREFS_KEY = | 53 private static final String ACCOUNT_RENAME_EVENT_INDEX_PREFS_KEY = |
| 57 "prefs_sync_account_rename_event_index"; | 54 "prefs_sync_account_rename_event_index"; |
| 58 | 55 |
| 59 private static final String ANDROID_ACCOUNTS_PREFS_KEY = "prefs_sync_android
_accounts"; | |
| 60 | |
| 61 private static SigninHelper sInstance; | 56 private static SigninHelper sInstance; |
| 62 | 57 |
| 63 /** | 58 /** |
| 64 * Retrieve more detailed information from account changed intents. | 59 * Retrieve more detailed information from account changed intents. |
| 65 */ | 60 */ |
| 66 public static interface AccountChangeEventChecker { | 61 public static interface AccountChangeEventChecker { |
| 67 public List<String> getAccountChangeEvents( | 62 public List<String> getAccountChangeEvents( |
| 68 Context context, int index, String accountName); | 63 Context context, int index, String accountName); |
| 69 } | 64 } |
| 70 | 65 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 | 128 |
| 134 public void validateAccountSettings(boolean accountsChanged) { | 129 public void validateAccountSettings(boolean accountsChanged) { |
| 135 // Ensure System accounts have been seeded. | 130 // Ensure System accounts have been seeded. |
| 136 mAccountTrackerService.checkAndSeedSystemAccounts(); | 131 mAccountTrackerService.checkAndSeedSystemAccounts(); |
| 137 if (!accountsChanged) { | 132 if (!accountsChanged) { |
| 138 mAccountTrackerService.validateSystemAccounts(); | 133 mAccountTrackerService.validateSystemAccounts(); |
| 139 } | 134 } |
| 140 | 135 |
| 141 Account syncAccount = mChromeSigninController.getSignedInUser(); | 136 Account syncAccount = mChromeSigninController.getSignedInUser(); |
| 142 if (syncAccount == null) { | 137 if (syncAccount == null) { |
| 138 ChromePreferenceManager chromePreferenceManager = |
| 139 ChromePreferenceManager.getInstance(mContext); |
| 140 if (chromePreferenceManager.getShowSigninPromo()) return; |
| 141 |
| 143 // Never shows a signin promo if user has manually disconnected. | 142 // Never shows a signin promo if user has manually disconnected. |
| 144 String lastSyncAccountName = | 143 String lastSyncAccountName = |
| 145 PrefServiceBridge.getInstance().getSyncLastAccountName(); | 144 PrefServiceBridge.getInstance().getSyncLastAccountName(); |
| 146 if (lastSyncAccountName != null && !lastSyncAccountName.isEmpty()) r
eturn; | 145 if (lastSyncAccountName != null && !lastSyncAccountName.isEmpty()) r
eturn; |
| 147 | 146 |
| 148 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences
(); | 147 if (!chromePreferenceManager.getSigninPromoShown() |
| 149 boolean hasKnownAccountKeys = sharedPrefs.contains(ANDROID_ACCOUNTS_
PREFS_KEY); | 148 && AccountManagerHelper.get(mContext).getGoogleAccountNames(
).size() > 0) { |
| 150 // Nothing to do if Android accounts are not changed and already kno
wn to Chrome. | 149 chromePreferenceManager.setShowSigninPromo(true); |
| 151 if (hasKnownAccountKeys && !accountsChanged) return; | |
| 152 | |
| 153 List<String> currentAccountNames = | |
| 154 AccountManagerHelper.get(mContext).getGoogleAccountNames(); | |
| 155 if (hasKnownAccountKeys) { | |
| 156 ChromePreferenceManager chromePreferenceManager = | |
| 157 ChromePreferenceManager.getInstance(mContext); | |
| 158 if (!chromePreferenceManager.getSigninPromoShown()) { | |
| 159 Set<String> lastKnownAccountNames = sharedPrefs.getStringSet
( | |
| 160 ANDROID_ACCOUNTS_PREFS_KEY, new HashSet<String>()); | |
| 161 Set<String> newAccountNames = new HashSet<String>(currentAcc
ountNames); | |
| 162 newAccountNames.removeAll(lastKnownAccountNames); | |
| 163 if (!newAccountNames.isEmpty()) { | |
| 164 chromePreferenceManager.setShowSigninPromo(true); | |
| 165 } | |
| 166 } | |
| 167 } | 150 } |
| 168 | 151 |
| 169 sharedPrefs.edit().putStringSet( | |
| 170 ANDROID_ACCOUNTS_PREFS_KEY, new HashSet<String>(currentAccou
ntNames)).apply(); | |
| 171 return; | 152 return; |
| 172 } | 153 } |
| 173 | 154 |
| 174 String renamedAccount = getNewSignedInAccountName(mContext); | 155 String renamedAccount = getNewSignedInAccountName(mContext); |
| 175 if (accountsChanged && renamedAccount != null) { | 156 if (accountsChanged && renamedAccount != null) { |
| 176 handleAccountRename(ChromeSigninController.get(mContext).getSignedIn
AccountName(), | 157 handleAccountRename(ChromeSigninController.get(mContext).getSignedIn
AccountName(), |
| 177 renamedAccount); | 158 renamedAccount); |
| 178 return; | 159 return; |
| 179 } | 160 } |
| 180 | 161 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 .getBoolean(ACCOUNTS_CHANGED_PREFS_KEY, false)) { | 368 .getBoolean(ACCOUNTS_CHANGED_PREFS_KEY, false)) { |
| 388 // Clear the value in prefs. | 369 // Clear the value in prefs. |
| 389 ContextUtils.getAppSharedPreferences() | 370 ContextUtils.getAppSharedPreferences() |
| 390 .edit().putBoolean(ACCOUNTS_CHANGED_PREFS_KEY, false).apply(
); | 371 .edit().putBoolean(ACCOUNTS_CHANGED_PREFS_KEY, false).apply(
); |
| 391 return true; | 372 return true; |
| 392 } else { | 373 } else { |
| 393 return false; | 374 return false; |
| 394 } | 375 } |
| 395 } | 376 } |
| 396 } | 377 } |
| OLD | NEW |