From 33b0d91d74bb29cbfd49e3f4c3ebd9d99001bfa9 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Wed, 9 Nov 2016 11:35:10 -0800 Subject: [PATCH] Add video to each gesture preference screen. - Refactor GesturePreference to a generic VideoPreference. - The old video_preference.xml is only for magnification video, so renamed. - And use VideoPreference in gesture setting pages. - Refactor common logic into GesturePreferenceController. Bug: 32637613 Test: RunSettingsRoboTests Change-Id: I58580b01a32873cb32c5dc5bf2ec021d5b1400cc --- res/layout/magnification_video_preference.xml | 38 +++++ res/layout/video_preference.xml | 77 +++++---- res/values/attrs.xml | 5 + res/xml/double_tap_power_settings.xml | 8 +- res/xml/double_tap_screen_settings.xml | 8 +- res/xml/double_twist_gesture_settings.xml | 8 +- res/xml/pick_up_gesture_settings.xml | 8 +- res/xml/swipe_to_notification_settings.xml | 8 +- ...oggleScreenMagnificationPreferenceFragment.java | 2 +- .../lifecycle/ObservablePreferenceFragment.java | 7 + .../DoubleTapPowerPreferenceController.java | 32 +--- .../settings/gestures/DoubleTapPowerSettings.java | 2 +- .../DoubleTapScreenPreferenceController.java | 42 ++--- .../settings/gestures/DoubleTapScreenSettings.java | 4 +- .../gestures/DoubleTwistGestureSettings.java | 2 +- .../gestures/DoubleTwistPreferenceController.java | 46 ++---- .../gestures/GesturePreferenceController.java | 89 ++++++++++ .../android/settings/gestures/GestureSettings.java | 22 +-- .../PickupGesturePreferenceController.java | 34 ++-- .../settings/gestures/PickupGestureSettings.java | 4 +- .../SwipeToNotificationPreferenceController.java | 39 ++--- .../gestures/SwipeToNotificationSettings.java | 2 +- .../inputmethod/InputAndGestureSettings.java | 13 +- .../ConfigureNotificationSettings.java | 2 +- .../android/settings/widget/VideoPreference.java | 163 ++++++++++++++++++ .../DoubleTapPowerPreferenceControllerTest.java | 65 ++------ .../DoubleTapScreenPreferenceControllerTest.java | 62 ++----- .../DoubleTwistPreferenceControllerTest.java | 85 ++-------- .../gestures/GesturePreferenceControllerTest.java | 184 +++++++++++++++++++++ .../PIckupGesturePreferenceControllerTest.java | 61 ++----- ...wipeToNotificationPreferenceControllerTest.java | 66 ++------ 31 files changed, 723 insertions(+), 465 deletions(-) create mode 100644 res/layout/magnification_video_preference.xml create mode 100644 src/com/android/settings/gestures/GesturePreferenceController.java create mode 100644 src/com/android/settings/widget/VideoPreference.java create mode 100644 tests/robotests/src/com/android/settings/gestures/GesturePreferenceControllerTest.java diff --git a/res/layout/magnification_video_preference.xml b/res/layout/magnification_video_preference.xml new file mode 100644 index 0000000000..fe7f26fe2d --- /dev/null +++ b/res/layout/magnification_video_preference.xml @@ -0,0 +1,38 @@ + + + + + + + + + \ No newline at end of file diff --git a/res/layout/video_preference.xml b/res/layout/video_preference.xml index fe7f26fe2d..8b0e619808 100644 --- a/res/layout/video_preference.xml +++ b/res/layout/video_preference.xml @@ -1,38 +1,57 @@ - - + + android:layout_height="wrap_content" + android:background="@color/gestures_setting_background_color" + android:clipToPadding="false" + android:gravity="center" + android:minHeight="?android:attr/listPreferredItemHeightSmall" + android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" + android:orientation="horizontal"> - - - + android:padding="@dimen/gesture_animation_padding"> + + + + + + + + + + - \ No newline at end of file diff --git a/res/values/attrs.xml b/res/values/attrs.xml index 6df41d237a..9a74fa5763 100644 --- a/res/values/attrs.xml +++ b/res/values/attrs.xml @@ -142,6 +142,11 @@ + + + + + diff --git a/res/xml/double_tap_power_settings.xml b/res/xml/double_tap_power_settings.xml index b8835ea13f..3376f3c2cb 100644 --- a/res/xml/double_tap_power_settings.xml +++ b/res/xml/double_tap_power_settings.xml @@ -15,7 +15,13 @@ limitations under the License. --> - + + + - + + + - + + + - + + + - + + + getPreferenceControllers(Context context) { final List controllers = new ArrayList<>(); - controllers.add(new DoubleTapPowerPreferenceController(context)); + controllers.add(new DoubleTapPowerPreferenceController(context, getLifecycle())); return controllers; } } diff --git a/src/com/android/settings/gestures/DoubleTapScreenPreferenceController.java b/src/com/android/settings/gestures/DoubleTapScreenPreferenceController.java index be1c53fdb6..722d66aaaa 100644 --- a/src/com/android/settings/gestures/DoubleTapScreenPreferenceController.java +++ b/src/com/android/settings/gestures/DoubleTapScreenPreferenceController.java @@ -20,23 +20,22 @@ import android.annotation.UserIdInt; import android.content.Context; import android.provider.Settings; import android.support.v7.preference.Preference; -import android.support.v7.preference.TwoStatePreference; import com.android.internal.hardware.AmbientDisplayConfiguration; -import com.android.settings.core.PreferenceController; +import com.android.settings.core.lifecycle.Lifecycle; -public class DoubleTapScreenPreferenceController extends PreferenceController - implements Preference.OnPreferenceChangeListener { +public class DoubleTapScreenPreferenceController extends GesturePreferenceController { + private static final String PREF_KEY_VIDEO = "gesture_double_tap_screen_video"; private static final String PREF_KEY_DOUBLE_TAP_SCREEN = "gesture_double_tap_screen"; private final AmbientDisplayConfiguration mAmbientConfig; @UserIdInt private final int mUserId; - public DoubleTapScreenPreferenceController(Context context, AmbientDisplayConfiguration config, - @UserIdInt int userId) { - super(context); + public DoubleTapScreenPreferenceController(Context context, Lifecycle lifecycle, + AmbientDisplayConfiguration config, @UserIdInt int userId) { + super(context, lifecycle); mAmbientConfig = config; mUserId = userId; } @@ -47,25 +46,6 @@ public class DoubleTapScreenPreferenceController extends PreferenceController } @Override - public boolean handlePreferenceTreeClick(Preference preference) { - return false; - } - - @Override - public void updateState(Preference preference) { - final boolean isEnabled = mAmbientConfig.pulseOnDoubleTapEnabled(mUserId); - if (preference != null) { - if (preference instanceof TwoStatePreference) { - ((TwoStatePreference) preference).setChecked(isEnabled); - } else { - preference.setSummary(isEnabled - ? com.android.settings.R.string.gesture_setting_on - : com.android.settings.R.string.gesture_setting_off); - } - } - } - - @Override public String getPreferenceKey() { return PREF_KEY_DOUBLE_TAP_SCREEN; } @@ -77,4 +57,14 @@ public class DoubleTapScreenPreferenceController extends PreferenceController Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP, enabled ? 1 : 0); return true; } + + @Override + protected String getVideoPrefKey() { + return PREF_KEY_VIDEO; + } + + @Override + protected boolean isSwitchPrefEnabled() { + return mAmbientConfig.pulseOnDoubleTapEnabled(mUserId); + } } diff --git a/src/com/android/settings/gestures/DoubleTapScreenSettings.java b/src/com/android/settings/gestures/DoubleTapScreenSettings.java index 12e3b64bfe..3c53947725 100644 --- a/src/com/android/settings/gestures/DoubleTapScreenSettings.java +++ b/src/com/android/settings/gestures/DoubleTapScreenSettings.java @@ -54,8 +54,8 @@ public class DoubleTapScreenSettings extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { final List controllers = new ArrayList<>(); - controllers.add(new DoubleTapScreenPreferenceController( - context, new AmbientDisplayConfiguration(context), UserHandle.myUserId())); + controllers.add(new DoubleTapScreenPreferenceController(context, getLifecycle(), + new AmbientDisplayConfiguration(context), UserHandle.myUserId())); return controllers; } } diff --git a/src/com/android/settings/gestures/DoubleTwistGestureSettings.java b/src/com/android/settings/gestures/DoubleTwistGestureSettings.java index 4f8d2dea31..07b40bb2b2 100644 --- a/src/com/android/settings/gestures/DoubleTwistGestureSettings.java +++ b/src/com/android/settings/gestures/DoubleTwistGestureSettings.java @@ -52,7 +52,7 @@ public class DoubleTwistGestureSettings extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { final List controllers = new ArrayList<>(); - controllers.add(new DoubleTwistPreferenceController(context)); + controllers.add(new DoubleTwistPreferenceController(context, getLifecycle())); return controllers; } } diff --git a/src/com/android/settings/gestures/DoubleTwistPreferenceController.java b/src/com/android/settings/gestures/DoubleTwistPreferenceController.java index 071255ba40..5192484cd2 100644 --- a/src/com/android/settings/gestures/DoubleTwistPreferenceController.java +++ b/src/com/android/settings/gestures/DoubleTwistPreferenceController.java @@ -22,19 +22,18 @@ import android.hardware.Sensor; import android.hardware.SensorManager; import android.provider.Settings; import android.support.v7.preference.Preference; -import android.support.v7.preference.TwoStatePreference; import android.text.TextUtils; import com.android.settings.R; -import com.android.settings.core.PreferenceController; +import com.android.settings.core.lifecycle.Lifecycle; -public class DoubleTwistPreferenceController extends PreferenceController - implements Preference.OnPreferenceChangeListener { +public class DoubleTwistPreferenceController extends GesturePreferenceController { + private static final String PREF_KEY_VIDEO = "gesture_double_twist_video"; private static final String PREF_KEY_DOUBLE_TWIST = "gesture_double_twist"; - public DoubleTwistPreferenceController(Context context) { - super(context); + public DoubleTwistPreferenceController(Context context, Lifecycle lifecycle) { + super(context, lifecycle); } @Override @@ -44,30 +43,25 @@ public class DoubleTwistPreferenceController extends PreferenceController } @Override - public void updateState(Preference preference) { - final boolean isEnabled = isDoubleTwistEnabled(); - if (preference != null) { - if (preference instanceof TwoStatePreference) { - ((TwoStatePreference) preference).setChecked(isEnabled); - } else { - preference.setSummary(isEnabled - ? com.android.settings.R.string.gesture_setting_on - : com.android.settings.R.string.gesture_setting_off); - } - } + protected String getVideoPrefKey() { + return PREF_KEY_VIDEO; } @Override - public boolean handlePreferenceTreeClick(Preference preference) { - return false; + public String getPreferenceKey() { + return PREF_KEY_DOUBLE_TWIST; } @Override - public String getPreferenceKey() { - return PREF_KEY_DOUBLE_TWIST; + public boolean onPreferenceChange(Preference preference, Object newValue) { + final boolean enabled = (boolean) newValue; + Settings.Secure.putInt(mContext.getContentResolver(), + Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled ? 1 : 0); + return true; } - private boolean isDoubleTwistEnabled() { + @Override + protected boolean isSwitchPrefEnabled() { final int doubleTwistEnabled = Settings.Secure.getInt(mContext.getContentResolver(), Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1); return doubleTwistEnabled != 0; @@ -88,12 +82,4 @@ public class DoubleTwistPreferenceController extends PreferenceController } return false; } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - final boolean enabled = (boolean) newValue; - Settings.Secure.putInt(mContext.getContentResolver(), - Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled ? 1 : 0); - return true; - } } diff --git a/src/com/android/settings/gestures/GesturePreferenceController.java b/src/com/android/settings/gestures/GesturePreferenceController.java new file mode 100644 index 0000000000..345f7b7cac --- /dev/null +++ b/src/com/android/settings/gestures/GesturePreferenceController.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2016 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.gestures; + +import android.content.Context; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; +import android.support.v7.preference.TwoStatePreference; + +import com.android.settings.R; +import com.android.settings.core.PreferenceController; +import com.android.settings.core.lifecycle.Lifecycle; +import com.android.settings.core.lifecycle.LifecycleObserver; +import com.android.settings.core.lifecycle.events.OnStart; +import com.android.settings.core.lifecycle.events.OnStop; +import com.android.settings.widget.VideoPreference; + +public abstract class GesturePreferenceController extends PreferenceController + implements Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop { + + private VideoPreference mVideoPreference; + + public GesturePreferenceController(Context context, Lifecycle lifecycle) { + super(context); + if (lifecycle != null) { + lifecycle.addObserver(this); + } + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + if (isAvailable()) { + mVideoPreference = (VideoPreference) screen.findPreference(getVideoPrefKey()); + } + } + + @Override + public void updateState(Preference preference) { + super.updateState(preference); + final boolean isEnabled = isSwitchPrefEnabled(); + if (preference != null) { + if (preference instanceof TwoStatePreference) { + ((TwoStatePreference) preference).setChecked(isEnabled); + } else { + preference.setSummary(isEnabled + ? R.string.gesture_setting_on + : R.string.gesture_setting_off); + } + } + } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + return false; + } + + @Override + public void onStop() { + if (mVideoPreference != null) { + mVideoPreference.onViewInvisible(); + } + } + + @Override + public void onStart() { + if (mVideoPreference != null) { + mVideoPreference.onViewVisible(); + } + } + + protected abstract String getVideoPrefKey(); + + protected abstract boolean isSwitchPrefEnabled(); +} diff --git a/src/com/android/settings/gestures/GestureSettings.java b/src/com/android/settings/gestures/GestureSettings.java index 792150e2c5..98ed5fb5ff 100644 --- a/src/com/android/settings/gestures/GestureSettings.java +++ b/src/com/android/settings/gestures/GestureSettings.java @@ -30,6 +30,7 @@ import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.settings.R; import com.android.settings.core.PreferenceController; +import com.android.settings.core.lifecycle.Lifecycle; import com.android.settings.dashboard.DashboardFragment; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.Indexable; @@ -56,13 +57,14 @@ public class GestureSettings extends DashboardFragment { protected List getPreferenceControllers(Context context) { final AmbientDisplayConfiguration ambientConfig = new AmbientDisplayConfiguration(context); final List controllers = new ArrayList<>(); - controllers.add(new SwipeToNotificationPreferenceController(context)); - controllers.add(new DoubleTapPowerPreferenceController(context)); - controllers.add(new DoubleTwistPreferenceController(context)); + final Lifecycle lifecycle = getLifecycle(); + controllers.add(new SwipeToNotificationPreferenceController(context, lifecycle)); + controllers.add(new DoubleTapPowerPreferenceController(context, lifecycle)); + controllers.add(new DoubleTwistPreferenceController(context, lifecycle)); controllers.add(new PickupGesturePreferenceController( - context, ambientConfig, UserHandle.myUserId())); + context, lifecycle, ambientConfig, UserHandle.myUserId())); controllers.add(new DoubleTapScreenPreferenceController( - context, ambientConfig, UserHandle.myUserId())); + context, lifecycle, ambientConfig, UserHandle.myUserId())); return controllers; } @@ -167,17 +169,17 @@ public class GestureSettings extends DashboardFragment { ArrayList result = new ArrayList(); AmbientDisplayConfiguration ambientConfig = new AmbientDisplayConfiguration(context); - new DoubleTapPowerPreferenceController(context) + new DoubleTapPowerPreferenceController(context, null /* lifecycle */) .updateNonIndexableKeys(result); new PickupGesturePreferenceController( - context, ambientConfig, UserHandle.myUserId()) + context, null /* lifecycle */, ambientConfig, UserHandle.myUserId()) .updateNonIndexableKeys(result); new DoubleTapScreenPreferenceController( - context, ambientConfig, UserHandle.myUserId()) + context, null /* lifecycle */, ambientConfig, UserHandle.myUserId()) .updateNonIndexableKeys(result); - new SwipeToNotificationPreferenceController(context) + new SwipeToNotificationPreferenceController(context, null /* lifecycle */) .updateNonIndexableKeys(result); - new DoubleTwistPreferenceController(context) + new DoubleTwistPreferenceController(context, null /* lifecycle */) .updateNonIndexableKeys(result); return result; } diff --git a/src/com/android/settings/gestures/PickupGesturePreferenceController.java b/src/com/android/settings/gestures/PickupGesturePreferenceController.java index b37e756eb0..225e1d0693 100644 --- a/src/com/android/settings/gestures/PickupGesturePreferenceController.java +++ b/src/com/android/settings/gestures/PickupGesturePreferenceController.java @@ -20,23 +20,22 @@ import android.annotation.UserIdInt; import android.content.Context; import android.provider.Settings; import android.support.v7.preference.Preference; -import android.support.v7.preference.TwoStatePreference; import com.android.internal.hardware.AmbientDisplayConfiguration; -import com.android.settings.core.PreferenceController; +import com.android.settings.core.lifecycle.Lifecycle; -public class PickupGesturePreferenceController extends PreferenceController - implements Preference.OnPreferenceChangeListener { +public class PickupGesturePreferenceController extends GesturePreferenceController { + private static final String PREF_VIDEO_KEY = "gesture_pick_up_video"; private static final String PREF_KEY_PICK_UP = "gesture_pick_up"; private final AmbientDisplayConfiguration mAmbientConfig; @UserIdInt private final int mUserId; - public PickupGesturePreferenceController(Context context, AmbientDisplayConfiguration config, - @UserIdInt int userId) { - super(context); + public PickupGesturePreferenceController(Context context, Lifecycle lifecycle, + AmbientDisplayConfiguration config, @UserIdInt int userId) { + super(context, lifecycle); mAmbientConfig = config; mUserId = userId; } @@ -47,27 +46,18 @@ public class PickupGesturePreferenceController extends PreferenceController } @Override - public boolean handlePreferenceTreeClick(Preference preference) { - return false; + protected String getVideoPrefKey() { + return PREF_VIDEO_KEY; } @Override - public String getPreferenceKey() { - return PREF_KEY_PICK_UP; + protected boolean isSwitchPrefEnabled() { + return mAmbientConfig.pulseOnPickupEnabled(mUserId); } @Override - public void updateState(Preference preference) { - final boolean isEnabled = mAmbientConfig.pulseOnPickupEnabled(mUserId); - if (preference != null) { - if (preference instanceof TwoStatePreference) { - ((TwoStatePreference) preference).setChecked(isEnabled); - } else { - preference.setSummary(isEnabled - ? com.android.settings.R.string.gesture_setting_on - : com.android.settings.R.string.gesture_setting_off); - } - } + public String getPreferenceKey() { + return PREF_KEY_PICK_UP; } @Override diff --git a/src/com/android/settings/gestures/PickupGestureSettings.java b/src/com/android/settings/gestures/PickupGestureSettings.java index 96f07968f7..1298dfed9c 100644 --- a/src/com/android/settings/gestures/PickupGestureSettings.java +++ b/src/com/android/settings/gestures/PickupGestureSettings.java @@ -54,8 +54,8 @@ public class PickupGestureSettings extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { final List controllers = new ArrayList<>(); - controllers.add(new PickupGesturePreferenceController( - context, new AmbientDisplayConfiguration(context), UserHandle.myUserId())); + controllers.add(new PickupGesturePreferenceController(context, getLifecycle(), + new AmbientDisplayConfiguration(context), UserHandle.myUserId())); return controllers; } } diff --git a/src/com/android/settings/gestures/SwipeToNotificationPreferenceController.java b/src/com/android/settings/gestures/SwipeToNotificationPreferenceController.java index 0bacefb3d1..01af974f22 100644 --- a/src/com/android/settings/gestures/SwipeToNotificationPreferenceController.java +++ b/src/com/android/settings/gestures/SwipeToNotificationPreferenceController.java @@ -19,18 +19,16 @@ package com.android.settings.gestures; import android.content.Context; import android.provider.Settings; import android.support.v7.preference.Preference; -import android.support.v7.preference.TwoStatePreference; -import com.android.settings.R; -import com.android.settings.core.PreferenceController; +import com.android.settings.core.lifecycle.Lifecycle; -public class SwipeToNotificationPreferenceController extends PreferenceController - implements Preference.OnPreferenceChangeListener { +public class SwipeToNotificationPreferenceController extends GesturePreferenceController { + private static final String PREF_KEY_VIDEO = "gesture_swipe_down_fingerprint_video"; private static final String PREF_KEY_SWIPE_DOWN_FINGERPRINT = "gesture_swipe_down_fingerprint"; - public SwipeToNotificationPreferenceController(Context context) { - super(context); + public SwipeToNotificationPreferenceController(Context context, Lifecycle lifecycle) { + super(context, lifecycle); } @Override @@ -44,18 +42,8 @@ public class SwipeToNotificationPreferenceController extends PreferenceControlle } @Override - public void updateState(Preference preference) { - super.updateState(preference); - final boolean isEnabled = isSystemUINavigationEnabled(); - if (preference != null) { - if (preference instanceof TwoStatePreference) { - ((TwoStatePreference) preference).setChecked(isEnabled); - } else { - preference.setSummary(isEnabled - ? R.string.gesture_setting_on - : R.string.gesture_setting_off); - } - } + protected String getVideoPrefKey() { + return PREF_KEY_VIDEO; } @Override @@ -64,16 +52,17 @@ public class SwipeToNotificationPreferenceController extends PreferenceControlle com.android.internal.R.bool.config_supportSystemNavigationKeys); } - private boolean isSystemUINavigationEnabled() { - return Settings.Secure.getInt(mContext.getContentResolver(), - Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, 0) - == 1; - } - @Override public boolean onPreferenceChange(Preference preference, Object newValue) { Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, (boolean) newValue ? 1 : 0); return true; } + + @Override + protected boolean isSwitchPrefEnabled() { + return Settings.Secure.getInt(mContext.getContentResolver(), + Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, 0) + == 1; + } } diff --git a/src/com/android/settings/gestures/SwipeToNotificationSettings.java b/src/com/android/settings/gestures/SwipeToNotificationSettings.java index a351fddfe7..7eeba292f4 100644 --- a/src/com/android/settings/gestures/SwipeToNotificationSettings.java +++ b/src/com/android/settings/gestures/SwipeToNotificationSettings.java @@ -52,7 +52,7 @@ public class SwipeToNotificationSettings extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { final List controllers = new ArrayList<>(); - controllers.add(new SwipeToNotificationPreferenceController(context)); + controllers.add(new SwipeToNotificationPreferenceController(context, getLifecycle())); return controllers; } } diff --git a/src/com/android/settings/inputmethod/InputAndGestureSettings.java b/src/com/android/settings/inputmethod/InputAndGestureSettings.java index 965cdece80..eecfd31d25 100644 --- a/src/com/android/settings/inputmethod/InputAndGestureSettings.java +++ b/src/com/android/settings/inputmethod/InputAndGestureSettings.java @@ -23,6 +23,7 @@ import android.support.annotation.VisibleForTesting; import com.android.internal.hardware.AmbientDisplayConfiguration; import com.android.settings.R; import com.android.settings.core.PreferenceController; +import com.android.settings.core.lifecycle.Lifecycle; import com.android.settings.dashboard.DashboardFragment; import com.android.settings.gestures.DoubleTapPowerPreferenceController; import com.android.settings.gestures.DoubleTapScreenPreferenceController; @@ -62,6 +63,7 @@ public class InputAndGestureSettings extends DashboardFragment { @Override protected List getPreferenceControllers(Context context) { + final Lifecycle lifecycle = getLifecycle(); final GameControllerPreferenceController gameControllerPreferenceController = new GameControllerPreferenceController(context); getLifecycle().addObserver(gameControllerPreferenceController); @@ -72,13 +74,14 @@ public class InputAndGestureSettings extends DashboardFragment { final List controllers = new ArrayList<>(); controllers.add(gameControllerPreferenceController); // Gestures - controllers.add(new SwipeToNotificationPreferenceController(context)); - controllers.add(new DoubleTwistPreferenceController(context)); - controllers.add(new DoubleTapPowerPreferenceController(context)); + + controllers.add(new SwipeToNotificationPreferenceController(context, lifecycle)); + controllers.add(new DoubleTwistPreferenceController(context, lifecycle)); + controllers.add(new DoubleTapPowerPreferenceController(context, lifecycle)); controllers.add(new PickupGesturePreferenceController( - context, mAmbientDisplayConfig, UserHandle.myUserId())); + context, lifecycle, mAmbientDisplayConfig, UserHandle.myUserId())); controllers.add(new DoubleTapScreenPreferenceController( - context, mAmbientDisplayConfig, UserHandle.myUserId())); + context, lifecycle, mAmbientDisplayConfig, UserHandle.myUserId())); return controllers; } diff --git a/src/com/android/settings/notification/ConfigureNotificationSettings.java b/src/com/android/settings/notification/ConfigureNotificationSettings.java index 59bd238f03..fd59c97797 100644 --- a/src/com/android/settings/notification/ConfigureNotificationSettings.java +++ b/src/com/android/settings/notification/ConfigureNotificationSettings.java @@ -62,7 +62,7 @@ public class ConfigureNotificationSettings extends DashboardFragment { mLockScreenNotificationController = new LockScreenNotificationPreferenceController(context); getLifecycle().addObserver(pulseController); getLifecycle().addObserver(mLockScreenNotificationController); - controllers.add(new SwipeToNotificationPreferenceController(context)); + controllers.add(new SwipeToNotificationPreferenceController(context, getLifecycle())); controllers.add(pulseController); controllers.add(mLockScreenNotificationController); return controllers; diff --git a/src/com/android/settings/widget/VideoPreference.java b/src/com/android/settings/widget/VideoPreference.java new file mode 100644 index 0000000000..c8786dcb27 --- /dev/null +++ b/src/com/android/settings/widget/VideoPreference.java @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2016 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.widget; + +import android.content.ContentResolver; +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.SurfaceTexture; +import android.media.MediaPlayer; +import android.net.Uri; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceViewHolder; +import android.util.AttributeSet; +import android.util.Log; +import android.view.Surface; +import android.view.TextureView; +import android.view.View; +import android.widget.ImageView; + +import com.android.settings.R; + +/** + * A full width preference that hosts a MP4 video. + */ +public class VideoPreference extends Preference { + + private static final String TAG = "VideoPreference"; + private final Context mContext; + + private Uri mVideoPath; + private MediaPlayer mMediaPlayer; + private boolean mAnimationAvailable; + private boolean mVideoReady; + private int mPreviewResource; + + public VideoPreference(Context context, AttributeSet attrs) { + super(context, attrs); + mContext = context; + TypedArray attributes = context.getTheme().obtainStyledAttributes( + attrs, + com.android.settings.R.styleable.VideoPreference, + 0, 0); + try { + int animation = attributes.getResourceId(R.styleable.VideoPreference_animation, 0); + mVideoPath = new Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) + .authority(context.getPackageName()) + .appendPath(String.valueOf(animation)) + .build(); + mMediaPlayer = MediaPlayer.create(mContext, mVideoPath); + if (mMediaPlayer != null && mMediaPlayer.getDuration() > 0) { + setLayoutResource(R.layout.video_preference); + + mPreviewResource = attributes.getResourceId( + R.styleable.VideoPreference_preview, 0); + + mMediaPlayer.setOnSeekCompleteListener(mp -> mVideoReady = true); + + mMediaPlayer.setOnPreparedListener(mediaPlayer -> mediaPlayer.setLooping(true)); + mAnimationAvailable = true; + } + } catch (Exception e) { + Log.w(TAG, "Animation resource not found. Will not show animation."); + } finally { + attributes.recycle(); + } + } + + @Override + public void onBindViewHolder(PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + + if (!mAnimationAvailable) { + return; + } + + final TextureView video = (TextureView) holder.findViewById(R.id.video_texture_view); + final ImageView imageView = (ImageView) holder.findViewById(R.id.video_preview_image); + final ImageView playButton = (ImageView) holder.findViewById(R.id.video_play_button); + imageView.setImageResource(mPreviewResource); + + video.setOnClickListener(v -> { + if (mMediaPlayer != null) { + if (mMediaPlayer.isPlaying()) { + mMediaPlayer.pause(); + playButton.setVisibility(View.VISIBLE); + } else { + mMediaPlayer.start(); + playButton.setVisibility(View.GONE); + } + } + }); + + video.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { + @Override + public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, + int height) { + if (mMediaPlayer != null) { + mMediaPlayer.setSurface(new Surface(surfaceTexture)); + mVideoReady = false; + mMediaPlayer.seekTo(0); + } + } + + @Override + public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, + int height) { + } + + @Override + public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { + imageView.setVisibility(View.VISIBLE); + return false; + } + + @Override + public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { + if (mVideoReady && imageView.getVisibility() == View.VISIBLE) { + imageView.setVisibility(View.GONE); + } + if (mMediaPlayer != null && !mMediaPlayer.isPlaying() && + playButton.getVisibility() != View.VISIBLE) { + playButton.setVisibility(View.VISIBLE); + } + } + }); + } + + @Override + public void onDetached() { + if (mMediaPlayer != null) { + mMediaPlayer.stop(); + mMediaPlayer.reset(); + mMediaPlayer.release(); + } + super.onDetached(); + } + + public void onViewVisible() { + if (mVideoReady && mMediaPlayer != null && !mMediaPlayer.isPlaying()) { + mMediaPlayer.seekTo(0); + } + } + + public void onViewInvisible() { + if (mMediaPlayer != null && mMediaPlayer.isPlaying()) { + mMediaPlayer.pause(); + } + } +} diff --git a/tests/robotests/src/com/android/settings/gestures/DoubleTapPowerPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/DoubleTapPowerPreferenceControllerTest.java index 9024e0b4c3..6a2e4ea046 100644 --- a/tests/robotests/src/com/android/settings/gestures/DoubleTapPowerPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/gestures/DoubleTapPowerPreferenceControllerTest.java @@ -18,11 +18,8 @@ package com.android.settings.gestures; import android.content.Context; import android.provider.Settings; -import android.support.v7.preference.Preference; import android.support.v7.preference.PreferenceScreen; -import android.support.v7.preference.TwoStatePreference; -import com.android.settings.R; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; @@ -36,10 +33,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; import static android.provider.Settings.Secure.CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; +import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @@ -55,81 +49,46 @@ public class DoubleTapPowerPreferenceControllerTest { @Before public void setUp() { MockitoAnnotations.initMocks(this); - mController = new DoubleTapPowerPreferenceController(mContext); + mController = new DoubleTapPowerPreferenceController(mContext, null); } @Test - public void display_configIsTrue_shouldDisplay() { + public void isAvailable_configIsTrue_shouldReturnTrue() { when(mContext.getResources(). getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled)) .thenReturn(true); - mController.displayPreference(mScreen); - verify(mScreen, never()).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isTrue(); } @Test - public void display_configIsFalse_shouldNotDisplay() { + public void isAvailable_configIsTrue_shouldReturnFalse() { when(mContext.getResources(). getBoolean(com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled)) .thenReturn(false); - when(mScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mock(Preference.class)); - mController.displayPreference(mScreen); - - verify(mScreen).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isFalse(); } @Test - public void updateState_preferenceSetCheckedWhenSettingIsOn() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); + public void testSwitchEnabled_configIsNotSet_shouldReturnTrue() { // Set the setting to be enabled. final Context context = ShadowApplication.getInstance().getApplicationContext(); Settings.System.putInt(context.getContentResolver(), CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 0); + mController = new DoubleTapPowerPreferenceController(context, null); - // Run through updateState - mController = new DoubleTapPowerPreferenceController(context); - mController.updateState(preference); - - // Verify pref is checked (as setting is enabled). - verify(preference).setChecked(true); + assertThat(mController.isSwitchPrefEnabled()).isTrue(); } @Test - public void updateState_preferenceSetUncheckedWhenSettingIsOff() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); + public void testSwitchEnabled_configIsSet_shouldReturnFalse() { // Set the setting to be disabled. final Context context = ShadowApplication.getInstance().getApplicationContext(); Settings.System.putInt(context.getContentResolver(), CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 1); + mController = new DoubleTapPowerPreferenceController(context, null); - // Run through updateState - mController = new DoubleTapPowerPreferenceController(context); - mController.updateState(preference); - - // Verify pref is unchecked (as setting is disabled). - verify(preference).setChecked(false); + assertThat(mController.isSwitchPrefEnabled()).isFalse(); } - - @Test - public void updateState_notTwoStatePreference_setSummary() { - // Mock a regular preference - final Preference preference = mock(Preference.class); - // Set the setting to be disabled. - final Context context = ShadowApplication.getInstance().getApplicationContext(); - Settings.System.putInt(context.getContentResolver(), - CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED, 1); - - // Run through updateState - mController = new DoubleTapPowerPreferenceController(context); - mController.updateState(preference); - - // Verify summary is set to off (as setting is disabled). - verify(preference).setSummary(R.string.gesture_setting_off); - } - } diff --git a/tests/robotests/src/com/android/settings/gestures/DoubleTapScreenPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/DoubleTapScreenPreferenceControllerTest.java index 1e8020ef15..a25d34bd04 100644 --- a/tests/robotests/src/com/android/settings/gestures/DoubleTapScreenPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/gestures/DoubleTapScreenPreferenceControllerTest.java @@ -17,12 +17,8 @@ package com.android.settings.gestures; import android.content.Context; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceScreen; -import android.support.v7.preference.TwoStatePreference; import com.android.internal.hardware.AmbientDisplayConfiguration; -import com.android.settings.R; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; @@ -34,11 +30,8 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; -import static org.mockito.Matchers.any; +import static com.google.common.truth.Truth.assertThat; import static org.mockito.Matchers.anyInt; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @@ -47,8 +40,6 @@ public class DoubleTapScreenPreferenceControllerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private PreferenceScreen mScreen; @Mock private AmbientDisplayConfiguration mAmbientDisplayConfiguration; private DoubleTapScreenPreferenceController mController; @@ -57,68 +48,35 @@ public class DoubleTapScreenPreferenceControllerTest { public void setUp() { MockitoAnnotations.initMocks(this); mController = new DoubleTapScreenPreferenceController( - mContext, mAmbientDisplayConfiguration, 0); + mContext, null, mAmbientDisplayConfiguration, 0); } @Test - public void display_configIsTrue_shouldDisplay() { + public void isAvailable_configIsTrue_shouldReturnTrue() { when(mAmbientDisplayConfiguration.pulseOnDoubleTapAvailable()).thenReturn(true); - mController.displayPreference(mScreen); - - verify(mScreen, never()).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isTrue(); } @Test - public void display_configIsFalse_shouldNotDisplay() { + public void isAvailable_configIsFalse_shouldReturnFalse() { when(mAmbientDisplayConfiguration.pulseOnDoubleTapAvailable()).thenReturn(false); - when(mScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mock(Preference.class)); - - mController.displayPreference(mScreen); - verify(mScreen).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isFalse(); } @Test - public void updateState_preferenceSetCheckedWhenSettingIsOn() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); + public void testSwitchEnabled_configIsSet_shouldReturnTrue() { // Set the setting to be enabled. when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(true); - // Run through updateState - mController.updateState(preference); - - // Verify pref is checked (as setting is enabled). - verify(preference).setChecked(true); + assertThat(mController.isSwitchPrefEnabled()).isTrue(); } @Test - public void updateState_preferenceSetUncheckedWhenSettingIsOff() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); - // Set the setting to be disabled. + public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() { when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(false); - // Run through updateState - mController.updateState(preference); - - // Verify pref is unchecked (as setting is disabled). - verify(preference).setChecked(false); - } - - @Test - public void updateState_notTwoStatePreference_setSummary() { - // Mock a regular preference - final Preference preference = mock(Preference.class); - // Set the setting to be disabled. - when(mAmbientDisplayConfiguration.pulseOnDoubleTapEnabled(anyInt())).thenReturn(false); - - // Run through updateState - mController.updateState(preference); - - // Verify summary is set to off (as setting is disabled). - verify(preference).setSummary(R.string.gesture_setting_off); + assertThat(mController.isSwitchPrefEnabled()).isFalse(); } } diff --git a/tests/robotests/src/com/android/settings/gestures/DoubleTwistPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/DoubleTwistPreferenceControllerTest.java index 8692661309..5b02182748 100644 --- a/tests/robotests/src/com/android/settings/gestures/DoubleTwistPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/gestures/DoubleTwistPreferenceControllerTest.java @@ -20,9 +20,6 @@ import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorManager; import android.provider.Settings; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceScreen; -import android.support.v7.preference.TwoStatePreference; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; @@ -40,11 +37,9 @@ import java.util.ArrayList; import java.util.List; import static android.provider.Settings.Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED; -import static org.mockito.Matchers.any; +import static com.google.common.truth.Truth.assertThat; import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @@ -54,19 +49,17 @@ public class DoubleTwistPreferenceControllerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext; @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private PreferenceScreen mScreen; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) private SensorManager mSensorManager; private DoubleTwistPreferenceController mController; @Before public void setUp() { MockitoAnnotations.initMocks(this); - mController = new DoubleTwistPreferenceController(mContext); + mController = new DoubleTwistPreferenceController(mContext, null); } @Test - public void display_hasSensor_shouldDisplay() { + public void isAvailable_hasSensor_shouldReturnTrue() { // Mock sensors final List sensorList = new ArrayList<>(); sensorList.add(mock(Sensor.class)); @@ -75,28 +68,17 @@ public class DoubleTwistPreferenceControllerTest { when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList); when(sensorList.get(0).getName()).thenReturn("test"); when(sensorList.get(0).getVendor()).thenReturn("test"); - when(mScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mock(Preference.class)); - - // Run through display - mController.displayPreference(mScreen); - // Verify preference is not removed - verify(mScreen, never()).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isTrue(); } @Test - public void display_noSensor_shouldNotDisplay() { - when(mScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mock(Preference.class)); - - mController.displayPreference(mScreen); - - verify(mScreen).removePreference(any(Preference.class)); + public void isAvailable_noSensor_shouldReturnFalse() { + assertThat(mController.isAvailable()).isFalse(); } @Test - public void display_differentSensor_shouldNotDisplay() { + public void isAvailable_differentSensor_shouldReturnFalse() { // Mock sensors final List sensorList = new ArrayList<>(); sensorList.add(mock(Sensor.class)); @@ -104,68 +86,29 @@ public class DoubleTwistPreferenceControllerTest { when(mContext.getSystemService(Context.SENSOR_SERVICE)).thenReturn(mSensorManager); when(mSensorManager.getSensorList(anyInt())).thenReturn(sensorList); when(sensorList.get(0).getName()).thenReturn("not_test"); - when(mScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mock(Preference.class)); - when(mScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mock(Preference.class)); - - // Run through display - mController.displayPreference(mScreen); - // Verify preference is not removed - verify(mScreen).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isFalse(); } - @Test - public void updateState_preferenceSetCheckedWhenSettingIsOn() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); + public void testSwitchEnabled_configIsSet_shouldReturnTrue() { // Set the setting to be enabled. final Context context = ShadowApplication.getInstance().getApplicationContext(); Settings.System.putInt(context.getContentResolver(), CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 1); + mController = new DoubleTwistPreferenceController(context, null); - // Run through updateState - mController = new DoubleTwistPreferenceController(context); - mController.updateState(preference); - - // Verify pref is checked (as setting is enabled). - verify(preference).setChecked(true); - } - - @Test - public void updateState_preferenceSetUncheckedWhenSettingIsOff() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); - // Set the setting to be disabled. - final Context context = ShadowApplication.getInstance().getApplicationContext(); - Settings.System.putInt(context.getContentResolver(), - CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0); - - // Run through updateState - mController = new DoubleTwistPreferenceController(context); - mController.updateState(preference); - - // Verify pref is unchecked (as setting is disabled). - verify(preference).setChecked(false); + assertThat(mController.isSwitchPrefEnabled()).isTrue(); } @Test - public void updateState_notTwoStatePreference_setSummary() { - // Mock a regular preference - final Preference preference = mock(Preference.class); + public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() { // Set the setting to be disabled. final Context context = ShadowApplication.getInstance().getApplicationContext(); Settings.System.putInt(context.getContentResolver(), CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, 0); + mController = new DoubleTwistPreferenceController(context, null); - // Run through updateState - mController = new DoubleTwistPreferenceController(context); - mController.updateState(preference); - - // Verify summary is set to off (as setting is disabled). - verify(preference).setSummary(com.android.settings.R.string.gesture_setting_off); + assertThat(mController.isSwitchPrefEnabled()).isFalse(); } - } diff --git a/tests/robotests/src/com/android/settings/gestures/GesturePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/GesturePreferenceControllerTest.java new file mode 100644 index 0000000000..01c192163c --- /dev/null +++ b/tests/robotests/src/com/android/settings/gestures/GesturePreferenceControllerTest.java @@ -0,0 +1,184 @@ +/* + * Copyright (C) 2016 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.gestures; + +import android.content.Context; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; +import android.support.v7.preference.TwoStatePreference; + +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; +import com.android.settings.core.lifecycle.Lifecycle; +import com.android.settings.widget.VideoPreference; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class GesturePreferenceControllerTest { + + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private Context mContext; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private PreferenceScreen mScreen; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private Lifecycle mLifecycle; + private TestPrefController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mController = new TestPrefController(mContext, mLifecycle); + } + + @Test + public void display_configIsTrue_shouldDisplay() { + mController.mIsPrefAvailable = true; + when(mScreen.findPreference(anyString())).thenReturn(mock(VideoPreference.class)); + + mController.displayPreference(mScreen); + + verify(mScreen, never()).removePreference(any(Preference.class)); + } + + @Test + public void display_configIsFalse_shouldNotDisplay() { + mController.mIsPrefAvailable = false; + when(mScreen.findPreference(mController.getPreferenceKey())) + .thenReturn(mock(Preference.class)); + + mController.displayPreference(mScreen); + + verify(mScreen).removePreference(any(Preference.class)); + } + + @Test + public void onStart_shouldStartVideoPreference() { + final VideoPreference videoPreference = mock(VideoPreference.class); + when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(videoPreference); + mController.mIsPrefAvailable = true; + + mController.displayPreference(mScreen); + mController.onStart(); + + verify(videoPreference).onViewVisible(); + } + + @Test + public void onStop_shouldStopVideoPreference() { + final VideoPreference videoPreference = mock(VideoPreference.class); + when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(videoPreference); + mController.mIsPrefAvailable = true; + + mController.displayPreference(mScreen); + mController.onStop(); + + verify(videoPreference).onViewInvisible(); + } + + @Test + public void updateState_preferenceSetCheckedWhenSettingIsOn() { + // Mock a TwoStatePreference + final TwoStatePreference preference = mock(TwoStatePreference.class); + // Set the setting to be enabled. + mController.mIsPrefEnabled = true; + // Run through updateState + mController.updateState(preference); + + // Verify pref is checked (as setting is enabled). + verify(preference).setChecked(true); + } + + @Test + public void updateState_preferenceSetUncheckedWhenSettingIsOff() { + // Mock a TwoStatePreference + final TwoStatePreference preference = mock(TwoStatePreference.class); + // Set the setting to be disabled. + mController.mIsPrefEnabled = false; + + // Run through updateState + mController.updateState(preference); + + // Verify pref is unchecked (as setting is disabled). + verify(preference).setChecked(false); + } + + @Test + public void updateState_notTwoStatePreference_setSummary() { + // Mock a regular preference + final Preference preference = mock(Preference.class); + // Set the setting to be disabled. + mController.mIsPrefEnabled = false; + + // Run through updateState + mController.updateState(preference); + + // Verify summary is set to off (as setting is disabled). + verify(preference).setSummary(com.android.settings.R.string.gesture_setting_off); + } + + private class TestPrefController extends GesturePreferenceController { + + boolean mIsPrefAvailable; + boolean mIsPrefEnabled; + + public TestPrefController(Context context, + Lifecycle lifecycle) { + super(context, lifecycle); + } + + @Override + public boolean isAvailable() { + return mIsPrefAvailable; + } + + @Override + public String getPreferenceKey() { + return null; + } + + @Override + protected String getVideoPrefKey() { + return null; + } + + @Override + protected boolean isSwitchPrefEnabled() { + return mIsPrefEnabled; + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + return false; + } + } +} diff --git a/tests/robotests/src/com/android/settings/gestures/PIckupGesturePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/PIckupGesturePreferenceControllerTest.java index 217d0b82f9..66ae679147 100644 --- a/tests/robotests/src/com/android/settings/gestures/PIckupGesturePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/gestures/PIckupGesturePreferenceControllerTest.java @@ -17,12 +17,8 @@ package com.android.settings.gestures; import android.content.Context; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceScreen; -import android.support.v7.preference.TwoStatePreference; import com.android.internal.hardware.AmbientDisplayConfiguration; -import com.android.settings.R; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; @@ -34,11 +30,8 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.annotation.Config; -import static org.mockito.Matchers.any; +import static com.google.common.truth.Truth.assertThat; import static org.mockito.Matchers.anyInt; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @@ -47,8 +40,6 @@ public class PIckupGesturePreferenceControllerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private PreferenceScreen mScreen; @Mock private AmbientDisplayConfiguration mAmbientDisplayConfiguration; @@ -58,69 +49,37 @@ public class PIckupGesturePreferenceControllerTest { public void setUp() { MockitoAnnotations.initMocks(this); mController = new PickupGesturePreferenceController( - mContext, mAmbientDisplayConfiguration, 0); + mContext, null, mAmbientDisplayConfiguration, 0); } @Test - public void display_configIsTrue_shouldDisplay() { + public void isAvailable_configIsTrue_shouldReturnTrue() { when(mAmbientDisplayConfiguration.pulseOnPickupAvailable()).thenReturn(true); - mController.displayPreference(mScreen); - - verify(mScreen, never()).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isTrue(); } @Test - public void display_configIsFalse_shouldNotDisplay() { + public void isAvailable_configIsFalse_shouldReturnFalse() { when(mAmbientDisplayConfiguration.pulseOnPickupAvailable()).thenReturn(false); - when(mScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mock(Preference.class)); - - mController.displayPreference(mScreen); - verify(mScreen).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isFalse(); } @Test - public void updateState_preferenceSetCheckedWhenSettingIsOn() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); + public void testSwitchEnabled_configIsSet_shouldReturnTrue() { // Set the setting to be enabled. when(mAmbientDisplayConfiguration.pulseOnPickupEnabled(anyInt())).thenReturn(true); - // Run through updateState - mController.updateState(preference); - - // Verify pref is checked (as setting is enabled). - verify(preference).setChecked(true); - } - - @Test - public void updateState_preferenceSetUncheckedWhenSettingIsOff() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); - // Set the setting to be disabled. - when(mAmbientDisplayConfiguration.pulseOnPickupEnabled(anyInt())).thenReturn(false); - - // Run through updateState - mController.updateState(preference); - - // Verify pref is unchecked (as setting is disabled). - verify(preference).setChecked(false); + assertThat(mController.isSwitchPrefEnabled()).isTrue(); } @Test - public void updateState_notTwoStatePreference_setSummary() { - // Mock a regular preference - final Preference preference = mock(Preference.class); + public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() { // Set the setting to be disabled. when(mAmbientDisplayConfiguration.pulseOnPickupEnabled(anyInt())).thenReturn(false); - // Run through updateState - mController.updateState(preference); - - // Verify summary is set to off (as setting is disabled). - verify(preference).setSummary(R.string.gesture_setting_off); + assertThat(mController.isSwitchPrefEnabled()).isFalse(); } } diff --git a/tests/robotests/src/com/android/settings/gestures/SwipeToNotificationPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/SwipeToNotificationPreferenceControllerTest.java index e4841351cb..7b34666c94 100644 --- a/tests/robotests/src/com/android/settings/gestures/SwipeToNotificationPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/gestures/SwipeToNotificationPreferenceControllerTest.java @@ -18,11 +18,7 @@ package com.android.settings.gestures; import android.content.Context; import android.provider.Settings; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceScreen; -import android.support.v7.preference.TwoStatePreference; -import com.android.settings.R; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; @@ -36,10 +32,7 @@ import org.robolectric.annotation.Config; import org.robolectric.shadows.ShadowApplication; import static android.provider.Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; +import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @@ -48,85 +41,50 @@ public class SwipeToNotificationPreferenceControllerTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private PreferenceScreen mScreen; private SwipeToNotificationPreferenceController mController; @Before public void setUp() { MockitoAnnotations.initMocks(this); - mController = new SwipeToNotificationPreferenceController(mContext); + mController = new SwipeToNotificationPreferenceController(mContext, null); } @Test - public void display_configIsTrue_shouldDisplay() { + public void isAvailable_configIsTrue_shouldReturnTrue() { when(mContext.getResources(). getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys)) .thenReturn(true); - mController.displayPreference(mScreen); - verify(mScreen, never()).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isTrue(); } @Test - public void display_configIsFalse_shouldNotDisplay() { + public void isAvailable_configIsFalse_shouldReturnFalse() { when(mContext.getResources(). getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys)) .thenReturn(false); - when(mScreen.findPreference(mController.getPreferenceKey())) - .thenReturn(mock(Preference.class)); - - mController.displayPreference(mScreen); - verify(mScreen).removePreference(any(Preference.class)); + assertThat(mController.isAvailable()).isFalse(); } @Test - public void updateState_preferenceSetCheckedWhenSettingIsOn() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); + public void testSwitchEnabled_configIsSet_shouldReturnTrue() { // Set the setting to be enabled. final Context context = ShadowApplication.getInstance().getApplicationContext(); Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 1); + mController = new SwipeToNotificationPreferenceController(context, null); - // Run through updateState - mController = new SwipeToNotificationPreferenceController(context); - mController.updateState(preference); - - // Verify pref is checked (as setting is enabled). - verify(preference).setChecked(true); - } - - @Test - public void updateState_preferenceSetUncheckedWhenSettingIsOff() { - // Mock a TwoStatePreference - final TwoStatePreference preference = mock(TwoStatePreference.class); - // Set the setting to be disabled. - final Context context = ShadowApplication.getInstance().getApplicationContext(); - Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 0); - - // Run through updateState - mController = new SwipeToNotificationPreferenceController(context); - mController.updateState(preference); - - // Verify pref is unchecked (as setting is disabled). - verify(preference).setChecked(false); + assertThat(mController.isSwitchPrefEnabled()).isTrue(); } @Test - public void updateState_notTwoStatePreference_setSummary() { - // Mock a regular preference - final Preference preference = mock(Preference.class); + public void testSwitchEnabled_configIsNotSet_shouldReturnFalse() { // Set the setting to be disabled. final Context context = ShadowApplication.getInstance().getApplicationContext(); Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 0); + mController = new SwipeToNotificationPreferenceController(context, null); - // Run through updateState - mController = new SwipeToNotificationPreferenceController(context); - mController.updateState(preference); - - // Verify summary is set to off (as setting is disabled). - verify(preference).setSummary(R.string.gesture_setting_off); + assertThat(mController.isSwitchPrefEnabled()).isFalse(); } } -- 2.11.0