HC05AC-CTRL/src/main/java/io/gitlab/jfronny/hc05ac/util.kt

64 lines
2.4 KiB
Kotlin

package io.gitlab.jfronny.hc05ac
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
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.view.MotionEvent
import androidx.core.app.ActivityCompat
import kotlin.math.max
import kotlin.math.min
private val TAG = "HC05AC"
val MotionEvent.jAction get() = when (action) {
MotionEvent.ACTION_UP -> Action.UP
MotionEvent.ACTION_DOWN -> Action.DOWN
MotionEvent.ACTION_MOVE -> Action.MOVE
else -> Action.OTHER
}
enum class Action {
UP, DOWN, MOVE, OTHER;
}
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 Float.clamp(min: Float, max: Float) = min(max(this, min), max)
fun logI(message: String) = Log.i(TAG, message)
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()