nx: devicesettings: Refactor and clean up

- Migrate static prefs to xml
- Migrate static arrays to resources
- Migrate to API 30
- Stop using deprecated APIs
- Migrate to AppCompat
- Fix formatting

Change-Id: Ie9b7ddce51ac7c6e0575eb051d76cb98ad46fa2c
This commit is contained in:
tmakin authored and Nolen Johnson committed 2024-01-26 11:53:38 -05:00
1 parent 9979bdb8df
commit cc708f6e45
12 files changed
+157 -130

No files matched your search

+1 -1
View File
@@ -14,7 +14,7 @@ LOCAL_USE_AAPT2 := true
LOCAL_STATIC_ANDROID_LIBRARIES := \
androidx.core_core \
androidx.preference_preference \
androidx.preference_preference
LOCAL_STATIC_JAVA_LIBRARIES := \
org.lineageos.platform.internal \
+3 -3
View File
@@ -11,8 +11,8 @@
android:required="false" />
<uses-sdk
android:minSdkVersion="28"
android:targetSdkVersion="28" />
android:minSdkVersion="30"
android:targetSdkVersion="30" />
<protected-broadcast android:name="com.lineage.devicesettings.UPDATE_POWER" />
@@ -47,7 +47,7 @@
<activity
android:name=".DisplaySettingsActivity"
android:label="@string/display_panel_title"
android:theme="@style/Theme.Main">
android:theme="@style/Theme.AppCompat.DayNight">
<intent-filter>
<action android:name="com.android.settings.action.IA_SETTINGS" />
<action android:name="android.intent.action.MAIN" />
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
+40
View File
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2023 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.
-->
<resources>
<string-array name="panel_mode_map">
<item>Basic</item>
<item>Washed</item>
<item>Natural</item>
<item>Vivid</item>
<item>Night 0</item>
<item>Night 1</item>
<item>Night 2</item>
<item>Night </item>
</string-array>
<string-array name="panel_modes">
<item>0x03</item> <!-- DCS_SM_COLOR_MODE_BASIC -->
<item>0x45</item> <!-- DCS_SM_COLOR_MODE_WASHED -->
<item>0x23</item> <!-- DCS_SM_COLOR_MODE_NATURAL -->
<item>0x65</item> <!-- DCS_SM_COLOR_MODE_VIVID -->
<item>0x43</item> <!-- DCS_SM_COLOR_MODE_NIGHT0 -->
<item>0x15</item> <!-- DCS_SM_COLOR_MODE_NIGHT1 -->
<item>0x35</item> <!-- DCS_SM_COLOR_MODE_NIGHT2 -->
<item>0x75</item> <!-- DCS_SM_COLOR_MODE_NIGHT3 -->
</string-array>
</resources>
+1 -1
View File
@@ -17,7 +17,7 @@
<resources>
<string name="display_panel_title">Switch Configuration</string>
<string name="display_panel_summary">Adjust docking, performance, and controller settings</string>
<string name="perf_category_title">Customization</string>
<string name="cust_category_title">Customization</string>
<string name="perf_setting_title">Performance Mode</string>
<string name="perf_setting_summary">Modifies CPU and GPU clocks in order to get the best possible performance at the cost of higher battery wear and usage</string>
<string name="perf_warning_title">WARNING</string>
+25 -2
View File
@@ -1,15 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2020 The LineageOS Project
Copyright (C) 2020-2023 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
android:key="customization_category"
android:title="@string/cust_category_title">
<SwitchPreferenceCompat
android:key="perf_mode"
android:title="@string/perf_setting_title"
android:summary="@string/perf_setting_summary" />
<ListPreference
android:key="panel_color_mode"
android:title="@string/panel_color_setting_title"
android:entries="@array/panel_mode_map"
app:useSimpleSummaryProvider="false"
android:entryValues="@array/panel_modes" />
</PreferenceCategory>
</PreferenceScreen>
@@ -33,7 +33,7 @@ public class BootCompletedReceiver extends BroadcastReceiver {
// Set preferred OLED panel mode
if(SystemProperties.get("ro.boot.hardware.sku", "").equals("frig")) {
final SharedPreferences sharedPrefs = context.getSharedPreferences(
"org.lineageos.settings.device_preferences", context.MODE_PRIVATE);
"org.lineageos.settings.device_preferences", Context.MODE_PRIVATE);
final String panelMode = sharedPrefs.getString("panel_color_mode", "0x23");
DisplayUtils.setPanelColorMode(panelMode);
@@ -37,7 +37,6 @@ import static android.provider.SearchIndexablesContract.NON_INDEXABLES_KEYS_COLU
public class DeviceSettingsSearchIndexablesProvider extends SearchIndexablesProvider {
public static final int SEARCH_IDX_BUTTON_PANEL = 0;
private static final String TAG = "ConfigPanelSearchIndexablesProvider";
private static SearchIndexableResource[] INDEXABLE_RES =
new SearchIndexableResource[]{
new SearchIndexableResource(1, R.xml.display_panel,
@@ -22,35 +22,40 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.SystemProperties;
import android.preference.PreferenceActivity;
import android.view.WindowManagerPolicyConstants;
public class DisplaySettingsActivity extends PreferenceActivity {
import androidx.appcompat.app.AppCompatActivity;
public class DisplaySettingsActivity extends AppCompatActivity {
public final Receiver mReceiver = new Receiver();
public boolean mExternalDisplayConnected;
private DisplaySettingsFragment mFragment;
private String sku = SystemProperties.get("ro.product.name", "");
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mFragment = new DisplaySettingsFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, mFragment)
.commit();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.setReorderingAllowed(true)
.add(android.R.id.content, DisplaySettingsFragment.class, null)
.commit();
}
}
@Override
protected void onPause() {
super.onPause();
if(!sku.equals("vali"))
if (!sku.equals("vali")) {
unregisterReceiver(mReceiver);
}
}
@Override
protected void onResume() {
super.onResume();
if(!sku.equals("vali"))
if (!sku.equals("vali")) {
mReceiver.init(this);
}
}
final class Receiver extends BroadcastReceiver {
@@ -68,11 +73,15 @@ public class DisplaySettingsActivity extends PreferenceActivity {
public void onReceive(Context context, Intent intent) {
mExternalDisplayConnected = intent.getBooleanExtra(
WindowManagerPolicyConstants.EXTRA_HDMI_PLUGGED_STATE, false);
WindowManagerPolicyConstants.EXTRA_HDMI_PLUGGED_STATE, false);
if (mBlocked) return;
if (mBlocked) {
return;
}
if (mReceivedInitialIntent) recreate();
if (mReceivedInitialIntent) {
recreate();
}
mReceivedInitialIntent = true;
}
@@ -16,11 +16,10 @@
package org.lineageos.settings.device;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
@@ -32,10 +31,10 @@ import android.view.MenuItem;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragment;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.preference.SwitchPreferenceCompat;
import java.util.ArrayList;
import java.util.Collections;
@@ -48,39 +47,17 @@ import vendor.nvidia.hardware.graphics.display.V1_0.HwcSvcDisplayType;
import vendor.nvidia.hardware.graphics.display.V1_0.HwcSvcModeType;
import vendor.nvidia.hardware.graphics.display.V1_0.INvDisplay;
public class DisplaySettingsFragment extends PreferenceFragment
public class DisplaySettingsFragment extends PreferenceFragmentCompat
implements SharedPreferences.OnSharedPreferenceChangeListener {
public static String[] modes = {
"0x03", /* DCS_SM_COLOR_MODE_BASIC */
"0x45", /* DCS_SM_COLOR_MODE_WASHED */
"0x23", /* DCS_SM_COLOR_MODE_NATURAL */
"0x65", /* DCS_SM_COLOR_MODE_VIVID */
"0x43", /* DCS_SM_COLOR_MODE_NIGHT0 */
"0x15", /* DCS_SM_COLOR_MODE_NIGHT1 */
"0x35", /* DCS_SM_COLOR_MODE_NIGHT2 */
"0x75" /* DCS_SM_COLOR_MODE_NIGHT3 */
};
public static String[] modeMap = {
"Basic",
"Washed",
"Natural",
"Vivid",
"Night 0",
"Night 1",
"Night 2",
"Night 3"
};
private static final String TAG = DisplaySettingsFragment.class.getSimpleName();
private final String sku = SystemProperties.get("ro.product.name", "");
public boolean mInModeChange = false;
private INvDisplay mDisplayService;
private String sku = SystemProperties.get("ro.product.name", "");
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
if(!sku.equals("vali")) {
if (!sku.equals("vali")) {
try {
mDisplayService = INvDisplay.getService(true /* retry */);
} catch (RemoteException e) {
@@ -91,13 +68,11 @@ public class DisplaySettingsFragment extends PreferenceFragment
addPreferencesFromResource(R.xml.display_panel);
PreferenceScreen preferenceScreen = this.getPreferenceScreen();
createPerfSettings(preferenceScreen);
createPerfSettings();
if(!sku.equals("vali"))
if (!sku.equals("vali")) {
createDisplaySettings(preferenceScreen);
final ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
@Override
@@ -234,16 +209,8 @@ public class DisplaySettingsFragment extends PreferenceFragment
}.start();
}
private void createPerfSettings(PreferenceScreen preferenceScreen) {
PreferenceCategory perfCategory = new PreferenceCategory(preferenceScreen
.getContext());
perfCategory.setTitle(R.string.perf_category_title);
SwitchPreference perfPreference = new SwitchPreference(perfCategory
.getContext());
perfPreference.setTitle(R.string.perf_setting_title);
perfPreference.setSummary(R.string.perf_setting_summary);
perfPreference.setKey("perf_mode");
private void createPerfSettings() {
SwitchPreferenceCompat perfPreference = findPreference("perf_mode");
perfPreference.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@@ -297,16 +264,17 @@ public class DisplaySettingsFragment extends PreferenceFragment
return true;
}
});
preferenceScreen.addPreference(perfCategory);
perfCategory.addPreference(perfPreference);
}
private void createPanelModeSettings(PreferenceCategory perfCategory) {
ListPreference panelColorPref = new ListPreference(perfCategory.getContext());
private void createPanelModeSettings() {
ListPreference panelColorPref = findPreference("panel_color_mode");
String current = DisplayUtils.getPanelColorMode();
int index;
Resources res = getResources();
String[] modes = res.getStringArray(R.array.panel_modes);
String[] modeMap = res.getStringArray(R.array.panel_mode_map);
for(index = 0; index < modes.length; index++) {
if(current.equals(modes[index]))
break;
@@ -318,38 +286,27 @@ public class DisplaySettingsFragment extends PreferenceFragment
Log.w(TAG, "OLED Panel Mode Index: " + String.valueOf(index));
// matches sysfs node for consistency
panelColorPref.setKey("panel_color_mode");
panelColorPref.setTitle(R.string.panel_color_setting_title);
panelColorPref.setSummary(String.format(
getString(R.string.panel_color_setting_summary), modeMap[index]));
panelColorPref.setEntries(modeMap);
panelColorPref.setEntryValues(modes);
panelColorPref.setValue(current);
panelColorPref.setSummary(modeMap[index]);
panelColorPref.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
int newIndex;
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
int newIndex;
DisplayUtils.setPanelColorMode((String)newValue);
DisplayUtils.setPanelColorMode((String) newValue);
for(newIndex = 0; newIndex < modes.length; newIndex++) {
if(((String)newValue).equals(modes[newIndex]))
break;
}
panelColorPref.setSummary(String.format(
getString(R.string.panel_color_setting_summary),
modeMap[newIndex]));
return true;
}
});
perfCategory.addPreference(panelColorPref);
for (newIndex = 0; newIndex < modes.length; newIndex++) {
if (((String) newValue).equals(modes[newIndex])) {
panelColorPref.setSummary(modeMap[newIndex]);
break;
}
}
return true;
}
});
}
}
@@ -458,23 +415,21 @@ public class DisplaySettingsFragment extends PreferenceFragment
// Show checkbox to disable internal panel when an external display is connected
if (display == HwcSvcDisplay.HWC_SVC_DISPLAY_PANEL) {
SwitchPreference disableInternalOnExternalConnectedPreference =
new SwitchPreference(category.getContext());
SwitchPreferenceCompat disableInternalOnExternalConnectedPreference =
new SwitchPreferenceCompat(category.getContext());
disableInternalOnExternalConnectedPreference
.setTitle(R.string.disable_internal_on_external_connected_title);
.setTitle(R.string.disable_internal_on_external_connected_title);
disableInternalOnExternalConnectedPreference
.setSummaryOn(
R.string.disable_internal_on_external_connected_summary_on);
.setSummaryOn(R.string.disable_internal_on_external_connected_summary_on);
disableInternalOnExternalConnectedPreference
.setSummaryOff(
R.string.disable_internal_on_external_connected_summary_off);
.setSummaryOff(R.string.disable_internal_on_external_connected_summary_off);
disableInternalOnExternalConnectedPreference
.setKey("disable_internal_on_external_connected");
.setKey("disable_internal_on_external_connected");
category.addPreference(disableInternalOnExternalConnectedPreference);
if(sku.equals("frig")) {
createPanelModeSettings(category);
if (sku.equals("frig")) {
createPanelModeSettings();
}
}
}
@@ -38,19 +38,24 @@ import vendor.nvidia.hardware.graphics.display.V1_0.INvDisplay;
public class DisplayUtils {
private static final String TAG = DisplayUtils.class.getSimpleName();
public static final String POWER_UPDATE_INTENT = "com.lineage.devicesettings.UPDATE_POWER";
public static final String PANEL_MODE_SYSFS =
"/sys/devices/50000000.host1x/tegradc.0/panel_color_mode";
public static final String PWM_FAN_PROFILE_SYSFS = "/sys/devices/pwm-fan/fan_profile";
public static final String EST_FAN_PROFILE_SYSFS = "/sys/devices/thermal-fan-est/fan_profile";
public static final String INT_DISPLAY_MODE_SYSFS =
"/sys/bus/platform/devices/tegradc.0/enable";
public static void setInternalDisplayState(boolean state) {
Log.d(TAG, "setInternalDisplayState: " + String.valueOf(state));
try {
FileOutputStream colorModeFile =
new FileOutputStream("/sys/bus/platform/devices/tegradc.0/enable");
FileOutputStream intModeFile = new FileOutputStream(INT_DISPLAY_MODE_SYSFS);
byte[] buf = new byte[2];
buf[0] = (byte) (state ? '1' : '0');
buf[1] = '\n';
colorModeFile.write(buf);
colorModeFile.close();
intModeFile.write(buf);
intModeFile.close();
} catch (IOException e) {
Log.w(TAG, "Failed to write display state");
}
@@ -156,8 +161,7 @@ public class DisplayUtils {
public static void setPanelColorMode(String mode) {
Log.d(TAG, "OLED panel mode set: " + mode);
try {
FileOutputStream colorModeFile = new FileOutputStream(
"/sys/devices/50000000.host1x/tegradc.0/panel_color_mode");
FileOutputStream colorModeFile = new FileOutputStream(PANEL_MODE_SYSFS);
byte[] buf = new byte[4];
buf = mode.getBytes(StandardCharsets.US_ASCII);
@@ -174,8 +178,7 @@ public class DisplayUtils {
String out;
try {
FileInputStream colorModeFile = new FileInputStream(
"/sys/devices/50000000.host1x/tegradc.0/panel_color_mode");
FileInputStream colorModeFile = new FileInputStream(PANEL_MODE_SYSFS);
colorModeFile.read(buf);
out = new String(buf, StandardCharsets.US_ASCII);
@@ -192,12 +195,10 @@ public class DisplayUtils {
public static void setFanProfile(String profile) {
Log.i(TAG, "Setting fan profile: " + profile);
try {
final FileOutputStream pwmProfile = new FileOutputStream(
"/sys/devices/pwm-fan/fan_profile");
final FileOutputStream pwmProfile = new FileOutputStream(PWM_FAN_PROFILE_SYSFS);
pwmProfile.write(profile.getBytes());
pwmProfile.close();
final FileOutputStream estProfile = new FileOutputStream(
"/sys/devices/thermal-fan-est/fan_profile");
final FileOutputStream estProfile = new FileOutputStream(EST_FAN_PROFILE_SYSFS);
estProfile.write(profile.getBytes());
estProfile.close();
} catch (IOException e) {
@@ -76,7 +76,7 @@ public class DockService extends Service {
private void updatePowerState(Context context, boolean connected) {
final SharedPreferences sharedPrefs = context
.getSharedPreferences("org.lineageos.settings.device_preferences",
context.MODE_PRIVATE);
Context.MODE_PRIVATE);
final boolean perfMode = sharedPrefs.getBoolean("perf_mode", false);
final String sku = SystemProperties.get("ro.boot.hardware.sku", "");
@@ -117,10 +117,10 @@ public class DockService extends Service {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPrefs = context.getSharedPreferences(
"org.lineageos.settings.device_preferences", context.MODE_PRIVATE);
"org.lineageos.settings.device_preferences", Context.MODE_PRIVATE);
final String action = intent.getAction();
final boolean connected = intent.getBooleanExtra(
WindowManagerPolicyConstants.EXTRA_HDMI_PLUGGED_STATE, false);
WindowManagerPolicyConstants.EXTRA_HDMI_PLUGGED_STATE, false);
switch (action) {
case WindowManagerPolicyConstants.ACTION_HDMI_PLUGGED:
@@ -138,9 +138,9 @@ public class DockService extends Service {
case Intent.ACTION_SCREEN_ON:
Log.i(TAG, "Screen on");
DisplayUtils.setInternalDisplayState(!(mExternalDisplayConnected
&& sharedPrefs.getBoolean(
"disable_internal_on_external_connected", false)));
DisplayUtils.setInternalDisplayState(!(mExternalDisplayConnected &&
sharedPrefs.getBoolean("disable_internal_on_external_connected",
false)));
// Unlock device automatically if docked and reset res otherwise to
// work around broken HWC rotation