1
0

impr: add notification permission check

This commit is contained in:
Sweetbread 2025-05-04 17:47:58 +03:00
parent 8a54db266f
commit a3ff36ba67
Signed by: Sweetbread
GPG Key ID: 17A5CB9A7DD85319
2 changed files with 54 additions and 37 deletions

View File

@ -1,5 +1,7 @@
package ru.risdeveau.geotracker
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.Manifest.permission.POST_NOTIFICATIONS
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
@ -38,6 +40,17 @@ class LocationForegroundService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("Service", "onStartCommand")
if (!(hasPermission(ACCESS_FINE_LOCATION)
&& (
(Build.VERSION.SDK_INT < 33)
|| hasPermission(POST_NOTIFICATIONS)
)
)) {
stopSelf()
return START_NOT_STICKY
}
createNotificationChannel()
val notification = createNotification()
startForeground(1, notification)

View File

@ -5,8 +5,8 @@
package ru.risdeveau.geotracker
import android.Manifest
import android.content.Context
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.Manifest.permission.POST_NOTIFICATIONS
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
@ -87,7 +87,14 @@ class MainActivity : ComponentActivity() {
CircularProgressIndicator(Modifier.align(Alignment.Center))
LaunchedEffect(true) {
launch {
screen = if (health(SettingsPreferences.url))
screen = if (
health(SettingsPreferences.url)
&& hasPermission(ACCESS_FINE_LOCATION)
&& (
(Build.VERSION.SDK_INT < 33)
|| hasPermission(POST_NOTIFICATIONS)
)
)
Screen.Main
else
Screen.Settings
@ -116,6 +123,13 @@ fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
var url by remember { mutableStateOf(SettingsPreferences.url) }
var urlIsValid by remember { mutableStateOf(false) }
var loading by remember { mutableStateOf(false) }
var fineLoc by remember { mutableStateOf(hasPermission(ACCESS_FINE_LOCATION)) }
var notifications by remember { mutableStateOf(
if (Build.VERSION.SDK_INT >= 33) {
hasPermission(POST_NOTIFICATIONS)
} else true
) }
val hasPerms = fineLoc && notifications
LaunchedEffect(url) {
if (url.isNotEmpty()) {
@ -148,7 +162,10 @@ fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
}
)
LocationPermissionScreen()
GetPermission(ACCESS_FINE_LOCATION) { fineLoc = true; }
if (Build.VERSION.SDK_INT >= 33)
GetPermission(POST_NOTIFICATIONS) { notifications = true; }
Button({
SettingsPreferences.username = username.trim()
@ -157,7 +174,7 @@ fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
}, enabled = urlIsValid
&& !loading
&& username.trim().isNotEmpty()
&& hasLocationPermissions(appCtx)
&& hasPerms
) {
Text(appStr(R.string.apply))
}
@ -165,47 +182,34 @@ fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
}
@Composable
fun LocationPermissionScreen() {
fun GetPermission(permission: String, onSuccess: () -> Unit) {
val context = LocalContext.current
val locationPermissions = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION
)
var hasPerm by remember { mutableStateOf(
ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
)}
val hasLocationPermissions = remember {
mutableStateOf(
locationPermissions.all {
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
)
}
val launcher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
) { granted -> hasPerm = granted }
val permissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
hasLocationPermissions.value = permissions.all { it.value }
}
if (hasLocationPermissions.value) {
Button({}, enabled = false) {
Button(
onClick = { launcher.launch(permission) },
enabled = !hasPerm
) {
if (hasPerm) {
onSuccess()
Icon(Icons.Outlined.Done, "Done")
Text("Разрешения получены")
}
Text("Разрешение получено")
} else {
Button(onClick = {
permissionLauncher.launch(locationPermissions)
}) {
Text("Получить разрешения")
Text("Получить разрешение")
}
}
}
fun hasLocationPermissions(context: Context): Boolean {
fun hasPermission(permission: String): Boolean {
return ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
context,
Manifest.permission.ACCESS_COARSE_LOCATION
appCtx,
permission
) == PackageManager.PERMISSION_GRANTED
}