|
| 1 | +/* |
| 2 | + * Copyright 2023 Signal Messenger, LLC |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +package org.thoughtcrime.securesms.apkupdate |
| 7 | + |
| 8 | +import android.app.PendingIntent |
| 9 | +import android.content.Context |
| 10 | +import android.content.Intent |
| 11 | +import android.content.pm.PackageInstaller |
| 12 | +import android.os.Build |
| 13 | +import org.signal.core.util.PendingIntentFlags |
| 14 | +import org.signal.core.util.StreamUtil |
| 15 | +import org.signal.core.util.getDownloadManager |
| 16 | +import org.signal.core.util.logging.Log |
| 17 | +import org.thoughtcrime.securesms.dependencies.ApplicationDependencies |
| 18 | +import org.thoughtcrime.securesms.keyvalue.SignalStore |
| 19 | +import org.thoughtcrime.securesms.util.FileUtils |
| 20 | +import java.io.FileInputStream |
| 21 | +import java.io.IOException |
| 22 | +import java.io.InputStream |
| 23 | +import java.security.MessageDigest |
| 24 | + |
| 25 | +object ApkUpdateInstaller { |
| 26 | + |
| 27 | + private val TAG = Log.tag(ApkUpdateInstaller::class.java) |
| 28 | + |
| 29 | + /** |
| 30 | + * Installs the downloaded APK silently if possible. If not, prompts the user with a notification to install. |
| 31 | + * May show errors instead under certain conditions. |
| 32 | + * |
| 33 | + * A common pattern you may see is that this is called with [userInitiated] = false (or some other state |
| 34 | + * that prevents us from auto-updating, like the app being in the foreground), causing this function |
| 35 | + * to show an install prompt notification. The user clicks that notification, calling this with |
| 36 | + * [userInitiated] = true, and then everything installs. |
| 37 | + */ |
| 38 | + fun installOrPromptForInstall(context: Context, downloadId: Long, userInitiated: Boolean) { |
| 39 | + if (downloadId != SignalStore.apkUpdate().downloadId) { |
| 40 | + Log.w(TAG, "DownloadId doesn't match the one we're waiting for! We likely have newer data. Ignoring.") |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + val digest = SignalStore.apkUpdate().digest |
| 45 | + if (digest == null) { |
| 46 | + Log.w(TAG, "DownloadId matches, but digest is null! Inconsistent state. Failing and clearing state.") |
| 47 | + SignalStore.apkUpdate().clearDownloadAttributes() |
| 48 | + ApkUpdateNotifications.showInstallFailed(context, ApkUpdateNotifications.FailureReason.UNKNOWN) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + if (!isMatchingDigest(context, downloadId, digest)) { |
| 53 | + Log.w(TAG, "DownloadId matches, but digest does not! Bad download or inconsistent state. Failing and clearing state.") |
| 54 | + SignalStore.apkUpdate().clearDownloadAttributes() |
| 55 | + ApkUpdateNotifications.showInstallFailed(context, ApkUpdateNotifications.FailureReason.UNKNOWN) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if (!userInitiated && !shouldAutoUpdate()) { |
| 60 | + Log.w(TAG, "Not user-initiated and not eligible for auto-update. Prompting. (API=${Build.VERSION.SDK_INT}, Foreground=${ApplicationDependencies.getAppForegroundObserver().isForegrounded}, AutoUpdate=${SignalStore.apkUpdate().autoUpdate})") |
| 61 | + ApkUpdateNotifications.showInstallPrompt(context, downloadId) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + installApk(context, downloadId, userInitiated) |
| 67 | + } catch (e: IOException) { |
| 68 | + Log.w(TAG, "Hit IOException when trying to install APK!", e) |
| 69 | + SignalStore.apkUpdate().clearDownloadAttributes() |
| 70 | + ApkUpdateNotifications.showInstallFailed(context, ApkUpdateNotifications.FailureReason.UNKNOWN) |
| 71 | + } catch (e: SecurityException) { |
| 72 | + Log.w(TAG, "Hit SecurityException when trying to install APK!", e) |
| 73 | + SignalStore.apkUpdate().clearDownloadAttributes() |
| 74 | + ApkUpdateNotifications.showInstallFailed(context, ApkUpdateNotifications.FailureReason.UNKNOWN) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Throws(IOException::class, SecurityException::class) |
| 79 | + private fun installApk(context: Context, downloadId: Long, userInitiated: Boolean) { |
| 80 | + val apkInputStream: InputStream? = getDownloadedApkInputStream(context, downloadId) |
| 81 | + if (apkInputStream == null) { |
| 82 | + Log.w(TAG, "Could not open download APK input stream!") |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + Log.d(TAG, "Beginning APK install...") |
| 87 | + val packageInstaller: PackageInstaller = context.packageManager.packageInstaller |
| 88 | + |
| 89 | + Log.d(TAG, "Clearing inactive sessions...") |
| 90 | + packageInstaller.mySessions |
| 91 | + .filter { session -> !session.isActive } |
| 92 | + .forEach { session -> |
| 93 | + try { |
| 94 | + packageInstaller.abandonSession(session.sessionId) |
| 95 | + } catch (e: SecurityException) { |
| 96 | + Log.w(TAG, "Failed to abandon inactive session!", e) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + val sessionParams = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL).apply { |
| 101 | + // At this point, we always want to set this if possible, since we've already prompted the user with our own notification when necessary. |
| 102 | + // This lets us skip the system-generated notification. |
| 103 | + if (Build.VERSION.SDK_INT >= 31) { |
| 104 | + setRequireUserAction(PackageInstaller.SessionParams.USER_ACTION_NOT_REQUIRED) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + Log.d(TAG, "Creating install session...") |
| 109 | + val sessionId: Int = packageInstaller.createSession(sessionParams) |
| 110 | + val session: PackageInstaller.Session = packageInstaller.openSession(sessionId) |
| 111 | + |
| 112 | + Log.d(TAG, "Writing APK data...") |
| 113 | + session.use { activeSession -> |
| 114 | + val sessionOutputStream = activeSession.openWrite(context.packageName, 0, -1) |
| 115 | + StreamUtil.copy(apkInputStream, sessionOutputStream) |
| 116 | + } |
| 117 | + |
| 118 | + val installerPendingIntent = PendingIntent.getBroadcast( |
| 119 | + context, |
| 120 | + sessionId, |
| 121 | + Intent(context, ApkUpdatePackageInstallerReceiver::class.java).apply { |
| 122 | + putExtra(ApkUpdatePackageInstallerReceiver.EXTRA_USER_INITIATED, userInitiated) |
| 123 | + putExtra(ApkUpdatePackageInstallerReceiver.EXTRA_DOWNLOAD_ID, downloadId) |
| 124 | + }, |
| 125 | + PendingIntentFlags.mutable() or PendingIntentFlags.updateCurrent() |
| 126 | + ) |
| 127 | + |
| 128 | + Log.d(TAG, "Committing session...") |
| 129 | + session.commit(installerPendingIntent.intentSender) |
| 130 | + } |
| 131 | + |
| 132 | + private fun getDownloadedApkInputStream(context: Context, downloadId: Long): InputStream? { |
| 133 | + return try { |
| 134 | + FileInputStream(context.getDownloadManager().openDownloadedFile(downloadId).fileDescriptor) |
| 135 | + } catch (e: IOException) { |
| 136 | + Log.w(TAG, e) |
| 137 | + null |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private fun isMatchingDigest(context: Context, downloadId: Long, expectedDigest: ByteArray): Boolean { |
| 142 | + return try { |
| 143 | + FileInputStream(context.getDownloadManager().openDownloadedFile(downloadId).fileDescriptor).use { stream -> |
| 144 | + val digest = FileUtils.getFileDigest(stream) |
| 145 | + MessageDigest.isEqual(digest, expectedDigest) |
| 146 | + } |
| 147 | + } catch (e: IOException) { |
| 148 | + Log.w(TAG, e) |
| 149 | + false |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private fun shouldAutoUpdate(): Boolean { |
| 154 | + // TODO Auto-updates temporarily disabled. Once we have designs for allowing users to opt-out of auto-updates, we can re-enable this |
| 155 | + return false |
| 156 | +// return Build.VERSION.SDK_INT >= 31 && SignalStore.apkUpdate().autoUpdate && !ApplicationDependencies.getAppForegroundObserver().isForegrounded |
| 157 | + } |
| 158 | +} |
0 commit comments