Blur developer setting

New dev-option for enabling and disabling blurs, on devices that support
it.

Bug: 149792636
Test: manual
Test: atest EnableBlursPreferenceControllerTest
Change-Id: I26b4739a7b811c461557316a9247f6a9c397048a
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 618b77b..5a2fdc6 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -11563,6 +11563,11 @@
     <!-- UI debug setting: Force enable "smart dark" UI rendering feature summary [CHAR LIMIT=NONE] -->
     <string name="hwui_force_dark_summary">Overrides the force-dark feature to be always-on</string>
 
+    <!-- If blurs are supported on SurfaceFlinger. [CHAR LIMIT=60] -->
+    <string name="enable_blurs_on_windows_title">Enable blurs</string>
+    <!-- If blurs are supported on SurfaceFlinger, summary. [CHAR LIMIT=NONE] -->
+    <string name="enable_blurs_on_windows_summary">Enables window blurs at compositor level. Requires device reboot.</string>
+
     <!-- Title for the top level Privacy Settings [CHAR LIMIT=30]-->
     <string name="privacy_dashboard_title">Privacy</string>
     <!-- Summary for the top level Privacy Settings [CHAR LIMIT=NONE]-->
diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml
index 834fb48..b4d0bba 100644
--- a/res/xml/development_settings.xml
+++ b/res/xml/development_settings.xml
@@ -482,6 +482,11 @@
             android:summary="@string/hwui_force_dark_summary" />
 
         <SwitchPreference
+            android:key="enable_blurs_on_windows"
+            android:title="@string/enable_blurs_on_windows_title"
+            android:summary="@string/enable_blurs_on_windows_summary" />
+
+        <SwitchPreference
             android:key="force_msaa"
             android:title="@string/force_msaa"
             android:summary="@string/force_msaa_summary" />
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index f3f2a3a..8c79f2a 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -487,6 +487,7 @@
         controllers.add(new DebugGpuOverdrawPreferenceController(context));
         controllers.add(new DebugNonRectClipOperationsPreferenceController(context));
         controllers.add(new ForceDarkPreferenceController(context));
+        controllers.add(new EnableBlursPreferenceController(context));
         controllers.add(new ForceMSAAPreferenceController(context));
         controllers.add(new HardwareOverlaysPreferenceController(context));
         controllers.add(new SimulateColorSpacePreferenceController(context));
diff --git a/src/com/android/settings/development/EnableBlursPreferenceController.java b/src/com/android/settings/development/EnableBlursPreferenceController.java
new file mode 100644
index 0000000..88e0db1
--- /dev/null
+++ b/src/com/android/settings/development/EnableBlursPreferenceController.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2020 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.development;
+
+import android.content.Context;
+import android.os.SystemProperties;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.Preference;
+import androidx.preference.SwitchPreference;
+
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.development.DeveloperOptionsPreferenceController;
+import com.android.settingslib.development.SystemPropPoker;
+
+/**
+ * Controller that toggles window blurs on SurfaceFlinger on devices that support it.
+ */
+public final class EnableBlursPreferenceController extends DeveloperOptionsPreferenceController
+        implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
+
+    @VisibleForTesting
+    static final String DISABLE_BLURS_SYSPROP = "persist.sys.sf.disable_blurs";
+    private static final String ENABLE_BLURS_ON_WINDOWS = "enable_blurs_on_windows";
+    private final boolean mBlurSupported;
+
+    public EnableBlursPreferenceController(Context context) {
+        this(context, SystemProperties
+                .getBoolean("ro.surface_flinger.supports_background_blur", false));
+    }
+
+    @VisibleForTesting
+    public EnableBlursPreferenceController(Context context, boolean blurSupported) {
+        super(context);
+        mBlurSupported = blurSupported;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return ENABLE_BLURS_ON_WINDOWS;
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        final boolean isDisabled = !(Boolean) newValue;
+        SystemProperties.set(DISABLE_BLURS_SYSPROP, isDisabled ? "1" : "0");
+        SystemPropPoker.getInstance().poke();
+        return true;
+    }
+
+    @Override
+    public boolean isAvailable() {
+        return mBlurSupported;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        boolean isEnabled = !SystemProperties.getBoolean(
+                DISABLE_BLURS_SYSPROP, false /* default */);
+        ((SwitchPreference) mPreference).setChecked(isEnabled);
+    }
+
+    @Override
+    protected void onDeveloperOptionsSwitchDisabled() {
+        super.onDeveloperOptionsSwitchDisabled();
+        SystemProperties.set(DISABLE_BLURS_SYSPROP, null);
+        updateState(null);
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/development/EnableBlursPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/EnableBlursPreferenceControllerTest.java
new file mode 100644
index 0000000..d943cb1
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/development/EnableBlursPreferenceControllerTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2020 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.development;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.SystemProperties;
+
+import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+@RunWith(RobolectricTestRunner.class)
+public final class EnableBlursPreferenceControllerTest {
+
+    @Mock
+    private SwitchPreference mPreference;
+    @Mock
+    private PreferenceScreen mPreferenceScreen;
+
+    @Rule
+    public MockitoRule mockitoRule = MockitoJUnit.rule();
+
+    private Context mContext;
+    private EnableBlursPreferenceController mController;
+
+    @Before
+    public void setup() {
+        mContext = RuntimeEnvironment.application;
+        mController = new EnableBlursPreferenceController(mContext, true);
+        when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
+                mPreference);
+        mController.displayPreference(mPreferenceScreen);
+    }
+
+    @Test
+    public void onPreferenceChanged_settingEnabled_enableBlurs() {
+        mController.onPreferenceChange(mPreference, true /* new value */);
+
+        final boolean mode = SystemProperties
+                .getBoolean(EnableBlursPreferenceController.DISABLE_BLURS_SYSPROP,
+                        false /* default */);
+        assertThat(mode).isFalse();
+    }
+
+    @Test
+    public void onPreferenceChanged_settingDisabled_disableBlurs() {
+        mController.onPreferenceChange(mPreference, false /* new value */);
+
+        final boolean mode = SystemProperties
+                .getBoolean(EnableBlursPreferenceController.DISABLE_BLURS_SYSPROP,
+                        false /* default */);
+
+        assertThat(mode).isTrue();
+    }
+
+    @Test
+    public void updateState_settingEnabled_preferenceShouldNotBeChecked() {
+        SystemProperties.set(EnableBlursPreferenceController.DISABLE_BLURS_SYSPROP, "1");
+        mController.updateState(mPreference);
+
+        verify(mPreference).setChecked(false);
+    }
+
+    @Test
+    public void updateState_settingDisabled_preferenceShouldBeChecked() {
+        SystemProperties.set(EnableBlursPreferenceController.DISABLE_BLURS_SYSPROP, "0");
+        mController.updateState(mPreference);
+
+        verify(mPreference).setChecked(true);
+    }
+
+    @Test
+    public void onDeveloperOptionsDisabled_shouldResetPreference() {
+        mController.onDeveloperOptionsDisabled();
+        // Can predict true or false, depends on device config.
+        verify(mPreference).setChecked(anyBoolean());
+    }
+
+    @Test
+    public void isAvailable_whenSupported() {
+        assertThat(mController.isAvailable()).isTrue();
+
+        mController = new EnableBlursPreferenceController(mContext, false);
+        assertThat(mController.isAvailable()).isFalse();
+    }
+}