Skip to content

Commit

Permalink
improve battery optimization settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Oct 8, 2023
1 parent 312b60e commit 0ead846
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "org.blitzortung.android.app"
minSdkVersion 15
targetSdkVersion 33
versionCode 287
versionName '2.1.13'
versionCode 290
versionName '2.1.14'
multiDexEnabled false
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Expand Down
30 changes: 20 additions & 10 deletions app/src/main/java/org/blitzortung/android/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.graphics.Color
import android.location.LocationManager.*
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.PowerManager
Expand Down Expand Up @@ -129,6 +130,7 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
Log.d(LOG_TAG, "Main.onDataUpdate() received request started event")
statusComponent.startProgress()
}

is ResultEvent -> {

statusComponent.indicateError(event.failed)
Expand Down Expand Up @@ -180,6 +182,7 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {

binding.legendView.invalidate()
}

is StatusEvent -> {
setStatusString(event.status)
}
Expand Down Expand Up @@ -591,7 +594,9 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
false
}

val requiresPermission = checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED
val checkSelfPermission = checkSelfPermission(permission)
Log.v(LOG_TAG, "self permission: $checkSelfPermission")
val requiresPermission = checkSelfPermission != PackageManager.PERMISSION_GRANTED

val requestCode = (LocationProviderRelation.byProviderName[locationProviderName]?.ordinal
?: Int.MAX_VALUE)
Expand All @@ -602,13 +607,9 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
.setCancelable(false)
.setNeutralButton(android.R.string.ok) { dialog, count ->
requestPermission(
if (requiresBackgroundPermission) {
arrayOf(permission, ACCESS_BACKGROUND_LOCATION)
} else {
arrayOf(permission)
},
arrayOf(permission),
requestCode,
if (requiresBackgroundPermission) R.string.location_permission_background_required else R.string.location_permission_required
R.string.location_permission_required
)
}
.show()
Expand All @@ -630,7 +631,7 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
val permissionStatus = checkSelfPermission(permission[0])
Log.v(
LOG_TAG,
"Main.requestPermission() permission: $permission, status: $permissionStatus, shouldRequest: ${!shouldShowPermissionRationale}"
"Main.requestPermission() permission: ${permission.joinToString()}, status: $permissionStatus, shouldRequest: ${!shouldShowPermissionRationale}"
)
if (!shouldShowPermissionRationale && permissionStatus != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permission, requestCode)
Expand Down Expand Up @@ -658,8 +659,16 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
DialogInterface.BUTTON_POSITIVE -> {
Log.v(LOG_TAG, "requestWakeupPermissions() request ignore battery optimizations")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val intent = Intent()
intent.action = Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
val allowIgnoreBatteryOptimization =
context.checkSelfPermission(REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_GRANTED
val intent = if (allowIgnoreBatteryOptimization) {
Intent(
Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:" + packageName)
)
} else {
Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
}

try {
startActivity(intent)
Expand Down Expand Up @@ -742,6 +751,7 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener {
PreferenceKey.BACKGROUND_QUERY_PERIOD -> {
backgroundAlerts = sharedPreferences.get(key, "0").toInt() > 0
}

else -> {}
}
}
Expand Down

0 comments on commit 0ead846

Please sign in to comment.