impr: add notification permission check
This commit is contained in:
parent
8a54db266f
commit
a3ff36ba67
@ -1,5 +1,7 @@
|
|||||||
package ru.risdeveau.geotracker
|
package ru.risdeveau.geotracker
|
||||||
|
|
||||||
|
import android.Manifest.permission.ACCESS_FINE_LOCATION
|
||||||
|
import android.Manifest.permission.POST_NOTIFICATIONS
|
||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
import android.app.NotificationChannel
|
import android.app.NotificationChannel
|
||||||
import android.app.NotificationManager
|
import android.app.NotificationManager
|
||||||
@ -38,6 +40,17 @@ class LocationForegroundService : Service() {
|
|||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
Log.d("Service", "onStartCommand")
|
Log.d("Service", "onStartCommand")
|
||||||
|
|
||||||
|
if (!(hasPermission(ACCESS_FINE_LOCATION)
|
||||||
|
&& (
|
||||||
|
(Build.VERSION.SDK_INT < 33)
|
||||||
|
|| hasPermission(POST_NOTIFICATIONS)
|
||||||
|
)
|
||||||
|
)) {
|
||||||
|
stopSelf()
|
||||||
|
return START_NOT_STICKY
|
||||||
|
}
|
||||||
|
|
||||||
createNotificationChannel()
|
createNotificationChannel()
|
||||||
val notification = createNotification()
|
val notification = createNotification()
|
||||||
startForeground(1, notification)
|
startForeground(1, notification)
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
package ru.risdeveau.geotracker
|
package ru.risdeveau.geotracker
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest.permission.ACCESS_FINE_LOCATION
|
||||||
import android.content.Context
|
import android.Manifest.permission.POST_NOTIFICATIONS
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
@ -87,7 +87,14 @@ class MainActivity : ComponentActivity() {
|
|||||||
CircularProgressIndicator(Modifier.align(Alignment.Center))
|
CircularProgressIndicator(Modifier.align(Alignment.Center))
|
||||||
LaunchedEffect(true) {
|
LaunchedEffect(true) {
|
||||||
launch {
|
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
|
Screen.Main
|
||||||
else
|
else
|
||||||
Screen.Settings
|
Screen.Settings
|
||||||
@ -116,6 +123,13 @@ fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
|
|||||||
var url by remember { mutableStateOf(SettingsPreferences.url) }
|
var url by remember { mutableStateOf(SettingsPreferences.url) }
|
||||||
var urlIsValid by remember { mutableStateOf(false) }
|
var urlIsValid by remember { mutableStateOf(false) }
|
||||||
var loading 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) {
|
LaunchedEffect(url) {
|
||||||
if (url.isNotEmpty()) {
|
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({
|
Button({
|
||||||
SettingsPreferences.username = username.trim()
|
SettingsPreferences.username = username.trim()
|
||||||
@ -157,7 +174,7 @@ fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
|
|||||||
}, enabled = urlIsValid
|
}, enabled = urlIsValid
|
||||||
&& !loading
|
&& !loading
|
||||||
&& username.trim().isNotEmpty()
|
&& username.trim().isNotEmpty()
|
||||||
&& hasLocationPermissions(appCtx)
|
&& hasPerms
|
||||||
) {
|
) {
|
||||||
Text(appStr(R.string.apply))
|
Text(appStr(R.string.apply))
|
||||||
}
|
}
|
||||||
@ -165,47 +182,34 @@ fun Settings(modifier: Modifier = Modifier, onConfirm: () -> Unit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun LocationPermissionScreen() {
|
fun GetPermission(permission: String, onSuccess: () -> Unit) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val locationPermissions = arrayOf(
|
var hasPerm by remember { mutableStateOf(
|
||||||
Manifest.permission.ACCESS_FINE_LOCATION
|
ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
|
||||||
)
|
)}
|
||||||
|
|
||||||
val hasLocationPermissions = remember {
|
val launcher = rememberLauncherForActivityResult(
|
||||||
mutableStateOf(
|
ActivityResultContracts.RequestPermission()
|
||||||
locationPermissions.all {
|
) { granted -> hasPerm = granted }
|
||||||
ContextCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
val permissionLauncher = rememberLauncherForActivityResult(
|
Button(
|
||||||
contract = ActivityResultContracts.RequestMultiplePermissions()
|
onClick = { launcher.launch(permission) },
|
||||||
) { permissions ->
|
enabled = !hasPerm
|
||||||
hasLocationPermissions.value = permissions.all { it.value }
|
) {
|
||||||
}
|
if (hasPerm) {
|
||||||
|
onSuccess()
|
||||||
if (hasLocationPermissions.value) {
|
|
||||||
Button({}, enabled = false) {
|
|
||||||
Icon(Icons.Outlined.Done, "Done")
|
Icon(Icons.Outlined.Done, "Done")
|
||||||
Text("Разрешения получены")
|
Text("Разрешение получено")
|
||||||
}
|
} else {
|
||||||
} else {
|
Text("Получить разрешение")
|
||||||
Button(onClick = {
|
|
||||||
permissionLauncher.launch(locationPermissions)
|
|
||||||
}) {
|
|
||||||
Text("Получить разрешения")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun hasLocationPermissions(context: Context): Boolean {
|
fun hasPermission(permission: String): Boolean {
|
||||||
return ContextCompat.checkSelfPermission(
|
return ContextCompat.checkSelfPermission(
|
||||||
context,
|
appCtx,
|
||||||
Manifest.permission.ACCESS_FINE_LOCATION
|
permission
|
||||||
) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(
|
|
||||||
context,
|
|
||||||
Manifest.permission.ACCESS_COARSE_LOCATION
|
|
||||||
) == PackageManager.PERMISSION_GRANTED
|
) == PackageManager.PERMISSION_GRANTED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user