mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-05-29 21:10:20 +02:00
fix(setup_activity): permission screen crash
This commit is contained in:
parent
d4d0362b0e
commit
2917f1b6e5
@ -141,6 +141,10 @@ class SetupActivity : ComponentActivity() {
|
|||||||
) {
|
) {
|
||||||
requiredScreens.forEach { screen ->
|
requiredScreens.forEach { screen ->
|
||||||
screen.allowNext = { canGoNext = it }
|
screen.allowNext = { canGoNext = it }
|
||||||
|
screen.goNext = {
|
||||||
|
canGoNext = true
|
||||||
|
nextScreen()
|
||||||
|
}
|
||||||
composable(screen.route) {
|
composable(screen.route) {
|
||||||
BackHandler(true) {}
|
BackHandler(true) {}
|
||||||
Column(
|
Column(
|
||||||
|
@ -12,6 +12,7 @@ import me.rhunk.snapenhance.RemoteSideContext
|
|||||||
abstract class SetupScreen {
|
abstract class SetupScreen {
|
||||||
lateinit var context: RemoteSideContext
|
lateinit var context: RemoteSideContext
|
||||||
lateinit var allowNext: (Boolean) -> Unit
|
lateinit var allowNext: (Boolean) -> Unit
|
||||||
|
lateinit var goNext: () -> Unit
|
||||||
lateinit var route: String
|
lateinit var route: String
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
@ -18,6 +18,7 @@ import androidx.compose.material3.Icon
|
|||||||
import androidx.compose.material3.OutlinedCard
|
import androidx.compose.material3.OutlinedCard
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -56,66 +57,82 @@ class PermissionsScreen : SetupScreen() {
|
|||||||
var isBatteryOptimisationIgnored by remember { mutableStateOf(false) }
|
var isBatteryOptimisationIgnored by remember { mutableStateOf(false) }
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
||||||
notificationPermissionGranted = context.androidContext.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
|
|
||||||
}
|
|
||||||
val powerManager = context.androidContext.getSystemService(Context.POWER_SERVICE) as PowerManager
|
|
||||||
isBatteryOptimisationIgnored = powerManager.isIgnoringBatteryOptimizations(context.androidContext.packageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isBatteryOptimisationIgnored && notificationPermissionGranted) {
|
if (isBatteryOptimisationIgnored && notificationPermissionGranted) {
|
||||||
allowNext(true)
|
allowNext(true)
|
||||||
} else {
|
} else {
|
||||||
allowNext(false)
|
allowNext(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(Unit) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
notificationPermissionGranted =
|
||||||
|
context.androidContext.checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
|
||||||
|
}
|
||||||
|
val powerManager =
|
||||||
|
context.androidContext.getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||||
|
isBatteryOptimisationIgnored =
|
||||||
|
powerManager.isIgnoringBatteryOptimizations(context.androidContext.packageName)
|
||||||
|
if (isBatteryOptimisationIgnored && notificationPermissionGranted) {
|
||||||
|
goNext()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DialogText(text = context.translation["setup.permissions.dialog"])
|
DialogText(text = context.translation["setup.permissions.dialog"])
|
||||||
|
|
||||||
OutlinedCard(
|
OutlinedCard(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(16.dp)
|
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
) {
|
) {
|
||||||
Column(
|
Column(
|
||||||
|
verticalArrangement = Arrangement.spacedBy(16.dp),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(5.dp)
|
.padding(all = 16.dp),
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
horizontalArrangement = Arrangement.Absolute.SpaceAround
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
) {
|
) {
|
||||||
DialogText(text = context.translation["setup.permissions.dialog"], modifier = Modifier.weight(1f))
|
DialogText(
|
||||||
|
text = context.translation["setup.permissions.notification_access"],
|
||||||
|
modifier = Modifier.weight(1f)
|
||||||
|
)
|
||||||
if (notificationPermissionGranted) {
|
if (notificationPermissionGranted) {
|
||||||
GrantedIcon()
|
GrantedIcon()
|
||||||
return@Row
|
} else {
|
||||||
}
|
RequestButton {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
RequestButton {
|
activityLauncherHelper.requestPermission(Manifest.permission.POST_NOTIFICATIONS) { resultCode, _ ->
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
coroutineScope.launch {
|
||||||
activityLauncherHelper.requestPermission(Manifest.permission.POST_NOTIFICATIONS) { resultCode, _ ->
|
notificationPermissionGranted =
|
||||||
coroutineScope.launch {
|
resultCode == ComponentActivity.RESULT_OK
|
||||||
notificationPermissionGranted = resultCode == ComponentActivity.RESULT_OK
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Row {
|
|
||||||
DialogText(text = context.translation["setup.permissions.battery_optimization"], modifier = Modifier.weight(1f))
|
Row(
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
) {
|
||||||
|
DialogText(
|
||||||
|
text = context.translation["setup.permissions.battery_optimization"],
|
||||||
|
modifier = Modifier.weight(1f)
|
||||||
|
)
|
||||||
if (isBatteryOptimisationIgnored) {
|
if (isBatteryOptimisationIgnored) {
|
||||||
GrantedIcon()
|
GrantedIcon()
|
||||||
return@Row
|
} else {
|
||||||
}
|
RequestButton {
|
||||||
RequestButton {
|
activityLauncherHelper.launch(Intent().apply {
|
||||||
activityLauncherHelper.launch(Intent().apply {
|
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||||
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
data = Uri.parse("package:${context.androidContext.packageName}")
|
||||||
data = Uri.parse("package:${context.androidContext.packageName}")
|
}) { resultCode, _ ->
|
||||||
}) { resultCode, _ ->
|
coroutineScope.launch {
|
||||||
coroutineScope.launch {
|
isBatteryOptimisationIgnored = resultCode == 0
|
||||||
isBatteryOptimisationIgnored = resultCode == 0
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user