Add ResultPayload for always-on ambient display.

Bug: 62022517
Test: robotest
Change-Id: Ib6382030ffa0c159ab6ca4b45dd966ee9287181f
Merged-In: Ib4ee4d8d999e5a1f9edf34ea6068644b41fc4838
diff --git a/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java b/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java
index 5f97608..e062276 100644
--- a/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java
+++ b/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceController.java
@@ -15,21 +15,26 @@
  */
 package com.android.settings.display;
 
-import static android.provider.Settings.Secure.DOZE_ALWAYS_ON;
-
-
 import android.content.Context;
+import android.content.Intent;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.support.v14.preference.SwitchPreference;
 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.search.DatabaseIndexingUtils;
+import com.android.settings.search.InlineSwitchPayload;
+import com.android.settings.search.ResultPayload;
 
 public class AmbientDisplayAlwaysOnPreferenceController extends PreferenceController
         implements Preference.OnPreferenceChangeListener {
 
+    private final int ON = 1;
+    private final int OFF = 0;
+
     private static final String KEY_ALWAYS_ON = "ambient_display_always_on";
     private static final int MY_USER = UserHandle.myUserId();
 
@@ -59,8 +64,9 @@
 
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
-        int enabled = (boolean) newValue ? 1 : 0;
-        Settings.Secure.putInt(mContext.getContentResolver(), DOZE_ALWAYS_ON, enabled);
+        int enabled = (boolean) newValue ? ON : OFF;
+        Settings.Secure.putInt(
+                mContext.getContentResolver(), Settings.Secure.DOZE_ALWAYS_ON, enabled);
         if (mCallback != null) {
             mCallback.onPreferenceChanged();
         }
@@ -71,4 +77,14 @@
     public boolean isAvailable() {
         return mConfig.alwaysOnAvailable();
     }
+
+    @Override
+    public ResultPayload getResultPayload() {
+        final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext,
+                AmbientDisplaySettings.class.getName(), KEY_ALWAYS_ON,
+                mContext.getString(R.string.ambient_display_screen_title));
+
+        return new InlineSwitchPayload(Settings.Secure.DOZE_ALWAYS_ON,
+                ResultPayload.SettingsSource.SECURE, ON, intent, isAvailable());
+    }
 }
diff --git a/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java
index f01190a..4d40300 100644
--- a/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/AmbientDisplayAlwaysOnPreferenceControllerTest.java
@@ -22,13 +22,16 @@
 import static org.mockito.Mockito.verify;
 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.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.TestConfig;
+import com.android.settings.search.InlinePayload;
+import com.android.settings.search.InlineSwitchPayload;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.testutils.shadow.ShadowSecureSettings;
 
 import org.junit.Before;
@@ -115,4 +118,34 @@
 
         assertThat(mController.isAvailable()).isFalse();
     }
+
+    @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_ALWAYS_ON, 0);
+
+        ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
+        int updatedValue = Settings.Secure.getInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, -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_ALWAYS_ON, currentValue);
+
+        int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
+
+        assertThat(newValue).isEqualTo(currentValue);
+    }
 }