Move color mode to DisplayTransformManager

Bug: 68159303
Test: make -j100 && make RunSettingsRoboTests -j100

Merged-In: I6cb3287ab4a8043e41765d55da24914f1744dcfa
Change-Id: I6cb3287ab4a8043e41765d55da24914f1744dcfa
(cherry picked from commit 3d1d4a66284ece018cafd6619731fa64c8fff73b)
diff --git a/src/com/android/settings/display/ColorModePreferenceFragment.java b/src/com/android/settings/display/ColorModePreferenceFragment.java
index 7c8b841..07cf82e 100644
--- a/src/com/android/settings/display/ColorModePreferenceFragment.java
+++ b/src/com/android/settings/display/ColorModePreferenceFragment.java
@@ -13,18 +13,11 @@
  */
 package com.android.settings.display;
 
-import android.app.ActivityManager;
-import android.app.IActivityManager;
 import android.content.Context;
 import android.graphics.drawable.Drawable;
-import android.os.IBinder;
-import android.os.Parcel;
-import android.os.RemoteException;
-import android.os.ServiceManager;
-import android.os.SystemProperties;
 import android.support.annotation.VisibleForTesting;
-import android.util.Log;
 
+import com.android.internal.app.NightDisplayController;
 import com.android.internal.logging.nano.MetricsProto;
 
 import com.android.settings.R;
@@ -35,20 +28,6 @@
 
 @SuppressWarnings("WeakerAccess")
 public class ColorModePreferenceFragment extends RadioButtonPickerFragment {
-    private static final String TAG = "ColorModePreferenceFragment";
-
-    @VisibleForTesting
-    static final float COLOR_SATURATION_NATURAL = 1.0f;
-    @VisibleForTesting
-    static final float COLOR_SATURATION_BOOSTED = 1.1f;
-
-    private static final int SURFACE_FLINGER_TRANSACTION_SATURATION = 1022;
-    private static final int SURFACE_FLINGER_TRANSACTION_NATIVE_MODE = 1023;
-
-    @VisibleForTesting
-    static final String PERSISTENT_PROPERTY_SATURATION = "persist.sys.sf.color_saturation";
-    @VisibleForTesting
-    static final String PERSISTENT_PROPERTY_NATIVE_MODE = "persist.sys.sf.native_mode";
 
     @VisibleForTesting
     static final String KEY_COLOR_MODE_NATURAL = "color_mode_natural";
@@ -57,14 +36,12 @@
     @VisibleForTesting
     static final String KEY_COLOR_MODE_SATURATED = "color_mode_saturated";
 
-    private IBinder mSurfaceFlinger;
-    private IActivityManager mActivityManager;
+    private NightDisplayController mController;
 
     @Override
     public void onAttach(Context context) {
         super.onAttach(context);
-        mSurfaceFlinger = ServiceManager.getService("SurfaceFlinger");
-        mActivityManager = ActivityManager.getService();
+        mController = new NightDisplayController(context);
     }
 
     @Override
@@ -82,10 +59,10 @@
 
     @Override
     protected String getDefaultKey() {
-        if (isNativeModeEnabled()) {
+        if (mController.getColorMode() == NightDisplayController.COLOR_MODE_SATURATED) {
             return KEY_COLOR_MODE_SATURATED;
         }
-        if (getSaturationValue() > COLOR_SATURATION_NATURAL) {
+        if (mController.getColorMode() == NightDisplayController.COLOR_MODE_BOOSTED) {
             return KEY_COLOR_MODE_BOOSTED;
         }
         return KEY_COLOR_MODE_NATURAL;
@@ -95,89 +72,23 @@
     protected boolean setDefaultKey(String key) {
         switch (key) {
             case KEY_COLOR_MODE_NATURAL:
-                applySaturation(COLOR_SATURATION_NATURAL);
-                setNativeMode(false);
+                mController.setColorMode(NightDisplayController.COLOR_MODE_NATURAL);
                 break;
             case KEY_COLOR_MODE_BOOSTED:
-                applySaturation(COLOR_SATURATION_BOOSTED);
-                setNativeMode(false);
+                mController.setColorMode(NightDisplayController.COLOR_MODE_BOOSTED);
                 break;
             case KEY_COLOR_MODE_SATURATED:
-                applySaturation(COLOR_SATURATION_NATURAL);
-                setNativeMode(true);
+                mController.setColorMode(NightDisplayController.COLOR_MODE_SATURATED);
                 break;
         }
-
-        updateConfiguration();
-
         return true;
     }
 
-    @VisibleForTesting
-    void updateConfiguration() {
-        try {
-            mActivityManager.updateConfiguration(null);
-        } catch (RemoteException e) {
-            Log.d(TAG, "Could not update configuration", e);
-        }
-    }
-
     @Override
     public int getMetricsCategory() {
         return MetricsProto.MetricsEvent.COLOR_MODE_SETTINGS;
     }
 
-    /**
-     * Propagates the provided saturation to the SurfaceFlinger.
-     */
-    private void applySaturation(float saturation) {
-        SystemProperties.set(PERSISTENT_PROPERTY_SATURATION, Float.toString(saturation));
-        if (mSurfaceFlinger != null) {
-            final Parcel data = Parcel.obtain();
-            data.writeInterfaceToken("android.ui.ISurfaceComposer");
-            data.writeFloat(saturation);
-            try {
-                mSurfaceFlinger.transact(SURFACE_FLINGER_TRANSACTION_SATURATION, data, null, 0);
-            } catch (RemoteException ex) {
-                Log.e(TAG, "Failed to set saturation", ex);
-            } finally {
-                data.recycle();
-            }
-        }
-    }
-
-    private static float getSaturationValue() {
-        try {
-            return Float.parseFloat(SystemProperties.get(
-                    PERSISTENT_PROPERTY_SATURATION, Float.toString(COLOR_SATURATION_NATURAL)));
-        } catch (NumberFormatException e) {
-            return COLOR_SATURATION_NATURAL;
-        }
-    }
-
-    /**
-     * Toggles native mode on/off in SurfaceFlinger.
-     */
-    private void setNativeMode(boolean enabled) {
-        SystemProperties.set(PERSISTENT_PROPERTY_NATIVE_MODE, enabled ? "1" : "0");
-        if (mSurfaceFlinger != null) {
-            final Parcel data = Parcel.obtain();
-            data.writeInterfaceToken("android.ui.ISurfaceComposer");
-            data.writeInt(enabled ? 1 : 0);
-            try {
-                mSurfaceFlinger.transact(SURFACE_FLINGER_TRANSACTION_NATIVE_MODE, data, null, 0);
-            } catch (RemoteException ex) {
-                Log.e(TAG, "Failed to set native mode", ex);
-            } finally {
-                data.recycle();
-            }
-        }
-    }
-
-    private static boolean isNativeModeEnabled() {
-        return SystemProperties.getBoolean(PERSISTENT_PROPERTY_NATIVE_MODE, false);
-    }
-
     @VisibleForTesting
     static class ColorModeCandidateInfo extends CandidateInfo {
         private final CharSequence mLabel;
@@ -204,4 +115,5 @@
             return mKey;
         }
     }
+
 }
diff --git a/src/com/android/settings/display/NightDisplaySettings.java b/src/com/android/settings/display/NightDisplaySettings.java
index 23ddf07..b84e76e 100644
--- a/src/com/android/settings/display/NightDisplaySettings.java
+++ b/src/com/android/settings/display/NightDisplaySettings.java
@@ -119,6 +119,7 @@
         onCustomStartTimeChanged(mController.getCustomStartTime());
         onCustomEndTimeChanged(mController.getCustomEndTime());
         onColorTemperatureChanged(mController.getColorTemperature());
+        onDisplayColorModeChanged(mController.getColorMode());
     }
 
     @Override
diff --git a/tests/robotests/src/com/android/internal/app/NightDisplayController.java b/tests/robotests/src/com/android/internal/app/NightDisplayController.java
index 2125c19..bc2a0ca 100644
--- a/tests/robotests/src/com/android/internal/app/NightDisplayController.java
+++ b/tests/robotests/src/com/android/internal/app/NightDisplayController.java
@@ -21,7 +21,22 @@
  */
 public class NightDisplayController {
 
-    public interface Callback {
+    public static final int AUTO_MODE_CUSTOM = 1;
+    public static final int COLOR_MODE_NATURAL = 0;
 
+    public static final int AUTO_MODE_TWILIGHT = 2;
+    public static final int COLOR_MODE_BOOSTED = 1;
+    public static final int COLOR_MODE_SATURATED = 2;
+    private int mColorMode;
+
+    public void setColorMode(int colorMode) {
+        mColorMode = colorMode;
+    }
+
+    public int getColorMode() {
+        return mColorMode;
+    }
+
+    public interface Callback {
     }
 }
diff --git a/tests/robotests/src/com/android/settings/display/ColorModePreferenceFragmentTest.java b/tests/robotests/src/com/android/settings/display/ColorModePreferenceFragmentTest.java
index 435f4f2..9ee79ff 100644
--- a/tests/robotests/src/com/android/settings/display/ColorModePreferenceFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/display/ColorModePreferenceFragmentTest.java
@@ -16,18 +16,11 @@
 package com.android.settings.display;
 
 import static com.google.common.truth.Truth.assertThat;
-
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
 
-import android.app.IActivityManager;
-import android.content.res.Configuration;
-import android.os.IBinder;
-import android.os.RemoteException;
-
+import com.android.internal.app.NightDisplayController;
 import com.android.internal.logging.nano.MetricsProto;
-
 import com.android.settings.TestConfig;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
@@ -37,6 +30,7 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
@@ -47,24 +41,19 @@
 @RunWith(SettingsRobolectricTestRunner.class)
 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
 public class ColorModePreferenceFragmentTest {
-    @Mock
-    private IBinder mSurfaceFlinger;
-    @Mock
-    private IActivityManager mActivityManager;
 
     private ColorModePreferenceFragment mFragment;
 
+    @Mock
+    private NightDisplayController mController;
+
     @Before
     public void setup() {
         MockitoAnnotations.initMocks(this);
         SettingsShadowSystemProperties.clear();
 
         mFragment = spy(new ColorModePreferenceFragment());
-        doReturn(RuntimeEnvironment.application).when(mFragment).getContext();
-        doNothing().when(mFragment).updateConfiguration();
-
-        ReflectionHelpers.setField(mFragment, "mSurfaceFlinger", mSurfaceFlinger);
-        ReflectionHelpers.setField(mFragment, "mActivityManager", mActivityManager);
+        ReflectionHelpers.setField(mFragment, "mController", mController);
     }
 
     @Test
@@ -75,6 +64,7 @@
 
     @Test
     public void getCandidates() {
+        when(mFragment.getContext()).thenReturn(RuntimeEnvironment.application);
         List<? extends RadioButtonPickerFragment.CandidateInfo> candidates =
                 mFragment.getCandidates();
 
@@ -90,11 +80,8 @@
     @Config(shadows = {SettingsShadowSystemProperties.class})
     @Test
     public void getKey_natural() {
-        SettingsShadowSystemProperties.set(
-                ColorModePreferenceFragment.PERSISTENT_PROPERTY_SATURATION,
-                Float.toString(ColorModePreferenceFragment.COLOR_SATURATION_NATURAL));
-        SettingsShadowSystemProperties.set(
-                ColorModePreferenceFragment.PERSISTENT_PROPERTY_NATIVE_MODE, "0");
+        Mockito.when(mController.getColorMode()).thenReturn(
+            NightDisplayController.COLOR_MODE_NATURAL);
 
         assertThat(mFragment.getDefaultKey())
                 .isEqualTo(ColorModePreferenceFragment.KEY_COLOR_MODE_NATURAL);
@@ -103,11 +90,8 @@
     @Config(shadows = {SettingsShadowSystemProperties.class})
     @Test
     public void getKey_boosted() {
-        SettingsShadowSystemProperties.set(
-                ColorModePreferenceFragment.PERSISTENT_PROPERTY_SATURATION,
-                Float.toString(ColorModePreferenceFragment.COLOR_SATURATION_BOOSTED));
-        SettingsShadowSystemProperties.set(
-                ColorModePreferenceFragment.PERSISTENT_PROPERTY_NATIVE_MODE, "0");
+        Mockito.when(mController.getColorMode()).thenReturn(
+            NightDisplayController.COLOR_MODE_BOOSTED);
 
         assertThat(mFragment.getDefaultKey())
                 .isEqualTo(ColorModePreferenceFragment.KEY_COLOR_MODE_BOOSTED);
@@ -116,55 +100,31 @@
     @Config(shadows = {SettingsShadowSystemProperties.class})
     @Test
     public void getKey_saturated() {
-        SettingsShadowSystemProperties.set(
-                ColorModePreferenceFragment.PERSISTENT_PROPERTY_NATIVE_MODE, "1");
+        Mockito.when(mController.getColorMode()).thenReturn(
+            NightDisplayController.COLOR_MODE_SATURATED);
 
         assertThat(mFragment.getDefaultKey())
-                .isEqualTo(ColorModePreferenceFragment.KEY_COLOR_MODE_SATURATED);
+            .isEqualTo(ColorModePreferenceFragment.KEY_COLOR_MODE_SATURATED);
     }
 
     @Config(shadows = {SettingsShadowSystemProperties.class})
     @Test
     public void setKey_natural() {
         mFragment.setDefaultKey(ColorModePreferenceFragment.KEY_COLOR_MODE_NATURAL);
-
-        String saturation = SettingsShadowSystemProperties
-                .get(ColorModePreferenceFragment.PERSISTENT_PROPERTY_SATURATION);
-        assertThat(saturation)
-                .isEqualTo(Float.toString(ColorModePreferenceFragment.COLOR_SATURATION_NATURAL));
-
-        String nativeMode = SettingsShadowSystemProperties
-                .get(ColorModePreferenceFragment.PERSISTENT_PROPERTY_NATIVE_MODE);
-        assertThat(nativeMode).isEqualTo("0");
+        Mockito.verify(mController).setColorMode(NightDisplayController.COLOR_MODE_NATURAL);
     }
 
     @Config(shadows = {SettingsShadowSystemProperties.class})
     @Test
     public void setKey_boosted() {
         mFragment.setDefaultKey(ColorModePreferenceFragment.KEY_COLOR_MODE_BOOSTED);
-
-        String saturation = SettingsShadowSystemProperties
-                .get(ColorModePreferenceFragment.PERSISTENT_PROPERTY_SATURATION);
-        assertThat(saturation)
-                .isEqualTo(Float.toString(ColorModePreferenceFragment.COLOR_SATURATION_BOOSTED));
-
-        String nativeMode = SettingsShadowSystemProperties
-                .get(ColorModePreferenceFragment.PERSISTENT_PROPERTY_NATIVE_MODE);
-        assertThat(nativeMode).isEqualTo("0");
+        Mockito.verify(mController).setColorMode(NightDisplayController.COLOR_MODE_BOOSTED);
     }
 
     @Config(shadows = {SettingsShadowSystemProperties.class})
     @Test
     public void setKey_saturated() {
         mFragment.setDefaultKey(ColorModePreferenceFragment.KEY_COLOR_MODE_SATURATED);
-
-        String saturation = SettingsShadowSystemProperties
-                .get(ColorModePreferenceFragment.PERSISTENT_PROPERTY_SATURATION);
-        assertThat(saturation)
-                .isEqualTo(Float.toString(ColorModePreferenceFragment.COLOR_SATURATION_NATURAL));
-
-        String nativeMode = SettingsShadowSystemProperties
-                .get(ColorModePreferenceFragment.PERSISTENT_PROPERTY_NATIVE_MODE);
-        assertThat(nativeMode).isEqualTo("1");
+        Mockito.verify(mController).setColorMode(NightDisplayController.COLOR_MODE_SATURATED);
     }
 }