Loading src/com/android/documentsui/BaseActivity.java +34 −17 Original line number Diff line number Diff line Loading @@ -30,10 +30,10 @@ import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.MessageQueue.IdleHandler; import android.preference.PreferenceManager; import android.provider.DocumentsContract; import android.support.annotation.CallSuper; import android.support.annotation.LayoutRes; import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import android.util.Log; import android.view.Menu; Loading @@ -52,7 +52,9 @@ import com.android.documentsui.base.State.ViewMode; import com.android.documentsui.dirlist.AnimationView; import com.android.documentsui.dirlist.DirectoryFragment; import com.android.documentsui.prefs.LocalPreferences; import com.android.documentsui.prefs.Preferences; import com.android.documentsui.prefs.PreferencesMonitor; import com.android.documentsui.prefs.ScopedPreferences; import com.android.documentsui.queries.DebugCommandProcessor; import com.android.documentsui.queries.SearchViewManager; import com.android.documentsui.queries.SearchViewManager.SearchManagerListener; Loading @@ -69,6 +71,8 @@ import java.util.Date; import java.util.List; import java.util.concurrent.Executor; import javax.annotation.Nullable; public abstract class BaseActivity extends Activity implements CommonAddons, NavigationViewManager.Environment { Loading Loading @@ -182,12 +186,28 @@ public abstract class BaseActivity mSearchManager = new SearchViewManager(searchListener, dbgCommands, icicle); mSortController = SortController.create(this, mState.derivedMode, mState.sortModel); mPreferencesMonitor = new PreferencesMonitor(getApplicationContext()); mPreferencesMonitor = new PreferencesMonitor( getApplicationContext().getPackageName(), PreferenceManager.getDefaultSharedPreferences(this), this::onPreferenceChanged); mPreferencesMonitor.start(); // Base classes must update result in their onCreate. setResult(Activity.RESULT_CANCELED); } public void onPreferenceChanged(String pref) { // For now, we only work with prefs that we backup. This // just limits the scope of what we expect to come flowing // through here until we know we want more and fancier options. assert(Preferences.shouldBackup(pref)); switch (pref) { case ScopedPreferences.INCLUDE_DEVICE_ROOT: updateDisplayAdvancedDevices(mInjector.prefs.getShowDeviceRoot()); } } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); Loading @@ -214,18 +234,6 @@ public abstract class BaseActivity return showMenu; } @Override protected void onResume() { super.onResume(); mPreferencesMonitor.start(); } @Override protected void onPause() { super.onPause(); mPreferencesMonitor.stop(); } @Override @CallSuper public boolean onPrepareOptionsMenu(Menu menu) { Loading @@ -237,6 +245,7 @@ public abstract class BaseActivity @Override protected void onDestroy() { mRootsMonitor.stop(); mPreferencesMonitor.stop(); super.onDestroy(); } Loading Loading @@ -335,7 +344,7 @@ public abstract class BaseActivity return true; case R.id.menu_advanced: setDisplayAdvancedDevices(!mState.showAdvanced); onDisplayAdvancedDevices(); return true; case R.id.menu_select_all: Loading Loading @@ -442,13 +451,21 @@ public abstract class BaseActivity /** * Set internal storage visible based on explicit user action. */ void setDisplayAdvancedDevices(boolean display) { private void onDisplayAdvancedDevices() { boolean display = !mState.showAdvanced; Metrics.logUserAction(this, display ? Metrics.USER_ACTION_SHOW_ADVANCED : Metrics.USER_ACTION_HIDE_ADVANCED); mInjector.prefs.setShowDeviceRoot(display); updateDisplayAdvancedDevices(display); } private void updateDisplayAdvancedDevices(boolean display) { mState.showAdvanced = display; RootsFragment.get(getFragmentManager()).onDisplayStateChanged(); @Nullable RootsFragment fragment = RootsFragment.get(getFragmentManager()); if (fragment != null) { fragment.onDisplayStateChanged(); } invalidateOptionsMenu(); } Loading src/com/android/documentsui/prefs/LocalPreferences.java +1 −1 Original line number Diff line number Diff line Loading @@ -124,7 +124,7 @@ public class LocalPreferences { : userId + "|" + packageName + "|" + uuid + "|" + directory; } static boolean shouldBackup(String s) { public static boolean shouldBackup(String s) { return (s != null) ? s.startsWith(ROOT_VIEW_MODE_PREFIX) : false; } } src/com/android/documentsui/prefs/Preferences.java 0 → 100644 +24 −0 Original line number Diff line number Diff line /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.documentsui.prefs; public final class Preferences { private Preferences() {} public static boolean shouldBackup(String s) { return LocalPreferences.shouldBackup(s) || ScopedPreferences.shouldBackup(s); } } src/com/android/documentsui/prefs/PreferencesMonitor.java +28 −15 Original line number Diff line number Diff line Loading @@ -17,37 +17,50 @@ package com.android.documentsui.prefs; import android.app.backup.BackupManager; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import com.android.documentsui.base.ApplicationScope; import java.util.function.Consumer; /** * A class that monitors changes to the default shared preferences file. If a preference which * should be backed up changed, schedule a backup. * * Also, notifies a callback when such changes are noticed. This is the key mechanism by which * we learn about preference changes in other instances of the app. */ public final class PreferencesMonitor implements SharedPreferences.OnSharedPreferenceChangeListener { public final class PreferencesMonitor { private final String mPackageName; private final SharedPreferences mPrefs; private final OnSharedPreferenceChangeListener mListener = this::onSharedPreferenceChanged; private final Consumer<String> mChangeCallback; private Context mContext; public PreferencesMonitor( @ApplicationScope String packageName, SharedPreferences prefs, Consumer<String> listener) { public PreferencesMonitor(Context context) { mContext = context; mPackageName = packageName; mPrefs = prefs; mChangeCallback = listener; } public void start() { PreferenceManager.getDefaultSharedPreferences(mContext) .registerOnSharedPreferenceChangeListener(this); mPrefs.registerOnSharedPreferenceChangeListener(mListener); } public void stop() { PreferenceManager.getDefaultSharedPreferences(mContext) .unregisterOnSharedPreferenceChangeListener(this); mPrefs.unregisterOnSharedPreferenceChangeListener(mListener); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (PrefsBackupHelper.shouldBackup(key)) { BackupManager.dataChanged(mContext.getPackageName()); // visible for use as a lambda, otherwise treat as a private. void onSharedPreferenceChanged(SharedPreferences prefs, String key) { if (Preferences.shouldBackup(key)) { mChangeCallback.accept(key); BackupManager.dataChanged(mPackageName); } } } src/com/android/documentsui/prefs/PrefsBackupHelper.java +1 −5 Original line number Diff line number Diff line Loading @@ -62,7 +62,7 @@ final class PrefsBackupHelper { private void copyMatchingPreferences(SharedPreferences source, Editor destination) { for (Map.Entry<String, ?> preference : source.getAll().entrySet()) { if (shouldBackup(preference.getKey())) { if (Preferences.shouldBackup(preference.getKey())) { setPreference(destination, preference); } } Loading @@ -83,8 +83,4 @@ final class PrefsBackupHelper { + (value == null ? null : value.getClass())); } } static boolean shouldBackup(String s) { return LocalPreferences.shouldBackup(s) || ScopedPreferences.shouldBackup(s); } } Loading
src/com/android/documentsui/BaseActivity.java +34 −17 Original line number Diff line number Diff line Loading @@ -30,10 +30,10 @@ import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.MessageQueue.IdleHandler; import android.preference.PreferenceManager; import android.provider.DocumentsContract; import android.support.annotation.CallSuper; import android.support.annotation.LayoutRes; import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import android.util.Log; import android.view.Menu; Loading @@ -52,7 +52,9 @@ import com.android.documentsui.base.State.ViewMode; import com.android.documentsui.dirlist.AnimationView; import com.android.documentsui.dirlist.DirectoryFragment; import com.android.documentsui.prefs.LocalPreferences; import com.android.documentsui.prefs.Preferences; import com.android.documentsui.prefs.PreferencesMonitor; import com.android.documentsui.prefs.ScopedPreferences; import com.android.documentsui.queries.DebugCommandProcessor; import com.android.documentsui.queries.SearchViewManager; import com.android.documentsui.queries.SearchViewManager.SearchManagerListener; Loading @@ -69,6 +71,8 @@ import java.util.Date; import java.util.List; import java.util.concurrent.Executor; import javax.annotation.Nullable; public abstract class BaseActivity extends Activity implements CommonAddons, NavigationViewManager.Environment { Loading Loading @@ -182,12 +186,28 @@ public abstract class BaseActivity mSearchManager = new SearchViewManager(searchListener, dbgCommands, icicle); mSortController = SortController.create(this, mState.derivedMode, mState.sortModel); mPreferencesMonitor = new PreferencesMonitor(getApplicationContext()); mPreferencesMonitor = new PreferencesMonitor( getApplicationContext().getPackageName(), PreferenceManager.getDefaultSharedPreferences(this), this::onPreferenceChanged); mPreferencesMonitor.start(); // Base classes must update result in their onCreate. setResult(Activity.RESULT_CANCELED); } public void onPreferenceChanged(String pref) { // For now, we only work with prefs that we backup. This // just limits the scope of what we expect to come flowing // through here until we know we want more and fancier options. assert(Preferences.shouldBackup(pref)); switch (pref) { case ScopedPreferences.INCLUDE_DEVICE_ROOT: updateDisplayAdvancedDevices(mInjector.prefs.getShowDeviceRoot()); } } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); Loading @@ -214,18 +234,6 @@ public abstract class BaseActivity return showMenu; } @Override protected void onResume() { super.onResume(); mPreferencesMonitor.start(); } @Override protected void onPause() { super.onPause(); mPreferencesMonitor.stop(); } @Override @CallSuper public boolean onPrepareOptionsMenu(Menu menu) { Loading @@ -237,6 +245,7 @@ public abstract class BaseActivity @Override protected void onDestroy() { mRootsMonitor.stop(); mPreferencesMonitor.stop(); super.onDestroy(); } Loading Loading @@ -335,7 +344,7 @@ public abstract class BaseActivity return true; case R.id.menu_advanced: setDisplayAdvancedDevices(!mState.showAdvanced); onDisplayAdvancedDevices(); return true; case R.id.menu_select_all: Loading Loading @@ -442,13 +451,21 @@ public abstract class BaseActivity /** * Set internal storage visible based on explicit user action. */ void setDisplayAdvancedDevices(boolean display) { private void onDisplayAdvancedDevices() { boolean display = !mState.showAdvanced; Metrics.logUserAction(this, display ? Metrics.USER_ACTION_SHOW_ADVANCED : Metrics.USER_ACTION_HIDE_ADVANCED); mInjector.prefs.setShowDeviceRoot(display); updateDisplayAdvancedDevices(display); } private void updateDisplayAdvancedDevices(boolean display) { mState.showAdvanced = display; RootsFragment.get(getFragmentManager()).onDisplayStateChanged(); @Nullable RootsFragment fragment = RootsFragment.get(getFragmentManager()); if (fragment != null) { fragment.onDisplayStateChanged(); } invalidateOptionsMenu(); } Loading
src/com/android/documentsui/prefs/LocalPreferences.java +1 −1 Original line number Diff line number Diff line Loading @@ -124,7 +124,7 @@ public class LocalPreferences { : userId + "|" + packageName + "|" + uuid + "|" + directory; } static boolean shouldBackup(String s) { public static boolean shouldBackup(String s) { return (s != null) ? s.startsWith(ROOT_VIEW_MODE_PREFIX) : false; } }
src/com/android/documentsui/prefs/Preferences.java 0 → 100644 +24 −0 Original line number Diff line number Diff line /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.documentsui.prefs; public final class Preferences { private Preferences() {} public static boolean shouldBackup(String s) { return LocalPreferences.shouldBackup(s) || ScopedPreferences.shouldBackup(s); } }
src/com/android/documentsui/prefs/PreferencesMonitor.java +28 −15 Original line number Diff line number Diff line Loading @@ -17,37 +17,50 @@ package com.android.documentsui.prefs; import android.app.backup.BackupManager; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import com.android.documentsui.base.ApplicationScope; import java.util.function.Consumer; /** * A class that monitors changes to the default shared preferences file. If a preference which * should be backed up changed, schedule a backup. * * Also, notifies a callback when such changes are noticed. This is the key mechanism by which * we learn about preference changes in other instances of the app. */ public final class PreferencesMonitor implements SharedPreferences.OnSharedPreferenceChangeListener { public final class PreferencesMonitor { private final String mPackageName; private final SharedPreferences mPrefs; private final OnSharedPreferenceChangeListener mListener = this::onSharedPreferenceChanged; private final Consumer<String> mChangeCallback; private Context mContext; public PreferencesMonitor( @ApplicationScope String packageName, SharedPreferences prefs, Consumer<String> listener) { public PreferencesMonitor(Context context) { mContext = context; mPackageName = packageName; mPrefs = prefs; mChangeCallback = listener; } public void start() { PreferenceManager.getDefaultSharedPreferences(mContext) .registerOnSharedPreferenceChangeListener(this); mPrefs.registerOnSharedPreferenceChangeListener(mListener); } public void stop() { PreferenceManager.getDefaultSharedPreferences(mContext) .unregisterOnSharedPreferenceChangeListener(this); mPrefs.unregisterOnSharedPreferenceChangeListener(mListener); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (PrefsBackupHelper.shouldBackup(key)) { BackupManager.dataChanged(mContext.getPackageName()); // visible for use as a lambda, otherwise treat as a private. void onSharedPreferenceChanged(SharedPreferences prefs, String key) { if (Preferences.shouldBackup(key)) { mChangeCallback.accept(key); BackupManager.dataChanged(mPackageName); } } }
src/com/android/documentsui/prefs/PrefsBackupHelper.java +1 −5 Original line number Diff line number Diff line Loading @@ -62,7 +62,7 @@ final class PrefsBackupHelper { private void copyMatchingPreferences(SharedPreferences source, Editor destination) { for (Map.Entry<String, ?> preference : source.getAll().entrySet()) { if (shouldBackup(preference.getKey())) { if (Preferences.shouldBackup(preference.getKey())) { setPreference(destination, preference); } } Loading @@ -83,8 +83,4 @@ final class PrefsBackupHelper { + (value == null ? null : value.getClass())); } } static boolean shouldBackup(String s) { return LocalPreferences.shouldBackup(s) || ScopedPreferences.shouldBackup(s); } }