Compare commits
1 Commits
dev-server
...
2.1.119
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
95b3a072b4 |
@@ -11,12 +11,14 @@ import android.content.ServiceConnection
|
|||||||
import android.hardware.usb.UsbDevice
|
import android.hardware.usb.UsbDevice
|
||||||
import android.hardware.usb.UsbDeviceConnection
|
import android.hardware.usb.UsbDeviceConnection
|
||||||
import android.hardware.usb.UsbManager
|
import android.hardware.usb.UsbManager
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.view.get
|
import androidx.core.view.get
|
||||||
@@ -43,6 +45,7 @@ open class HemocubeActivity : AppCompatActivity() {
|
|||||||
private val TAG = "HemoCube"
|
private val TAG = "HemoCube"
|
||||||
|
|
||||||
private val broadcastReceiver = object : BroadcastReceiver() {
|
private val broadcastReceiver = object : BroadcastReceiver() {
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
@@ -81,6 +84,7 @@ open class HemocubeActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityHemocubeBinding.inflate(layoutInflater)
|
binding = ActivityHemocubeBinding.inflate(layoutInflater)
|
||||||
@@ -105,6 +109,7 @@ open class HemocubeActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
open fun connectUsb(permissionGranted: Boolean) {
|
open fun connectUsb(permissionGranted: Boolean) {
|
||||||
Log.d(TAG, "connectUsb() called, permission variable = $permissionGranted")
|
Log.d(TAG, "connectUsb() called, permission variable = $permissionGranted")
|
||||||
val manager = getSystemService(Context.USB_SERVICE) as UsbManager
|
val manager = getSystemService(Context.USB_SERVICE) as UsbManager
|
||||||
@@ -125,32 +130,40 @@ open class HemocubeActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressLint("MutableImplicitPendingIntent")
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
|
@SuppressLint("UnspecifiedImmutableFlag")
|
||||||
private fun requestUserPermission(manager: UsbManager, device: UsbDevice) {
|
private fun requestUserPermission(manager: UsbManager, device: UsbDevice) {
|
||||||
val mPendingIntent: PendingIntent
|
val mPendingIntent: PendingIntent
|
||||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
|
val pendingIntentFlags = when {
|
||||||
mPendingIntent = PendingIntent.getBroadcast(
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||||
this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE
|
PendingIntent.FLAG_IMMUTABLE
|
||||||
)
|
}
|
||||||
} else {
|
else -> {
|
||||||
|
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mPendingIntent = PendingIntent.getBroadcast(
|
mPendingIntent = PendingIntent.getBroadcast(
|
||||||
this,
|
this,
|
||||||
0,
|
0,
|
||||||
Intent(Constants.HEMOCUBE_USB_PERMISSION),
|
Intent(Constants.HEMOCUBE_USB_PERMISSION),
|
||||||
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
|
pendingIntentFlags
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION)
|
val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION)
|
||||||
registerReceiver(broadcastReceiver, filter)
|
registerReceiver(broadcastReceiver, filter, RECEIVER_EXPORTED)
|
||||||
|
|
||||||
|
// Request permission
|
||||||
manager.requestPermission(device, mPendingIntent)
|
manager.requestPermission(device, mPendingIntent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun setupService() {
|
fun setupService() {
|
||||||
val intent = Intent(this, UsbService::class.java)
|
val intent = Intent(this, UsbService::class.java)
|
||||||
bindService(intent, connection, Context.BIND_AUTO_CREATE)
|
bindService(intent, connection, Context.BIND_AUTO_CREATE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.O)
|
||||||
open fun reconnectDevice() {
|
open fun reconnectDevice() {
|
||||||
mService.disconnect()
|
mService.disconnect()
|
||||||
unbindService(connection)
|
unbindService(connection)
|
||||||
|
|||||||
Reference in New Issue
Block a user