Meshtastic-Android/app/src/main/java/com/geeksville/meshutil/MainActivity.kt

119 lines
4.2 KiB
Kotlin
Raw Normal View History

2020-01-20 15:53:22 -08:00
package com.geeksville.meshutil
2020-01-21 13:12:01 -08:00
import android.Manifest
2020-01-21 09:37:39 -08:00
import android.bluetooth.*
import android.content.Context
import android.content.Intent
2020-01-21 13:12:01 -08:00
import android.content.pm.PackageManager
2020-01-20 15:53:22 -08:00
import android.os.Bundle
2020-01-21 09:37:39 -08:00
import android.os.Handler
import android.util.Log
2020-01-20 15:53:22 -08:00
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
2020-01-22 13:02:24 -08:00
import android.widget.Toast
2020-01-21 13:12:01 -08:00
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
2020-01-22 14:27:22 -08:00
import com.geeksville.android.Logging
2020-01-20 15:53:22 -08:00
import kotlinx.android.synthetic.main.activity_main.*
2020-01-21 09:37:39 -08:00
import java.util.*
2020-01-20 15:53:22 -08:00
2020-01-22 14:27:22 -08:00
class MainActivity : AppCompatActivity(), Logging {
2020-01-20 15:53:22 -08:00
2020-01-21 09:37:39 -08:00
companion object {
const val REQUEST_ENABLE_BT = 10
2020-01-21 13:12:01 -08:00
const val DID_REQUEST_PERM = 11
2020-01-21 09:37:39 -08:00
}
2020-01-22 13:02:24 -08:00
private val bluetoothAdapter: BluetoothAdapter? by lazy(LazyThreadSafetyMode.NONE) {
2020-01-21 09:37:39 -08:00
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
2020-01-22 13:02:24 -08:00
bluetoothManager.adapter
2020-01-21 09:37:39 -08:00
}
2020-01-21 13:12:01 -08:00
fun requestPermission() {
2020-01-22 14:27:22 -08:00
debug("Checking permissions")
2020-01-21 13:12:01 -08:00
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
}
}
2020-01-20 15:53:22 -08:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
2020-01-22 13:02:24 -08:00
fab.setOnClickListener { _ ->
/* Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show() */
2020-01-22 14:27:22 -08:00
// test crash reporting
// logAssert(false)
2020-01-22 13:40:57 -08:00
// throw NotImplementedError("I like crap")
2020-01-22 14:27:22 -08:00
2020-01-22 13:02:24 -08:00
if(bluetoothAdapter != null) {
SoftwareUpdateService.enqueueWork(this, SoftwareUpdateService.scanDevicesIntent)
}
2020-01-21 09:37:39 -08:00
}
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
2020-01-22 13:02:24 -08:00
if(bluetoothAdapter != null) {
bluetoothAdapter!!.takeIf { !it.isEnabled }?.apply {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}
}
else {
Toast.makeText(this, "Error - this app requires bluetooth", Toast.LENGTH_LONG).show()
2020-01-20 15:53:22 -08:00
}
2020-01-21 12:07:03 -08:00
2020-01-21 13:12:01 -08:00
requestPermission()
2020-01-20 15:53:22 -08:00
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
2020-01-20 16:13:40 -08:00
return when (item.itemId) {
2020-01-20 15:53:22 -08:00
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
2020-01-21 09:37:39 -08:00