diff --git a/.forgejo/workflows/build-debug.yml b/.forgejo/workflows/build-debug.yml index 660dabc..0818b8b 100644 --- a/.forgejo/workflows/build-debug.yml +++ b/.forgejo/workflows/build-debug.yml @@ -33,14 +33,23 @@ jobs: uses: https://gitea.angry.im/actions/setup-android@v3 - name: Build Debug APKs - run: ./gradlew --no-daemon assembleDebug + run: ./gradlew --no-daemon assembleDebug :app:assembleDebugMagiskModuleDir - name: Copy Artifacts - run: find . -name 'app*-debug.apk' -exec cp {} . \; + run: | + find . -name 'app*-debug.apk' -exec cp {} . \; + cp -r app/build/magisk/debug ./magisk-debug - - name: Upload Artifacts + - name: Upload APK Artifacts uses: https://gitea.angry.im/actions/upload-artifact@v3 with: name: Debug APKs compression-level: 0 path: app*-debug.apk + + - name: Upload Magisk Artifacts + uses: https://gitea.angry.im/actions/upload-artifact@v3 + with: + name: magisk-debug + compression-level: 0 + path: magisk-debug diff --git a/.idea/.gitignore b/.idea/.gitignore index b7c2402..2e12995 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -2,7 +2,7 @@ /caches /libraries /assetWizardSettings.xml -/deploymentTargetDropDown.xml +/deploymentTarget*.xml /gradle.xml /misc.xml /modules.xml diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml deleted file mode 100644 index e40be60..0000000 --- a/.idea/deploymentTargetSelector.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelFactory.kt b/app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelFactory.kt index 87a0eea..78a8c3f 100644 --- a/app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelFactory.kt +++ b/app-common/src/main/java/im/angry/openeuicc/core/DefaultEuiccChannelFactory.kt @@ -21,7 +21,7 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha override suspend fun tryOpenEuiccChannel( port: UiccPortInfoCompat, isdrAid: ByteArray - ): EuiccChannel? { + ): EuiccChannel? = try { if (port.portIndex != 0) { Log.w( DefaultEuiccChannelManager.TAG, @@ -35,58 +35,52 @@ open class DefaultEuiccChannelFactory(protected val context: Context) : EuiccCha DefaultEuiccChannelManager.TAG, "Trying OMAPI for physical slot ${port.card.physicalSlotIndex}" ) - try { - return EuiccChannelImpl( - context.getString(R.string.channel_type_omapi), + EuiccChannelImpl( + context.getString(R.string.channel_type_omapi), + port, + intrinsicChannelName = null, + OmapiApduInterface( + seService!!, port, - intrinsicChannelName = null, - OmapiApduInterface( - seService!!, - port, - context.preferenceRepository.verboseLoggingFlow - ), - isdrAid, - context.preferenceRepository.verboseLoggingFlow, - context.preferenceRepository.ignoreTLSCertificateFlow, - ).also { - Log.i(DefaultEuiccChannelManager.TAG, "Is OMAPI channel, setting MSS to 60") - it.lpa.setEs10xMss(60) - } - } catch (_: IllegalArgumentException) { - // Failed - Log.w( - DefaultEuiccChannelManager.TAG, - "OMAPI APDU interface unavailable for physical slot ${port.card.physicalSlotIndex} with ISD-R AID: ${isdrAid.encodeHex()}." - ) - } - - return null + context.preferenceRepository.verboseLoggingFlow + ), + isdrAid, + context.preferenceRepository.verboseLoggingFlow, + context.preferenceRepository.ignoreTLSCertificateFlow, + context.preferenceRepository.es10xMssFlow, + ) + } catch (_: IllegalArgumentException) { + // Failed + Log.w( + DefaultEuiccChannelManager.TAG, + "OMAPI APDU interface unavailable for physical slot ${port.card.physicalSlotIndex} with ISD-R AID: ${isdrAid.encodeHex()}." + ) + null } override fun tryOpenUsbEuiccChannel( ccidCtx: UsbCcidContext, isdrAid: ByteArray - ): EuiccChannel? { - try { - return EuiccChannelImpl( - context.getString(R.string.channel_type_usb), - FakeUiccPortInfoCompat(FakeUiccCardInfoCompat(EuiccChannelManager.USB_CHANNEL_ID)), - intrinsicChannelName = ccidCtx.productName, - UsbApduInterface( - ccidCtx - ), - isdrAid, - context.preferenceRepository.verboseLoggingFlow, - context.preferenceRepository.ignoreTLSCertificateFlow, - ) - } catch (_: IllegalArgumentException) { - // Failed - Log.w( - DefaultEuiccChannelManager.TAG, - "USB APDU interface unavailable for ISD-R AID: ${isdrAid.encodeHex()}." - ) - } - return null + ): EuiccChannel? = try { + EuiccChannelImpl( + context.getString(R.string.channel_type_usb), + FakeUiccPortInfoCompat(FakeUiccCardInfoCompat(EuiccChannelManager.USB_CHANNEL_ID)), + intrinsicChannelName = ccidCtx.productName, + UsbApduInterface( + ccidCtx + ), + isdrAid, + context.preferenceRepository.verboseLoggingFlow, + context.preferenceRepository.ignoreTLSCertificateFlow, + context.preferenceRepository.es10xMssFlow, + ) + } catch (_: IllegalArgumentException) { + // Failed + Log.w( + DefaultEuiccChannelManager.TAG, + "USB APDU interface unavailable for ISD-R AID: ${isdrAid.encodeHex()}." + ) + null } override fun cleanup() { diff --git a/app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelImpl.kt b/app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelImpl.kt index 2a33c20..eaec522 100644 --- a/app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelImpl.kt +++ b/app-common/src/main/java/im/angry/openeuicc/core/EuiccChannelImpl.kt @@ -1,8 +1,9 @@ package im.angry.openeuicc.core import im.angry.openeuicc.util.UiccPortInfoCompat -import im.angry.openeuicc.util.decodeHex import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking import net.typeblog.lpac_jni.ApduInterface import net.typeblog.lpac_jni.LocalProfileAssistant import net.typeblog.lpac_jni.impl.HttpInterfaceImpl @@ -15,7 +16,8 @@ class EuiccChannelImpl( override val apduInterface: ApduInterface, override val isdrAid: ByteArray, verboseLoggingFlow: Flow, - ignoreTLSCertificateFlow: Flow + ignoreTLSCertificateFlow: Flow, + es10xMssFlow: Flow, ) : EuiccChannel { override val slotId = port.card.physicalSlotIndex override val logicalSlotId = port.logicalSlotIndex @@ -25,8 +27,10 @@ class EuiccChannelImpl( LocalProfileAssistantImpl( isdrAid, apduInterface, - HttpInterfaceImpl(verboseLoggingFlow, ignoreTLSCertificateFlow) - ) + HttpInterfaceImpl(verboseLoggingFlow, ignoreTLSCertificateFlow), + ).also { + it.setEs10xMss(runBlocking { es10xMssFlow.first().toByte() }) + } override val atr: ByteArray? get() = (apduInterface as? ApduInterfaceAtrProvider)?.atr diff --git a/app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt b/app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt index 6554142..7a717ac 100644 --- a/app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt +++ b/app-common/src/main/java/im/angry/openeuicc/ui/SettingsFragment.kt @@ -8,6 +8,7 @@ import android.provider.Settings import android.widget.Toast import androidx.lifecycle.lifecycleScope import androidx.preference.CheckBoxPreference +import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceCategory import androidx.preference.PreferenceFragmentCompat @@ -16,7 +17,6 @@ import im.angry.openeuicc.util.* import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch -import kotlinx.coroutines.runBlocking open class SettingsFragment: PreferenceFragmentCompat() { private lateinit var developerPref: PreferenceCategory @@ -34,7 +34,7 @@ open class SettingsFragment: PreferenceFragmentCompat() { // Show / hide developer preference based on whether it is enabled lifecycleScope.launch { preferenceRepository.developerOptionsEnabledFlow - .onEach { developerPref.isVisible = it } + .onEach(developerPref::setVisible) .collect() } @@ -84,6 +84,9 @@ open class SettingsFragment: PreferenceFragmentCompat() { requirePreference("pref_developer_euicc_memory_reset") .bindBooleanFlow(preferenceRepository.euiccMemoryResetFlow) + requirePreference("pref_developer_es10x_mss") + .bindIntFlow(preferenceRepository.es10xMssFlow, 63) + requirePreference("pref_developer_isdr_aid_list").apply { intent = Intent(requireContext(), IsdrAidListActivity::class.java) } @@ -100,51 +103,53 @@ open class SettingsFragment: PreferenceFragmentCompat() { @Suppress("UNUSED_PARAMETER") private fun onAppVersionClicked(pref: Preference): Boolean { if (developerPref.isVisible) return false + val now = System.currentTimeMillis() - if (now - lastClickTimestamp >= 1000) { - numClicks = 1 - } else { - numClicks++ - } + numClicks = if (now - lastClickTimestamp >= 1000) 1 else numClicks + 1 lastClickTimestamp = now - if (numClicks == 7) { - lifecycleScope.launch { - preferenceRepository.developerOptionsEnabledFlow.updatePreference(true) - - lastToast?.cancel() - Toast.makeText( - requireContext(), - R.string.developer_options_enabled, - Toast.LENGTH_SHORT - ).show() - } - } else if (numClicks > 1) { - lastToast?.cancel() - lastToast = Toast.makeText( - requireContext(), - getString(R.string.developer_options_steps, 7 - numClicks), - Toast.LENGTH_SHORT - ) - lastToast!!.show() + lifecycleScope.launch { + preferenceRepository.developerOptionsEnabledFlow.updatePreference(numClicks >= 7) } + val toastText = when { + numClicks == 7 -> getString(R.string.developer_options_enabled) + numClicks > 1 -> getString(R.string.developer_options_steps, 7 - numClicks) + else -> return true + } + + lastToast?.cancel() + lastToast = Toast.makeText(requireContext(), toastText, Toast.LENGTH_SHORT) + lastToast!!.show() return true } protected fun CheckBoxPreference.bindBooleanFlow(flow: PreferenceFlowWrapper) { lifecycleScope.launch { - flow.collect { isChecked = it } + flow.collect(::setChecked) } setOnPreferenceChangeListener { _, newValue -> - runBlocking { + lifecycleScope.launch { flow.updatePreference(newValue as Boolean) } true } } + private fun ListPreference.bindIntFlow(flow: PreferenceFlowWrapper, defaultValue: Int) { + lifecycleScope.launch { + flow.collect { value = it.toString() } + } + + setOnPreferenceChangeListener { _, newValue -> + lifecycleScope.launch { + flow.updatePreference((newValue as String).toIntOrNull() ?: defaultValue) + } + true + } + } + protected fun mergePreferenceOverlay(overlayKey: String, targetKey: String) { val overlayCat = requirePreference(overlayKey) val targetCat = requirePreference(targetKey) diff --git a/app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt b/app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt index 464aeee..2fef3db 100644 --- a/app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt +++ b/app-common/src/main/java/im/angry/openeuicc/util/PreferenceUtils.kt @@ -5,6 +5,7 @@ import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.booleanPreferencesKey import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.intPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.preferencesDataStore import androidx.fragment.app.Fragment @@ -38,6 +39,7 @@ internal object PreferenceKeys { val IGNORE_TLS_CERTIFICATE = booleanPreferencesKey("ignore_tls_certificate") val EUICC_MEMORY_RESET = booleanPreferencesKey("euicc_memory_reset") val ISDR_AID_LIST = stringPreferencesKey("isdr_aid_list") + val ES10X_MSS = intPreferencesKey("es10x_mss") } const val EUICC_DEFAULT_ISDR_AID = "A0000005591010FFFFFFFF8900000100" @@ -89,6 +91,7 @@ open class PreferenceRepository(private val context: Context) { PreferenceConstants.DEFAULT_AID_LIST, { Base64.getEncoder().encodeToString(it.encodeToByteArray()) }, { Base64.getDecoder().decode(it).decodeToString() }) + val es10xMssFlow = bindFlow(PreferenceKeys.ES10X_MSS, 63) protected fun bindFlow( key: Preferences.Key, diff --git a/app-common/src/main/res/values/strings.xml b/app-common/src/main/res/values/strings.xml index e09da9f..ae0700b 100644 --- a/app-common/src/main/res/values/strings.xml +++ b/app-common/src/main/res/values/strings.xml @@ -200,6 +200,16 @@ Accept any TLS certificate used by the RSP server Allow erasing eUICC This is a dangerous operation and hidden by default. As an alternative, you can delete all profiles manually. + ES10x MSS + Global ES10x MSS + + High Speed + Compatibility Mode + + + 255 + 63 + Customize ISD-R AID list Some brands of removable eUICCs may use their own non-standard ISD-R AID, rendering them inaccessible to third-party apps. We can attempt to use non-standard AIDs added in this list, but there is no guarantee that they will work. Info diff --git a/app-common/src/main/res/xml/pref_settings.xml b/app-common/src/main/res/xml/pref_settings.xml index 17505e1..831b04d 100644 --- a/app-common/src/main/res/xml/pref_settings.xml +++ b/app-common/src/main/res/xml/pref_settings.xml @@ -81,6 +81,14 @@ app:summary="@string/pref_developer_euicc_memory_reset_desc" app:title="@string/pref_developer_euicc_memory_reset" /> + + ("assembleDebugMagiskModuleDir") { + variant = "debug" + appName = "OpenEUICC" + permsFile = project.rootProject.file("privapp_whitelist_im.angry.openeuicc.xml") + moduleInstaller = project.file("magisk/module_installer.sh") + moduleCustomizeScriptText = moduleCustomizeScript + moduleUninstallScriptText = moduleUninstallScript + moduleProp = modulePropsTemplate.let { + it["description"] = "(debug build) ${it["description"]}" + it["versionCode"] = "${(android.applicationVariants.find { it.name == "debug" }!!.outputs.first() as ApkVariantOutputImpl).versionCodeOverride}" + it["updateJson"] = "https://openeuicc.com/magisk/magisk-debug.json" + it + } + dependsOn("assembleDebug") +} + +tasks.register("assembleDebugMagiskModule") { + dependsOn("assembleDebugMagiskModuleDir") + from((tasks.getByName("assembleDebugMagiskModuleDir") as MagiskModuleDirTask).outputDir) + archiveFileName = "magisk-debug.zip" + destinationDirectory = project.layout.buildDirectory.dir("magisk") + entryCompression = ZipEntryCompression.STORED +} + +tasks.register("assembleReleaseMagiskModuleDir") { + variant = "release" + appName = "OpenEUICC" + permsFile = project.rootProject.file("privapp_whitelist_im.angry.openeuicc.xml") + moduleInstaller = project.file("magisk/module_installer.sh") + moduleCustomizeScriptText = moduleCustomizeScript + moduleUninstallScriptText = moduleUninstallScript + moduleProp = modulePropsTemplate + dependsOn("assembleRelease") +} + +tasks.register("assembleReleaseMagiskModule") { + dependsOn("assembleReleaseMagiskModuleDir") + from((tasks.getByName("assembleReleaseMagiskModuleDir") as MagiskModuleDirTask).outputDir) + archiveFileName = "magisk-release.zip" + destinationDirectory = project.layout.buildDirectory.dir("magisk") + entryCompression = ZipEntryCompression.STORED } \ No newline at end of file diff --git a/app/magisk/customize.sh b/app/magisk/customize.sh new file mode 100644 index 0000000..707b401 --- /dev/null +++ b/app/magisk/customize.sh @@ -0,0 +1,9 @@ +TMP_FILE="$TMPDIR/{APK_NAME}" + +chmod u+x "$MODPATH/uninstall.sh" +cp "$MODPATH/system/system_ext/{APK_NAME}/{APK_NAME}.apk" "$TMP_FILE" + +pm install -r "$TMP_FILE" +rm -f "$TMP_FILE" + +pm grant "{PKG_NAME}" android.permission.READ_PHONE_STATE \ No newline at end of file diff --git a/app/magisk/module_installer.sh b/app/magisk/module_installer.sh new file mode 100644 index 0000000..28b48e5 --- /dev/null +++ b/app/magisk/module_installer.sh @@ -0,0 +1,33 @@ +#!/sbin/sh + +################# +# Initialization +################# + +umask 022 + +# echo before loading util_functions +ui_print() { echo "$1"; } + +require_new_magisk() { + ui_print "*******************************" + ui_print " Please install Magisk v20.4+! " + ui_print "*******************************" + exit 1 +} + +######################### +# Load util_functions.sh +######################### + +OUTFD=$2 +ZIPFILE=$3 + +mount /data 2>/dev/null + +[ -f /data/adb/magisk/util_functions.sh ] || require_new_magisk +. /data/adb/magisk/util_functions.sh +[ $MAGISK_VER_CODE -lt 20400 ] && require_new_magisk + +install_module +exit 0 diff --git a/app/magisk/uninstall.sh b/app/magisk/uninstall.sh new file mode 100644 index 0000000..1eb0200 --- /dev/null +++ b/app/magisk/uninstall.sh @@ -0,0 +1 @@ +pm uninstall "{PKG_NAME}" \ No newline at end of file diff --git a/app/src/main/java/im/angry/openeuicc/core/PrivilegedEuiccChannelFactory.kt b/app/src/main/java/im/angry/openeuicc/core/PrivilegedEuiccChannelFactory.kt index 978e886..876387f 100644 --- a/app/src/main/java/im/angry/openeuicc/core/PrivilegedEuiccChannelFactory.kt +++ b/app/src/main/java/im/angry/openeuicc/core/PrivilegedEuiccChannelFactory.kt @@ -42,6 +42,7 @@ class PrivilegedEuiccChannelFactory(context: Context) : DefaultEuiccChannelFacto isdrAid, context.preferenceRepository.verboseLoggingFlow, context.preferenceRepository.ignoreTLSCertificateFlow, + context.preferenceRepository.es10xMssFlow, ) } catch (_: IllegalArgumentException) { // Failed diff --git a/buildSrc/src/main/kotlin/im/angry/openeuicc/build/Magisk.kt b/buildSrc/src/main/kotlin/im/angry/openeuicc/build/Magisk.kt new file mode 100644 index 0000000..6245d8c --- /dev/null +++ b/buildSrc/src/main/kotlin/im/angry/openeuicc/build/Magisk.kt @@ -0,0 +1,74 @@ +package im.angry.openeuicc.build + +import org.gradle.api.DefaultTask +import org.gradle.api.provider.MapProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.TaskAction +import java.io.File + +abstract class MagiskModuleDirTask : DefaultTask() { + @get:Input + abstract val variant : Property + + @get:Input + abstract val appName : Property + + @get:InputFile + abstract val permsFile : Property + + @get:InputFile + abstract val moduleInstaller : Property + + @get:Input + abstract val moduleCustomizeScriptText : Property + + @get:Input + abstract val moduleUninstallScriptText : Property + + @get:Input + abstract val moduleProp : MapProperty + + @InputDirectory + val inputDir = variant.map { project.layout.buildDirectory.dir("outputs/apk/${it}") } + + @OutputDirectory + val outputDir = variant.map { project.layout.buildDirectory.dir("magisk/${it}") } + + @TaskAction + fun build() { + val dir = outputDir.get().get() + project.mkdir(dir) + val systemExtDir = dir.dir("system/system_ext") + val permDir = dir.dir("system/system_ext/etc/permissions") + val appDir = systemExtDir.dir("priv-app/${appName.get()}") + val metaInfDir = dir.dir("META-INF/com/google/android") + project.mkdir(systemExtDir) + project.mkdir(metaInfDir) + project.mkdir(appDir) + project.mkdir(permDir) + project.copy { + into(appDir) + from(inputDir) { + include("app-${variant.get()}.apk") + rename("app-${variant.get()}.apk", "${appName.get()}.apk") + } + } + project.copy { + from(permsFile) + into(permDir) + } + project.copy { + from(moduleInstaller) + into(metaInfDir) + rename(".*", "update-binary") + } + dir.file("customize.sh").asFile.writeText(moduleCustomizeScriptText.get()) + dir.file("uninstall.sh").asFile.writeText(moduleUninstallScriptText.get()) + metaInfDir.file("updater-script").asFile.writeText("# MAGISK") + dir.file("module.prop").asFile.writeText(moduleProp.get().map { (k, v) -> "$k=$v" }.joinToString("\n")) + } +} \ No newline at end of file