Meshtastic-Android/app/src/main/java/com/geeksville/mesh/android/ContextServices.kt

139 lines
4.6 KiB
Kotlin
Raw Normal View History

package com.geeksville.mesh.android
import android.Manifest
2022-04-28 23:09:06 -03:00
import android.annotation.SuppressLint
import android.app.NotificationManager
import android.bluetooth.BluetoothManager
2022-04-28 23:09:06 -03:00
import android.companion.CompanionDeviceManager
import android.content.Context
import android.content.pm.PackageManager
import android.hardware.usb.UsbManager
import android.os.Build
import androidx.core.content.ContextCompat
2022-05-03 12:16:44 -03:00
import com.geeksville.android.GeeksvilleApplication
import com.geeksville.mesh.MainActivity
/**
* @return null on platforms without a BlueTooth driver (i.e. the emulator)
*/
val Context.bluetoothManager: BluetoothManager? get() = getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager?
2022-04-28 23:09:06 -03:00
val Context.deviceManager: CompanionDeviceManager?
@SuppressLint("InlinedApi")
2022-05-03 12:16:44 -03:00
get() {
val activity: MainActivity? = GeeksvilleApplication.currentActivity as MainActivity?
return if (hasCompanionDeviceApi()) activity?.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager?
else null
}
2022-04-28 23:09:06 -03:00
val Context.usbManager: UsbManager get() = requireNotNull(getSystemService(Context.USB_SERVICE) as? UsbManager?) { "USB_SERVICE is not available"}
val Context.notificationManager: NotificationManager get() = requireNotNull(getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager?)
/**
* @return true if CompanionDeviceManager API is present
*/
fun Context.hasCompanionDeviceApi(): Boolean =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
packageManager.hasSystemFeature(PackageManager.FEATURE_COMPANION_DEVICE_SETUP)
else false
2022-05-31 18:31:14 -03:00
/**
* @return true if the device has a GPS receiver
*/
fun Context.hasGps(): Boolean =
packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)
/**
* 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
}
/**
* Bluetooth connect permissions (or empty if we already have what we need)
*/
fun Context.getConnectPermissions(): List<String> {
val perms = mutableListOf<String>()
/* TODO - wait for targetSdkVersion 31
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
perms.add(Manifest.permission.BLUETOOTH_CONNECT)
} else {
perms.add(Manifest.permission.BLUETOOTH)
}
*/
return getMissingPermissions(perms)
}
/** @return true if the user already has Bluetooth connect permission */
fun Context.hasConnectPermission() = getConnectPermissions().isEmpty()
2022-01-25 18:14:10 -03:00
/**
* Bluetooth scan/discovery permissions (or empty if we already have what we need)
*/
fun Context.getScanPermissions(): List<String> {
val perms = mutableListOf<String>()
/* TODO - wait for targetSdkVersion 31
2022-01-25 18:14:10 -03:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
perms.add(Manifest.permission.BLUETOOTH_SCAN)
} else if (!BluetoothInterface.hasCompanionDeviceApi(this)) {
perms.add(Manifest.permission.ACCESS_FINE_LOCATION)
perms.add(Manifest.permission.BLUETOOTH_ADMIN)
}
*/
if (!hasCompanionDeviceApi()) {
perms.add(Manifest.permission.ACCESS_FINE_LOCATION)
perms.add(Manifest.permission.BLUETOOTH_ADMIN)
}
2022-01-25 18:14:10 -03:00
return getMissingPermissions(perms)
}
/** @return true if the user already has Bluetooth scan/discovery permission */
fun Context.hasScanPermission() = getScanPermissions().isEmpty()
2021-11-19 01:20:54 -03:00
/**
* Camera permission (or empty if we already have what we need)
*/
fun Context.getCameraPermissions(): List<String> {
val perms = mutableListOf(Manifest.permission.CAMERA)
return getMissingPermissions(perms)
}
/** @return true if the user already has camera permission */
fun Context.hasCameraPermission() = getCameraPermissions().isEmpty()
2021-12-15 09:04:44 -03:00
/**
2022-01-25 15:59:45 -03:00
* Location permission (or empty if we already have what we need)
2021-12-15 09:04:44 -03:00
*/
fun Context.getLocationPermissions(): List<String> {
val perms = mutableListOf(Manifest.permission.ACCESS_FINE_LOCATION)
return getMissingPermissions(perms)
}
2022-01-25 18:14:10 -03:00
/** @return true if the user already has location permission */
2021-12-15 09:04:44 -03:00
fun Context.hasLocationPermission() = getLocationPermissions().isEmpty()
/**
* 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 */
2021-11-19 01:20:54 -03:00
fun Context.hasBackgroundPermission() = getBackgroundPermissions().isEmpty()