Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupAgent.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupAgent.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupAgent.java |
| index a95ec53a1afed2847628a8c94db8a3b1f2afe8bb..1e537559a24eac78bb09c9d33a03af738ffe82a3 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupAgent.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupAgent.java |
| @@ -4,6 +4,8 @@ |
| package org.chromium.chrome.browser; |
| +import android.accounts.Account; |
| +import android.accounts.AccountManager; |
| import android.annotation.TargetApi; |
| import android.app.backup.BackupAgent; |
| import android.app.backup.BackupDataInput; |
| @@ -60,26 +62,51 @@ public class ChromeBackupAgent extends BackupAgent { |
| // Android Backup |
| } |
| + // May be overriden by downstream products that access account information in a different way. |
| + protected Account[] getAccounts() { |
| + Log.d(TAG, "Getting accounts from AccountManager"); |
| + AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE); |
| + return manager.getAccounts(); |
| + } |
| + |
| + private boolean accountExistsOnDevice(String userName) { |
|
Bernhard Bauer
2016/05/23 12:41:24
I would also mention that we're not using AccountM
aberent
2016/06/01 12:34:31
Done. I have also put a comment at the top of the
|
| + for (Account account: getAccounts()) { |
| + if (account.name.equals(userName)) return true; |
| + } |
| + return false; |
| + } |
| + |
| @Override |
| public void onRestoreFinished() { |
| - SharedPreferences sharedPrefs = |
| - PreferenceManager.getDefaultSharedPreferences(ChromeBackupAgent.this); |
| + if (getApplicationContext() instanceof ChromeApplication) { |
| + // This should never happen in real use, but will happen during testing if Chrome is |
| + // already running (even in background, started to provide a service, for example). |
| + Log.w(TAG, "Running with wrong type of Application class"); |
| + return; |
| + } |
| + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); |
| Set<String> prefNames = sharedPrefs.getAll().keySet(); |
| // Save the user name for later restoration. |
| String userName = sharedPrefs.getString(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY, null); |
| + Log.d(TAG, "Previous signed in user name = " + userName); |
| + // If the user hasn't signed in, or can't sign in, then don't restore anything. |
| + if (userName == null || !accountExistsOnDevice(userName)) { |
| + boolean commitResult = sharedPrefs.edit().clear().commit(); |
| + Log.d(TAG, "onRestoreFinished complete, nothing restored; commit result = " |
|
Bernhard Bauer
2016/05/23 12:40:15
Use a format string for logging. (Or just remove t
aberent
2016/06/01 12:34:31
Done.
|
| + + commitResult); |
| + return; |
| + } |
| + |
| SharedPreferences.Editor editor = sharedPrefs.edit(); |
| // Throw away prefs we don't want to restore. |
| Set<String> restoredPrefs = new HashSet<>(Arrays.asList(RESTORED_ANDROID_PREFS)); |
| for (String pref : prefNames) { |
| - Log.d(TAG, "Checking pref " + pref); |
| if (!restoredPrefs.contains(pref)) editor.remove(pref); |
| } |
| // Because FirstRunSignInProcessor.FIRST_RUN_FLOW_SIGNIN_COMPLETE is not restored Chrome |
| // will sign in the user on first run to the account in FIRST_RUN_FLOW_SIGNIN_ACCOUNT_NAME |
| // if any. If the rest of FRE has been completed this will happen silently. |
| - if (userName != null) { |
| - editor.putString(FirstRunSignInProcessor.FIRST_RUN_FLOW_SIGNIN_ACCOUNT_NAME, userName); |
| - } |
| + editor.putString(FirstRunSignInProcessor.FIRST_RUN_FLOW_SIGNIN_ACCOUNT_NAME, userName); |
| boolean commitResult = editor.commit(); |
| Log.d(TAG, "onRestoreFinished complete; commit result = " + commitResult); |