From b00811da646a2ea335074f2a478f3c18b1a09274 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Tue, 28 Feb 2017 10:52:46 -0800 Subject: [PATCH] Let fragment and indexProvider share prefControllers Bug: 35812240 Test: make RunSettingsRoboTests Change-Id: Ifd96f935836a52e0a56f170f3cdf9b9ddf7c499a --- .../android/settings/DeviceInfoSettings.java | 65 +++++----- src/com/android/settings/DisplaySettings.java | 40 ++---- .../applications/assist/ManageAssist.java | 38 +++--- .../BuildNumberPreferenceController.java | 9 +- .../deviceinfo/StorageDashboardFragment.java | 23 ++-- .../SystemUpdatePreferenceController.java | 10 -- .../network/TetherPreferenceController.java | 12 -- .../CastPreferenceController.java | 10 -- ...otificationVolumePreferenceController.java | 2 +- .../notification/SettingPrefController.java | 7 - .../settings/notification/SoundSettings.java | 122 +++++++----------- .../search/BaseSearchIndexProvider.java | 15 ++- .../system/SystemDashboardFragment.java | 17 +-- .../BuildNumberPreferenceControllerTest.java | 28 ++-- .../TetherPreferenceControllerTest.java | 3 - .../CastPreferenceControllerTest.java | 20 +-- .../search/BaseSearchIndexProviderTest.java | 99 ++++++++++++++ 17 files changed, 265 insertions(+), 255 deletions(-) create mode 100644 tests/robotests/src/com/android/settings/search/BaseSearchIndexProviderTest.java diff --git a/src/com/android/settings/DeviceInfoSettings.java b/src/com/android/settings/DeviceInfoSettings.java index 47cb98294a5..89ba999585c 100644 --- a/src/com/android/settings/DeviceInfoSettings.java +++ b/src/com/android/settings/DeviceInfoSettings.java @@ -17,6 +17,7 @@ package com.android.settings; import android.app.Activity; +import android.app.Fragment; import android.content.Context; import android.content.Intent; import android.os.Build; @@ -24,6 +25,7 @@ import android.provider.SearchIndexableResource; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.settings.core.PreferenceController; +import com.android.settings.core.lifecycle.Lifecycle; import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.SummaryLoader; import com.android.settings.deviceinfo.AdditionalSystemUpdatePreferenceController; @@ -38,7 +40,6 @@ import com.android.settings.deviceinfo.ManualPreferenceController; import com.android.settings.deviceinfo.RegulatoryInfoPreferenceController; import com.android.settings.deviceinfo.SELinuxStatusPreferenceController; import com.android.settings.deviceinfo.SafetyInfoPreferenceController; -import com.android.settings.deviceinfo.SafetyLegalPreferenceController; import com.android.settings.deviceinfo.SecurityPatchPreferenceController; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.Indexable; @@ -51,8 +52,6 @@ public class DeviceInfoSettings extends DashboardFragment implements Indexable { private static final String LOG_TAG = "DeviceInfoSettings"; - private BuildNumberPreferenceController mBuildNumberPreferenceController; - @Override public int getMetricsCategory() { return MetricsEvent.DEVICEINFO; @@ -65,7 +64,9 @@ public class DeviceInfoSettings extends DashboardFragment implements Indexable { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (mBuildNumberPreferenceController.onActivityResult(requestCode, resultCode, data)) { + final BuildNumberPreferenceController buildNumberPreferenceController = + getPreferenceController(BuildNumberPreferenceController.class); + if (buildNumberPreferenceController.onActivityResult(requestCode, resultCode, data)) { return; } super.onActivityResult(requestCode, resultCode, data); @@ -83,24 +84,8 @@ public class DeviceInfoSettings extends DashboardFragment implements Indexable { @Override protected List getPreferenceControllers(Context context) { - final List controllers = new ArrayList<>(); - mBuildNumberPreferenceController = - new BuildNumberPreferenceController(context, getActivity(), this /* fragment */); - getLifecycle().addObserver(mBuildNumberPreferenceController); - controllers.add(mBuildNumberPreferenceController); - controllers.add(new AdditionalSystemUpdatePreferenceController(context)); - controllers.add(new ManualPreferenceController(context)); - controllers.add(new FeedbackPreferenceController(this, context)); - controllers.add(new KernelVersionPreferenceController(context)); - controllers.add(new BasebandVersionPreferenceController(context)); - controllers.add(new FirmwareVersionPreferenceController(context, getLifecycle())); - controllers.add(new RegulatoryInfoPreferenceController(context)); - controllers.add(new DeviceModelPreferenceController(context)); - controllers.add(new SecurityPatchPreferenceController(context)); - controllers.add(new FccEquipmentIdPreferenceController(context)); - controllers.add(new SELinuxStatusPreferenceController(context)); - controllers.add(new SafetyInfoPreferenceController(context)); - return controllers; + return buildPreferenceControllers(context, getActivity(), this /* fragment */, + getLifecycle()); } private static class SummaryProvider implements SummaryLoader.SummaryProvider { @@ -131,6 +116,26 @@ public class DeviceInfoSettings extends DashboardFragment implements Indexable { } }; + private static List buildPreferenceControllers(Context context, + Activity activity, Fragment fragment, Lifecycle lifecycle) { + final List controllers = new ArrayList<>(); + controllers.add( + new BuildNumberPreferenceController(context, activity, fragment, lifecycle)); + controllers.add(new AdditionalSystemUpdatePreferenceController(context)); + controllers.add(new ManualPreferenceController(context)); + controllers.add(new FeedbackPreferenceController(fragment, context)); + controllers.add(new KernelVersionPreferenceController(context)); + controllers.add(new BasebandVersionPreferenceController(context)); + controllers.add(new FirmwareVersionPreferenceController(context, lifecycle)); + controllers.add(new RegulatoryInfoPreferenceController(context)); + controllers.add(new DeviceModelPreferenceController(context)); + controllers.add(new SecurityPatchPreferenceController(context)); + controllers.add(new FccEquipmentIdPreferenceController(context)); + controllers.add(new SELinuxStatusPreferenceController(context)); + controllers.add(new SafetyInfoPreferenceController(context)); + return controllers; + } + /** * For Search. */ @@ -146,19 +151,9 @@ public class DeviceInfoSettings extends DashboardFragment implements Indexable { } @Override - public List getNonIndexableKeys(Context context) { - final List keys = new ArrayList<>(); - new SafetyLegalPreferenceController(context).updateNonIndexableKeys(keys); - new BasebandVersionPreferenceController(context).updateNonIndexableKeys(keys); - new FeedbackPreferenceController(null, context).updateNonIndexableKeys(keys); - new AdditionalSystemUpdatePreferenceController(context) - .updateNonIndexableKeys(keys); - new RegulatoryInfoPreferenceController(context).updateNonIndexableKeys(keys); - new SecurityPatchPreferenceController(context).updateNonIndexableKeys(keys); - new FccEquipmentIdPreferenceController(context).updateNonIndexableKeys(keys); - new SELinuxStatusPreferenceController(context).updateNonIndexableKeys(keys); - new SafetyInfoPreferenceController(context).updateNonIndexableKeys(keys); - return keys; + public List getPreferenceControllers(Context context) { + return buildPreferenceControllers(context, null /*activity */, + null /* fragment */, null /* lifecycle */); } }; } diff --git a/src/com/android/settings/DisplaySettings.java b/src/com/android/settings/DisplaySettings.java index 1e0547b4355..6fbffe80315 100644 --- a/src/com/android/settings/DisplaySettings.java +++ b/src/com/android/settings/DisplaySettings.java @@ -69,6 +69,15 @@ public class DisplaySettings extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { + return buildPreferenceControllers(context); + } + + @Override + protected int getHelpResource() { + return R.string.help_uri_display; + } + + private static List buildPreferenceControllers(Context context) { final List controllers = new ArrayList<>(); controllers.add(new AutoBrightnessPreferenceController(context)); controllers.add(new AutoRotatePreferenceController(context)); @@ -87,11 +96,6 @@ public class DisplaySettings extends DashboardFragment { return controllers; } - @Override - protected int getHelpResource() { - return R.string.help_uri_display; - } - public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = new BaseSearchIndexProvider() { @Override @@ -105,33 +109,9 @@ public class DisplaySettings extends DashboardFragment { return result; } - @Override - public List getNonIndexableKeys(Context context) { - ArrayList result = new ArrayList<>(); - - new AutoBrightnessPreferenceController(context).updateNonIndexableKeys(result); - new AutoRotatePreferenceController(context).updateNonIndexableKeys(result); - new CameraGesturePreferenceController(context).updateNonIndexableKeys(result); - new DozePreferenceController(context).updateNonIndexableKeys(result); - new FontSizePreferenceController(context).updateNonIndexableKeys(result); - new LiftToWakePreferenceController(context).updateNonIndexableKeys(result); - new NightDisplayPreferenceController(context).updateNonIndexableKeys(result); - new NightModePreferenceController(context).updateNonIndexableKeys(result); - new ScreenSaverPreferenceController(context).updateNonIndexableKeys(result); - new TapToWakePreferenceController(context).updateNonIndexableKeys(result); - new TimeoutPreferenceController(context).updateNonIndexableKeys(result); - new VrDisplayPreferenceController(context).updateNonIndexableKeys(result); - new WallpaperPreferenceController(context).updateNonIndexableKeys(result); - new ThemePreferenceController(context).updateNonIndexableKeys(result); - - return result; - } - @Override public List getPreferenceControllers(Context context) { - final List controllers = new ArrayList<>(); - controllers.add(new AutoBrightnessPreferenceController(context)); - return controllers; + return buildPreferenceControllers(context); } }; } diff --git a/src/com/android/settings/applications/assist/ManageAssist.java b/src/com/android/settings/applications/assist/ManageAssist.java index b1ca5c659c7..a54f0ffba6f 100644 --- a/src/com/android/settings/applications/assist/ManageAssist.java +++ b/src/com/android/settings/applications/assist/ManageAssist.java @@ -50,15 +50,7 @@ public class ManageAssist extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { - final Lifecycle lifecycle = getLifecycle(); - final List controllers = new ArrayList<>(); - controllers.add(new DefaultAssistPreferenceController(context)); - controllers.add(new GestureAssistPreferenceController(context)); - controllers.add(new AssistContextPreferenceController(context, lifecycle)); - controllers.add(new AssistScreenshotPreferenceController(context, lifecycle)); - controllers.add(new AssistFlashScreenPreferenceController(context, lifecycle)); - controllers.add(new DefaultVoiceInputPreferenceController(context, lifecycle)); - return controllers; + return buildPreferenceControllers(context, getLifecycle()); } @Override @@ -74,6 +66,19 @@ public class ManageAssist extends DashboardFragment { .setTitle(R.string.assist_footer); } + private static List buildPreferenceControllers(Context context, + Lifecycle lifecycle) { + final List controllers = new ArrayList<>(); + controllers.add(new DefaultAssistPreferenceController(context)); + controllers.add(new GestureAssistPreferenceController(context)); + controllers.add(new AssistContextPreferenceController(context, lifecycle)); + controllers.add(new AssistScreenshotPreferenceController(context, lifecycle)); + controllers.add(new AssistFlashScreenPreferenceController(context, lifecycle)); + controllers.add(new DefaultVoiceInputPreferenceController(context, lifecycle)); + return controllers; + } + + public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = new BaseSearchIndexProvider() { @Override @@ -85,19 +90,8 @@ public class ManageAssist extends DashboardFragment { } @Override - public List getNonIndexableKeys(Context context) { - List result = new ArrayList<>(); - new DefaultAssistPreferenceController(context).updateNonIndexableKeys(result); - new GestureAssistPreferenceController(context).updateNonIndexableKeys(result); - new AssistContextPreferenceController(context, null) - .updateNonIndexableKeys(result); - new AssistScreenshotPreferenceController(context, null) - .updateNonIndexableKeys(result); - new AssistFlashScreenPreferenceController(context, null) - .updateNonIndexableKeys(result); - new DefaultVoiceInputPreferenceController(context, null) - .updateNonIndexableKeys(result); - return result; + public List getPreferenceControllers(Context context) { + return buildPreferenceControllers(context, null /* lifecycle */); } }; } diff --git a/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java b/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java index a9fd21b0ebe..6c492cc01d8 100644 --- a/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java +++ b/src/com/android/settings/deviceinfo/BuildNumberPreferenceController.java @@ -36,6 +36,7 @@ import com.android.settings.R; import com.android.settings.Utils; import com.android.settings.core.PreferenceController; import com.android.settings.core.instrumentation.MetricsFeatureProvider; +import com.android.settings.core.lifecycle.Lifecycle; import com.android.settings.core.lifecycle.LifecycleObserver; import com.android.settings.core.lifecycle.events.OnResume; import com.android.settings.overlay.FeatureFactory; @@ -61,12 +62,16 @@ public class BuildNumberPreferenceController extends PreferenceController private int mDevHitCountdown; private boolean mProcessingLastDevHit; - public BuildNumberPreferenceController(Context context, Activity activity, Fragment fragment) { + public BuildNumberPreferenceController(Context context, Activity activity, Fragment fragment, + Lifecycle lifecycle) { super(context); mActivity = activity; mFragment = fragment; - mUm = UserManager.get(activity); + mUm = UserManager.get(context); mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); + if (lifecycle != null) { + lifecycle.addObserver(this); + } } @Override diff --git a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java index 72e1493409c..298a7add8c5 100644 --- a/src/com/android/settings/deviceinfo/StorageDashboardFragment.java +++ b/src/com/android/settings/deviceinfo/StorageDashboardFragment.java @@ -184,16 +184,19 @@ public class StorageDashboardFragment extends DashboardFragment } @Override - public List getNonIndexableKeys(Context context) { - if (!FeatureFactory.getFactory(context).getDashboardFeatureProvider(context) - .isEnabled()) { - return null; - } - final ManageStoragePreferenceController controller = - new ManageStoragePreferenceController(context); - final List keys = new ArrayList<>(); - controller.updateNonIndexableKeys(keys); - return keys; + public List getPreferenceControllers(Context context) { + final StorageManager sm = context.getSystemService(StorageManager.class); + final UserManagerWrapper userManager = + new UserManagerWrapperImpl(context.getSystemService(UserManager.class)); + final List controllers = new ArrayList<>(); + controllers.add(new StorageSummaryDonutPreferenceController(context)); + controllers.add(new StorageItemPreferenceController(context, null /* host */, + null /* volume */, new StorageManagerVolumeProvider(sm))); + controllers.addAll(SecondaryUserController.getSecondaryUserControllers( + context, userManager)); + controllers.add(new ManageStoragePreferenceController(context)); + return controllers; } + }; } diff --git a/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java b/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java index 0bf43e24dc2..4fcacd87dcb 100644 --- a/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java +++ b/src/com/android/settings/deviceinfo/SystemUpdatePreferenceController.java @@ -28,8 +28,6 @@ import android.util.Log; import com.android.settings.Utils; import com.android.settings.core.PreferenceController; -import java.util.List; - import static android.content.Context.CARRIER_CONFIG_SERVICE; public class SystemUpdatePreferenceController extends PreferenceController { @@ -66,14 +64,6 @@ public class SystemUpdatePreferenceController extends PreferenceController { } } - @Override - public void updateNonIndexableKeys(List keys) { - // TODO: system update needs to be fixed for non-owner user b/22760654 - if (!isAvailable()) { - keys.add(KEY_SYSTEM_UPDATE_SETTINGS); - } - } - @Override public boolean handlePreferenceTreeClick(Preference preference) { if (KEY_SYSTEM_UPDATE_SETTINGS.equals(preference.getKey())) { diff --git a/src/com/android/settings/network/TetherPreferenceController.java b/src/com/android/settings/network/TetherPreferenceController.java index 236fa96a282..5b8d55eee22 100644 --- a/src/com/android/settings/network/TetherPreferenceController.java +++ b/src/com/android/settings/network/TetherPreferenceController.java @@ -21,7 +21,6 @@ import android.bluetooth.BluetoothProfile; import android.content.Context; import android.net.ConnectivityManager; import android.os.UserHandle; -import android.os.UserManager; import android.support.annotation.VisibleForTesting; import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceScreen; @@ -30,7 +29,6 @@ import com.android.settings.R; import com.android.settings.TetherSettings; import com.android.settings.core.PreferenceController; -import java.util.List; import java.util.concurrent.atomic.AtomicReference; import static android.os.UserManager.DISALLOW_CONFIG_TETHERING; @@ -45,7 +43,6 @@ public class TetherPreferenceController extends PreferenceController { private final AtomicReference mBluetoothPan; private final ConnectivityManager mConnectivityManager; private final BluetoothAdapter mBluetoothAdapter; - private final UserManager mUserManager; private final BluetoothProfile.ServiceListener mBtProfileServiceListener = new android.bluetooth.BluetoothProfile.ServiceListener() { @@ -68,7 +65,6 @@ public class TetherPreferenceController extends PreferenceController { mBluetoothPan = null; mConnectivityManager = null; mBluetoothAdapter = null; - mUserManager = null; } public TetherPreferenceController(Context context) { @@ -78,7 +74,6 @@ public class TetherPreferenceController extends PreferenceController { context, DISALLOW_CONFIG_TETHERING, UserHandle.myUserId()) != null; mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter != null) { mBluetoothAdapter.getProfileProxy(context, mBtProfileServiceListener, @@ -113,13 +108,6 @@ public class TetherPreferenceController extends PreferenceController { updateSummary(); } - @Override - public void updateNonIndexableKeys(List keys) { - if (!mUserManager.isAdminUser() || !mConnectivityManager.isTetheringSupported()) { - keys.add(KEY_TETHER_SETTINGS); - } - } - @Override public String getPreferenceKey() { return KEY_TETHER_SETTINGS; diff --git a/src/com/android/settings/notification/CastPreferenceController.java b/src/com/android/settings/notification/CastPreferenceController.java index 7de5c4873a3..0156dfdbe84 100644 --- a/src/com/android/settings/notification/CastPreferenceController.java +++ b/src/com/android/settings/notification/CastPreferenceController.java @@ -17,11 +17,8 @@ package com.android.settings.notification; import android.content.Context; -import android.support.v7.preference.Preference; -import com.android.settings.Utils; import com.android.settings.core.PreferenceController; -import java.util.List; public class CastPreferenceController extends PreferenceController { @@ -41,11 +38,4 @@ public class CastPreferenceController extends PreferenceController { return true; } - @Override - public void updateNonIndexableKeys(List keys) { - if (!Utils.isVoiceCapable(mContext)) { - keys.add(KEY_WIFI_DISPLAY); - } - } - } diff --git a/src/com/android/settings/notification/NotificationVolumePreferenceController.java b/src/com/android/settings/notification/NotificationVolumePreferenceController.java index e4759d71abc..40448a432fd 100644 --- a/src/com/android/settings/notification/NotificationVolumePreferenceController.java +++ b/src/com/android/settings/notification/NotificationVolumePreferenceController.java @@ -37,7 +37,7 @@ public class NotificationVolumePreferenceController extends @VisibleForTesting NotificationVolumePreferenceController(Context context, - VolumeSeekBarPreference.Callback callback, Lifecycle lifecycle, AudioHelper helper) { + Callback callback, Lifecycle lifecycle, AudioHelper helper) { super(context, callback, lifecycle); mHelper = helper; } diff --git a/src/com/android/settings/notification/SettingPrefController.java b/src/com/android/settings/notification/SettingPrefController.java index d126fc4727a..64d65bb8411 100644 --- a/src/com/android/settings/notification/SettingPrefController.java +++ b/src/com/android/settings/notification/SettingPrefController.java @@ -70,13 +70,6 @@ public abstract class SettingPrefController extends PreferenceController impleme return mPreference.isApplicable(mContext); } - @Override - public void updateNonIndexableKeys(List keys) { - if (!mPreference.isApplicable(mContext)) { - keys.add(mPreference.getKey()); - } - } - @Override public void updateState(Preference preference) { mPreference.update(mContext); diff --git a/src/com/android/settings/notification/SoundSettings.java b/src/com/android/settings/notification/SoundSettings.java index a5a1bfd52d6..df605736c02 100644 --- a/src/com/android/settings/notification/SoundSettings.java +++ b/src/com/android/settings/notification/SoundSettings.java @@ -33,7 +33,6 @@ import com.android.settings.RingtonePreference; import com.android.settings.core.PreferenceController; import com.android.settings.core.lifecycle.Lifecycle; import com.android.settings.dashboard.DashboardFragment; -import com.android.settings.overlay.FeatureFactory; import com.android.settings.search.BaseSearchIndexProvider; import java.util.ArrayList; @@ -51,7 +50,6 @@ public class SoundSettings extends DashboardFragment { private final VolumePreferenceCallback mVolumeCallback = new VolumePreferenceCallback(); private final H mHandler = new H(); - private WorkSoundPreferenceController mWorkSoundController; private RingtonePreference mRequestPreference; @Override @@ -105,40 +103,7 @@ public class SoundSettings extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { - final List controllers = new ArrayList<>(); - Lifecycle lifecycle = getLifecycle(); - controllers.add(new ZenModePreferenceController(context)); - controllers.add(new EmergencyBroadcastPreferenceController(context)); - controllers.add(new VibrateWhenRingPreferenceController(context)); - - // === Volumes === - controllers.add(new AlarmVolumePreferenceController(context, mVolumeCallback, lifecycle)); - controllers.add(new MediaVolumePreferenceController(context, mVolumeCallback, lifecycle)); - controllers.add( - new NotificationVolumePreferenceController(context, mVolumeCallback, lifecycle)); - controllers.add(new RingVolumePreferenceController(context, mVolumeCallback, lifecycle)); - - // === Phone & notification ringtone === - controllers.add(new PhoneRingtonePreferenceController(context)); - controllers.add(new AlarmRingtonePreferenceController(context)); - controllers.add(new NotificationRingtonePreferenceController(context)); - - // === Work Sound Settings === - mWorkSoundController = new WorkSoundPreferenceController(context, this, getLifecycle()); - controllers.add(mWorkSoundController); - - // === Other Sound Settings === - controllers.add(new DialPadTonePreferenceController(context, this, lifecycle)); - controllers.add(new ScreenLockSoundPreferenceController(context, this, lifecycle)); - controllers.add(new ChargingSoundPreferenceController(context, this, lifecycle)); - controllers.add(new DockingSoundPreferenceController(context, this, lifecycle)); - controllers.add(new TouchSoundPreferenceController(context, this, lifecycle)); - controllers.add(new VibrateOnTouchPreferenceController(context, this, lifecycle)); - controllers.add(new DockAudioMediaPreferenceController(context, this, lifecycle)); - controllers.add(new BootSoundPreferenceController(context)); - controllers.add(new EmergencyTonePreferenceController(context, this, lifecycle)); - - return controllers; + return buildPreferenceControllers(context, this, mVolumeCallback, getLifecycle()); } @Override @@ -206,6 +171,43 @@ public class SoundSettings extends DashboardFragment { } } + private static List buildPreferenceControllers(Context context, + SoundSettings fragment, VolumeSeekBarPreference.Callback callback, + Lifecycle lifecycle) { + final List controllers = new ArrayList<>(); + controllers.add(new ZenModePreferenceController(context)); + controllers.add(new EmergencyBroadcastPreferenceController(context)); + controllers.add(new VibrateWhenRingPreferenceController(context)); + + // === Volumes === + controllers.add(new AlarmVolumePreferenceController(context, callback, lifecycle)); + controllers.add(new MediaVolumePreferenceController(context, callback, lifecycle)); + controllers.add( + new NotificationVolumePreferenceController(context, callback, lifecycle)); + controllers.add(new RingVolumePreferenceController(context, callback, lifecycle)); + + // === Phone & notification ringtone === + controllers.add(new PhoneRingtonePreferenceController(context)); + controllers.add(new AlarmRingtonePreferenceController(context)); + controllers.add(new NotificationRingtonePreferenceController(context)); + + // === Work Sound Settings === + controllers.add(new WorkSoundPreferenceController(context, fragment, lifecycle)); + + // === Other Sound Settings === + controllers.add(new DialPadTonePreferenceController(context, fragment, lifecycle)); + controllers.add(new ScreenLockSoundPreferenceController(context, fragment, lifecycle)); + controllers.add(new ChargingSoundPreferenceController(context, fragment, lifecycle)); + controllers.add(new DockingSoundPreferenceController(context, fragment, lifecycle)); + controllers.add(new TouchSoundPreferenceController(context, fragment, lifecycle)); + controllers.add(new VibrateOnTouchPreferenceController(context, fragment, lifecycle)); + controllers.add(new DockAudioMediaPreferenceController(context, fragment, lifecycle)); + controllers.add(new BootSoundPreferenceController(context)); + controllers.add(new EmergencyTonePreferenceController(context, fragment, lifecycle)); + + return controllers; + } + // === Indexing === public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = @@ -218,52 +220,20 @@ public class SoundSettings extends DashboardFragment { return Arrays.asList(sir); } - public List getNonIndexableKeys(Context context) { - final ArrayList rt = new ArrayList(); - new NotificationVolumePreferenceController( - context, null /* Callback */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new RingVolumePreferenceController( - context, null /* Callback */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new PhoneRingtonePreferenceController(context).updateNonIndexableKeys(rt); - new VibrateWhenRingPreferenceController(context).updateNonIndexableKeys(rt); - new EmergencyBroadcastPreferenceController(context).updateNonIndexableKeys(rt); - new DialPadTonePreferenceController(context, - null /* SettingsPreferenceFragment */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new ScreenLockSoundPreferenceController(context, - null /* SettingsPreferenceFragment */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new ChargingSoundPreferenceController(context, - null /* SettingsPreferenceFragment */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new DockingSoundPreferenceController(context, - null /* SettingsPreferenceFragment */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new TouchSoundPreferenceController(context, null /* - SettingsPreferenceFragment */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new VibrateOnTouchPreferenceController(context, - null /* SettingsPreferenceFragment */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new DockAudioMediaPreferenceController(context, - null /* SettingsPreferenceFragment */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - new BootSoundPreferenceController(context).updateNonIndexableKeys(rt); - new EmergencyTonePreferenceController(context, - null /* SettingsPreferenceFragment */, - null /* Lifecycle */).updateNonIndexableKeys(rt); - - return rt; + @Override + public List getPreferenceControllers(Context context) { + return buildPreferenceControllers(context, null /* fragment */, + null /* callback */, null /* lifecycle */); } }; // === Work Sound Settings === void enableWorkSync() { - if (mWorkSoundController != null) { - mWorkSoundController.enableWorkSync(); + final WorkSoundPreferenceController workSoundController = + getPreferenceController(WorkSoundPreferenceController.class); + if (workSoundController != null) { + workSoundController.enableWorkSync(); } } } diff --git a/src/com/android/settings/search/BaseSearchIndexProvider.java b/src/com/android/settings/search/BaseSearchIndexProvider.java index bb518ba6625..38c8d40116a 100644 --- a/src/com/android/settings/search/BaseSearchIndexProvider.java +++ b/src/com/android/settings/search/BaseSearchIndexProvider.java @@ -18,8 +18,10 @@ package com.android.settings.search; import android.content.Context; import android.provider.SearchIndexableResource; + import com.android.settings.core.PreferenceController; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -28,7 +30,7 @@ import java.util.List; */ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider { - private static final List EMPTY_LIST = Collections.emptyList(); + private static final List EMPTY_LIST = Collections.emptyList(); public BaseSearchIndexProvider() { } @@ -45,7 +47,16 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider { @Override public List getNonIndexableKeys(Context context) { - return EMPTY_LIST; + final List controllers = getPreferenceControllers(context); + if (controllers != null && !controllers.isEmpty()) { + final List nonIndexableKeys = new ArrayList<>(); + for (PreferenceController controller : controllers) { + controller.updateNonIndexableKeys(nonIndexableKeys); + } + return nonIndexableKeys; + } else { + return EMPTY_LIST; + } } @Override diff --git a/src/com/android/settings/system/SystemDashboardFragment.java b/src/com/android/settings/system/SystemDashboardFragment.java index 2898ce27bf2..c4bf0da6084 100644 --- a/src/com/android/settings/system/SystemDashboardFragment.java +++ b/src/com/android/settings/system/SystemDashboardFragment.java @@ -55,6 +55,10 @@ public class SystemDashboardFragment extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { + return buildPreferenceControllers(context); + } + + private static List buildPreferenceControllers(Context context) { final List controllers = new ArrayList<>(); controllers.add(new SystemUpdatePreferenceController(context, UserManager.get(context))); controllers.add(new AdditionalSystemUpdatePreferenceController(context)); @@ -79,17 +83,8 @@ public class SystemDashboardFragment extends DashboardFragment { } @Override - public List getNonIndexableKeys(Context context) { - if (!FeatureFactory.getFactory(context).getDashboardFeatureProvider(context) - .isEnabled()) { - return null; - } - final List keys = new ArrayList<>(); - new SystemUpdatePreferenceController(context, UserManager.get(context)) - .updateNonIndexableKeys(keys); - new AdditionalSystemUpdatePreferenceController(context) - .updateNonIndexableKeys(keys); - return keys; + public List getPreferenceControllers(Context context) { + return buildPreferenceControllers(context); } }; } diff --git a/tests/robotests/src/com/android/settings/deviceinfo/BuildNumberPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/BuildNumberPreferenceControllerTest.java index 017c5a8b9e3..0a74cc44bab 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/BuildNumberPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/BuildNumberPreferenceControllerTest.java @@ -29,6 +29,7 @@ import com.android.internal.logging.nano.MetricsProto; import com.android.settings.DevelopmentSettings; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; +import com.android.settings.core.lifecycle.Lifecycle; import com.android.settings.testutils.FakeFeatureFactory; import org.junit.Before; @@ -40,6 +41,7 @@ import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; +import org.robolectric.util.ReflectionHelpers; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Answers.RETURNS_DEEP_STUBS; @@ -47,6 +49,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -65,6 +68,7 @@ public class BuildNumberPreferenceControllerTest { @Mock private UserManager mUserManager; + private Lifecycle mLifecycle; private FakeFeatureFactory mFactory; private Preference mPreference; private BuildNumberPreferenceController mController; @@ -74,8 +78,10 @@ public class BuildNumberPreferenceControllerTest { MockitoAnnotations.initMocks(this); FakeFeatureFactory.setupForTest(mContext); mFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); - when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); - mController = new BuildNumberPreferenceController(mContext, mActivity, mFragment); + mLifecycle = new Lifecycle(); + when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); + mController = new BuildNumberPreferenceController( + mContext, mActivity, mFragment, mLifecycle); mPreference = new Preference(RuntimeEnvironment.application); mPreference.setKey(mController.getPreferenceKey()); @@ -104,11 +110,13 @@ public class BuildNumberPreferenceControllerTest { @Test public void handlePrefTreeClick_deviceNotProvisioned_doNothing() { when(mUserManager.isAdminUser()).thenReturn(true); - final Context context = ShadowApplication.getInstance().getApplicationContext(); + final Context context = RuntimeEnvironment.application; Settings.Global.putInt(context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0); - mController = new BuildNumberPreferenceController(context, mActivity, mFragment); + mController = new BuildNumberPreferenceController( + context, mActivity, mFragment, mLifecycle); + ReflectionHelpers.setField(mController, "mContext", context); assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); verify(mFactory.metricsFeatureProvider).action( @@ -118,14 +126,17 @@ public class BuildNumberPreferenceControllerTest { @Test public void handlePrefTreeClick_userHasRestriction_doNothing() { - when(mUserManager.isAdminUser()).thenReturn(true); - final Context context = ShadowApplication.getInstance().getApplicationContext(); + final Context context = spy(RuntimeEnvironment.application); Settings.Global.putInt(context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1); + + when(mUserManager.isAdminUser()).thenReturn(true); when(mUserManager.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)) .thenReturn(true); - mController = new BuildNumberPreferenceController(context, mActivity, mFragment); + mController = new BuildNumberPreferenceController( + mContext, mActivity, mFragment, mLifecycle); + ReflectionHelpers.setField(mController, "mContext", context); assertThat(mController.handlePreferenceTreeClick(mPreference)).isFalse(); verify(mFactory.metricsFeatureProvider).action( @@ -161,7 +172,8 @@ public class BuildNumberPreferenceControllerTest { public void onActivityResult_confirmPasswordRequestCompleted_enableDevPref() { final Context context = ShadowApplication.getInstance().getApplicationContext(); - mController = new BuildNumberPreferenceController(context, mActivity, mFragment); + mController = new BuildNumberPreferenceController( + context, mActivity, mFragment, mLifecycle); final boolean activityResultHandled = mController.onActivityResult( BuildNumberPreferenceController.REQUEST_CONFIRM_PASSWORD_FOR_DEV_PREF, diff --git a/tests/robotests/src/com/android/settings/network/TetherPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/TetherPreferenceControllerTest.java index 3d6a2335172..a856e8c9a5e 100644 --- a/tests/robotests/src/com/android/settings/network/TetherPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/network/TetherPreferenceControllerTest.java @@ -51,8 +51,6 @@ public class TetherPreferenceControllerTest { @Mock private BluetoothAdapter mBluetoothAdapter; @Mock - private UserManager mUserManager; - @Mock private Preference mPreference; private TetherPreferenceController mController; @@ -64,7 +62,6 @@ public class TetherPreferenceControllerTest { ReflectionHelpers.setField(mController, "mContext", mContext); ReflectionHelpers.setField(mController, "mConnectivityManager", mConnectivityManager); ReflectionHelpers.setField(mController, "mBluetoothAdapter", mBluetoothAdapter); - ReflectionHelpers.setField(mController, "mUserManager", mUserManager); } @Test diff --git a/tests/robotests/src/com/android/settings/notification/CastPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/CastPreferenceControllerTest.java index 5fe4c42a6c5..fc9867e1fae 100644 --- a/tests/robotests/src/com/android/settings/notification/CastPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/CastPreferenceControllerTest.java @@ -22,9 +22,6 @@ import android.telephony.TelephonyManager; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; -import java.util.ArrayList; -import java.util.List; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -33,8 +30,10 @@ import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; +import java.util.ArrayList; +import java.util.List; + import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @@ -61,8 +60,7 @@ public class CastPreferenceControllerTest { } @Test - public void updateNonIndexableKeys_voiceCapable_shouldNotUpdate() { - when(mTelephonyManager.isVoiceCapable()).thenReturn(true); + public void updateNonIndexableKeys_shouldNotUpdate() { final List keys = new ArrayList<>(); mController.updateNonIndexableKeys(keys); @@ -70,14 +68,4 @@ public class CastPreferenceControllerTest { assertThat(keys).isEmpty(); } - @Test - public void updateNonIndexableKeys_voiceIncapable_shouldUpdate() { - when(mTelephonyManager.isVoiceCapable()).thenReturn(false); - final List keys = new ArrayList<>(); - - mController.updateNonIndexableKeys(keys); - - assertThat(keys).isNotEmpty(); - } - } diff --git a/tests/robotests/src/com/android/settings/search/BaseSearchIndexProviderTest.java b/tests/robotests/src/com/android/settings/search/BaseSearchIndexProviderTest.java new file mode 100644 index 00000000000..818f8a25655 --- /dev/null +++ b/tests/robotests/src/com/android/settings/search/BaseSearchIndexProviderTest.java @@ -0,0 +1,99 @@ +/* + * 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.settings.search; + + +import android.content.Context; + +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; +import com.android.settings.core.PreferenceController; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class BaseSearchIndexProviderTest { + + private static final String TEST_PREF_KEY = "test_pref_key"; + + @Mock + private Context mContext; + private BaseSearchIndexProvider mIndexProvider; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mIndexProvider = spy(BaseSearchIndexProvider.class); + } + + @Test + public void getNonIndexableKeys_noPreferenceController_shouldReturnEmptyList() { + assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEqualTo(Collections.EMPTY_LIST); + } + + @Test + public void getNonIndexableKeys_preferenceIsAvailable_shouldReturnEmptyList() { + List controllers = new ArrayList<>(); + controllers.add(new PreferenceController(mContext) { + @Override + public boolean isAvailable() { + return true; + } + + @Override + public String getPreferenceKey() { + return TEST_PREF_KEY; + } + }); + doReturn(controllers).when(mIndexProvider).getPreferenceControllers(mContext); + + assertThat(mIndexProvider.getNonIndexableKeys(mContext)).isEqualTo(Collections.EMPTY_LIST); + } + + @Test + public void getNonIndexableKeys_preferenceIsNotAvailable_shouldReturnKey() { + List controllers = new ArrayList<>(); + controllers.add(new PreferenceController(mContext) { + @Override + public boolean isAvailable() { + return false; + } + + @Override + public String getPreferenceKey() { + return TEST_PREF_KEY; + } + }); + doReturn(controllers).when(mIndexProvider).getPreferenceControllers(mContext); + + assertThat(mIndexProvider.getNonIndexableKeys(mContext)).contains(TEST_PREF_KEY); + } +}