Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupWatcher.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupWatcher.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupWatcher.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c21b94ae1b03b57cf1dfd50b9112dfec87efa375 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackupWatcher.java |
| @@ -0,0 +1,66 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser; |
| + |
| +import android.app.backup.BackupManager; |
| +import android.content.Context; |
| +import android.content.SharedPreferences; |
| + |
| +import org.chromium.base.ContextUtils; |
| +import org.chromium.base.VisibleForTesting; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| +import org.chromium.components.signin.ChromeSigninController; |
| + |
| +/** |
| + * Class for watching for changes to the Android preferences that are backed up using Android |
| + * key/value backup. |
| + */ |
| +@JNINamespace("chrome::android") |
| +public class ChromeBackupWatcher { |
| + private BackupManager mBackupManager; |
| + private static final String TAG = "ChromeBackupWatcher"; |
| + |
| + @VisibleForTesting |
| + @CalledByNative |
| + static ChromeBackupWatcher createChromeBackupWatcher() { |
| + return new ChromeBackupWatcher(); |
| + } |
| + |
| + private ChromeBackupWatcher() { |
| + Context context = ContextUtils.getApplicationContext(); |
| + if (context == null) return; |
| + |
| + mBackupManager = new BackupManager(context); |
| + // Watch the Java preferences that are backed up |
|
Bernhard Bauer
2016/11/14 10:05:38
Nit: Could you put periods at the end of sentences
aberent
2016/11/14 17:23:39
Done.
|
| + SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); |
| + sharedPrefs.registerOnSharedPreferenceChangeListener( |
| + new SharedPreferences.OnSharedPreferenceChangeListener() { |
| + |
| + @Override |
| + public void onSharedPreferenceChanged( |
| + SharedPreferences sharedPreferences, String key) { |
| + // We need to update the backup if the user id or any of the backed up |
| + // Android preferences change |
| + if (key.equals(ChromeSigninController.SIGNED_IN_ACCOUNT_KEY)) { |
| + onBackupPrefsChanged(); |
| + return; |
| + } |
| + for (String pref : ChromeBackupAgent.BACKUP_ANDROID_BOOL_PREFS) { |
| + if (key.equals(pref)) { |
| + onBackupPrefsChanged(); |
| + return; |
| + } |
| + } |
| + } |
| + |
| + }); |
| + } |
| + |
| + @CalledByNative |
| + private void onBackupPrefsChanged() { |
| + mBackupManager.dataChanged(); |
| + } |
| +} |