Add display white balance setting

Bug: 111215474
Test: make RunSettingsRoboTests -j100
Change-Id: Icf31ff820008740312ad811d0eda72fd3fb90a63
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 126a835..56b7c66 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2644,6 +2644,9 @@
     <!-- Description about the feature adaptive brightness -->
     <string name="auto_brightness_description">Your screen brightness will automatically adjust to your environment and activities. You can move the slider manually to help adaptive brightness learn your preferences.</string>
 
+    <!-- Display settings screen, display white balance settings title [CHAR LIMIT=30] -->
+    <string name="display_white_balance_title">Display white balance</string>
+
     <!-- Night display screen, setting option name to enable night display (renamed "Night Light" with title caps). [CHAR LIMIT=30] -->
     <string name="night_display_title">Night Light</string>
     <!-- Night display screen, description of night display feature (renamed "Night Light" with title caps). [CHAR LIMIT=NONE] -->
diff --git a/res/xml/display_settings.xml b/res/xml/display_settings.xml
index 50a5649..76b3d2e 100644
--- a/res/xml/display_settings.xml
+++ b/res/xml/display_settings.xml
@@ -79,6 +79,11 @@
         settings:controller="com.android.settings.display.ColorModePreferenceController"
         settings:keywords="@string/keywords_color_mode" />
 
+    <SwitchPreference
+        android:key="display_white_balance"
+        android:title="@string/display_white_balance_title"
+        settings:controller="com.android.settings.display.DisplayWhiteBalancePreferenceController" />
+
     <Preference
         android:key="font_size"
         android:title="@string/title_font_size"
diff --git a/src/com/android/settings/display/DisplayWhiteBalancePreferenceController.java b/src/com/android/settings/display/DisplayWhiteBalancePreferenceController.java
new file mode 100644
index 0000000..f1363a3
--- /dev/null
+++ b/src/com/android/settings/display/DisplayWhiteBalancePreferenceController.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2018 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.display;
+
+import android.content.Context;
+import android.hardware.display.ColorDisplayManager;
+import android.os.UserHandle;
+import android.provider.Settings.Secure;
+import com.android.settings.core.TogglePreferenceController;
+
+public class DisplayWhiteBalancePreferenceController extends TogglePreferenceController {
+
+    public DisplayWhiteBalancePreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return ColorDisplayManager.isDisplayWhiteBalanceAvailable(mContext) ?
+                AVAILABLE : DISABLED_FOR_USER;
+    }
+
+    @Override
+    public boolean isChecked() {
+        return Secure.getIntForUser(mContext.getContentResolver(),
+                Secure.DISPLAY_WHITE_BALANCE_ENABLED, 0, UserHandle.USER_CURRENT) == 1;
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        Secure.putIntForUser(mContext.getContentResolver(), Secure.DISPLAY_WHITE_BALANCE_ENABLED,
+                isChecked ? 1 : 0, UserHandle.USER_CURRENT);
+        return true;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/display/DisplayWhiteBalancePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/DisplayWhiteBalancePreferenceControllerTest.java
new file mode 100644
index 0000000..0ce72fb
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/display/DisplayWhiteBalancePreferenceControllerTest.java
@@ -0,0 +1,78 @@
+package com.android.settings.display;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.provider.Settings.Secure;
+import com.android.settings.testutils.shadow.SettingsShadowResources;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(shadows = {
+    SettingsShadowResources.class
+})
+public class DisplayWhiteBalancePreferenceControllerTest {
+
+  private Context mContext;
+  private DisplayWhiteBalancePreferenceController mController;
+
+  @After
+  public void tearDown() {
+    SettingsShadowResources.reset();
+  }
+
+  @Before
+  public void setUp() {
+    mContext = RuntimeEnvironment.application;
+    mController = new DisplayWhiteBalancePreferenceController(mContext, "display_white_balance");
+  }
+
+  @Test
+  public void isAvailable_configuredAvailable() {
+    SettingsShadowResources.overrideResource(
+        com.android.internal.R.bool.config_displayWhiteBalanceAvailable, true);
+    assertThat(mController.isAvailable()).isTrue();
+  }
+
+  @Test
+  public void isAvailable_configuredUnavailable() {
+    SettingsShadowResources.overrideResource(
+        com.android.internal.R.bool.config_displayWhiteBalanceAvailable, false);
+    assertThat(mController.isAvailable()).isFalse();
+  }
+
+  @Test
+  public void setChecked_true() {
+    mController.setChecked(true);
+    assertThat(Settings.Secure
+        .getInt(mContext.getContentResolver(), Secure.DISPLAY_WHITE_BALANCE_ENABLED, 0) == 1)
+        .isTrue();
+  }
+
+  @Test
+  public void setChecked_false() {
+    mController.setChecked(false);
+    assertThat(Settings.Secure
+        .getInt(mContext.getContentResolver(), Secure.DISPLAY_WHITE_BALANCE_ENABLED, 0) == 1)
+        .isFalse();
+  }
+
+  @Test
+  public void isChecked_true() {
+    Settings.Secure.putInt(mContext.getContentResolver(), Secure.DISPLAY_WHITE_BALANCE_ENABLED, 1);
+    assertThat(mController.isChecked()).isTrue();
+  }
+
+  @Test
+  public void isChecked_false() {
+    Settings.Secure.putInt(mContext.getContentResolver(), Secure.DISPLAY_WHITE_BALANCE_ENABLED, 0);
+    assertThat(mController.isChecked()).isFalse();
+  }
+}