-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBatteryOptimizationDialog.kt
More file actions
72 lines (66 loc) · 2.19 KB
/
Copy pathBatteryOptimizationDialog.kt
File metadata and controls
72 lines (66 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package eu.kanade.presentation.components
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import tachiyomi.i18n.MR
import tachiyomi.presentation.core.i18n.stringResource
/**
* Dialog shown when the user queues 10+ items for download and the app is subject
* to battery optimization. Prompts user to exempt the app from battery optimization.
*/
@Composable
fun BatteryOptimizationDialog(
onDismiss: () -> Unit,
) {
val context = LocalContext.current
AlertDialog(
onDismissRequest = onDismiss,
title = {
Text(text = stringResource(MR.strings.battery_optimization_title))
},
text = {
Text(text = stringResource(MR.strings.battery_optimization_description))
},
dismissButton = {
TextButton(onClick = onDismiss) {
Text(text = stringResource(MR.strings.action_cancel))
}
},
confirmButton = {
TextButton(onClick = {
openBatteryOptimizationSettings(context)
onDismiss()
}) {
Text(text = stringResource(MR.strings.battery_optimization_settings))
}
},
)
}
/**
* Opens the battery optimization settings for the app.
*/
fun openBatteryOptimizationSettings(context: Context) {
val packageName = context.packageName
val intent = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).apply {
data = android.net.Uri.parse("package:$packageName")
}
}
else -> {
Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
}
}
try {
context.startActivity(intent)
} catch (e: Exception) {
// Fallback to general battery settings if specific intent fails
context.startActivity(Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS))
}
}