2020-09-17 19:49:50 +02:00
|
|
|
package com.geeksville.mesh.android
|
|
|
|
|
|
2021-06-10 10:58:45 -07:00
|
|
|
import android.Manifest
|
2020-10-01 22:20:19 +02:00
|
|
|
import android.app.NotificationManager
|
2020-09-17 19:49:50 +02:00
|
|
|
import android.bluetooth.BluetoothManager
|
|
|
|
|
import android.content.Context
|
2021-06-10 10:58:45 -07:00
|
|
|
import android.content.pm.PackageManager
|
2020-09-17 19:49:50 +02:00
|
|
|
import android.hardware.usb.UsbManager
|
2021-06-10 10:58:45 -07:00
|
|
|
import android.os.Build
|
|
|
|
|
import androidx.core.content.ContextCompat
|
2020-09-17 19:49:50 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return null on platforms without a BlueTooth driver (i.e. the emulator)
|
|
|
|
|
*/
|
|
|
|
|
val Context.bluetoothManager: BluetoothManager? get() = getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager?
|
|
|
|
|
|
2020-09-17 19:55:36 +02:00
|
|
|
val Context.usbManager: UsbManager get() = requireNotNull(getSystemService(Context.USB_SERVICE) as? UsbManager?) { "USB_SERVICE is not available"}
|
2020-10-01 22:20:19 +02:00
|
|
|
|
|
|
|
|
val Context.notificationManager: NotificationManager get() = requireNotNull(getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager?)
|
2021-06-10 10:58:45 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* return a list of the permissions we don't have
|
|
|
|
|
*/
|
|
|
|
|
fun Context.getMissingPermissions(perms: List<String>) = perms.filter {
|
|
|
|
|
ContextCompat.checkSelfPermission(
|
|
|
|
|
this,
|
|
|
|
|
it
|
|
|
|
|
) != PackageManager.PERMISSION_GRANTED
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A list of missing background location permissions (or empty if we already have what we need)
|
|
|
|
|
*/
|
|
|
|
|
fun Context.getBackgroundPermissions(): List<String> {
|
|
|
|
|
val perms = mutableListOf<String>()
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 29) // only added later
|
|
|
|
|
perms.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
|
|
|
|
|
|
|
|
|
|
return getMissingPermissions(perms)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return true if the user already has background location permission */
|
|
|
|
|
fun Context.hasBackgroundPermission() = getBackgroundPermissions().isEmpty()
|