we now request perms

This commit is contained in:
geeksville 2020-01-21 13:12:01 -08:00
parent 1c942a10b3
commit f48ce97d5e
5 changed files with 148 additions and 85 deletions

View file

@ -1,8 +1,10 @@
package com.geeksville.meshutil
import android.Manifest
import android.bluetooth.*
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import android.util.Log
@ -10,6 +12,8 @@ import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
@ -19,6 +23,7 @@ class MainActivity : AppCompatActivity() {
companion object {
const val REQUEST_ENABLE_BT = 10
const val DID_REQUEST_PERM = 11
}
private val bluetoothAdapter: BluetoothAdapter by lazy(LazyThreadSafetyMode.NONE) {
@ -26,6 +31,38 @@ class MainActivity : AppCompatActivity() {
bluetoothManager.adapter!!
}
fun requestPermission() {
val perms = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.WAKE_LOCK)
val missingPerms = perms.filter { ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED }
if (missingPerms.isNotEmpty()) {
missingPerms.forEach {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, it)) {
// FIXME
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
}
}
// Ask for all the missing perms
ActivityCompat.requestPermissions(this, missingPerms.toTypedArray(), DID_REQUEST_PERM)
// DID_REQUEST_PERM is an
// app-defined int constant. The callback method gets the
// result of the request.
} else {
// Permission has already been granted
SoftwareUpdateService.enqueueWork(this, SoftwareUpdateService.scanDevicesIntent)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@ -43,7 +80,7 @@ class MainActivity : AppCompatActivity() {
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
SoftwareUpdateService.enqueueWork(this, SoftwareUpdateService.scanDevicesIntent)
requestPermission()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {