From e4020a7517fb25d8e048282429a71c216ce84668 Mon Sep 17 00:00:00 2001 From: Tyler Nijmeh Date: Wed, 31 Mar 2021 02:12:53 -0700 Subject: Read settings in from device Signed-off-by: Tyler Nijmeh --- .../draco/buoy/fragments/MainPreferenceFragment.kt | 42 ++++++-- .../buoy/models/BatterySaverConstantsConfig.kt | 4 +- .../com/draco/buoy/utils/BatterySaverManager.kt | 10 ++ app/src/main/res/values/arrays.xml | 17 +++ app/src/main/res/values/strings.xml | 105 ++++++++++++++++--- app/src/main/res/xml/main.xml | 114 ++++++++++++++++++--- 6 files changed, 250 insertions(+), 42 deletions(-) create mode 100644 app/src/main/res/values/arrays.xml (limited to 'app/src') diff --git a/app/src/main/java/com/draco/buoy/fragments/MainPreferenceFragment.kt b/app/src/main/java/com/draco/buoy/fragments/MainPreferenceFragment.kt index e0be6d3..f757d39 100644 --- a/app/src/main/java/com/draco/buoy/fragments/MainPreferenceFragment.kt +++ b/app/src/main/java/com/draco/buoy/fragments/MainPreferenceFragment.kt @@ -4,8 +4,7 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.os.Bundle -import androidx.preference.Preference -import androidx.preference.PreferenceFragmentCompat +import androidx.preference.* import com.draco.buoy.R import com.draco.buoy.models.BatterySaverConstantsConfig import com.draco.buoy.repositories.BatterySaverConstantsConfigProfiles @@ -27,11 +26,11 @@ class MainPreferenceFragment : PreferenceFragmentCompat() { override fun onPreferenceTreeClick(preference: Preference): Boolean { when (preference.key) { - getString(R.string.profile_key_reset) -> resetProfile() - getString(R.string.profile_key_light) -> applyProfile(BatterySaverConstantsConfigProfiles.LIGHT) - getString(R.string.profile_key_moderate) -> applyProfile(BatterySaverConstantsConfigProfiles.MODERATE) - getString(R.string.profile_key_high) -> applyProfile(BatterySaverConstantsConfigProfiles.HIGH) - getString(R.string.profile_key_extreme) -> applyProfile(BatterySaverConstantsConfigProfiles.EXTREME) + getString(R.string.pref_profile_key_reset) -> resetProfile() + getString(R.string.pref_profile_key_light) -> applyProfile(BatterySaverConstantsConfigProfiles.LIGHT) + getString(R.string.pref_profile_key_moderate) -> applyProfile(BatterySaverConstantsConfigProfiles.MODERATE) + getString(R.string.pref_profile_key_high) -> applyProfile(BatterySaverConstantsConfigProfiles.HIGH) + getString(R.string.pref_profile_key_extreme) -> applyProfile(BatterySaverConstantsConfigProfiles.EXTREME) getString(R.string.pref_developer_key) -> openURL(getString(R.string.developer_url)) getString(R.string.pref_source_key) -> openURL(getString(R.string.source_url)) @@ -45,11 +44,39 @@ class MainPreferenceFragment : PreferenceFragmentCompat() { return true } + private fun updateSettings() { + val currentProfileString = batterySaverManager.getConstantsString() + val currentProfile = BatterySaverConstantsConfig().also { + if (currentProfileString != null) + it.import(currentProfileString) + } + + findPreference(getString(R.string.pref_config_key_advertise_is_enabled))?.isChecked = currentProfile.advertiseIsEnabled + findPreference(getString(R.string.pref_config_key_datasaver_enabled))?.isChecked = !currentProfile.dataSaverDisabled + findPreference(getString(R.string.pref_config_key_enable_night_mode))?.isChecked = currentProfile.enableNightMode + findPreference(getString(R.string.pref_config_key_launch_boost_enabled))?.isChecked = !currentProfile.launchBoostDisabled + findPreference(getString(R.string.pref_config_key_vibration_enabled))?.isChecked = !currentProfile.vibrationDisabled + findPreference(getString(R.string.pref_config_key_animation_enabled))?.isChecked = !currentProfile.animationDisabled + findPreference(getString(R.string.pref_config_key_soundtrigger_enabled))?.isChecked = !currentProfile.soundTriggerDisabled + findPreference(getString(R.string.pref_config_key_fullbackup_deferred))?.isChecked = currentProfile.fullBackupDeferred + findPreference(getString(R.string.pref_config_key_keyvaluebackup_deferred))?.isChecked = currentProfile.keyValueBackupDeferred + findPreference(getString(R.string.pref_config_key_firewall_enabled))?.isChecked = !currentProfile.fireWallDisabled + findPreference(getString(R.string.pref_config_key_gps_mode))?.value = currentProfile.gpsMode.toString() + findPreference(getString(R.string.pref_config_key_adjust_brightness_enabled))?.isChecked = !currentProfile.adjustBrightnessDisabled + findPreference(getString(R.string.pref_config_key_adjust_brightness_factor))?.value = (currentProfile.adjustBrightnessFactor * 100).toInt() + findPreference(getString(R.string.pref_config_key_force_all_apps_standby))?.isChecked = currentProfile.forceAllAppsStandby + findPreference(getString(R.string.pref_config_key_force_background_check))?.isChecked = currentProfile.forceBackgroundCheck + findPreference(getString(R.string.pref_config_key_optional_sensors_disabled))?.isChecked = currentProfile.optionalSensorsDisabled + findPreference(getString(R.string.pref_config_key_aod_enabled))?.isChecked = currentProfile.aodDisabled + findPreference(getString(R.string.pref_config_key_quick_doze_enabled))?.isChecked = currentProfile.quickDozeEnabled + } + private fun applyProfile(profile: BatterySaverConstantsConfig) { batterySaverManager.setConstantsConfig(profile) batterySaverManager.setLowPower(true) batterySaverManager.setLowPowerSticky(true) batterySaverManager.setLowPowerStickyAutoDisableEnabled(false) + updateSettings() } private fun resetProfile() { @@ -57,6 +84,7 @@ class MainPreferenceFragment : PreferenceFragmentCompat() { batterySaverManager.setLowPower(false) batterySaverManager.setLowPowerSticky(false) batterySaverManager.setLowPowerStickyAutoDisableEnabled(true) + updateSettings() } private fun openURL(url: String) { diff --git a/app/src/main/java/com/draco/buoy/models/BatterySaverConstantsConfig.kt b/app/src/main/java/com/draco/buoy/models/BatterySaverConstantsConfig.kt index 6733227..c26ab01 100644 --- a/app/src/main/java/com/draco/buoy/models/BatterySaverConstantsConfig.kt +++ b/app/src/main/java/com/draco/buoy/models/BatterySaverConstantsConfig.kt @@ -32,7 +32,7 @@ data class BatterySaverConstantsConfig( "${BatterySaverConstants.ANIMATION_DISABLED}=$animationDisabled," + "${BatterySaverConstants.SOUNDTRIGGER_DISABLED}=$soundTriggerDisabled," + "${BatterySaverConstants.FULLBACKUP_DEFERRED}=$fullBackupDeferred," + - "${BatterySaverConstants.KEYVALUEBACKUP_DEFERRED}=$keyValueBackupDeferred" + + "${BatterySaverConstants.KEYVALUEBACKUP_DEFERRED}=$keyValueBackupDeferred," + "${BatterySaverConstants.FIREWALL_DISABLED}=$fireWallDisabled," + "${BatterySaverConstants.GPS_MODE}=$gpsMode," + "${BatterySaverConstants.ADJUST_BRIGHTNESS_DISABLED}=$adjustBrightnessDisabled," + @@ -44,7 +44,7 @@ data class BatterySaverConstantsConfig( "${BatterySaverConstants.QUICK_DOZE_ENABLED}=$quickDozeEnabled" } - fun fromString(string: String) { + fun import(string: String) { val keyValueMap = string.split(",").associate { val (key, value) = it.split("=") key to value diff --git a/app/src/main/java/com/draco/buoy/utils/BatterySaverManager.kt b/app/src/main/java/com/draco/buoy/utils/BatterySaverManager.kt index dbc2d37..f63b681 100644 --- a/app/src/main/java/com/draco/buoy/utils/BatterySaverManager.kt +++ b/app/src/main/java/com/draco/buoy/utils/BatterySaverManager.kt @@ -64,6 +64,16 @@ class BatterySaverManager(private val contentResolver: ContentResolver) { ) } + /** + * Get the raw battery saver constants secure setting + */ + fun getConstantsString(): String? { + return Settings.Global.getString( + contentResolver, + BatterySaverSecureSettings.BATTERY_SAVER_CONSTANTS + ) + } + /** * Set the battery saver constants secure setting via a config */ diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 0000000..6f690d3 --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,17 @@ + + + + 0 + 1 + 2 + 3 + 4 + + + Standard location access + GPS disabled when screen off + All disabled when screen off + Foreground location updates only + Throttle when screen off + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1ffd3d1..8e22ca1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,28 +16,101 @@ https://www.github.com/tytydraco/Buoy mailto:tylernij@gmail.com - Actions + Profiles About + Configuration - reset - Reset - Undo all changes done by the app and restore secure settings to default + reset + Reset + Undo all changes done by the app and restore secure settings to default - light - Light - Marginally conserve battery without impeding the user experience + light + Light + Marginally conserve battery without impeding the user experience - moderate - Moderate - Apply additional tweaks to save more battery without yet sacrificing performance + moderate + Moderate + Apply additional tweaks to save more battery without yet sacrificing performance - high - High - Sacrifice significant performance for a significant gain in power + high + High + Sacrifice significant performance for a significant gain in power - extreme - Extreme - Apply the maximum changes available to save power at the expense of a fluid user experience + extreme + Extreme + Apply the maximum changes available to save power at the expense of a fluid user experience + + advertise_is_enabled + Advertise Power Saver + Alert apps that the device is in low power mode; some apps will use this information to reduce their power draw + + datasaver_enabled + Enable Data Saver + Reduce the data sent and received on metered connections and on mobile data + + enable_night_mode + Enable Night Mode + Use darker colors to reduce the power draw on OLED panels + + launch_boost_enabled + Enable Launch Boost + Accelerate the CPU while opening applications to expedite their startup time + + vibration_enabled + Enable Vibrations + Allow touch and notification vibrations + + animation_enabled + Enable Animations + Continue to display animations + + soundtrigger_enabled + Enable Sound Triggers + ::TODO:: + + fullbackup_deferred + Defer Full Backups + Reschedule device cloud backups for later + + keyvaluebackup_deferred + Defer KeyValue Backups + Reschedule application settings cloud backups for later + + firewall_enabled + Enable Firewall + Defend against possibly malicious web requests + + gps_mode + Location Mode + How location requests are handled in low power mode + + adjust_brightness_disabled + Adjust Brightness + Reduce the maximum brightness on the panel + + adjust_brightness_factor + Brightness Factor + By what factor to reduce the screen brightness by + + force_all_apps_standby + Force Apps Into Standby + Background apps are put to sleep very quickly + + force_background_check + Force Background Check + ::TODO:: + + optional_sensors_disabled + Disable Optional Sensors + Do not detect unnecessary sensor requests (i.e. edge sensors) + + aod_enabled + Always-On-Display Enabled + Display the AOD in sleep mode + + quick_doze_enabled + Quick Doze Enabled + Enter deep sleep as soon as the screen is turned off Developer Tyler Nijmeh diff --git a/app/src/main/res/xml/main.xml b/app/src/main/res/xml/main.xml index b383e10..a08daa6 100644 --- a/app/src/main/res/xml/main.xml +++ b/app/src/main/res/xml/main.xml @@ -1,26 +1,106 @@ - - + + + android:title="@string/pref_profile_title_reset" + android:summary="@string/pref_profile_summary_reset" + android:key="@string/pref_profile_key_reset" /> + android:title="@string/pref_profile_title_light" + android:summary="@string/pref_profile_summary_light" + android:key="@string/pref_profile_key_light" /> + android:title="@string/pref_profile_title_moderate" + android:summary="@string/pref_profile_summary_moderate" + android:key="@string/pref_profile_key_moderate" /> + android:title="@string/pref_profile_title_high" + android:summary="@string/pref_profile_summary_high" + android:key="@string/pref_profile_key_high" /> + android:title="@string/pref_profile_title_extreme" + android:summary="@string/pref_profile_summary_extreme" + android:key="@string/pref_profile_key_extreme" /> + + + + + + + + + + + + + + + + + + + +