From f7354a3d79ab317b64bedc9435f5bf1cfc64d0a7 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 27 Sep 2024 16:31:15 -0300 Subject: [PATCH] Show when launchable system apps do not allow backup Even though we use d2d, backup is only forced for user apps. --- .../java/com/stevesoltys/seedvault/App.kt | 1 + .../seedvault/settings/AppListRetriever.kt | 5 +++-- .../seedvault/settings/AppStatusAdapter.kt | 9 +-------- .../seedvault/ui/AppBackupState.kt | 2 +- .../NotificationBackupObserver.kt | 19 +++++++++++++++++-- logcat-verbose.sh | 1 + 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/App.kt b/app/src/main/java/com/stevesoltys/seedvault/App.kt index 46a57452..eac57f83 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/App.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/App.kt @@ -196,6 +196,7 @@ const val ANCESTRAL_RECORD_KEY = "@ancestral_record@" const val NO_DATA_END_SENTINEL = "@end@" const val GLOBAL_METADATA_KEY = "@meta@" const val ERROR_BACKUP_CANCELLED: Int = BackupManager.ERROR_BACKUP_CANCELLED +const val ERROR_BACKUP_NOT_ALLOWED: Int = BackupManager.ERROR_BACKUP_NOT_ALLOWED // TODO this doesn't work for LineageOS as they do public debug builds fun isDebugBuild() = Build.TYPE == "userdebug" diff --git a/app/src/main/java/com/stevesoltys/seedvault/settings/AppListRetriever.kt b/app/src/main/java/com/stevesoltys/seedvault/settings/AppListRetriever.kt index da127149..9d6cdb3c 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/settings/AppListRetriever.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/settings/AppListRetriever.kt @@ -98,7 +98,7 @@ internal class AppListRetriever( packageName = it.packageName, enabled = settingsManager.isBackupEnabled(it.packageName), icon = getIconFromPackageManager(it.packageName), - name = getAppName(context, it.packageName).toString(), + name = metadata?.name?.toString() ?: getAppName(context, it.packageName).toString(), time = time, size = metadata?.size, status = status, @@ -113,7 +113,8 @@ internal class AppListRetriever( packageName = packageName, enabled = settingsManager.isBackupEnabled(packageName), icon = getIconFromPackageManager(packageName), - name = it.loadLabel(context.packageManager).toString(), + name = metadata?.name?.toString() + ?: it.loadLabel(context.packageManager).toString(), time = metadata?.time ?: 0, size = metadata?.size, status = metadata?.state.toAppBackupState(), diff --git a/app/src/main/java/com/stevesoltys/seedvault/settings/AppStatusAdapter.kt b/app/src/main/java/com/stevesoltys/seedvault/settings/AppStatusAdapter.kt index 960fbf88..6cd85797 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/settings/AppStatusAdapter.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/settings/AppStatusAdapter.kt @@ -24,7 +24,6 @@ import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.Adapter import androidx.recyclerview.widget.RecyclerView.NO_POSITION import com.stevesoltys.seedvault.R -import com.stevesoltys.seedvault.ui.AppBackupState.FAILED_NOT_ALLOWED import com.stevesoltys.seedvault.ui.AppBackupState.SUCCEEDED import com.stevesoltys.seedvault.ui.AppViewHolder import com.stevesoltys.seedvault.ui.toRelativeTime @@ -114,13 +113,7 @@ internal class AppStatusAdapter(private val toggleListener: AppStatusToggleListe startActivity(context, intent, null) true } - if (item.status == FAILED_NOT_ALLOWED) { - appStatus.visibility = INVISIBLE - progressBar.visibility = INVISIBLE - appInfo.visibility = GONE - } else { - setState(item.status, false) - } + setState(item.status, false) if (item.status == SUCCEEDED) { appInfo.text = if (item.size == null) { item.time.toRelativeTime(context) diff --git a/app/src/main/java/com/stevesoltys/seedvault/ui/AppBackupState.kt b/app/src/main/java/com/stevesoltys/seedvault/ui/AppBackupState.kt index 6e63ad88..34ccb6b7 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/ui/AppBackupState.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/ui/AppBackupState.kt @@ -28,7 +28,7 @@ enum class AppBackupState { FAILED -> notShownString FAILED_NO_DATA -> context.getString(R.string.backup_app_no_data) FAILED_WAS_STOPPED -> context.getString(R.string.backup_app_was_stopped) - FAILED_NOT_ALLOWED -> notShownString + FAILED_NOT_ALLOWED -> context.getString(R.string.restore_app_not_allowed) FAILED_NOT_INSTALLED -> context.getString(R.string.restore_app_not_installed) FAILED_QUOTA_EXCEEDED -> context.getString(R.string.backup_app_quota_exceeded) } diff --git a/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt b/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt index ba0e2b54..49fad317 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/ui/notification/NotificationBackupObserver.kt @@ -16,8 +16,11 @@ import android.util.Log import android.util.Log.INFO import android.util.Log.isLoggable import com.stevesoltys.seedvault.ERROR_BACKUP_CANCELLED +import com.stevesoltys.seedvault.ERROR_BACKUP_NOT_ALLOWED import com.stevesoltys.seedvault.MAGIC_PACKAGE_MANAGER import com.stevesoltys.seedvault.R +import com.stevesoltys.seedvault.metadata.MetadataManager +import com.stevesoltys.seedvault.metadata.PackageState.NOT_ALLOWED import com.stevesoltys.seedvault.repo.AppBackupManager import com.stevesoltys.seedvault.repo.hexFromProto import com.stevesoltys.seedvault.settings.SettingsManager @@ -39,6 +42,7 @@ internal class NotificationBackupObserver( private val nm: BackupNotificationManager by inject() private val packageService: PackageService by inject() private val settingsManager: SettingsManager by inject() + private val metadataManager: MetadataManager by inject() private val appBackupManager: AppBackupManager by inject() private var currentPackage: String? = null private var numPackages: Int = 0 @@ -46,6 +50,9 @@ internal class NotificationBackupObserver( private var pmCounted: Boolean = false private var errorPackageName: String? = null + private val launchableSystemApps by lazy { + packageService.launchableSystemApps.map { it.activityInfo.packageName }.toSet() + } init { // Inform the notification manager that a backup has started @@ -77,7 +84,7 @@ internal class NotificationBackupObserver( * that was initialized * @param status Zero on success; a nonzero error code if the backup operation failed. */ - override fun onResult(target: String?, status: Int) { + override fun onResult(target: String, status: Int) { if (isLoggable(TAG, INFO)) { Log.i(TAG, "Completed. Target: $target, status: $status") } @@ -91,7 +98,7 @@ internal class NotificationBackupObserver( numPackages += 1 } // count package if success and not a system app - if (status == 0 && target != null && target != MAGIC_PACKAGE_MANAGER) try { + if (status == 0 && target != MAGIC_PACKAGE_MANAGER) try { val appInfo = context.packageManager.getApplicationInfo(target, 0) // exclude system apps from final count for now if (appInfo.flags and FLAG_SYSTEM == 0) { @@ -101,6 +108,14 @@ internal class NotificationBackupObserver( // should only happen for MAGIC_PACKAGE_MANAGER, but better save than sorry Log.e(TAG, "Error getting ApplicationInfo: ", e) } + // record status for not-allowed apps visible in UI + if (status == ERROR_BACKUP_NOT_ALLOWED) { + // this should only ever happen for system apps, as we use only d2d now + if (target in launchableSystemApps) { + val packageInfo = context.packageManager.getPackageInfo(target, 0) + metadataManager.onPackageBackupError(packageInfo, NOT_ALLOWED) + } + } // Apps that get killed while interacting with their [BackupAgent] cancel the entire backup. // In order to prevent them from DoSing us, we remember them here to auto-disable them. diff --git a/logcat-verbose.sh b/logcat-verbose.sh index 4bef7525..a5a66c4b 100755 --- a/logcat-verbose.sh +++ b/logcat-verbose.sh @@ -12,4 +12,5 @@ adb shell setprop log.tag.BackupTransportManager VERBOSE adb shell setprop log.tag.KeyValueBackupJob VERBOSE adb shell setprop log.tag.KeyValueBackupTask VERBOSE adb shell setprop log.tag.TransportClient VERBOSE +adb shell setprop log.tag.BackupAgent VERBOSE adb shell setprop log.tag.PMBA VERBOSE