Add: Ignore battery optimization's dialog (to fix crashes on A12+)

This commit is contained in:
machiav3lli 2022-04-10 14:48:32 +02:00
parent 00f6822cc5
commit 57f2944697
4 changed files with 49 additions and 2 deletions

View File

@ -37,7 +37,8 @@ object Preferences {
Key.Theme,
Key.DefaultTab,
Key.UpdateNotify,
Key.UpdateUnstable
Key.UpdateUnstable,
Key.IgnoreIgnoreBatteryOptimization
).map { Pair(it.name, it) }.toMap()
fun init(context: Context) {
@ -178,6 +179,9 @@ object Preferences {
object UpdateNotify : Key<Boolean>("update_notify", Value.BooleanValue(true))
object UpdateUnstable : Key<Boolean>("update_unstable", Value.BooleanValue(false))
object IgnoreIgnoreBatteryOptimization :
Key<Boolean>("ignore_ignore_battery_optimization", Value.BooleanValue(false))
}
sealed class AutoSync(override val valueString: String) : Enumeration<AutoSync> {

View File

@ -3,6 +3,7 @@ package com.looker.droidify.ui.activities
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.PowerManager
import android.view.Menu
import android.view.MenuItem
import android.view.inputmethod.InputMethodManager
@ -31,6 +32,7 @@ import com.looker.droidify.ui.fragments.Source
import com.looker.droidify.ui.viewmodels.MainActivityViewModelX
import com.looker.droidify.utility.extension.android.Android
import com.looker.droidify.utility.extension.text.nullIfEmpty
import com.looker.droidify.utility.showBatteryOptimizationDialog
import kotlinx.coroutines.launch
import kotlin.properties.Delegates
@ -52,6 +54,7 @@ class MainActivityX : AppCompatActivity() {
lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var navController: NavController
private val viewModel: MainActivityViewModelX by viewModels()
private lateinit var powerManager: PowerManager
val menuSetup = MutableLiveData<Boolean>()
val syncConnection = Connection(SyncService::class.java, onBind = { _, _ ->
@ -95,6 +98,7 @@ class MainActivityX : AppCompatActivity() {
setupActionBarWithNavController(navController, appBarConfiguration)
binding.bottomNavigation.selectedItemId = currentTab
powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
if (savedInstanceState == null && (intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
handleIntent(intent)
}
@ -117,6 +121,8 @@ class MainActivityX : AppCompatActivity() {
super.onResume()
if (currentTheme != Preferences[Preferences.Key.Theme].getResId(resources.configuration))
recreate()
if (!powerManager.isIgnoringBatteryOptimizations(this.packageName) && !Preferences[Preferences.Key.IgnoreIgnoreBatteryOptimization])
showBatteryOptimizationDialog()
}
override fun onSupportNavigateUp(): Boolean {

View File

@ -3,11 +3,19 @@ package com.looker.droidify.utility
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.Signature
import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.PowerManager
import android.provider.Settings
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import com.looker.droidify.BuildConfig
import com.looker.droidify.PREFS_LANGUAGE_DEFAULT
import com.looker.droidify.R
@ -193,4 +201,28 @@ val isBlackTheme: Boolean
is Preferences.Theme.Amoled -> true
is Preferences.Theme.AmoledSystem -> true
else -> false
}
}
fun Context.showBatteryOptimizationDialog() {
AlertDialog.Builder(this)
.setTitle(R.string.ignore_battery_optimization_title)
.setMessage(R.string.ignore_battery_optimization_message)
.setPositiveButton(R.string.dialog_approve) { dialog: DialogInterface?, _: Int ->
val intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
intent.data = Uri.parse("package:" + this.packageName)
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
Toast.makeText(
this,
R.string.ignore_battery_optimization_not_supported,
Toast.LENGTH_LONG
).show()
Preferences[Preferences.Key.IgnoreIgnoreBatteryOptimization] = true
}
}
.setNeutralButton(R.string.dialog_refuse) { _: DialogInterface?, _: Int ->
Preferences[Preferences.Key.IgnoreIgnoreBatteryOptimization] = true
}
.show()
}

View File

@ -189,4 +189,9 @@
<string name="default_tab">Default Tab</string>
<string name="pending">Pending</string>
<string name="installing">Installing</string>
<string name="ignore_battery_optimization_title">Ignore Battery Optimization</string>
<string name="ignore_battery_optimization_message">Starting Android 12 there\'s restrictions on running foreground services causing the app to crash on downloads. To prevent this, you should turn off the battery optimization</string>
<string name="ignore_battery_optimization_not_supported">The device doesn\'t support ignoring battery optimizations!</string>
<string name="dialog_refuse">Don\'t need it</string>
<string name="dialog_approve">Let\'s do it!</string>
</resources>