63 lines
2.5 KiB
Kotlin
63 lines
2.5 KiB
Kotlin
package io.gitlab.jfronny.hc05ac.util
|
|
|
|
import android.app.Activity
|
|
import android.bluetooth.BluetoothAdapter
|
|
import android.bluetooth.BluetoothManager
|
|
import android.content.Context
|
|
import android.content.ContextWrapper
|
|
import android.content.Intent
|
|
import android.content.pm.PackageManager
|
|
import android.net.Uri
|
|
import android.os.Build
|
|
import android.provider.Settings
|
|
import android.util.Log
|
|
import android.widget.Toast
|
|
import androidx.core.app.ActivityCompat
|
|
import kotlin.math.max
|
|
import kotlin.math.min
|
|
|
|
private val TAG = "HC05AC"
|
|
|
|
fun Context.goToNotificationSettings() {
|
|
val intent = Intent()
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
|
intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
|
|
// intent.data = Uri.fromParts(SCHEME, context.packageName, null);
|
|
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
|
|
intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
|
|
intent.putExtra("app_package", packageName)
|
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
|
|
intent.putExtra("app_package", packageName)
|
|
intent.putExtra("app_uid", applicationInfo.uid)
|
|
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
|
|
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
|
intent.addCategory(Intent.CATEGORY_DEFAULT)
|
|
intent.data = Uri.parse("package:" + packageName)
|
|
} else {
|
|
return
|
|
}
|
|
startActivity(intent)
|
|
}
|
|
|
|
fun <K, V> MutableMap<K, V>.putIfMissing(k: K, m: (K) -> V): V {
|
|
if (!containsKey(k)) put(k, m(k))
|
|
return get(k)!!
|
|
}
|
|
|
|
fun Int.clamp(min: Int, max: Int) = min(max(this, min), max)
|
|
|
|
fun logI(message: String) = Log.i(TAG, message)
|
|
|
|
fun logE(message: String, t: Throwable) = Log.e(TAG, message, t)
|
|
|
|
fun Context.hasPermission(permission: String) = ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
|
|
|
|
fun Activity.requestPermissions(vararg permissions: String, @androidx.annotation.IntRange(from = 0) requestCode: Int = 0)
|
|
= ActivityCompat.requestPermissions(this, permissions, requestCode)
|
|
|
|
val Activity.btAdapter get() = if (Build.VERSION.SDK_INT >= 31) (getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter
|
|
else BluetoothAdapter.getDefaultAdapter()
|
|
|
|
fun ContextWrapper.toast(msg: String) = Toast.makeText(applicationContext, msg, Toast.LENGTH_LONG).show() |