Settings: Add preference for KEY_MIN_REFRESH_RATE

Squashed with the following:

Author: LuK1337 <priv.luk@gmail.com>
Date:   Sun May 9 15:49:43 2021 +0200

    Settings: Remove dependency on pref object for min refresh rate isAvailable()

    Preference is not available during search indexing.

    Change-Id: Ia53aee2a0e4d3c6ef8a56d202af3da48cf6b1d0e

Author: LuK1337 <priv.luk@gmail.com>
Date:   Fri Nov 27 14:33:15 2020 +0100

    Settings: Pass Locale.US when formatting refresh rate string

    * Some locales use comma rather than dot for floating point numbers
      thus causing NumberFormatException when trying to convert string
      to float.
    * Also make sure to strip `,00` too when needed.

    Change-Id: I57d5606858587daf316194c87ece8b5e30423575

Change-Id: Iac1f65ab09717ea55a5b471e094385c77ba894ee
diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml
index 1318a66..9729894 100644
--- a/res/values/cm_strings.xml
+++ b/res/values/cm_strings.xml
@@ -50,6 +50,9 @@
     <!-- Max refresh rate -->
     <string name="max_refresh_rate_title">Peak refresh rate</string>
 
+    <!-- Min refresh rate -->
+    <string name="min_refresh_rate_title">Minimum refresh rate</string>
+
     <!-- PIN scramble -->
     <string name="unlock_scramble_pin_layout_title">Scramble layout</string>
     <string name="unlock_scramble_pin_layout_summary">Scramble PIN layout when unlocking device</string>
diff --git a/res/values/leaf_config.xml b/res/values/leaf_config.xml
index 1f9d592..1a7dc7d 100644
--- a/res/values/leaf_config.xml
+++ b/res/values/leaf_config.xml
@@ -17,4 +17,7 @@
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <!-- Whether to show peak refresh rate in display settings -->
     <bool name="config_show_peak_refresh_rate_switch">false</bool>
+
+    <!-- Whether to show min refresh rate in display settings -->
+    <bool name="config_show_min_refresh_rate_switch">false</bool>
 </resources>
diff --git a/res/xml/display_settings.xml b/res/xml/display_settings.xml
index cd8898f..6c4b8f6 100644
--- a/res/xml/display_settings.xml
+++ b/res/xml/display_settings.xml
@@ -164,6 +164,12 @@
             android:summary="@string/summary_placeholder"
             settings:controller="com.android.settings.display.PeakRefreshRateListPreferenceController" />
 
+        <ListPreference
+            android:key="min_refresh_rate"
+            android:title="@string/min_refresh_rate_title"
+            android:summary="@string/summary_placeholder"
+            settings:controller="com.android.settings.display.MinRefreshRatePreferenceController" />
+
         <SwitchPreferenceCompat
             android:key="peak_refresh_rate"
             android:title="@string/peak_refresh_rate_title"
diff --git a/src/com/android/settings/display/MinRefreshRatePreferenceController.java b/src/com/android/settings/display/MinRefreshRatePreferenceController.java
new file mode 100644
index 0000000..caaf11b
--- /dev/null
+++ b/src/com/android/settings/display/MinRefreshRatePreferenceController.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2020 The LineageOS 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 static android.provider.Settings.System.MIN_REFRESH_RATE;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.view.Display;
+
+import androidx.preference.ListPreference;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+public class MinRefreshRatePreferenceController extends BasePreferenceController
+        implements Preference.OnPreferenceChangeListener {
+
+    private static final String KEY_MIN_REFRESH_RATE = "min_refresh_rate";
+
+    private ListPreference mListPreference;
+
+    private List<String> mEntries = new ArrayList<>();
+    private List<String> mValues = new ArrayList<>();
+
+    public MinRefreshRatePreferenceController(Context context) {
+        super(context, KEY_MIN_REFRESH_RATE);
+
+        if (mContext.getResources().getBoolean(R.bool.config_show_min_refresh_rate_switch)) {
+            Display.Mode mode = mContext.getDisplay().getMode();
+            Display.Mode[] modes = mContext.getDisplay().getSupportedModes();
+            for (Display.Mode m : modes) {
+                if (m.getPhysicalWidth() == mode.getPhysicalWidth() &&
+                        m.getPhysicalHeight() == mode.getPhysicalHeight()) {
+                    mEntries.add(String.format("%.02fHz", m.getRefreshRate())
+                            .replaceAll("[\\.,]00", ""));
+                    mValues.add(String.format(Locale.US, "%.02f", m.getRefreshRate()));
+                }
+            }
+        }
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return mEntries.size() > 1 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return KEY_MIN_REFRESH_RATE;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        mListPreference = screen.findPreference(getPreferenceKey());
+        mListPreference.setEntries(mEntries.toArray(new String[mEntries.size()]));
+        mListPreference.setEntryValues(mValues.toArray(new String[mValues.size()]));
+
+        super.displayPreference(screen);
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        final float currentValue = Settings.System.getFloat(mContext.getContentResolver(),
+                MIN_REFRESH_RATE, 60.00f);
+        int index = mListPreference.findIndexOfValue(
+                String.format(Locale.US, "%.02f", currentValue));
+        if (index < 0) index = 0;
+        mListPreference.setValueIndex(index);
+        mListPreference.setSummary(mListPreference.getEntries()[index]);
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        Settings.System.putFloat(mContext.getContentResolver(), MIN_REFRESH_RATE,
+                Float.valueOf((String) newValue));
+        updateState(preference);
+        return true;
+    }
+
+}