cherish: Smart Charging (2/4)

Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
Pranav Vashi
2022-09-01 13:23:08 +05:30
committed by Hưng Phan
parent 6857a13e3e
commit 1526c11a2f
5 changed files with 165 additions and 31 deletions

View File

@@ -680,4 +680,13 @@
<string name="power_menu_transparency">Power/reboot menu opacity</string>
<string name="power_menu_dialog_dim">Power/reboot dialog dim background amount</string>
<!-- Smart charging -->
<string name="smart_charging_title">Smart Charging</string>
<string name="smart_charging_summary">Set maximum charging level</string>
<string name="smart_charging_switch_title">Enable Smart Charging</string>
<string name="smart_charging_level_title">Stop trigger level</string>
<string name="smart_charging_resume_level_title">Start trigger level</string>
<string name="smart_charging_reset_stats_title">Reset battery statistics</string>
<string name="smart_charging_reset_stats_summary">Reset battery statistics after reaching user defined charging level</string>
<string name="smart_charging_footer">Smart Charging allows you to set maximum charging level to extend the lifespan of your battery. When enabled, battery stops charging at Stop trigger level and resumes charging at Start trigger level. Stop trigger level should be always greater than Start trigger level.</string>
</resources>

View File

@@ -31,6 +31,13 @@
android:summary="@string/laboratory_ignore_window_secure_summary"
android:defaultValue="false"/>
<!-- Smart Charging -->
<Preference
android:key="smart_charging"
android:title="@string/smart_charging_title"
android:summary="@string/smart_charging_summary"
android:fragment="com.cherish.settings.fragments.SmartCharging"/>
<!-- Unlock FPS for specific games -->
<SwitchPreference
android:key="use_games_spoof"

View File

@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2020-2022 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/smart_charging_title">
<com.cherish.settings.preferences.SystemSettingSwitchPreference
android:key="smart_charging"
android:title="@string/smart_charging_switch_title"
android:summary="@string/smart_charging_summary"
android:defaultValue="false" />
<com.cherish.settings.preferences.SystemSettingSwitchPreference
android:key="smart_charging_reset_stats"
android:title="@string/smart_charging_reset_stats_title"
android:summary="@string/smart_charging_reset_stats_summary"
android:defaultValue="false"
android:dependency="smart_charging" />
<com.cherish.settings.preferences.SystemSettingSeekBarPreference
android:key="smart_charging_level"
android:title="@string/smart_charging_level_title"
android:max="100"
settings:min="65"
settings:units="%"
android:defaultValue="80"
android:dependency="smart_charging" />
<com.cherish.settings.preferences.SystemSettingSeekBarPreference
android:key="smart_charging_resume_level"
android:title="@string/smart_charging_resume_level_title"
android:max="99"
settings:min="15"
settings:units="%"
android:defaultValue="60"
android:dependency="smart_charging" />
<com.android.settingslib.widget.FooterPreference
android:key="smart_charging_footer"
android:selectable="false"
settings:searchable="false"
android:dependency="smart_charging" />
</PreferenceScreen>

View File

@@ -39,10 +39,11 @@ import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
import android.provider.SearchIndexableResource;
import com.cherish.settings.fragments.SmartCharging;
import java.util.ArrayList;
import java.util.List;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
@SearchIndexable
public class MiscSettings extends SettingsPreferenceFragment implements
OnPreferenceChangeListener {
private static final String KEY_GAMES_SPOOF = "use_games_spoof";
@@ -51,8 +52,11 @@ public class MiscSettings extends SettingsPreferenceFragment implements
private static final String SYS_GAMES_SPOOF = "persist.sys.pixelprops.games";
private static final String SYS_PHOTOS_SPOOF = "persist.sys.pixelprops.gphotos";
private static final String SMART_CHARGING = "smart_charging";
private SwitchPreference mGamesSpoof;
private SwitchPreference mPhotosSpoof;
private Preference mSmartCharging;
@Override
public void onCreate(Bundle icicle) {
@@ -61,15 +65,8 @@ public class MiscSettings extends SettingsPreferenceFragment implements
addPreferencesFromResource(R.xml.cherish_settings_misc);
Resources res = null;
Context ctx = getContext();
float density = Resources.getSystem().getDisplayMetrics().density;
try {
res = ctx.getPackageManager().getResourcesForApplication("com.android.systemui");
} catch (NameNotFoundException e) {
e.printStackTrace();
}
final PreferenceScreen prefScreen = getPreferenceScreen();
final Resources res = getResources();
mGamesSpoof = (SwitchPreference) findPreference(KEY_GAMES_SPOOF);
mGamesSpoof.setChecked(SystemProperties.getBoolean(SYS_GAMES_SPOOF, false));
@@ -79,6 +76,12 @@ public class MiscSettings extends SettingsPreferenceFragment implements
mPhotosSpoof.setChecked(SystemProperties.getBoolean(SYS_PHOTOS_SPOOF, true));
mPhotosSpoof.setOnPreferenceChangeListener(this);
mSmartCharging = (Preference) prefScreen.findPreference(SMART_CHARGING);
boolean mSmartChargingSupported = res.getBoolean(
com.android.internal.R.bool.config_smartChargingAvailable);
if (!mSmartChargingSupported)
prefScreen.removePreference(mSmartCharging);
}
@Override
@@ -101,27 +104,17 @@ public class MiscSettings extends SettingsPreferenceFragment implements
return MetricsProto.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_misc;
result.add(sir);
return result;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.cherish_settings_misc) {
@Override
public List<String> getNonIndexableKeys(Context context) {
List<String> keys = super.getNonIndexableKeys(context);
boolean mSmartChargingSupported = context.getResources().getBoolean(
com.android.internal.R.bool.config_smartChargingAvailable);
if (!mSmartChargingSupported)
keys.add(SMART_CHARGING);
return keys;
}
};

View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2020-2022 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.Context;
import android.content.ContentResolver;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.SwitchPreference;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
public class SmartCharging extends SettingsPreferenceFragment {
private static final String TAG = "SmartCharging";
private static final String SMART_CHARGING_FOOTER = "smart_charging_footer";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.smart_charging);
findPreference(SMART_CHARGING_FOOTER).setTitle(R.string.smart_charging_footer);
}
public static void reset(Context mContext) {
ContentResolver resolver = mContext.getContentResolver();
Settings.System.putIntForUser(resolver,
Settings.System.SMART_CHARGING, 0, UserHandle.USER_CURRENT);
Settings.System.putIntForUser(resolver,
Settings.System.SMART_CHARGING_RESET_STATS, 0, UserHandle.USER_CURRENT);
Settings.System.putIntForUser(resolver,
Settings.System.SMART_CHARGING_LEVEL, 80, UserHandle.USER_CURRENT);
Settings.System.putIntForUser(resolver,
Settings.System.SMART_CHARGING_RESUME_LEVEL, 60, UserHandle.USER_CURRENT);
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.CHERISH_SETTINGS;
}
}