refactor: [4/n] Stop using abstract classes

For the unprivileged case, the barebones base classes should be usable
directly.
This commit is contained in:
Peter Cai 2023-11-28 21:36:09 -05:00
parent af66bc440e
commit c4869acdea
8 changed files with 29 additions and 24 deletions

View file

@ -3,14 +3,16 @@ package im.angry.openeuicc
import android.app.Application import android.app.Application
import android.telephony.SubscriptionManager import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager import android.telephony.TelephonyManager
import im.angry.openeuicc.core.BaseEuiccChannelManager import im.angry.openeuicc.core.EuiccChannelManager
abstract class BaseOpenEuiccApplication : Application() { open class OpenEuiccApplication : Application() {
val telephonyManager by lazy { val telephonyManager by lazy {
getSystemService(TelephonyManager::class.java)!! getSystemService(TelephonyManager::class.java)!!
} }
abstract val euiccChannelManager: BaseEuiccChannelManager open val euiccChannelManager: EuiccChannelManager by lazy {
EuiccChannelManager(this)
}
val subscriptionManager by lazy { val subscriptionManager by lazy {
getSystemService(SubscriptionManager::class.java)!! getSystemService(SubscriptionManager::class.java)!!

View file

@ -6,7 +6,7 @@ import android.os.HandlerThread
import android.se.omapi.SEService import android.se.omapi.SEService
import android.telephony.UiccCardInfo import android.telephony.UiccCardInfo
import android.util.Log import android.util.Log
import im.angry.openeuicc.BaseOpenEuiccApplication import im.angry.openeuicc.OpenEuiccApplication
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
@ -16,9 +16,9 @@ import java.lang.IllegalArgumentException
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
abstract class BaseEuiccChannelManager(protected val context: Context) { open class EuiccChannelManager(protected val context: Context) {
companion object { companion object {
const val TAG = "BaseEuiccChannelManager" const val TAG = "EuiccChannelManager"
} }
private val channels = mutableListOf<EuiccChannel>() private val channels = mutableListOf<EuiccChannel>()
@ -28,7 +28,7 @@ abstract class BaseEuiccChannelManager(protected val context: Context) {
private val lock = Mutex() private val lock = Mutex()
protected val tm by lazy { protected val tm by lazy {
(context.applicationContext as BaseOpenEuiccApplication).telephonyManager (context.applicationContext as OpenEuiccApplication).telephonyManager
} }
private val handler = Handler(HandlerThread("BaseEuiccChannelManager").also { it.start() }.looper) private val handler = Handler(HandlerThread("BaseEuiccChannelManager").also { it.start() }.looper)
@ -48,7 +48,10 @@ abstract class BaseEuiccChannelManager(protected val context: Context) {
} }
} }
abstract fun tryOpenEuiccChannelPrivileged(uiccInfo: UiccCardInfo, channelInfo: EuiccChannelInfo): EuiccChannel? open fun tryOpenEuiccChannelPrivileged(uiccInfo: UiccCardInfo, channelInfo: EuiccChannelInfo): EuiccChannel? {
// No-op when unprivileged
return null
}
private suspend fun tryOpenEuiccChannel(uiccInfo: UiccCardInfo): EuiccChannel? { private suspend fun tryOpenEuiccChannel(uiccInfo: UiccCardInfo): EuiccChannel? {
lock.withLock { lock.withLock {

View file

@ -2,7 +2,7 @@ package im.angry.openeuicc.ui
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import im.angry.openeuicc.core.BaseEuiccChannelManager import im.angry.openeuicc.core.EuiccChannelManager
import im.angry.openeuicc.core.EuiccChannel import im.angry.openeuicc.core.EuiccChannel
import im.angry.openeuicc.util.openEuiccApplication import im.angry.openeuicc.util.openEuiccApplication
@ -19,7 +19,7 @@ fun <T> newInstanceEuicc(clazz: Class<T>, slotId: Int): T where T: Fragment, T:
val <T> T.slotId: Int where T: Fragment, T: EuiccFragmentMarker val <T> T.slotId: Int where T: Fragment, T: EuiccFragmentMarker
get() = requireArguments().getInt("slotId") get() = requireArguments().getInt("slotId")
val <T> T.euiccChannelManager: BaseEuiccChannelManager where T: Fragment, T: EuiccFragmentMarker val <T> T.euiccChannelManager: EuiccChannelManager where T: Fragment, T: EuiccFragmentMarker
get() = openEuiccApplication.euiccChannelManager get() = openEuiccApplication.euiccChannelManager
val <T> T.channel: EuiccChannel where T: Fragment, T: EuiccFragmentMarker val <T> T.channel: EuiccChannel where T: Fragment, T: EuiccFragmentMarker

View file

@ -11,7 +11,7 @@ import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import im.angry.openeuicc.common.R import im.angry.openeuicc.common.R
import im.angry.openeuicc.core.BaseEuiccChannelManager import im.angry.openeuicc.core.EuiccChannelManager
import im.angry.openeuicc.util.* import im.angry.openeuicc.util.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -22,7 +22,7 @@ open class MainActivity : AppCompatActivity() {
const val TAG = "MainActivity" const val TAG = "MainActivity"
} }
protected lateinit var manager: BaseEuiccChannelManager protected lateinit var manager: EuiccChannelManager
private lateinit var spinnerAdapter: ArrayAdapter<String> private lateinit var spinnerAdapter: ArrayAdapter<String>
private lateinit var spinner: Spinner private lateinit var spinner: Spinner

View file

@ -6,12 +6,12 @@ import android.graphics.Rect
import android.view.ViewGroup import android.view.ViewGroup
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import im.angry.openeuicc.BaseOpenEuiccApplication import im.angry.openeuicc.OpenEuiccApplication
val Activity.openEuiccApplication: BaseOpenEuiccApplication val Activity.openEuiccApplication: OpenEuiccApplication
get() = application as BaseOpenEuiccApplication get() = application as OpenEuiccApplication
val Fragment.openEuiccApplication: BaseOpenEuiccApplication val Fragment.openEuiccApplication: OpenEuiccApplication
get() = requireActivity().openEuiccApplication get() = requireActivity().openEuiccApplication
// Source: <https://stackoverflow.com/questions/12478520/how-to-set-dialogfragments-width-and-height> // Source: <https://stackoverflow.com/questions/12478520/how-to-set-dialogfragments-width-and-height>

View file

@ -1,10 +1,10 @@
package im.angry.openeuicc package im.angry.openeuicc
import im.angry.openeuicc.core.BaseEuiccChannelManager import im.angry.openeuicc.core.EuiccChannelManager
import im.angry.openeuicc.core.PrivilegedEuiccChannelManager import im.angry.openeuicc.core.PrivilegedEuiccChannelManager
class PrivilegedOpenEuiccApplication: BaseOpenEuiccApplication() { class PrivilegedOpenEuiccApplication: OpenEuiccApplication() {
override val euiccChannelManager: BaseEuiccChannelManager by lazy { override val euiccChannelManager: EuiccChannelManager by lazy {
PrivilegedEuiccChannelManager(this) PrivilegedEuiccChannelManager(this)
} }

View file

@ -3,12 +3,12 @@ package im.angry.openeuicc.core
import android.content.Context import android.content.Context
import android.telephony.UiccCardInfo import android.telephony.UiccCardInfo
import android.util.Log import android.util.Log
import im.angry.openeuicc.BaseOpenEuiccApplication import im.angry.openeuicc.OpenEuiccApplication
import im.angry.openeuicc.util.* import im.angry.openeuicc.util.*
import java.lang.Exception import java.lang.Exception
import java.lang.IllegalArgumentException import java.lang.IllegalArgumentException
class PrivilegedEuiccChannelManager(context: Context): BaseEuiccChannelManager(context) { class PrivilegedEuiccChannelManager(context: Context): EuiccChannelManager(context) {
override fun tryOpenEuiccChannelPrivileged(uiccInfo: UiccCardInfo, channelInfo: EuiccChannelInfo): EuiccChannel? { override fun tryOpenEuiccChannelPrivileged(uiccInfo: UiccCardInfo, channelInfo: EuiccChannelInfo): EuiccChannel? {
if (uiccInfo.isEuicc && !uiccInfo.isRemovable) { if (uiccInfo.isEuicc && !uiccInfo.isRemovable) {
Log.d(TAG, "Using TelephonyManager for slot ${uiccInfo.slotIndex}") Log.d(TAG, "Using TelephonyManager for slot ${uiccInfo.slotIndex}")
@ -38,7 +38,7 @@ class PrivilegedEuiccChannelManager(context: Context): BaseEuiccChannelManager(c
} }
override fun notifyEuiccProfilesChanged(slotId: Int) { override fun notifyEuiccProfilesChanged(slotId: Int) {
(context.applicationContext as BaseOpenEuiccApplication).subscriptionManager.apply { (context.applicationContext as OpenEuiccApplication).subscriptionManager.apply {
findEuiccChannelBySlotBlocking(slotId)?.let { findEuiccChannelBySlotBlocking(slotId)?.let {
tryRefreshCachedEuiccInfo(it.cardId) tryRefreshCachedEuiccInfo(it.cardId)
} }

View file

@ -4,13 +4,13 @@ import android.service.euicc.*
import android.telephony.euicc.DownloadableSubscription import android.telephony.euicc.DownloadableSubscription
import android.telephony.euicc.EuiccInfo import android.telephony.euicc.EuiccInfo
import net.typeblog.lpac_jni.LocalProfileInfo import net.typeblog.lpac_jni.LocalProfileInfo
import im.angry.openeuicc.BaseOpenEuiccApplication import im.angry.openeuicc.OpenEuiccApplication
import im.angry.openeuicc.core.EuiccChannel import im.angry.openeuicc.core.EuiccChannel
import im.angry.openeuicc.util.* import im.angry.openeuicc.util.*
class OpenEuiccService : EuiccService() { class OpenEuiccService : EuiccService() {
private val openEuiccApplication private val openEuiccApplication
get() = application as BaseOpenEuiccApplication get() = application as OpenEuiccApplication
private fun findChannel(slotId: Int): EuiccChannel? = private fun findChannel(slotId: Int): EuiccChannel? =
openEuiccApplication.euiccChannelManager openEuiccApplication.euiccChannelManager