Cherish: Add statusbar battery options
Allows the user to tune: - battery styles - battery percentage rcstar6696: - Adapted to our battery styles Change-Id: Icf9d072442d09b3a5dada4c21404eff5539d5c12 Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
@@ -1166,4 +1166,36 @@
|
||||
<item>7</item>
|
||||
<item>8</item>
|
||||
</string-array>
|
||||
|
||||
<!-- Battery percent -->
|
||||
<string-array name="battery_percent_entries">
|
||||
<item>@string/status_bar_battery_percentage_default</item>
|
||||
<item>@string/status_bar_battery_percentage_text_inside</item>
|
||||
<item>@string/status_bar_battery_percentage_text_next</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="battery_percent_values" translatable="false">
|
||||
<item>0</item>
|
||||
<item>1</item>
|
||||
<item>2</item>
|
||||
</string-array>
|
||||
|
||||
<!-- Battery -->
|
||||
<string-array name="status_bar_battery_style_entries" translatable="false">
|
||||
<item>@string/status_bar_battery_style_icon_portrait</item>
|
||||
<item>@string/status_bar_battery_style_circle</item>
|
||||
<item>@string/status_bar_battery_style_dotted_circle</item>
|
||||
<item>@string/status_bar_battery_style_filled_circle</item>
|
||||
<item>@string/status_bar_battery_style_text</item>
|
||||
<item>@string/status_bar_battery_style_hidden</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="status_bar_battery_style_values" translatable="false">
|
||||
<item>0</item>
|
||||
<item>1</item>
|
||||
<item>2</item>
|
||||
<item>3</item>
|
||||
<item>4</item>
|
||||
<item>5</item>
|
||||
</string-array>
|
||||
</resources>
|
||||
|
||||
@@ -1044,4 +1044,19 @@
|
||||
<string name="qs_columns_portrait_title">Tiles columns on portrait</string>
|
||||
<string name="qs_columns_landscape_title">Tiles columns on landscape</string>
|
||||
<string name="qs_tile_title_visibility_title">Show tiles title</string>
|
||||
|
||||
<!-- Battery Styles -->
|
||||
<string name="battery_percent_title">Statusbar battery indicator</string>
|
||||
<string name="battery_percent_summary">Configure icon and text styles for the statusbar battery indicator</string>
|
||||
<string name="status_bar_battery_style_title">Battery style</string>
|
||||
<string name="status_bar_battery_style_icon_portrait">Icon portrait</string>
|
||||
<string name="status_bar_battery_style_circle">Circle</string>
|
||||
<string name="status_bar_battery_style_dotted_circle">Dotted circle</string>
|
||||
<string name="status_bar_battery_style_filled_circle">Filled circle</string>
|
||||
<string name="status_bar_battery_style_text">Text</string>
|
||||
<string name="status_bar_battery_style_hidden">Hidden</string>
|
||||
<string name="status_bar_battery_percentage_title">Battery percentage</string>
|
||||
<string name="status_bar_battery_percentage_default">Hidden</string>
|
||||
<string name="status_bar_battery_percentage_text_inside">Inside the icon</string>
|
||||
<string name="status_bar_battery_percentage_text_next">Next to the icon</string>
|
||||
</resources>
|
||||
|
||||
@@ -24,6 +24,13 @@
|
||||
android:icon="@drawable/ic_network_traffic"
|
||||
android:fragment="com.cherish.settings.fragments.Traffic"
|
||||
android:title="@string/traffic_title" />
|
||||
|
||||
<Preference
|
||||
android:key="statusbar_battery"
|
||||
android:title="@string/battery_percent_title"
|
||||
android:icon="@drawable/ic_battery_styles"
|
||||
android:summary="@string/battery_percent_summary"
|
||||
android:fragment="com.cherish.settings.fragments.StatusbarBatterySettings"/>
|
||||
|
||||
<Preference
|
||||
android:key="clock_settings"
|
||||
|
||||
40
res/xml/cherish_settings_statusbar_battery.xml
Normal file
40
res/xml/cherish_settings_statusbar_battery.xml
Normal file
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2020-2021 The CherishOS 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"
|
||||
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
|
||||
android:title="@string/battery_percent_title">
|
||||
|
||||
<ListPreference
|
||||
android:key="status_bar_battery_style"
|
||||
android:title="@string/status_bar_battery_style_title"
|
||||
android:summary="%s"
|
||||
android:dialogTitle="@string/status_bar_battery_style_title"
|
||||
android:entries="@array/status_bar_battery_style_entries"
|
||||
android:entryValues="@array/status_bar_battery_style_values"
|
||||
android:defaultValue="0" />
|
||||
|
||||
<ListPreference
|
||||
android:key="status_bar_show_battery_percent"
|
||||
android:title="@string/battery_percent_title"
|
||||
android:dialogTitle="@string/battery_percent_title"
|
||||
android:entries="@array/battery_percent_entries"
|
||||
android:entryValues="@array/battery_percent_values"
|
||||
android:defaultValue="0" />
|
||||
|
||||
</PreferenceScreen>
|
||||
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (C) 2020-2021 The CherishOS 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.cherish.settings.fragments;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.preference.ListPreference;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.Preference.OnPreferenceChangeListener;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
|
||||
public class StatusbarBatterySettings extends SettingsPreferenceFragment implements
|
||||
Preference.OnPreferenceChangeListener {
|
||||
|
||||
private static final String STATUS_BAR_BATTERY_STYLE = "status_bar_battery_style";
|
||||
private static final String STATUS_BAR_SHOW_BATTERY_PERCENT = "status_bar_show_battery_percent";
|
||||
|
||||
private ListPreference mBatteryPercent;
|
||||
private ListPreference mBatteryStyle;
|
||||
|
||||
private int mBatteryPercentValue;
|
||||
private int mBatteryPercentValuePrev;
|
||||
|
||||
private static final int BATTERY_STYLE_PORTRAIT = 0;
|
||||
private static final int BATTERY_STYLE_TEXT = 4;
|
||||
private static final int BATTERY_STYLE_HIDDEN = 5;
|
||||
private static final int BATTERY_PERCENT_HIDDEN = 0;
|
||||
private static final int BATTERY_PERCENT_SHOW = 2;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.cherish_settings_statusbar_battery);
|
||||
|
||||
int batterystyle = Settings.System.getIntForUser(getContentResolver(),
|
||||
Settings.System.STATUS_BAR_BATTERY_STYLE, BATTERY_STYLE_PORTRAIT, UserHandle.USER_CURRENT);
|
||||
mBatteryStyle = findPreference("status_bar_battery_style");
|
||||
mBatteryStyle.setValue(String.valueOf(batterystyle));
|
||||
mBatteryStyle.setSummary(mBatteryStyle.getEntry());
|
||||
mBatteryStyle.setOnPreferenceChangeListener(this);
|
||||
|
||||
mBatteryPercentValue = Settings.System.getIntForUser(getContentResolver(),
|
||||
Settings.System.STATUS_BAR_SHOW_BATTERY_PERCENT, 0, UserHandle.USER_CURRENT);
|
||||
mBatteryPercentValuePrev = Settings.System.getIntForUser(getContentResolver(),
|
||||
Settings.System.STATUS_BAR_SHOW_BATTERY_PERCENT + "_prev", -1, UserHandle.USER_CURRENT);
|
||||
mBatteryPercent = findPreference("status_bar_show_battery_percent");
|
||||
mBatteryPercent.setValue(String.valueOf(mBatteryPercentValue));
|
||||
mBatteryPercent.setSummary(mBatteryPercent.getEntry());
|
||||
mBatteryPercent.setOnPreferenceChangeListener(this);
|
||||
|
||||
updateBatteryOptions(batterystyle, mBatteryPercentValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
ContentResolver resolver = getActivity().getContentResolver();
|
||||
if (preference == mBatteryStyle) {
|
||||
int batterystyle = Integer.parseInt((String) newValue);
|
||||
updateBatteryOptions(batterystyle, mBatteryPercentValue);
|
||||
int index = mBatteryStyle.findIndexOfValue((String) newValue);
|
||||
mBatteryStyle.setSummary(mBatteryStyle.getEntries()[index]);
|
||||
return true;
|
||||
} else if (preference == mBatteryPercent) {
|
||||
mBatteryPercentValue = Integer.parseInt((String) newValue);
|
||||
Settings.System.putIntForUser(resolver,
|
||||
Settings.System.STATUS_BAR_SHOW_BATTERY_PERCENT, mBatteryPercentValue,
|
||||
UserHandle.USER_CURRENT);
|
||||
int index = mBatteryPercent.findIndexOfValue((String) newValue);
|
||||
mBatteryPercent.setSummary(mBatteryPercent.getEntries()[index]);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void updateBatteryOptions(int batterystyle, int batterypercent) {
|
||||
ContentResolver resolver = getActivity().getContentResolver();
|
||||
switch (batterystyle) {
|
||||
case BATTERY_STYLE_TEXT:
|
||||
handleTextPercentage(BATTERY_PERCENT_SHOW);
|
||||
break;
|
||||
case BATTERY_STYLE_HIDDEN:
|
||||
handleTextPercentage(BATTERY_PERCENT_HIDDEN);
|
||||
break;
|
||||
default:
|
||||
mBatteryPercent.setEnabled(true);
|
||||
if (mBatteryPercentValuePrev != -1) {
|
||||
Settings.System.putIntForUser(resolver,
|
||||
Settings.System.STATUS_BAR_SHOW_BATTERY_PERCENT,
|
||||
mBatteryPercentValuePrev, UserHandle.USER_CURRENT);
|
||||
Settings.System.putIntForUser(resolver,
|
||||
Settings.System.STATUS_BAR_SHOW_BATTERY_PERCENT + "_prev",
|
||||
-1, UserHandle.USER_CURRENT);
|
||||
mBatteryPercentValue = mBatteryPercentValuePrev;
|
||||
mBatteryPercentValuePrev = -1;
|
||||
int index = mBatteryPercent.findIndexOfValue(String.valueOf(mBatteryPercentValue));
|
||||
mBatteryPercent.setSummary(mBatteryPercent.getEntries()[index]);
|
||||
}
|
||||
|
||||
Settings.System.putIntForUser(resolver,
|
||||
Settings.System.STATUS_BAR_BATTERY_STYLE, batterystyle,
|
||||
UserHandle.USER_CURRENT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void handleTextPercentage(int batterypercent) {
|
||||
ContentResolver resolver = getActivity().getContentResolver();
|
||||
if (mBatteryPercentValuePrev == -1) {
|
||||
mBatteryPercentValuePrev = mBatteryPercentValue;
|
||||
Settings.System.putIntForUser(resolver,
|
||||
Settings.System.STATUS_BAR_SHOW_BATTERY_PERCENT + "_prev",
|
||||
mBatteryPercentValue, UserHandle.USER_CURRENT);
|
||||
}
|
||||
|
||||
Settings.System.putIntForUser(resolver,
|
||||
Settings.System.STATUS_BAR_SHOW_BATTERY_PERCENT,
|
||||
batterypercent, UserHandle.USER_CURRENT);
|
||||
Settings.System.putIntForUser(resolver,
|
||||
Settings.System.STATUS_BAR_BATTERY_STYLE, BATTERY_STYLE_TEXT,
|
||||
UserHandle.USER_CURRENT);
|
||||
int index = mBatteryPercent.findIndexOfValue(String.valueOf(batterypercent));
|
||||
mBatteryPercent.setSummary(mBatteryPercent.getEntries()[index]);
|
||||
mBatteryPercent.setEnabled(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.CHERISH_SETTINGS;
|
||||
}
|
||||
|
||||
/**
|
||||
* For Search.
|
||||
*/
|
||||
|
||||
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider() {
|
||||
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
|
||||
boolean enabled) {
|
||||
ArrayList<SearchIndexableResource> result =
|
||||
new ArrayList<SearchIndexableResource>();
|
||||
SearchIndexableResource sir = new SearchIndexableResource(context);
|
||||
sir.xmlResId = R.xml.cherish_settings_statusbar_battery;
|
||||
result.add(sir);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNonIndexableKeys(Context context) {
|
||||
List<String> keys = super.getNonIndexableKeys(context);
|
||||
return keys;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user