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
This commit is contained in:
Torsten Grote 2019-06-12 14:17:24 -03:00
parent 3a9aec56b2
commit ad89ea4187
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
5 changed files with 29 additions and 41 deletions

View file

@ -13,15 +13,12 @@ import android.widget.PopupWindow;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.google.android.collect.Sets;
import com.stevesoltys.backup.R; import com.stevesoltys.backup.R;
import com.stevesoltys.backup.activity.PopupWindowUtil; import com.stevesoltys.backup.activity.PopupWindowUtil;
import com.stevesoltys.backup.service.PackageService; import com.stevesoltys.backup.service.PackageService;
import com.stevesoltys.backup.service.backup.BackupService; import com.stevesoltys.backup.service.backup.BackupService;
import com.stevesoltys.backup.settings.SettingsManager; import com.stevesoltys.backup.settings.SettingsManager;
import java.util.LinkedList;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -32,11 +29,6 @@ class CreateBackupActivityController {
private static final String TAG = CreateBackupActivityController.class.getName(); private static final String TAG = CreateBackupActivityController.class.getName();
private static final Set<String> 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 BackupService backupService = new BackupService();
private final PackageService packageService = new PackageService(); private final PackageService packageService = new PackageService();
@ -53,14 +45,15 @@ class CreateBackupActivityController {
popupWindowButton.setOnClickListener(view -> parent.finish()); popupWindowButton.setOnClickListener(view -> parent.finish());
}); });
List<String> eligiblePackageList = new LinkedList<>(); String[] eligiblePackageList;
try { try {
eligiblePackageList.addAll(packageService.getEligiblePackages()); eligiblePackageList = packageService.getEligiblePackages();
eligiblePackageList.removeAll(IGNORED_PACKAGES);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "Error while obtaining package list: ", 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(() -> { parent.runOnUiThread(() -> {

View file

@ -7,8 +7,10 @@ import android.os.RemoteException;
import android.os.ServiceManager; import android.os.ServiceManager;
import android.os.UserHandle; import android.os.UserHandle;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import static com.google.android.collect.Sets.newArraySet;
/** /**
* @author Steve Soltys * @author Steve Soltys
@ -19,24 +21,28 @@ public class PackageService {
private final IPackageManager packageManager; private final IPackageManager packageManager;
private static final Set<String> 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() { public PackageService() {
backupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); backupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
} }
public List<String> getEligiblePackages() throws RemoteException { public String[] getEligiblePackages() throws RemoteException {
List<String> results = new ArrayList<>();
List<PackageInfo> packages = packageManager.getInstalledPackages(0, UserHandle.USER_SYSTEM).getList(); List<PackageInfo> 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) { return backupManager.filterAppsEligibleForBackup(packageArray);
for (PackageInfo packageInfo : packages) {
if (backupManager.isAppEligibleForBackup(packageInfo.packageName)) {
results.add(packageInfo.packageName);
}
}
}
return results;
} }
} }

View file

@ -8,14 +8,10 @@ import android.content.Intent;
import android.os.RemoteException; import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import com.google.android.collect.Sets;
import com.stevesoltys.backup.service.PackageService; import com.stevesoltys.backup.service.PackageService;
import com.stevesoltys.backup.transport.ConfigurableBackupTransport; import com.stevesoltys.backup.transport.ConfigurableBackupTransport;
import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; 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.app.backup.BackupManager.FLAG_NON_INCREMENTAL_BACKUP;
import static android.os.ServiceManager.getService; import static android.os.ServiceManager.getService;
import static com.stevesoltys.backup.transport.ConfigurableBackupTransportService.getBackupTransport; 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 final static String TAG = BackupJobService.class.getName();
private static final Set<String> 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 IBackupManager backupManager;
private final PackageService packageService = new PackageService(); private final PackageService packageService = new PackageService();
@ -41,13 +32,11 @@ public class BackupJobService extends JobService {
Log.i(TAG, "Triggering full backup"); Log.i(TAG, "Triggering full backup");
startService(new Intent(this, ConfigurableBackupTransportService.class)); startService(new Intent(this, ConfigurableBackupTransportService.class));
try { try {
LinkedList<String> packages = new LinkedList<>(packageService.getEligiblePackages()); String[] packages = packageService.getEligiblePackages();
packages.removeAll(IGNORED_PACKAGES);
// TODO use an observer to know when backups fail // TODO use an observer to know when backups fail
String[] packageArray = packages.toArray(new String[packages.size()]);
ConfigurableBackupTransport backupTransport = getBackupTransport(getApplication()); ConfigurableBackupTransport backupTransport = getBackupTransport(getApplication());
backupTransport.prepareBackup(packageArray.length); backupTransport.prepareBackup(packages.length);
int result = backupManager.requestBackup(packageArray, null, null, FLAG_NON_INCREMENTAL_BACKUP); int result = backupManager.requestBackup(packages, null, null, FLAG_NON_INCREMENTAL_BACKUP);
if (result == BackupManager.SUCCESS) { if (result == BackupManager.SUCCESS) {
Log.i(TAG, "Backup succeeded "); Log.i(TAG, "Backup succeeded ");
} else { } else {

View file

@ -29,7 +29,7 @@ public class BackupSession extends IBackupObserver.Stub {
} }
public void start() throws RemoteException { 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); backupManager.requestBackup(selectedPackageArray, this, null, FLAG_NON_INCREMENTAL_BACKUP);
} }

View file

@ -61,7 +61,7 @@ public class RestoreSession extends IRestoreObserver.Stub {
public void restoreSetsAvailable(RestoreSet[] restoreSets) throws RemoteException { public void restoreSetsAvailable(RestoreSet[] restoreSets) throws RemoteException {
if (restoreSets.length > 0) { if (restoreSets.length > 0) {
RestoreSet restoreSet = restoreSets[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); int result = restoreSession.restoreSome(restoreSet.token, this, null, packageArray);
if (result != BackupManager.SUCCESS) { if (result != BackupManager.SUCCESS) {