Skip to content

Commit

Permalink
[feature] allow to ignore battery optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Nov 27, 2024
1 parent c803b0b commit 9572a2d
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
<uses-permission android:name="android.permission.MANAGE_SUBSCRIPTION_USER_ASSOCIATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

<application
android:name=".App"
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/ui/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package me.capcom.smsgateway.ui

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.PowerManager
import android.provider.Settings
import android.text.InputType
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.content.edit
import androidx.preference.EditTextPreference
import androidx.preference.Preference
Expand All @@ -28,6 +36,15 @@ class SettingsFragment : PreferenceFragmentCompat() {

findPreference<Preference>("transient.app_version")?.summary =
"${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})"

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
findPreference<Preference>("system")?.isEnabled = false
findPreference<Preference>("system.disable_battery_optimizations")?.summary =
getString(R.string.battery_optimizations_not_supported_on_this_device)
} else {
findPreference<Preference>("system.disable_battery_optimizations")?.summary =
if (isIgnoringBatteryOptimizations()) getString(R.string.disabled) else getString(R.string.enabled)
}
}

override fun onDisplayPreferenceDialog(preference: Preference) {
Expand Down Expand Up @@ -62,6 +79,45 @@ class SettingsFragment : PreferenceFragmentCompat() {
super.onDisplayPreferenceDialog(preference)
}

override fun onPreferenceTreeClick(preference: Preference): Boolean {
if (preference.key == "system.disable_battery_optimizations") {
requestIgnoreBatteryOptimizations()
return true
}

return super.onPreferenceTreeClick(preference)
}

private fun requestIgnoreBatteryOptimizations() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Toast.makeText(
requireContext(),
getString(R.string.battery_optimizations_not_supported_on_this_device),
Toast.LENGTH_SHORT
).show()
return
}

if (isIgnoringBatteryOptimizations()) {
Toast.makeText(
requireContext(),
getString(R.string.battery_optimizations_already_disabled),
Toast.LENGTH_SHORT
).show()
return
}

val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
intent.data = Uri.parse("package:${requireContext().packageName}")
startActivity(intent)
}

@RequiresApi(Build.VERSION_CODES.M)
private fun isIgnoringBatteryOptimizations(): Boolean {
val powerManager = requireContext().getSystemService(Context.POWER_SERVICE) as PowerManager
return powerManager.isIgnoringBatteryOptimizations(requireContext().packageName)
}

companion object {
fun newInstance() = SettingsFragment()
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-notnight/ic_battery_optimizations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/black"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M16,4h-2V2h-4v2H8C7.45,4 7,4.45 7,5v16c0,0.55 0.45,1 1,1h8c0.55,0 1,-0.45 1,-1V5C17,4.45 16.55,4 16,4zM15,14h-2v2h-2v-2H9v-2h2v-2h2v2h2V14z" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_battery_optimizations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M16,4h-2V2h-4v2H8C7.45,4 7,4.45 7,5v16c0,0.55 0.45,1 1,1h8c0.55,0 1,-0.45 1,-1V5C17,4.45 16.55,4 16,4zM15,14h-2v2h-2v-2H9v-2h2v-2h2v2h2V14z" />
</vector>
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@
<string name="password">Password:</string>
<string name="server_address">Server address:</string>
<string name="to_apply_the_changes_restart_the_app_using_the_button_below">To apply the changes, restart the app using the button below.</string>
<string name="system">System</string>
<string name="can_affect_battery_life">can affect battery life</string>
<string name="disable_battery_optimizations">Disable battery optimizations</string>
<string name="disabled">Disabled</string>
<string name="enabled">Enabled</string>
<string name="battery_optimizations_not_supported_on_this_device">Battery optimizations not supported on this device</string>
<string name="battery_optimizations_already_disabled">Battery optimizations already disabled</string>
</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>

<PreferenceCategory
app:key="system"
app:title="@string/system">
<Preference
android:icon="@drawable/ic_battery_optimizations"
app:key="system.disable_battery_optimizations"
app:summary="@string/can_affect_battery_life"
app:title="@string/disable_battery_optimizations" />
</PreferenceCategory>

<PreferenceCategory app:title="@string/information">
<Preference
android:key="transient.app_version"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
plugins {
id 'com.android.application' version '8.1.2' apply false
id 'com.android.library' version '8.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.7.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.22' apply false
}

task clean(type: Delete) {
Expand Down

0 comments on commit 9572a2d

Please sign in to comment.