浏览代码

Extract some common logic as extension functions to LocalProfileInfo

Peter Cai 3 年之前
父节点
当前提交
557372766d

+ 2 - 4
app/src/main/java/im/angry/openeuicc/service/OpenEuiccService.kt

@@ -53,12 +53,10 @@ class OpenEuiccService : EuiccService() {
 
 
     override fun onGetEuiccProfileInfoList(slotId: Int): GetEuiccProfileInfoListResult? {
     override fun onGetEuiccProfileInfoList(slotId: Int): GetEuiccProfileInfoListResult? {
         val channel = findChannel(slotId) ?: return null
         val channel = findChannel(slotId) ?: return null
-        val profiles = channel.lpa.profiles.filter {
-            it.profileClass == LocalProfileInfo.Clazz.Operational
-        }.map {
+        val profiles = channel.lpa.profiles.operational.map {
             EuiccProfileInfo.Builder(it.iccidLittleEndian).apply {
             EuiccProfileInfo.Builder(it.iccidLittleEndian).apply {
                 setProfileName(it.name)
                 setProfileName(it.name)
-                setNickname(it.nickName)
+                setNickname(it.displayName)
                 setServiceProviderName(it.providerName)
                 setServiceProviderName(it.providerName)
                 setState(
                 setState(
                     when (it.state) {
                     when (it.state) {

+ 5 - 11
app/src/main/java/im/angry/openeuicc/ui/EuiccManagementFragment.kt

@@ -19,8 +19,7 @@ import com.truphone.lpad.progress.Progress
 import im.angry.openeuicc.R
 import im.angry.openeuicc.R
 import im.angry.openeuicc.databinding.EuiccProfileBinding
 import im.angry.openeuicc.databinding.EuiccProfileBinding
 import im.angry.openeuicc.databinding.FragmentEuiccBinding
 import im.angry.openeuicc.databinding.FragmentEuiccBinding
-import im.angry.openeuicc.util.openEuiccApplication
-import im.angry.openeuicc.util.tryRefreshCachedEuiccInfo
+import im.angry.openeuicc.util.*
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import kotlinx.coroutines.withContext
@@ -81,7 +80,7 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
             }
             }
 
 
             withContext(Dispatchers.Main) {
             withContext(Dispatchers.Main) {
-                adapter.profiles = profiles.filter { it.profileClass == LocalProfileInfo.Clazz.Operational }
+                adapter.profiles = profiles.operational
                 adapter.notifyDataSetChanged()
                 adapter.notifyDataSetChanged()
                 binding.swipeRefresh.isRefreshing = false
                 binding.swipeRefresh.isRefreshing = false
             }
             }
@@ -141,7 +140,7 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
 
 
         fun setProfile(profile: LocalProfileInfo) {
         fun setProfile(profile: LocalProfileInfo) {
             this.profile = profile
             this.profile = profile
-            binding.name.text = getName()
+            binding.name.text = profile.displayName
 
 
             binding.state.setText(
             binding.state.setText(
                 if (isEnabled()) {
                 if (isEnabled()) {
@@ -158,11 +157,6 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
         private fun isEnabled(): Boolean =
         private fun isEnabled(): Boolean =
             profile.state == LocalProfileInfo.State.Enabled
             profile.state == LocalProfileInfo.State.Enabled
 
 
-        private fun getName(): String =
-            profile.nickName.ifEmpty {
-                profile.name
-            }
-
         private fun showOptionsMenu() {
         private fun showOptionsMenu() {
             PopupMenu(binding.root.context, binding.profileMenu).apply {
             PopupMenu(binding.root.context, binding.profileMenu).apply {
                 setOnMenuItemClickListener(::onMenuItemClicked)
                 setOnMenuItemClickListener(::onMenuItemClicked)
@@ -188,12 +182,12 @@ class EuiccManagementFragment : Fragment(), EuiccFragmentMarker, EuiccProfilesCh
                     true
                     true
                 }
                 }
                 R.id.rename -> {
                 R.id.rename -> {
-                    ProfileRenameFragment.newInstance(slotId, profile.iccid, getName())
+                    ProfileRenameFragment.newInstance(slotId, profile.iccid, profile.displayName)
                         .show(childFragmentManager, ProfileRenameFragment.TAG)
                         .show(childFragmentManager, ProfileRenameFragment.TAG)
                     true
                     true
                 }
                 }
                 R.id.delete -> {
                 R.id.delete -> {
-                    ProfileDeleteFragment.newInstance(slotId, profile.iccid, getName())
+                    ProfileDeleteFragment.newInstance(slotId, profile.iccid, profile.displayName)
                         .show(childFragmentManager, ProfileDeleteFragment.TAG)
                         .show(childFragmentManager, ProfileDeleteFragment.TAG)
                     true
                     true
                 }
                 }

+ 10 - 1
app/src/main/java/im/angry/openeuicc/util/TelephonyUtils.kt

@@ -2,6 +2,7 @@ package im.angry.openeuicc.util
 
 
 import android.telephony.SubscriptionManager
 import android.telephony.SubscriptionManager
 import android.telephony.TelephonyManager
 import android.telephony.TelephonyManager
+import com.truphone.lpa.LocalProfileInfo
 import java.lang.Exception
 import java.lang.Exception
 
 
 val TelephonyManager.supportsDSDS: Boolean
 val TelephonyManager.supportsDSDS: Boolean
@@ -21,4 +22,12 @@ fun SubscriptionManager.tryRefreshCachedEuiccInfo(cardId: Int) {
             // Ignore
             // Ignore
         }
         }
     }
     }
-}
+}
+
+val LocalProfileInfo.displayName: String
+    get() = nickName.ifEmpty { name }
+
+val List<LocalProfileInfo>.operational: List<LocalProfileInfo>
+    get() = filter {
+        it.profileClass == LocalProfileInfo.Clazz.Operational
+    }