Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 146e53d6 authored by LuK1337's avatar LuK1337 Committed by Michael Bestas
Browse files

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
parent 679e6174
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -82,6 +82,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>

    <!-- Message shown in fingerprint enrollment dialog to locate the sensor -->
    <string name="fingerprint_enroll_find_sensor_message_front" product="tablet">Locate the fingerprint sensor on the front of your tablet.</string>
    <string name="fingerprint_enroll_find_sensor_message_front" product="device">Locate the fingerprint sensor on the front of your device.</string>
+3 −0
Original line number Diff line number Diff line
@@ -24,4 +24,7 @@

    <!-- 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>
+6 −0
Original line number Diff line number Diff line
@@ -143,6 +143,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" />

        <SwitchPreference
            android:key="peak_refresh_rate"
            android:title="@string/peak_refresh_rate_title"
+101 −0
Original line number Diff line number Diff line
/*
 * 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;
    }

}