Merge "Add ambient notifications as inline setting." into oc-dr1-dev
diff --git a/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceController.java b/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceController.java
index 9e15afb..ac99d71 100644
--- a/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceController.java
+++ b/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceController.java
@@ -14,6 +14,7 @@
 package com.android.settings.display;
 
 import android.content.Context;
+import android.content.Intent;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.support.annotation.VisibleForTesting;
@@ -21,8 +22,12 @@
 import android.support.v7.preference.Preference;
 
 import com.android.internal.hardware.AmbientDisplayConfiguration;
+import com.android.settings.R;
 import com.android.settings.core.PreferenceController;
 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
+import com.android.settings.search.DatabaseIndexingUtils;
+import com.android.settings.search.InlineSwitchPayload;
+import com.android.settings.search.ResultPayload;
 
 import static android.provider.Settings.Secure.DOZE_ENABLED;
 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.ACTION_AMBIENT_DISPLAY;
@@ -30,6 +35,9 @@
 public class AmbientDisplayNotificationsPreferenceController extends PreferenceController
         implements Preference.OnPreferenceChangeListener {
 
+    private final int ON = 1;
+    private final int OFF = 0;
+
     @VisibleForTesting
     static final String KEY_AMBIENT_DISPLAY_NOTIFICATIONS = "ambient_display_notification";
     private static final int MY_USER = UserHandle.myUserId();
@@ -65,7 +73,7 @@
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         boolean value = (Boolean) newValue;
-        Settings.Secure.putInt(mContext.getContentResolver(), DOZE_ENABLED, value ? 1 : 0);
+        Settings.Secure.putInt(mContext.getContentResolver(), DOZE_ENABLED, value ? ON : OFF);
         return true;
     }
 
@@ -73,4 +81,15 @@
     public boolean isAvailable() {
         return mConfig.pulseOnNotificationAvailable();
     }
+
+    @Override
+    public ResultPayload getResultPayload() {
+        final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext,
+                AmbientDisplaySettings.class.getName(), KEY_AMBIENT_DISPLAY_NOTIFICATIONS,
+                mContext.getString(R.string.ambient_display_screen_title));
+
+        return new InlineSwitchPayload(Settings.Secure.DOZE_ENABLED,
+                ResultPayload.SettingsSource.SECURE, ON /* onValue */, intent, isAvailable(),
+                ON /* defaultValue */);
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java
index 4d40300..3f3f4b5 100644
--- a/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java
@@ -132,7 +132,7 @@
         Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, 0);
 
         ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
-        int updatedValue = Settings.Secure.getInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, -1);
+        int updatedValue = Settings.Secure.getInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, 1);
 
         assertThat(updatedValue).isEqualTo(newValue);
     }
diff --git a/tests/robotests/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceControllerTest.java
index 77cb4c6..242a05d 100644
--- a/tests/robotests/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/AmbientDisplayNotificationsPreferenceControllerTest.java
@@ -27,11 +27,14 @@
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
+import android.content.ContentResolver;
 import android.content.Context;
 import android.provider.Settings;
 import android.support.v14.preference.SwitchPreference;
 
 import com.android.internal.hardware.AmbientDisplayConfiguration;
+import com.android.settings.search.InlinePayload;
+import com.android.settings.search.InlineSwitchPayload;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
@@ -133,4 +136,34 @@
 
         verifyNoMoreInteractions(mMetricsFeatureProvider);
     }
+
+    @Test
+    public void testPreferenceController_ProperResultPayloadType() {
+        assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class);
+    }
+
+    @Test
+    @Config(shadows = ShadowSecureSettings.class)
+    public void testSetValue_updatesCorrectly() {
+        int newValue = 1;
+        ContentResolver resolver = mContext.getContentResolver();
+        Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ENABLED, 0);
+
+        ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
+        int updatedValue = Settings.Secure.getInt(resolver, Settings.Secure.DOZE_ENABLED, 1);
+
+        assertThat(updatedValue).isEqualTo(newValue);
+    }
+
+    @Test
+    @Config(shadows = ShadowSecureSettings.class)
+    public void testGetValue_correctValueReturned() {
+        int currentValue = 1;
+        ContentResolver resolver = mContext.getContentResolver();
+        Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ENABLED, currentValue);
+
+        int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
+
+        assertThat(newValue).isEqualTo(currentValue);
+    }
 }