mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
chore: update compileSdk and targetSdk to API 33
This commit is contained in:
parent
ef11af6e0b
commit
15ed09680f
7 changed files with 131 additions and 34 deletions
|
|
@ -117,16 +117,28 @@ class MainActivity : AppCompatActivity(), Logging {
|
|||
private val bluetoothViewModel: BluetoothViewModel by viewModels()
|
||||
private val model: UIViewModel by viewModels()
|
||||
|
||||
private val requestPermissionsLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
|
||||
if (!permissions.entries.all { it.value }) {
|
||||
errormsg("User denied permissions")
|
||||
private val bluetoothPermissionsLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->
|
||||
if (result.entries.all { it.value }) {
|
||||
info("Bluetooth permissions granted")
|
||||
} else {
|
||||
warn("Bluetooth permissions denied")
|
||||
showSnackbar(permissionMissing)
|
||||
}
|
||||
requestedEnable = false
|
||||
bluetoothViewModel.permissionsUpdated()
|
||||
}
|
||||
|
||||
private val notificationPermissionsLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->
|
||||
if (result.entries.all { it.value }) {
|
||||
info("Notification permissions granted")
|
||||
} else {
|
||||
warn("Notification permissions denied")
|
||||
showSnackbar(getString(R.string.notification_denied))
|
||||
}
|
||||
}
|
||||
|
||||
data class TabInfo(val text: String, val icon: Int, val content: Fragment)
|
||||
|
||||
private val tabInfos = arrayOf(
|
||||
|
|
@ -385,6 +397,17 @@ class MainActivity : AppCompatActivity(), Logging {
|
|||
if (model.provideLocation.value == true)
|
||||
service.startProvideLocation()
|
||||
}
|
||||
|
||||
if (!hasNotificationPermission()) {
|
||||
val notificationPermissions = getNotificationPermissions()
|
||||
rationaleDialog(
|
||||
shouldShowRequestPermissionRationale(notificationPermissions),
|
||||
R.string.notification_required,
|
||||
getString(R.string.why_notification_required),
|
||||
) {
|
||||
notificationPermissionsLauncher.launch(notificationPermissions)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// For other connection states, just slam them in
|
||||
model.setConnectionState(newConnection)
|
||||
|
|
@ -640,17 +663,10 @@ class MainActivity : AppCompatActivity(), Logging {
|
|||
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
||||
bleRequestEnable.launch(enableBtIntent)
|
||||
} else {
|
||||
MaterialAlertDialogBuilder(this)
|
||||
.setTitle(getString(R.string.required_permissions))
|
||||
.setMessage(permissionMissing)
|
||||
.setNeutralButton(R.string.cancel) { _, _ ->
|
||||
warn("User bailed due to permissions")
|
||||
}
|
||||
.setPositiveButton(R.string.accept) { _, _ ->
|
||||
info("requesting permissions")
|
||||
requestPermissionsLauncher.launch(getBluetoothPermissions())
|
||||
}
|
||||
.show()
|
||||
val bluetoothPermissions = getBluetoothPermissions()
|
||||
rationaleDialog(shouldShowRequestPermissionRationale(bluetoothPermissions)) {
|
||||
bluetoothPermissionsLauncher.launch(bluetoothPermissions)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,15 +2,19 @@ package com.geeksville.mesh.android
|
|||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.app.NotificationManager
|
||||
import android.bluetooth.BluetoothManager
|
||||
import android.location.LocationManager
|
||||
import android.companion.CompanionDeviceManager
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.hardware.usb.UsbManager
|
||||
import android.location.LocationManager
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.geeksville.mesh.R
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
/**
|
||||
* @return null on platforms without a BlueTooth driver (i.e. the emulator)
|
||||
|
|
@ -48,7 +52,7 @@ fun Context.gpsDisabled(): Boolean =
|
|||
if (hasGps()) !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) else false
|
||||
|
||||
/**
|
||||
* return the text string of the permissions missing
|
||||
* @return the text string of the permissions missing
|
||||
*/
|
||||
val Context.permissionMissing: String
|
||||
get() = if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S) {
|
||||
|
|
@ -57,6 +61,55 @@ val Context.permissionMissing: String
|
|||
getString(R.string.permission_missing_31)
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if any given permissions need to show rationale.
|
||||
*
|
||||
* @return true if should show UI with rationale before requesting a permission.
|
||||
*/
|
||||
fun Activity.shouldShowRequestPermissionRationale(permissions: Array<String>): Boolean {
|
||||
for (permission in permissions) {
|
||||
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if any given permissions need to show rationale.
|
||||
*
|
||||
* @return true if should show UI with rationale before requesting a permission.
|
||||
*/
|
||||
fun Fragment.shouldShowRequestPermissionRationale(permissions: Array<String>): Boolean {
|
||||
for (permission in permissions) {
|
||||
if (shouldShowRequestPermissionRationale(permission)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles whether a rationale dialog should be shown before performing an action.
|
||||
*/
|
||||
fun Context.rationaleDialog(
|
||||
shouldShowRequestPermissionRationale: Boolean = true,
|
||||
title: Int = R.string.required_permissions,
|
||||
rationale: CharSequence = permissionMissing,
|
||||
invokeFun: () -> Unit,
|
||||
) {
|
||||
if (!shouldShowRequestPermissionRationale) invokeFun()
|
||||
else MaterialAlertDialogBuilder(this)
|
||||
.setTitle(title)
|
||||
.setMessage(rationale)
|
||||
.setNeutralButton(R.string.cancel) { _, _ ->
|
||||
}
|
||||
.setPositiveButton(R.string.accept) { _, _ ->
|
||||
invokeFun()
|
||||
}
|
||||
.show()
|
||||
}
|
||||
|
||||
/**
|
||||
* return a list of the permissions we don't have
|
||||
*/
|
||||
|
|
@ -123,3 +176,17 @@ fun Context.getBackgroundPermissions(): Array<String> {
|
|||
|
||||
/** @return true if the user already has background location permission */
|
||||
fun Context.hasBackgroundPermission() = getBackgroundPermissions().isEmpty()
|
||||
|
||||
/**
|
||||
* Notification permission (or empty if we already have what we need)
|
||||
*/
|
||||
fun Context.getNotificationPermissions(): Array<String> {
|
||||
val perms = mutableListOf<String>()
|
||||
if (android.os.Build.VERSION.SDK_INT >= 33)
|
||||
perms.add(Manifest.permission.POST_NOTIFICATIONS)
|
||||
|
||||
return getMissingPermissions(perms)
|
||||
}
|
||||
|
||||
/** @return true if the user already has notification permission */
|
||||
fun Context.hasNotificationPermission() = getNotificationPermissions().isEmpty()
|
||||
|
|
|
|||
|
|
@ -662,11 +662,12 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
|
|||
val requestPermissionAndScanLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
|
||||
if (permissions.entries.all { it.value }) {
|
||||
info("Bluetooth permissions granted")
|
||||
checkBTEnabled()
|
||||
if (!hasCompanionDeviceApi) checkLocationEnabled()
|
||||
scanLeDevice()
|
||||
} else {
|
||||
errormsg("User denied scan permissions")
|
||||
warn("Bluetooth permissions denied")
|
||||
model.showSnackbar(requireContext().permissionMissing)
|
||||
}
|
||||
bluetoothViewModel.permissionsUpdated()
|
||||
|
|
@ -675,21 +676,16 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
|
|||
binding.changeRadioButton.setOnClickListener {
|
||||
debug("User clicked changeRadioButton")
|
||||
scanLeDevice()
|
||||
if (requireContext().hasBluetoothPermission()) {
|
||||
val bluetoothPermissions = requireContext().getBluetoothPermissions()
|
||||
if (bluetoothPermissions.isEmpty()) {
|
||||
checkBTEnabled()
|
||||
if (!hasCompanionDeviceApi) checkLocationEnabled()
|
||||
} else {
|
||||
MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(getString(R.string.required_permissions))
|
||||
.setMessage(requireContext().permissionMissing)
|
||||
.setNeutralButton(R.string.cancel) { _, _ ->
|
||||
warn("User bailed due to permissions")
|
||||
}
|
||||
.setPositiveButton(R.string.accept) { _, _ ->
|
||||
info("requesting scan permissions")
|
||||
requestPermissionAndScanLauncher.launch(requireContext().getBluetoothPermissions())
|
||||
}
|
||||
.show()
|
||||
requireContext().rationaleDialog(
|
||||
shouldShowRequestPermissionRationale(bluetoothPermissions)
|
||||
) {
|
||||
requestPermissionAndScanLauncher.launch(bluetoothPermissions)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue