|
|
@@ -1,14 +1,7 @@
|
|
|
package im.angry.openeuicc.ui
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
-import android.app.PendingIntent
|
|
|
-import android.content.BroadcastReceiver
|
|
|
-import android.content.Context
|
|
|
import android.content.Intent
|
|
|
-import android.content.IntentFilter
|
|
|
-import android.hardware.usb.UsbDevice
|
|
|
-import android.hardware.usb.UsbManager
|
|
|
-import android.os.Build
|
|
|
import android.os.Bundle
|
|
|
import android.telephony.TelephonyManager
|
|
|
import android.util.Log
|
|
|
@@ -19,9 +12,9 @@ import android.widget.AdapterView
|
|
|
import android.widget.ArrayAdapter
|
|
|
import android.widget.ProgressBar
|
|
|
import android.widget.Spinner
|
|
|
+import androidx.fragment.app.Fragment
|
|
|
import androidx.lifecycle.lifecycleScope
|
|
|
import im.angry.openeuicc.common.R
|
|
|
-import im.angry.openeuicc.core.EuiccChannel
|
|
|
import im.angry.openeuicc.util.*
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.launch
|
|
|
@@ -30,7 +23,6 @@ import kotlinx.coroutines.withContext
|
|
|
open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
|
|
|
companion object {
|
|
|
const val TAG = "MainActivity"
|
|
|
- const val ACTION_USB_PERMISSION = "im.angry.openeuicc.USB_PERMISSION"
|
|
|
}
|
|
|
|
|
|
private lateinit var spinnerAdapter: ArrayAdapter<String>
|
|
|
@@ -48,31 +40,10 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private val fragments = arrayListOf<EuiccManagementFragment>()
|
|
|
+ private val fragments = arrayListOf<Fragment>()
|
|
|
|
|
|
protected lateinit var tm: TelephonyManager
|
|
|
|
|
|
- private val usbManager: UsbManager by lazy {
|
|
|
- getSystemService(USB_SERVICE) as UsbManager
|
|
|
- }
|
|
|
-
|
|
|
- private var usbDevice: UsbDevice? = null
|
|
|
- private var usbChannel: EuiccChannel? = null
|
|
|
-
|
|
|
- private lateinit var usbPendingIntent: PendingIntent
|
|
|
-
|
|
|
- private val usbPermissionReceiver = object : BroadcastReceiver() {
|
|
|
- override fun onReceive(context: Context?, intent: Intent?) {
|
|
|
- if (intent?.action == ACTION_USB_PERMISSION) {
|
|
|
- if (usbDevice != null && usbManager.hasPermission(usbDevice)) {
|
|
|
- lifecycleScope.launch(Dispatchers.Main) {
|
|
|
- switchToUsbFragmentIfPossible()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
@SuppressLint("WrongConstant", "UnspecifiedRegisterReceiverFlag")
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
@@ -83,15 +54,6 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
|
|
|
tm = telephonyManager
|
|
|
|
|
|
spinnerAdapter = ArrayAdapter<String>(this, R.layout.spinner_item)
|
|
|
-
|
|
|
- usbPendingIntent = PendingIntent.getBroadcast(this, 0,
|
|
|
- Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE)
|
|
|
- val filter = IntentFilter(ACTION_USB_PERMISSION)
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
- registerReceiver(usbPermissionReceiver, filter, Context.RECEIVER_EXPORTED)
|
|
|
- } else {
|
|
|
- registerReceiver(usbPermissionReceiver, filter)
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
@@ -114,11 +76,6 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
|
|
|
if (position < fragments.size) {
|
|
|
supportFragmentManager.beginTransaction()
|
|
|
.replace(R.id.fragment_root, fragments[position]).commit()
|
|
|
- } else if (position == fragments.size) {
|
|
|
- // If we are at the last position, this is the USB device
|
|
|
- lifecycleScope.launch(Dispatchers.Main) {
|
|
|
- switchToUsbFragmentIfPossible()
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -164,10 +121,8 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- withContext(Dispatchers.IO) {
|
|
|
- val res = euiccChannelManager.enumerateUsbEuiccChannel()
|
|
|
- usbDevice = res.first
|
|
|
- usbChannel = res.second
|
|
|
+ val (usbDevice, _) = withContext(Dispatchers.IO) {
|
|
|
+ euiccChannelManager.enumerateUsbEuiccChannel()
|
|
|
}
|
|
|
|
|
|
withContext(Dispatchers.Main) {
|
|
|
@@ -179,16 +134,19 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
|
|
|
}
|
|
|
|
|
|
// If USB readers exist, add them at the very last
|
|
|
- // The adapter logic depends on this assumption
|
|
|
- usbDevice?.let { spinnerAdapter.add(it.productName) }
|
|
|
+ // We use a wrapper fragment to handle logic specific to USB readers
|
|
|
+ usbDevice?.let {
|
|
|
+ spinnerAdapter.add(it.productName)
|
|
|
+ fragments.add(UsbCcidReaderFragment())
|
|
|
+ }
|
|
|
|
|
|
if (fragments.isNotEmpty()) {
|
|
|
if (this@MainActivity::spinner.isInitialized) {
|
|
|
spinnerItem.isVisible = true
|
|
|
}
|
|
|
- supportFragmentManager.beginTransaction().replace(R.id.fragment_root, fragments.first()).commit()
|
|
|
+ supportFragmentManager.beginTransaction()
|
|
|
+ .replace(R.id.fragment_root, fragments.first()).commit()
|
|
|
} else {
|
|
|
- // TODO: Handle cases where there is _only_ a USB reader
|
|
|
supportFragmentManager.beginTransaction().replace(
|
|
|
R.id.fragment_root,
|
|
|
appContainer.uiComponentFactory.createNoEuiccPlaceholderFragment()
|
|
|
@@ -196,27 +154,4 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private suspend fun switchToUsbFragmentIfPossible() {
|
|
|
- if (usbDevice != null && usbChannel == null) {
|
|
|
- if (!usbManager.hasPermission(usbDevice)) {
|
|
|
- usbManager.requestPermission(usbDevice, usbPendingIntent)
|
|
|
- return
|
|
|
- } else {
|
|
|
- val (device, channel) = withContext(Dispatchers.IO) {
|
|
|
- euiccChannelManager.enumerateUsbEuiccChannel()
|
|
|
- }
|
|
|
-
|
|
|
- if (device != null && channel != null) {
|
|
|
- usbDevice = device
|
|
|
- usbChannel = channel
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (usbChannel != null) {
|
|
|
- supportFragmentManager.beginTransaction().replace(R.id.fragment_root,
|
|
|
- appContainer.uiComponentFactory.createEuiccManagementFragment(usbChannel!!)).commit()
|
|
|
- }
|
|
|
- }
|
|
|
}
|