From 0971c5db19bf97ea780aec5b19445bc67f82bd04 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 9 Oct 2020 14:43:59 -0300 Subject: [PATCH] Do not back up APKs of test-only apps, as we can not re-install them anyway The flag to allow installation gets filtered out after we set it: http://aosp.opersys.com/xref/android-11.0.0_r5/xref/frameworks/base/services/core/java/com/android/server/pm/PackageInstallerService.java#544 --- .../stevesoltys/seedvault/restore/RestoreViewModel.kt | 9 ++------- .../seedvault/restore/install/ApkInstaller.kt | 1 - .../seedvault/transport/backup/ApkBackup.kt | 7 +++++++ .../seedvault/transport/backup/PackageService.kt | 6 ++++++ .../seedvault/transport/backup/ApkBackupTest.kt | 10 ++++++++++ 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/restore/RestoreViewModel.kt b/app/src/main/java/com/stevesoltys/seedvault/restore/RestoreViewModel.kt index b089ec10..b5f1131e 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/restore/RestoreViewModel.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/restore/RestoreViewModel.kt @@ -5,7 +5,6 @@ import android.app.backup.IBackupManager import android.app.backup.IRestoreObserver import android.app.backup.IRestoreSession import android.app.backup.RestoreSet -import android.content.pm.PackageManager import android.os.RemoteException import android.os.UserHandle import android.util.Log @@ -31,6 +30,7 @@ import com.stevesoltys.seedvault.restore.DisplayFragment.RESTORE_BACKUP import com.stevesoltys.seedvault.restore.install.ApkRestore import com.stevesoltys.seedvault.restore.install.InstallIntentCreator import com.stevesoltys.seedvault.restore.install.InstallResult +import com.stevesoltys.seedvault.restore.install.isInstalled import com.stevesoltys.seedvault.settings.SettingsManager import com.stevesoltys.seedvault.transport.TRANSPORT_ID import com.stevesoltys.seedvault.transport.restore.RestoreCoordinator @@ -244,12 +244,7 @@ internal class RestoreViewModel( QUOTA_EXCEEDED -> FAILED_QUOTA_EXCEEDED UNKNOWN_ERROR -> FAILED APK_AND_DATA -> { - try { - app.packageManager.getPackageInfo(packageName, 0) - FAILED - } catch (e: PackageManager.NameNotFoundException) { - FAILED_NOT_INSTALLED - } + if (app.packageManager.isInstalled(packageName)) FAILED else FAILED_NOT_INSTALLED } } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/restore/install/ApkInstaller.kt b/app/src/main/java/com/stevesoltys/seedvault/restore/install/ApkInstaller.kt index 992c8245..65f433d3 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/restore/install/ApkInstaller.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/restore/install/ApkInstaller.kt @@ -108,7 +108,6 @@ internal class ApkInstaller(private val context: Context) { } // update status and offer result - // TODO maybe don't back up statusMsg=INSTALL_FAILED_TEST_ONLY apps in the first place? val status = if (success) SUCCEEDED else FAILED return installResult.update(packageName) { it.copy(state = status) } } diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/ApkBackup.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/ApkBackup.kt index 139c996a..adf2eb61 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/ApkBackup.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/ApkBackup.kt @@ -53,6 +53,13 @@ class ApkBackup( // do not back up when setting is not enabled if (!settingsManager.backupApks()) return null + // do not back up test-only apps as we can't re-install them anyway + // see: https://commonsware.com/blog/2017/10/31/android-studio-3p0-flag-test-only.html + if (packageInfo.isTestOnly()) { + Log.d(TAG, "Package $packageName is test-only app. Not backing it up.") + return null + } + // do not back up system apps that haven't been updated if (packageInfo.isNotUpdatedSystemApp()) { Log.d(TAG, "Package $packageName is vanilla system app. Not backing it up.") diff --git a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/PackageService.kt b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/PackageService.kt index a547bf5f..2ca88dd0 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/transport/backup/PackageService.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/transport/backup/PackageService.kt @@ -5,6 +5,7 @@ import android.content.Context import android.content.pm.ApplicationInfo.FLAG_ALLOW_BACKUP import android.content.pm.ApplicationInfo.FLAG_STOPPED import android.content.pm.ApplicationInfo.FLAG_SYSTEM +import android.content.pm.ApplicationInfo.FLAG_TEST_ONLY import android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP import android.content.pm.PackageInfo import android.content.pm.PackageManager @@ -162,3 +163,8 @@ internal fun PackageInfo.isStopped(): Boolean { if (packageName == MAGIC_PACKAGE_MANAGER || applicationInfo == null) return false return applicationInfo.flags and FLAG_STOPPED != 0 } + +internal fun PackageInfo.isTestOnly(): Boolean { + if (packageName == MAGIC_PACKAGE_MANAGER || applicationInfo == null) return false + return applicationInfo.flags and FLAG_TEST_ONLY != 0 +} diff --git a/app/src/test/java/com/stevesoltys/seedvault/transport/backup/ApkBackupTest.kt b/app/src/test/java/com/stevesoltys/seedvault/transport/backup/ApkBackupTest.kt index e2235c32..626c11f5 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/transport/backup/ApkBackupTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/transport/backup/ApkBackupTest.kt @@ -1,6 +1,7 @@ package com.stevesoltys.seedvault.transport.backup import android.content.pm.ApplicationInfo.FLAG_SYSTEM +import android.content.pm.ApplicationInfo.FLAG_TEST_ONLY import android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP import android.content.pm.InstallSourceInfo import android.content.pm.PackageInfo @@ -65,6 +66,15 @@ internal class ApkBackupTest : BackupTest() { assertNull(apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, streamGetter)) } + @Test + fun `does not back up test-only apps`() = runBlocking { + packageInfo.applicationInfo.flags = FLAG_TEST_ONLY + + every { settingsManager.backupApks() } returns true + + assertNull(apkBackup.backupApkIfNecessary(packageInfo, UNKNOWN_ERROR, streamGetter)) + } + @Test fun `does not back up system apps`() = runBlocking { packageInfo.applicationInfo.flags = FLAG_SYSTEM