217 lines
8.9 KiB
Kotlin
217 lines
8.9 KiB
Kotlin
package com.catshark.lpt.ui
|
||
|
||
import android.content.ComponentName
|
||
import android.content.Context
|
||
import android.content.Intent
|
||
import android.net.Uri
|
||
import android.os.Build
|
||
import android.os.PowerManager
|
||
import android.provider.Settings
|
||
import androidx.compose.foundation.layout.Arrangement
|
||
import androidx.compose.foundation.layout.Column
|
||
import androidx.compose.foundation.layout.Row
|
||
import androidx.compose.foundation.layout.Spacer
|
||
import androidx.compose.foundation.layout.fillMaxSize
|
||
import androidx.compose.foundation.layout.fillMaxWidth
|
||
import androidx.compose.foundation.layout.height
|
||
import androidx.compose.foundation.layout.padding
|
||
import androidx.compose.foundation.rememberScrollState
|
||
import androidx.compose.foundation.verticalScroll
|
||
import androidx.compose.material3.Button
|
||
import androidx.compose.material3.Card
|
||
import androidx.compose.material3.MaterialTheme
|
||
import androidx.compose.material3.OutlinedButton
|
||
import androidx.compose.material3.Text
|
||
import androidx.compose.material3.TextButton
|
||
import androidx.compose.runtime.Composable
|
||
import androidx.compose.runtime.DisposableEffect
|
||
import androidx.compose.runtime.getValue
|
||
import androidx.compose.runtime.mutableIntStateOf
|
||
import androidx.compose.runtime.remember
|
||
import androidx.compose.runtime.setValue
|
||
import androidx.compose.ui.Alignment
|
||
import androidx.compose.ui.Modifier
|
||
import androidx.compose.ui.platform.LocalContext
|
||
import androidx.compose.ui.text.font.FontWeight
|
||
import androidx.compose.ui.unit.dp
|
||
import androidx.lifecycle.Lifecycle
|
||
import androidx.lifecycle.LifecycleEventObserver
|
||
import androidx.lifecycle.compose.LocalLifecycleOwner
|
||
|
||
@Composable
|
||
fun PermissionScreen(onBack: () -> Unit) {
|
||
val context = LocalContext.current
|
||
var refreshKey by remember { mutableIntStateOf(0) }
|
||
|
||
// 从系统设置页返回时自动刷新权限状态
|
||
val lifecycleOwner = LocalLifecycleOwner.current
|
||
DisposableEffect(lifecycleOwner) {
|
||
val observer = LifecycleEventObserver { _, event ->
|
||
if (event == Lifecycle.Event.ON_RESUME) refreshKey++
|
||
}
|
||
lifecycleOwner.lifecycle.addObserver(observer)
|
||
onDispose { lifecycleOwner.lifecycle.removeObserver(observer) }
|
||
}
|
||
|
||
val overlayGranted = remember(refreshKey) { Settings.canDrawOverlays(context) }
|
||
val batteryIgnored = remember(refreshKey) { isIgnoringBatteryOptimizations(context) }
|
||
val notificationGranted = remember(refreshKey) {
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||
context.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) ==
|
||
android.content.pm.PackageManager.PERMISSION_GRANTED
|
||
} else {
|
||
true
|
||
}
|
||
}
|
||
|
||
Column(
|
||
modifier = Modifier
|
||
.fillMaxSize()
|
||
.verticalScroll(rememberScrollState())
|
||
.padding(16.dp),
|
||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||
) {
|
||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
|
||
Text("权限与后台", style = MaterialTheme.typography.titleLarge, modifier = Modifier.weight(1f))
|
||
TextButton(onClick = onBack) { Text("返回") }
|
||
}
|
||
Text(
|
||
"悬浮窗是核心功能:没有悬浮窗权限,跨应用计时就不成立。MIUI 还需要额外放行后台。",
|
||
style = MaterialTheme.typography.bodySmall,
|
||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||
)
|
||
|
||
PermissionCard(
|
||
title = "悬浮窗权限(必须)",
|
||
description = "允许 LPT 在其他应用之上显示学习计时浮窗。",
|
||
granted = overlayGranted,
|
||
actionLabel = "去开启",
|
||
onAction = { openOverlaySettings(context) },
|
||
)
|
||
|
||
PermissionCard(
|
||
title = "通知权限",
|
||
description = "前台服务需要一条常驻通知;Android 13+ 需要授权。",
|
||
granted = notificationGranted,
|
||
actionLabel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) "去开启" else "无需(本机低于 13)",
|
||
onAction = { openAppNotificationSettings(context) },
|
||
)
|
||
|
||
PermissionCard(
|
||
title = "电池优化白名单",
|
||
description = "避免长时间学习时被系统冻结计时。",
|
||
granted = batteryIgnored,
|
||
actionLabel = "去设置",
|
||
onAction = { requestIgnoreBatteryOptimizations(context) },
|
||
)
|
||
|
||
Card(modifier = Modifier.fillMaxWidth()) {
|
||
Column(modifier = Modifier.padding(14.dp)) {
|
||
Text("小米 / MIUI 额外设置", style = MaterialTheme.typography.titleSmall, fontWeight = FontWeight.SemiBold)
|
||
Spacer(Modifier.height(6.dp))
|
||
Text(
|
||
"MIUI 会限制后台启动与后台弹窗,需要手动放行「自启动」和「其他权限 → 后台弹出界面」,并把省电策略设为「无限制」。",
|
||
style = MaterialTheme.typography.bodySmall,
|
||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||
)
|
||
Spacer(Modifier.height(10.dp))
|
||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||
OutlinedButton(onClick = { openMiuiAutostart(context) }) { Text("自启动管理") }
|
||
OutlinedButton(onClick = { openAppDetailSettings(context) }) { Text("应用详情") }
|
||
}
|
||
}
|
||
}
|
||
|
||
Button(onClick = onBack, modifier = Modifier.fillMaxWidth()) { Text("完成") }
|
||
Spacer(Modifier.height(16.dp))
|
||
}
|
||
}
|
||
|
||
@Composable
|
||
private fun PermissionCard(
|
||
title: String,
|
||
description: String,
|
||
granted: Boolean,
|
||
actionLabel: String,
|
||
onAction: () -> Unit,
|
||
) {
|
||
Card(modifier = Modifier.fillMaxWidth()) {
|
||
Column(modifier = Modifier.padding(14.dp)) {
|
||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) {
|
||
Text(title, style = MaterialTheme.typography.titleSmall, modifier = Modifier.weight(1f))
|
||
Text(
|
||
if (granted) "已授权" else "未授权",
|
||
style = MaterialTheme.typography.labelMedium,
|
||
color = if (granted) MaterialTheme.colorScheme.secondary else MaterialTheme.colorScheme.error,
|
||
)
|
||
}
|
||
Spacer(Modifier.height(4.dp))
|
||
Text(
|
||
description,
|
||
style = MaterialTheme.typography.bodySmall,
|
||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||
)
|
||
if (!granted) {
|
||
Spacer(Modifier.height(8.dp))
|
||
Button(onClick = onAction) { Text(actionLabel) }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private fun openOverlaySettings(context: Context) {
|
||
val intent = Intent(
|
||
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
|
||
Uri.parse("package:${context.packageName}"),
|
||
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||
runCatching { context.startActivity(intent) }
|
||
.onFailure { runCatching { context.startActivity(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)) } }
|
||
}
|
||
|
||
private fun openAppNotificationSettings(context: Context) {
|
||
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
|
||
.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
|
||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||
runCatching { context.startActivity(intent) }
|
||
.onFailure { openAppDetailSettings(context) }
|
||
}
|
||
|
||
private fun openAppDetailSettings(context: Context) {
|
||
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
||
.setData(Uri.parse("package:${context.packageName}"))
|
||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||
runCatching { context.startActivity(intent) }
|
||
}
|
||
|
||
private fun requestIgnoreBatteryOptimizations(context: Context) {
|
||
val intent = Intent(
|
||
Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
|
||
Uri.parse("package:${context.packageName}"),
|
||
).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||
runCatching { context.startActivity(intent) }
|
||
.onFailure { runCatching { context.startActivity(Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)) } }
|
||
}
|
||
|
||
private fun openMiuiAutostart(context: Context) {
|
||
val candidates = listOf(
|
||
ComponentName(
|
||
"com.miui.securitycenter",
|
||
"com.miui.permcenter.autostart.AutoStartManagementActivity",
|
||
),
|
||
ComponentName(
|
||
"com.miui.securitycenter",
|
||
"com.miui.powercenter.PowerSettings",
|
||
),
|
||
)
|
||
for (component in candidates) {
|
||
val intent = Intent().setComponent(component).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||
if (runCatching { context.startActivity(intent) }.isSuccess) return
|
||
}
|
||
openAppDetailSettings(context)
|
||
}
|
||
|
||
private fun isIgnoringBatteryOptimizations(context: Context): Boolean {
|
||
val powerManager = context.getSystemService(PowerManager::class.java) ?: return false
|
||
return powerManager.isIgnoringBatteryOptimizations(context.packageName)
|
||
}
|