Merge "Create the main architecture for Settings Dynamic Homepage"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 5bdd578..53abd54 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -10071,4 +10071,11 @@
 
     <!-- Title for Connected device shortcut [CHAR LIMIT=30] -->
     <string name="devices_title">Devices</string>
+
+    <!-- UI debug setting: Enable High Refresh Rate virtual panel [CHAR LIMIT=25] -->
+    <string name="high_frequency_display_device_title">High Frequency Panel</string>
+
+    <!-- UI debug setting: Enable High Refresh Rate virtual panel [CHAR LIMIT=50] -->
+    <string name="high_frequency_display_device_summary">Enable Virtual High Frequency Panel</string>
+
 </resources>
diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml
index a46ff2f..8cc8001 100644
--- a/res/xml/development_settings.xml
+++ b/res/xml/development_settings.xml
@@ -359,6 +359,11 @@
             android:entries="@array/overlay_display_devices_entries"
             android:entryValues="@array/overlay_display_devices_values" />
 
+        <SwitchPreference
+            android:key="high_frequency_display_device"
+            android:title="@string/high_frequency_display_device_title"
+            android:summary="@string/high_frequency_display_device_summary" />
+
         <com.android.settings.display.DensityPreference
             android:key="density"
             android:title="@string/developer_smallest_width" />
diff --git a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
index 5be381a..54ca6ef 100644
--- a/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
+++ b/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java
@@ -439,6 +439,7 @@
         controllers.add(new TransitionAnimationScalePreferenceController(context));
         controllers.add(new AnimatorDurationScalePreferenceController(context));
         controllers.add(new SecondaryDisplayPreferenceController(context));
+        controllers.add(new HighFrequencyDisplayPreferenceController(context));
         controllers.add(new GpuViewUpdatesPreferenceController(context));
         controllers.add(new HardwareLayersUpdatesPreferenceController(context));
         controllers.add(new DebugGpuOverdrawPreferenceController(context));
diff --git a/src/com/android/settings/development/HighFrequencyDisplayPreferenceController.java b/src/com/android/settings/development/HighFrequencyDisplayPreferenceController.java
new file mode 100644
index 0000000..cbb8d4c
--- /dev/null
+++ b/src/com/android/settings/development/HighFrequencyDisplayPreferenceController.java
@@ -0,0 +1,129 @@
+/*
+ * 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.development;
+
+import android.content.Context;
+import android.os.IBinder;
+import android.os.Parcel;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.text.TextUtils;
+
+import com.android.settings.R;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.development.DeveloperOptionsPreferenceController;
+
+import androidx.annotation.VisibleForTesting;
+import androidx.preference.ListPreference;
+import androidx.preference.Preference;
+import androidx.preference.SwitchPreference;
+
+public class HighFrequencyDisplayPreferenceController extends DeveloperOptionsPreferenceController
+        implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
+
+    private static final String HIGH_FREQUENCY_DISPLAY_KEY = "high_frequency_display_device";
+
+    private static final String SURFACE_FLINGER_SERVICE_KEY = "SurfaceFlinger";
+    private static final String SURFACE_COMPOSER_INTERFACE_KEY = "android.ui.ISurfaceComposer";
+    private static final int SURFACE_FLINGER_HIGH_FREQUENCY_DISPLAY_CODE = 1029;
+
+    private final IBinder mSurfaceFlingerBinder;
+
+    public HighFrequencyDisplayPreferenceController(Context context) {
+        super(context);
+        mSurfaceFlingerBinder = ServiceManager.getService(SURFACE_FLINGER_SERVICE_KEY);
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return HIGH_FREQUENCY_DISPLAY_KEY;
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        Boolean isEnabled = (Boolean) newValue;
+        writeHighFrequencyDisplaySetting(isEnabled);
+        ((SwitchPreference) preference).setChecked(isEnabled);
+        return true;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        boolean enableHighFrequencyPanel = readHighFrequencyDisplaySetting();
+        ((SwitchPreference) preference).setChecked(enableHighFrequencyPanel);
+    }
+
+    @Override
+    protected void onDeveloperOptionsSwitchDisabled() {
+        super.onDeveloperOptionsSwitchDisabled();
+        writeHighFrequencyDisplaySetting(false);
+        ((SwitchPreference) mPreference).setChecked(false);
+    }
+
+    @VisibleForTesting
+    boolean readHighFrequencyDisplaySetting() {
+        boolean isEnabled = false;
+        try {
+            if (mSurfaceFlingerBinder != null) {
+                final Parcel data = Parcel.obtain();
+                final Parcel result = Parcel.obtain();
+                data.writeInterfaceToken(SURFACE_COMPOSER_INTERFACE_KEY);
+                data.writeInt(0);
+                data.writeInt(0);
+                mSurfaceFlingerBinder.transact(
+                        SURFACE_FLINGER_HIGH_FREQUENCY_DISPLAY_CODE,
+                        data, result, 0);
+
+                if (result.readInt() != 1 || result.readInt() != 1) {
+                    isEnabled = true;
+                }
+            }
+        } catch (RemoteException ex) {
+            // intentional no-op
+        }
+        return isEnabled;
+    }
+
+    @VisibleForTesting
+    void writeHighFrequencyDisplaySetting(boolean isEnabled) {
+        int multiplier;
+        int divisor;
+
+        if (isEnabled) {
+            // 60Hz * 3/2 = 90Hz
+            multiplier = 2;
+            divisor = 3;
+        } else {
+            multiplier = 1;
+            divisor = 1;
+        }
+
+        try {
+            if (mSurfaceFlingerBinder != null) {
+                final Parcel data = Parcel.obtain();
+                data.writeInterfaceToken(SURFACE_COMPOSER_INTERFACE_KEY);
+                data.writeInt(multiplier);
+                data.writeInt(divisor);
+                mSurfaceFlingerBinder.transact(
+                        SURFACE_FLINGER_HIGH_FREQUENCY_DISPLAY_CODE,
+                        data, null, 0);
+            }
+        } catch (RemoteException ex) {
+            // intentional no-op
+        }
+    }
+}
diff --git a/src/com/android/settings/development/autofill/AutofillLoggingLevelPreferenceController.java b/src/com/android/settings/development/autofill/AutofillLoggingLevelPreferenceController.java
index a22295c..8618bc5 100644
--- a/src/com/android/settings/development/autofill/AutofillLoggingLevelPreferenceController.java
+++ b/src/com/android/settings/development/autofill/AutofillLoggingLevelPreferenceController.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.content.res.Resources;
 import android.provider.Settings;
+import android.util.Log;
 import android.view.autofill.AutofillManager;
 
 import com.android.settings.R;
@@ -32,6 +33,7 @@
         extends DeveloperOptionsPreferenceController
         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
 
+    private static final String TAG = "AutofillLoggingLevelPreferenceController";
     private static final String AUTOFILL_LOGGING_LEVEL_KEY = "autofill_logging_level";
 
     private final String[] mListValues;
@@ -73,6 +75,12 @@
     }
 
     private void updateOptions() {
+        if (mPreference == null) {
+            // TODO: there should be a hook on AbstractPreferenceController where we could
+            // unregister mObserver and avoid this check
+            Log.v(TAG, "ignoring Settings update because UI is gone");
+            return;
+        }
         final int level = Settings.Global.getInt(mContext.getContentResolver(),
                 Settings.Global.AUTOFILL_LOGGING_LEVEL, AutofillManager.DEFAULT_LOGGING_LEVEL);
 
diff --git a/tests/robotests/src/com/android/settings/development/HighFrequencyPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/development/HighFrequencyPreferenceControllerTest.java
new file mode 100644
index 0000000..cf8babb
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/development/HighFrequencyPreferenceControllerTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.development;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.os.IBinder;
+import android.os.RemoteException;
+
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+import com.android.settings.testutils.shadow.ShadowParcel;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+import org.robolectric.util.ReflectionHelpers;
+
+import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+public class HighFrequencyPreferenceControllerTest {
+
+    private Context mContext;
+    private SwitchPreference mPreference;
+
+    @Mock
+    private PreferenceScreen mScreen;
+    @Mock
+    private IBinder mSurfaceFlingerBinder;
+
+    private HighFrequencyDisplayPreferenceController mController;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = RuntimeEnvironment.application;
+        mPreference = new SwitchPreference(mContext);
+        mController = spy(new HighFrequencyDisplayPreferenceController(mContext));
+        ReflectionHelpers.setField(mController, "mSurfaceFlingerBinder", mSurfaceFlingerBinder);
+        when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
+        mController.displayPreference(mScreen);
+    }
+
+    @Test
+    public void onPreferenceChange_settingToggledOn_shouldWriteTrueToHighFrequencySetting() {
+        mController.onPreferenceChange(mPreference, true /* new value */);
+
+        verify(mController).writeHighFrequencyDisplaySetting(true);
+    }
+
+    @Test
+    public void onPreferenceChange_settingToggledOff_shouldWriteFalseToHighFrequencySetting() {
+        mController.onPreferenceChange(mPreference, false /* new value */);
+
+        verify(mController).writeHighFrequencyDisplaySetting(false);
+    }
+
+    @Test
+    public void updateState_settingEnabled_shouldCheckPreference() throws RemoteException {
+        mController.writeHighFrequencyDisplaySetting(true);
+        mController.updateState(mPreference);
+
+        verify(mController).readHighFrequencyDisplaySetting();
+    }
+
+    @Test
+    public void updateState_settingDisabled_shouldUnCheckPreference() throws RemoteException {
+        mController.writeHighFrequencyDisplaySetting(true);
+        mController.updateState(mPreference);
+
+        verify(mController).readHighFrequencyDisplaySetting();
+    }
+
+    @Test
+    public void onDeveloperOptionsSwitchDisabled_preferenceChecked_shouldTurnOffPreference() {
+        mController.onDeveloperOptionsSwitchDisabled();
+
+        verify(mController).writeHighFrequencyDisplaySetting(false);
+    }
+
+    @Test
+    public void onDeveloperOptionsSwitchDisabled_preferenceUnchecked_shouldNotTurnOffPreference() {
+        mController.onDeveloperOptionsSwitchDisabled();
+
+        verify(mController).writeHighFrequencyDisplaySetting(false);
+    }
+}