Add Grayscale conditional

Build Grayscale conditional that lets users quickly turn off Grayscale
in Settings if it is on.

Bug: 118387886
Test: visual, robotests
Change-Id: Ibfc2d88f4f3f60f9b0acf084a49084030674de37
diff --git a/res/drawable/ic_gray_scale_24dp.xml b/res/drawable/ic_gray_scale_24dp.xml
new file mode 100644
index 0000000..3fda134
--- /dev/null
+++ b/res/drawable/ic_gray_scale_24dp.xml
@@ -0,0 +1,25 @@
+<!--
+  Copyright (C) 2019 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.
+  -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24dp"
+        android:height="24dp"
+        android:viewportWidth="24"
+        android:viewportHeight="24">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10s10,-4.48 10,-10S17.52,2 12,2zM11,19.93C7.06,19.44 4,16.08 4,12s3.05,-7.44 7,-7.93V19.93zM13,4.07C14.03,4.2 15,4.52 15.87,5H13V4.07zM13,7h5.24c0.25,0.31 0.48,0.65 0.68,1H13V7zM13,10h6.74c0.08,0.33 0.15,0.66 0.19,1H13V10zM13,19.93V19h2.87C15,19.48 14.03,19.8 13,19.93zM18.24,17H13v-1h5.92C18.72,16.35 18.49,16.69 18.24,17zM19.74,14H13v-1h6.93C19.89,13.34 19.82,13.67 19.74,14z"/>
+</vector>
+
diff --git a/res/values/config.xml b/res/values/config.xml
index e6ada66..805469a 100755
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -356,4 +356,7 @@
 
     <!-- Slice Uri to query nearby devices. -->
     <string name="config_nearby_devices_slice_uri" translatable="false">content://com.google.android.gms.nearby.fastpair/device_status_list_item</string>
+
+    <!-- Grayscale settings intent -->
+    <string name="config_grayscale_settings_intent" translate="false"></string>
 </resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0d6eaeb..2e1f4c7 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -9275,6 +9275,12 @@
     <!-- Summary of condition that night display is on (renamed "Night Light" with title caps). [CHAR LIMIT=NONE] -->
     <string name="condition_night_display_summary">Screen tinted amber</string>
 
+    <!-- Title of condition that gray scale is on [CHAR LIMIT=NONE] -->
+    <string name="condition_grayscale_title">Greyscale</string>
+
+    <!-- Summary of condition that gray scale is on [CHAR LIMIT=NONE] -->
+    <string name="condition_grayscale_summary">Display only in grey color</string>
+
     <!-- Summary for the condition section on the dashboard, representing number of conditions. [CHAR LIMIT=10] -->
     <string name="condition_summary" translatable="false"><xliff:g name="count" example="3">%1$d</xliff:g></string>
 
diff --git a/src/com/android/settings/homepage/contextualcards/conditional/ConditionManager.java b/src/com/android/settings/homepage/contextualcards/conditional/ConditionManager.java
index c741b98..66f6c81 100644
--- a/src/com/android/settings/homepage/contextualcards/conditional/ConditionManager.java
+++ b/src/com/android/settings/homepage/contextualcards/conditional/ConditionManager.java
@@ -162,6 +162,7 @@
         mCardControllers.add(new RingerVibrateConditionController(mAppContext, this /* manager */));
         mCardControllers.add(new RingerMutedConditionController(mAppContext, this /* manager */));
         mCardControllers.add(new WorkModeConditionController(mAppContext, this /* manager */));
+        mCardControllers.add(new GrayscaleConditionController(mAppContext, this /* manager */));
     }
 
     /**
diff --git a/src/com/android/settings/homepage/contextualcards/conditional/GrayscaleConditionController.java b/src/com/android/settings/homepage/contextualcards/conditional/GrayscaleConditionController.java
new file mode 100644
index 0000000..664707d
--- /dev/null
+++ b/src/com/android/settings/homepage/contextualcards/conditional/GrayscaleConditionController.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2019 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.homepage.contextualcards.conditional;
+
+import android.app.settings.SettingsEnums;
+import android.content.Context;
+import android.content.Intent;
+import android.hardware.display.ColorDisplayManager;
+import android.util.Log;
+
+import com.android.settings.R;
+import com.android.settings.homepage.contextualcards.ContextualCard;
+
+import java.net.URISyntaxException;
+import java.util.Objects;
+
+public class GrayscaleConditionController implements ConditionalCardController {
+    static final int ID = Objects.hash("GrayscaleConditionController");
+
+    private static final String TAG = "GrayscaleCondition";
+
+    private final Context mAppContext;
+    private final ConditionManager mConditionManager;
+    private final ColorDisplayManager mColorDisplayManager;
+
+    private Intent mIntent;
+
+    public GrayscaleConditionController(Context appContext, ConditionManager conditionManager) {
+        mAppContext = appContext;
+        mConditionManager = conditionManager;
+        mColorDisplayManager = mAppContext.getSystemService(ColorDisplayManager.class);
+    }
+
+    @Override
+    public long getId() {
+        return ID;
+    }
+
+    @Override
+    public boolean isDisplayable() {
+        try {
+            mIntent = Intent.parseUri(
+                    mAppContext.getString(R.string.config_grayscale_settings_intent),
+                    Intent.URI_INTENT_SCHEME);
+        } catch (URISyntaxException e) {
+            Log.w(TAG, "Failure parsing grayscale settings intent, skipping", e);
+            return false;
+        }
+        return mColorDisplayManager.isSaturationActivated();
+    }
+
+    @Override
+    public void onPrimaryClick(Context context) {
+        mAppContext.startActivity(mIntent);
+    }
+
+    @Override
+    public void onActionClick() {
+        // Turn off grayscale
+        mColorDisplayManager.setSaturationLevel(100 /* staturationLevel */);
+        mConditionManager.onConditionChanged();
+    }
+
+    @Override
+    public ContextualCard buildContextualCard() {
+        return new ConditionalContextualCard.Builder()
+                .setConditionId(ID)
+                .setMetricsConstant(SettingsEnums.SETTINGS_CONDITION_GRAYSCALE_MODE)
+                .setActionText(mAppContext.getText(R.string.condition_turn_off))
+                .setName(mAppContext.getPackageName() + "/" + mAppContext.getText(
+                        R.string.condition_grayscale_title))
+                .setTitleText(mAppContext.getText(R.string.condition_grayscale_title).toString())
+                .setSummaryText(
+                        mAppContext.getText(R.string.condition_grayscale_summary).toString())
+                .setIconDrawable(mAppContext.getDrawable(R.drawable.ic_gray_scale_24dp))
+                .setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH)
+                .build();
+    }
+
+    @Override
+    public void startMonitoringStateChange() {
+
+    }
+
+    @Override
+    public void stopMonitoringStateChange() {
+
+    }
+}
diff --git a/tests/robotests/res/values-mcc999/config.xml b/tests/robotests/res/values-mcc999/config.xml
index 776a4d0..1fe4bbe 100644
--- a/tests/robotests/res/values-mcc999/config.xml
+++ b/tests/robotests/res/values-mcc999/config.xml
@@ -91,4 +91,7 @@
 
     <!-- Email address for the homepage contextual cards feedback -->
     <string name="config_contextual_card_feedback_email" translatable="false">test@test.test</string>
+
+    <!-- Grayscale settings intent -->
+    <string name="config_grayscale_settings_intent" translate="false">intent:#Intent;action=test.test;end</string>
 </resources>
diff --git a/tests/robotests/src/com/android/settings/homepage/contextualcards/conditional/GrayscaleConditionControllerTest.java b/tests/robotests/src/com/android/settings/homepage/contextualcards/conditional/GrayscaleConditionControllerTest.java
new file mode 100644
index 0000000..8c24735
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/homepage/contextualcards/conditional/GrayscaleConditionControllerTest.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2019 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.homepage.contextualcards.conditional;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import android.content.Context;
+import android.hardware.display.ColorDisplayManager;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(RobolectricTestRunner.class)
+public class GrayscaleConditionControllerTest {
+
+    @Mock
+    private ConditionManager mConditionManager;
+
+    private ColorDisplayManager mColorDisplayManager;
+    private Context mContext;
+    private GrayscaleConditionController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+        mColorDisplayManager = spy(mContext.getSystemService(ColorDisplayManager.class));
+        doReturn(mColorDisplayManager).when(mContext).getSystemService(ColorDisplayManager.class);
+        mController = new GrayscaleConditionController(mContext, mConditionManager);
+    }
+
+    @Test
+    public void isDisplayable_noIntent_shouldReturnFalse() {
+        assertThat(mController.isDisplayable()).isFalse();
+    }
+
+    @Test
+    @Config(qualifiers = "mcc999")
+    public void isDisplayable_validIntentAndGrayscaleOn_shouldReturnTrue() {
+        doReturn(true).when(mColorDisplayManager).isSaturationActivated();
+
+        assertThat(mController.isDisplayable()).isTrue();
+    }
+
+    @Test
+    @Config(qualifiers = "mcc999")
+    public void isDisplayable_validIntentAndGrayscaleOff_shouldReturnFalse() {
+        doReturn(false).when(mColorDisplayManager).isSaturationActivated();
+
+        assertThat(mController.isDisplayable()).isFalse();
+    }
+
+    @Test
+    public void onActionClick_shouldRefreshCondition() {
+        mController.onActionClick();
+
+        verify(mConditionManager).onConditionChanged();
+    }
+}