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.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<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 PackageService packageService = new PackageService();
@ -53,14 +45,15 @@ class CreateBackupActivityController {
popupWindowButton.setOnClickListener(view -> parent.finish());
});
List<String> 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(() -> {

View file

@ -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<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() {
backupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
}
public List<String> getEligiblePackages() throws RemoteException {
List<String> results = new ArrayList<>();
public String[] getEligiblePackages() throws RemoteException {
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) {
for (PackageInfo packageInfo : packages) {
if (backupManager.isAppEligibleForBackup(packageInfo.packageName)) {
results.add(packageInfo.packageName);
}
}
}
return results;
return backupManager.filterAppsEligibleForBackup(packageArray);
}
}

View file

@ -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<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 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<String> 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 {

View file

@ -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);
}

View file

@ -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) {