From ad89ea4187420fce14735aa3cd77a46230ce2e83 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Wed, 12 Jun 2019 14:17:24 -0300 Subject: [PATCH] Use new IBackupManager API to filter eligable packages There's also a bit of refactoring for simplifying the PackageService usage. Fixes #26 by adding com.android.externalstorage to ignore list --- .../CreateBackupActivityController.java | 17 +++------- .../backup/service/PackageService.java | 32 +++++++++++-------- .../service/backup/BackupJobService.java | 17 ++-------- .../backup/session/backup/BackupSession.java | 2 +- .../session/restore/RestoreSession.java | 2 +- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/backup/activity/backup/CreateBackupActivityController.java b/app/src/main/java/com/stevesoltys/backup/activity/backup/CreateBackupActivityController.java index 74cbf4bb..5109d229 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/backup/CreateBackupActivityController.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/backup/CreateBackupActivityController.java @@ -13,15 +13,12 @@ import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; -import com.google.android.collect.Sets; import com.stevesoltys.backup.R; import com.stevesoltys.backup.activity.PopupWindowUtil; import com.stevesoltys.backup.service.PackageService; import com.stevesoltys.backup.service.backup.BackupService; import com.stevesoltys.backup.settings.SettingsManager; -import java.util.LinkedList; -import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; @@ -32,11 +29,6 @@ class CreateBackupActivityController { private static final String TAG = CreateBackupActivityController.class.getName(); - private static final Set IGNORED_PACKAGES = Sets.newArraySet( - "com.android.providers.downloads.ui", "com.android.providers.downloads", "com.android.providers.media", - "com.android.providers.calendar", "com.android.providers.contacts", "com.stevesoltys.backup" - ); - private final BackupService backupService = new BackupService(); private final PackageService packageService = new PackageService(); @@ -53,14 +45,15 @@ class CreateBackupActivityController { popupWindowButton.setOnClickListener(view -> parent.finish()); }); - List eligiblePackageList = new LinkedList<>(); + String[] eligiblePackageList; try { - eligiblePackageList.addAll(packageService.getEligiblePackages()); - eligiblePackageList.removeAll(IGNORED_PACKAGES); - + eligiblePackageList = packageService.getEligiblePackages(); } catch (RemoteException e) { Log.e(TAG, "Error while obtaining package list: ", e); + Toast.makeText(parent, "Error obtaining package list", Toast.LENGTH_SHORT).show(); + parent.finish(); + return; } parent.runOnUiThread(() -> { diff --git a/app/src/main/java/com/stevesoltys/backup/service/PackageService.java b/app/src/main/java/com/stevesoltys/backup/service/PackageService.java index e64f958d..0b7560a6 100644 --- a/app/src/main/java/com/stevesoltys/backup/service/PackageService.java +++ b/app/src/main/java/com/stevesoltys/backup/service/PackageService.java @@ -7,8 +7,10 @@ import android.os.RemoteException; import android.os.ServiceManager; import android.os.UserHandle; -import java.util.ArrayList; import java.util.List; +import java.util.Set; + +import static com.google.android.collect.Sets.newArraySet; /** * @author Steve Soltys @@ -19,24 +21,28 @@ public class PackageService { private final IPackageManager packageManager; + private static final Set IGNORED_PACKAGES = newArraySet( + "com.android.externalstorage", + "com.android.providers.downloads.ui", + "com.android.providers.downloads", + "com.android.providers.media", + "com.android.providers.calendar", + "com.android.providers.contacts", + "com.stevesoltys.backup" + ); + public PackageService() { backupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); } - public List getEligiblePackages() throws RemoteException { - List results = new ArrayList<>(); + public String[] getEligiblePackages() throws RemoteException { List packages = packageManager.getInstalledPackages(0, UserHandle.USER_SYSTEM).getList(); + String[] packageArray = packages.stream() + .map(packageInfo -> packageInfo.packageName) + .filter(packageName -> !IGNORED_PACKAGES.contains(packageName)) + .toArray(String[]::new); - if (packages != null) { - for (PackageInfo packageInfo : packages) { - - if (backupManager.isAppEligibleForBackup(packageInfo.packageName)) { - results.add(packageInfo.packageName); - } - } - } - - return results; + return backupManager.filterAppsEligibleForBackup(packageArray); } } diff --git a/app/src/main/java/com/stevesoltys/backup/service/backup/BackupJobService.java b/app/src/main/java/com/stevesoltys/backup/service/backup/BackupJobService.java index 0c62be8f..3d73f9be 100644 --- a/app/src/main/java/com/stevesoltys/backup/service/backup/BackupJobService.java +++ b/app/src/main/java/com/stevesoltys/backup/service/backup/BackupJobService.java @@ -8,14 +8,10 @@ import android.content.Intent; import android.os.RemoteException; import android.util.Log; -import com.google.android.collect.Sets; import com.stevesoltys.backup.service.PackageService; import com.stevesoltys.backup.transport.ConfigurableBackupTransport; import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; -import java.util.LinkedList; -import java.util.Set; - import static android.app.backup.BackupManager.FLAG_NON_INCREMENTAL_BACKUP; import static android.os.ServiceManager.getService; import static com.stevesoltys.backup.transport.ConfigurableBackupTransportService.getBackupTransport; @@ -24,11 +20,6 @@ public class BackupJobService extends JobService { private final static String TAG = BackupJobService.class.getName(); - private static final Set IGNORED_PACKAGES = Sets.newArraySet( - "com.android.providers.downloads.ui", "com.android.providers.downloads", "com.android.providers.media", - "com.android.providers.calendar", "com.android.providers.contacts", "com.stevesoltys.backup" - ); - private final IBackupManager backupManager; private final PackageService packageService = new PackageService(); @@ -41,13 +32,11 @@ public class BackupJobService extends JobService { Log.i(TAG, "Triggering full backup"); startService(new Intent(this, ConfigurableBackupTransportService.class)); try { - LinkedList packages = new LinkedList<>(packageService.getEligiblePackages()); - packages.removeAll(IGNORED_PACKAGES); + String[] packages = packageService.getEligiblePackages(); // TODO use an observer to know when backups fail - String[] packageArray = packages.toArray(new String[packages.size()]); ConfigurableBackupTransport backupTransport = getBackupTransport(getApplication()); - backupTransport.prepareBackup(packageArray.length); - int result = backupManager.requestBackup(packageArray, null, null, FLAG_NON_INCREMENTAL_BACKUP); + backupTransport.prepareBackup(packages.length); + int result = backupManager.requestBackup(packages, null, null, FLAG_NON_INCREMENTAL_BACKUP); if (result == BackupManager.SUCCESS) { Log.i(TAG, "Backup succeeded "); } else { diff --git a/app/src/main/java/com/stevesoltys/backup/session/backup/BackupSession.java b/app/src/main/java/com/stevesoltys/backup/session/backup/BackupSession.java index 4e7d0ea7..dfd95173 100644 --- a/app/src/main/java/com/stevesoltys/backup/session/backup/BackupSession.java +++ b/app/src/main/java/com/stevesoltys/backup/session/backup/BackupSession.java @@ -29,7 +29,7 @@ public class BackupSession extends IBackupObserver.Stub { } public void start() throws RemoteException { - String [] selectedPackageArray = packages.toArray(new String[packages.size()]); + String [] selectedPackageArray = packages.toArray(new String[0]); backupManager.requestBackup(selectedPackageArray, this, null, FLAG_NON_INCREMENTAL_BACKUP); } diff --git a/app/src/main/java/com/stevesoltys/backup/session/restore/RestoreSession.java b/app/src/main/java/com/stevesoltys/backup/session/restore/RestoreSession.java index 8b8ed83a..d028d5f4 100644 --- a/app/src/main/java/com/stevesoltys/backup/session/restore/RestoreSession.java +++ b/app/src/main/java/com/stevesoltys/backup/session/restore/RestoreSession.java @@ -61,7 +61,7 @@ public class RestoreSession extends IRestoreObserver.Stub { public void restoreSetsAvailable(RestoreSet[] restoreSets) throws RemoteException { if (restoreSets.length > 0) { RestoreSet restoreSet = restoreSets[0]; - String[] packageArray = packages.toArray(new String[packages.size()]); + String[] packageArray = packages.toArray(new String[0]); int result = restoreSession.restoreSome(restoreSet.token, this, null, packageArray); if (result != BackupManager.SUCCESS) {