Add swipe to see notification to "Notification preferences"

Bug: 31799948
Test: RunSettingsRoboTests
Change-Id: Ib892ca4d5596c1c89e1efaf636a3eefd7cee371a
diff --git a/res/xml/configure_notification_settings.xml b/res/xml/configure_notification_settings.xml
index 2bdb790..626cd33 100644
--- a/res/xml/configure_notification_settings.xml
+++ b/res/xml/configure_notification_settings.xml
@@ -15,29 +15,33 @@
 -->
 
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-                  xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
-        android:title="@string/configure_notification_settings"
-        android:key="configure_notification_settings" >
+                  android:title="@string/configure_notification_settings"
+                  android:key="configure_notification_settings">
 
     <!-- Pulse notification light -->
     <SwitchPreference
-            android:key="notification_pulse"
-            android:title="@string/notification_pulse_title" />
+        android:key="notification_pulse"
+        android:title="@string/notification_pulse_title"/>
 
     <!-- When device is locked -->
     <com.android.settings.notification.RestrictedDropDownPreference
-            android:key="lock_screen_notifications"
-            android:title="@string/lock_screen_notifications_title"
-            android:summary="%s" />
+        android:key="lock_screen_notifications"
+        android:title="@string/lock_screen_notifications_title"
+        android:summary="%s"/>
+
+    <SwitchPreference
+        android:key="gesture_swipe_down_fingerprint"
+        android:title="@string/fingerprint_swipe_for_notifications_title"
+        android:summary="@string/fingerprint_swipe_for_notifications_summary"/>
 
     <PreferenceCategory
         android:key="lock_screen_notifications_profile_header"
-        android:title="@string/profile_section_header" >
+        android:title="@string/profile_section_header">
 
         <com.android.settings.notification.RestrictedDropDownPreference
             android:key="lock_screen_notifications_profile"
             android:title="@string/lock_screen_notifications_title"
-            android:summary="%s" />
+            android:summary="%s"/>
 
     </PreferenceCategory>
 
diff --git a/src/com/android/settings/gestures/GestureSettings.java b/src/com/android/settings/gestures/GestureSettings.java
index 6fc7abe..c68d922 100644
--- a/src/com/android/settings/gestures/GestureSettings.java
+++ b/src/com/android/settings/gestures/GestureSettings.java
@@ -34,9 +34,9 @@
 import com.android.internal.hardware.AmbientDisplayConfiguration;
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.settings.R;
+import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.search.BaseSearchIndexProvider;
 import com.android.settings.search.Indexable;
-import com.android.settings.SettingsPreferenceFragment;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -53,11 +53,11 @@
     private static final String PREF_KEY_DOUBLE_TAP_POWER = "gesture_double_tap_power";
     private static final String PREF_KEY_DOUBLE_TWIST = "gesture_double_twist";
     private static final String PREF_KEY_PICK_UP = "gesture_pick_up";
-    private static final String PREF_KEY_SWIPE_DOWN_FINGERPRINT = "gesture_swipe_down_fingerprint";
     private static final String PREF_KEY_DOUBLE_TAP_SCREEN = "gesture_double_tap_screen";
     private static final String DEBUG_DOZE_COMPONENT = "debug.doze.component";
 
     private List<GesturePreference> mPreferences;
+    private SwipeToNotificationPreferenceController mSwipeToNotificationPreferenceController;
 
     private AmbientDisplayConfiguration mAmbientConfig;
 
@@ -66,6 +66,8 @@
         super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.gesture_settings);
         Context context = getActivity();
+        mSwipeToNotificationPreferenceController =
+                new SwipeToNotificationPreferenceController(context);
         mPreferences = new ArrayList();
 
         // Double tap power for camera
@@ -93,11 +95,7 @@
         }
 
         // Fingerprint slide for notifications
-        if (isSystemUINavigationAvailable(context)) {
-            addPreference(PREF_KEY_SWIPE_DOWN_FINGERPRINT, isSystemUINavigationEnabled(context));
-        } else {
-            removePreference(PREF_KEY_SWIPE_DOWN_FINGERPRINT);
-        }
+        mSwipeToNotificationPreferenceController.displayPreference(getPreferenceScreen());
 
         // Double twist for camera mode
         if (isDoubleTwistAvailable(context)) {
@@ -112,7 +110,7 @@
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
-                             Bundle savedInstanceState) {
+            Bundle savedInstanceState) {
         View view = super.onCreateView(inflater, container, savedInstanceState);
         RecyclerView listview = getListView();
         listview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@@ -137,6 +135,13 @@
     }
 
     @Override
+    public void onResume() {
+        super.onResume();
+        mSwipeToNotificationPreferenceController.updateState(
+                findPreference(mSwipeToNotificationPreferenceController.getPreferenceKey()));
+    }
+
+    @Override
     public void onStart() {
         super.onStart();
         for (GesturePreference preference : mPreferences) {
@@ -163,9 +168,6 @@
             Secure.putInt(getContentResolver(), Secure.DOZE_PULSE_ON_PICK_UP, enabled ? 1 : 0);
         } else if (PREF_KEY_DOUBLE_TAP_SCREEN.equals(key)) {
             Secure.putInt(getContentResolver(), Secure.DOZE_PULSE_ON_DOUBLE_TAP, enabled ? 1 : 0);
-        } else if (PREF_KEY_SWIPE_DOWN_FINGERPRINT.equals(key)) {
-            Secure.putInt(getContentResolver(),
-                    Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, enabled ? 1 : 0);
         } else if (PREF_KEY_DOUBLE_TWIST.equals(key)) {
             Secure.putInt(getContentResolver(),
                     Secure.CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED, enabled ? 1 : 0);
@@ -188,16 +190,6 @@
                 com.android.internal.R.bool.config_cameraDoubleTapPowerGestureEnabled);
     }
 
-    private static boolean isSystemUINavigationAvailable(Context context) {
-        return context.getResources().getBoolean(
-                com.android.internal.R.bool.config_supportSystemNavigationKeys);
-    }
-
-    private static boolean isSystemUINavigationEnabled(Context context) {
-        return Secure.getInt(context.getContentResolver(), Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, 0)
-                == 1;
-    }
-
     private static boolean isDoubleTwistAvailable(Context context) {
         return hasSensor(context, R.string.gesture_double_twist_sensor_name,
                 R.string.gesture_double_twist_sensor_vendor);
@@ -227,42 +219,41 @@
     }
 
     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
-        new BaseSearchIndexProvider() {
-            @Override
-            public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
-                     boolean enabled) {
-                ArrayList<SearchIndexableResource> result =
-                        new ArrayList<SearchIndexableResource>();
+            new BaseSearchIndexProvider() {
+                @Override
+                public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
+                        boolean enabled) {
+                    ArrayList<SearchIndexableResource> result =
+                            new ArrayList<SearchIndexableResource>();
 
-                SearchIndexableResource sir = new SearchIndexableResource(context);
-                sir.xmlResId = R.xml.gesture_settings;
-                result.add(sir);
+                    SearchIndexableResource sir = new SearchIndexableResource(context);
+                    sir.xmlResId = R.xml.gesture_settings;
+                    result.add(sir);
 
-                return result;
-            }
+                    return result;
+                }
 
-            @Override
-            public List<String> getNonIndexableKeys(Context context) {
-                ArrayList<String> result = new ArrayList<String>();
-                AmbientDisplayConfiguration ambientConfig
-                        = new AmbientDisplayConfiguration(context);
-                if (!isCameraDoubleTapPowerGestureAvailable(context.getResources())) {
-                    result.add(PREF_KEY_DOUBLE_TAP_POWER);
+                @Override
+                public List<String> getNonIndexableKeys(Context context) {
+                    ArrayList<String> result = new ArrayList<String>();
+                    AmbientDisplayConfiguration ambientConfig
+                            = new AmbientDisplayConfiguration(context);
+                    if (!isCameraDoubleTapPowerGestureAvailable(context.getResources())) {
+                        result.add(PREF_KEY_DOUBLE_TAP_POWER);
+                    }
+                    if (!ambientConfig.pulseOnPickupAvailable()) {
+                        result.add(PREF_KEY_PICK_UP);
+                    }
+                    if (!ambientConfig.pulseOnDoubleTapAvailable()) {
+                        result.add(PREF_KEY_DOUBLE_TAP_SCREEN);
+                    }
+                    new SwipeToNotificationPreferenceController(context)
+                            .updateNonIndexableKeys(result);
+                    if (!isDoubleTwistAvailable(context)) {
+                        result.add(PREF_KEY_DOUBLE_TWIST);
+                    }
+                    return result;
                 }
-                if (!ambientConfig.pulseOnPickupAvailable()) {
-                    result.add(PREF_KEY_PICK_UP);
-                }
-                if (!ambientConfig.pulseOnDoubleTapAvailable()) {
-                    result.add(PREF_KEY_DOUBLE_TAP_SCREEN);
-                }
-                if (!isSystemUINavigationAvailable(context)) {
-                    result.add(PREF_KEY_SWIPE_DOWN_FINGERPRINT);
-                }
-                if (!isDoubleTwistAvailable(context)) {
-                    result.add(PREF_KEY_DOUBLE_TWIST);
-                }
-                return result;
-            }
-        };
+            };
 
 }
diff --git a/src/com/android/settings/gestures/SwipeToNotificationPreferenceController.java b/src/com/android/settings/gestures/SwipeToNotificationPreferenceController.java
new file mode 100644
index 0000000..dee532f
--- /dev/null
+++ b/src/com/android/settings/gestures/SwipeToNotificationPreferenceController.java
@@ -0,0 +1,71 @@
+/*
+ * 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.provider.Settings;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.TwoStatePreference;
+
+import com.android.settings.core.PreferenceController;
+
+public class SwipeToNotificationPreferenceController extends PreferenceController
+        implements Preference.OnPreferenceChangeListener {
+
+    private static final String PREF_KEY_SWIPE_DOWN_FINGERPRINT = "gesture_swipe_down_fingerprint";
+
+    public SwipeToNotificationPreferenceController(Context context) {
+        super(context);
+    }
+
+    @Override
+    public boolean handlePreferenceTreeClick(Preference preference) {
+        return false;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return PREF_KEY_SWIPE_DOWN_FINGERPRINT;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        super.updateState(preference);
+        if (preference != null && preference instanceof TwoStatePreference) {
+            ((TwoStatePreference) preference).setChecked(isSystemUINavigationEnabled());
+        }
+    }
+
+    @Override
+    protected boolean isAvailable() {
+        return mContext.getResources().getBoolean(
+                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;
+    }
+}
diff --git a/src/com/android/settings/notification/ConfigureNotificationSettings.java b/src/com/android/settings/notification/ConfigureNotificationSettings.java
index 0ed49d5..1f322d0 100644
--- a/src/com/android/settings/notification/ConfigureNotificationSettings.java
+++ b/src/com/android/settings/notification/ConfigureNotificationSettings.java
@@ -17,11 +17,14 @@
 package com.android.settings.notification;
 
 import android.content.Context;
+import android.os.Bundle;
 
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.settings.R;
 import com.android.settings.core.PreferenceController;
 import com.android.settings.dashboard.DashboardFragment;
+import com.android.settings.gestures.SwipeToNotificationPreferenceController;
+import com.android.settings.overlay.FeatureFactory;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -59,8 +62,21 @@
         mLockScreenNotificationController = new LockScreenNotificationPreferenceController(context);
         getLifecycle().addObserver(pulseController);
         getLifecycle().addObserver(mLockScreenNotificationController);
+        controllers.add(new SwipeToNotificationPreferenceController(context));
         controllers.add(pulseController);
         controllers.add(mLockScreenNotificationController);
         return controllers;
     }
+
+    @Override
+    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
+        super.onCreatePreferences(savedInstanceState, rootKey);
+        final Context context = getContext();
+        if (!FeatureFactory.getFactory(context).getDashboardFeatureProvider(context).isEnabled()) {
+            final String prefKey = getPreferenceController(
+                    SwipeToNotificationPreferenceController.class)
+                    .getPreferenceKey();
+            removePreference(prefKey);
+        }
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/gestures/SwipeToNotificationPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/gestures/SwipeToNotificationPreferenceControllerTest.java
new file mode 100644
index 0000000..e2b4473
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/gestures/SwipeToNotificationPreferenceControllerTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.provider.Settings;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceScreen;
+import android.support.v7.preference.TwoStatePreference;
+
+import com.android.settings.TestConfig;
+
+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.RobolectricTestRunner;
+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 org.mockito.Mockito.when;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+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);
+    }
+
+    @Test
+    public void display_configIsTrue_shouldDisplay() {
+        when(mContext.getResources().
+                getBoolean(com.android.internal.R.bool.config_supportSystemNavigationKeys))
+                .thenReturn(true);
+        mController.displayPreference(mScreen);
+
+        verify(mScreen, never()).removePreference(any(Preference.class));
+    }
+
+    @Test
+    public void display_configIsFalse_shouldNotDisplay() {
+        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));
+    }
+
+    @Test
+    public void updateState_preferenceSetCheckedWhenSettingIsOn() {
+        final TwoStatePreference preference = mock(TwoStatePreference.class);
+        final Context context = ShadowApplication.getInstance().getApplicationContext();
+        Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 1);
+
+        mController = new SwipeToNotificationPreferenceController(context);
+        mController.updateState(preference);
+
+        verify(preference).setChecked(true);
+    }
+
+    @Test
+    public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
+        final TwoStatePreference preference = mock(TwoStatePreference.class);
+        final Context context = ShadowApplication.getInstance().getApplicationContext();
+        Settings.System.putInt(context.getContentResolver(), SYSTEM_NAVIGATION_KEYS_ENABLED, 0);
+
+        mController = new SwipeToNotificationPreferenceController(context);
+        mController.updateState(preference);
+
+        verify(preference).setChecked(false);
+    }
+
+}