|
| 1 | +package com.aliucord.manager.installers.pm |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.app.PendingIntent |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.content.pm.* |
| 8 | +import android.content.pm.PackageInstaller.SessionParams |
| 9 | +import android.os.Build |
| 10 | +import android.os.Process |
| 11 | +import android.util.Log |
| 12 | +import com.aliucord.manager.BuildConfig |
| 13 | +import com.aliucord.manager.installers.Installer |
| 14 | +import com.aliucord.manager.installers.InstallerResult |
| 15 | +import kotlinx.coroutines.suspendCancellableCoroutine |
| 16 | +import java.io.File |
| 17 | + |
| 18 | +/** |
| 19 | + * APK installer using the [PackageInstaller] from the system's [PackageManager] service. |
| 20 | + */ |
| 21 | +class PMInstaller( |
| 22 | + val context: Context, |
| 23 | +) : Installer { |
| 24 | + init { |
| 25 | + val pkgInstaller = context.packageManager.packageInstaller |
| 26 | + |
| 27 | + // Destroy all open sessions that may have not been previously cleaned up |
| 28 | + for (session in pkgInstaller.mySessions) { |
| 29 | + Log.d(BuildConfig.TAG, "Deleting PackageInstaller session ${session.sessionId}") |
| 30 | + pkgInstaller.abandonSession(session.sessionId) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + override fun install(apks: List<File>, silent: Boolean) { |
| 35 | + startInstall(createInstallSession(silent), apks, false) |
| 36 | + } |
| 37 | + |
| 38 | + override suspend fun waitInstall(apks: List<File>, silent: Boolean): InstallerResult { |
| 39 | + val sessionId = createInstallSession(silent) |
| 40 | + |
| 41 | + return suspendCancellableCoroutine { continuation -> |
| 42 | + // This will receive parsed data forwarded by PMIntentReceiver |
| 43 | + val relayReceiver = PMResultReceiver(sessionId, continuation) |
| 44 | + |
| 45 | + // Unregister PMResultReceiver when this coroutine finishes |
| 46 | + // additionally, cancel the install session entirely |
| 47 | + continuation.invokeOnCancellation { |
| 48 | + context.unregisterReceiver(relayReceiver) |
| 49 | + context.packageManager.packageInstaller.abandonSession(sessionId) |
| 50 | + } |
| 51 | + |
| 52 | + @SuppressLint("UnspecifiedRegisterReceiverFlag") |
| 53 | + if (Build.VERSION.SDK_INT >= 33) { |
| 54 | + context.registerReceiver(relayReceiver, relayReceiver.filter, Context.RECEIVER_NOT_EXPORTED) |
| 55 | + } else { |
| 56 | + context.registerReceiver(relayReceiver, relayReceiver.filter) |
| 57 | + } |
| 58 | + |
| 59 | + startInstall(sessionId, apks, relay = true) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Starts a [PackageInstaller] session with the necessary params. |
| 65 | + * @param silent If this is an update, then the update will occur without user interaction. |
| 66 | + * @return The open install session id. |
| 67 | + */ |
| 68 | + private fun createInstallSession(silent: Boolean): Int { |
| 69 | + val params = SessionParams(SessionParams.MODE_FULL_INSTALL).apply { |
| 70 | + setInstallLocation(PackageInfo.INSTALL_LOCATION_AUTO) |
| 71 | + |
| 72 | + if (Build.VERSION.SDK_INT >= 24) { |
| 73 | + setOriginatingUid(Process.myUid()) |
| 74 | + } |
| 75 | + |
| 76 | + if (Build.VERSION.SDK_INT >= 26) { |
| 77 | + setInstallReason(PackageManager.INSTALL_REASON_USER) |
| 78 | + } |
| 79 | + |
| 80 | + if (Build.VERSION.SDK_INT >= 31) { |
| 81 | + setInstallScenario(PackageManager.INSTALL_SCENARIO_FAST) |
| 82 | + |
| 83 | + if (silent) { |
| 84 | + setRequireUserAction(SessionParams.USER_ACTION_NOT_REQUIRED) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (Build.VERSION.SDK_INT >= 34) { |
| 89 | + setPackageSource(PackageInstaller.PACKAGE_SOURCE_OTHER) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return context.packageManager.packageInstaller.createSession(params) |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Start a [PackageInstaller] session for installation. |
| 98 | + * @param apks The apks to install |
| 99 | + * @param relay Whether to use the [PMResultReceiver] flow. |
| 100 | + */ |
| 101 | + private fun startInstall(sessionId: Int, apks: List<File>, relay: Boolean) { |
| 102 | + val callbackIntent = Intent(context, PMIntentReceiver::class.java) |
| 103 | + .putExtra(PMIntentReceiver.EXTRA_SESSION_ID, sessionId) |
| 104 | + .putExtra(PMIntentReceiver.EXTRA_RELAY_ENABLED, relay) |
| 105 | + |
| 106 | + val pendingIntent = PendingIntent.getBroadcast( |
| 107 | + /* context = */ context, |
| 108 | + /* requestCode = */ 0, |
| 109 | + /* intent = */ callbackIntent, |
| 110 | + /* flags = */ PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT, |
| 111 | + ) |
| 112 | + |
| 113 | + context.packageManager.packageInstaller.openSession(sessionId).use { session -> |
| 114 | + val bufferSize = 1 * 1024 * 1024 // 1MiB |
| 115 | + |
| 116 | + for (apk in apks) { |
| 117 | + session.openWrite(apk.name, 0, apk.length()).use { outStream -> |
| 118 | + apk.inputStream().use { it.copyTo(outStream, bufferSize) } |
| 119 | + session.fsync(outStream) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + session.commit(pendingIntent.intentSender) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments