diff --git a/app/src/main/java/com/stevesoltys/backup/activity/backup/CreateBackupActivity.java b/app/src/main/java/com/stevesoltys/backup/activity/backup/CreateBackupActivity.java index 46503f12..3da9c37f 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/backup/CreateBackupActivity.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/backup/CreateBackupActivity.java @@ -6,11 +6,10 @@ import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; - import com.stevesoltys.backup.R; -import java.util.LinkedList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; public class CreateBackupActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener { @@ -18,7 +17,7 @@ public class CreateBackupActivity extends Activity implements View.OnClickListen private ListView packageListView; - private List selectedPackageList; + private Set selectedPackageList; private Uri contentUri; @@ -30,7 +29,7 @@ public class CreateBackupActivity extends Activity implements View.OnClickListen findViewById(R.id.create_confirm_button).setOnClickListener(this); packageListView = findViewById(R.id.create_package_list); - selectedPackageList = new LinkedList<>(); + selectedPackageList = new HashSet<>(); contentUri = getIntent().getData(); controller = new CreateBackupActivityController(); 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 685adcc8..4796edd7 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 @@ -29,6 +29,7 @@ import com.stevesoltys.backup.transport.component.provider.restore.ContentProvid import java.util.LinkedList; import java.util.List; +import java.util.Set; /** * @author Steve Soltys @@ -57,13 +58,12 @@ class CreateBackupActivityController { packageListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); } - void backupPackages(List selectedPackages, Uri contentUri, Activity parent) { + void backupPackages(Set selectedPackages, Uri contentUri, Activity parent) { try { - String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size() + 1]); - selectedPackageArray[selectedPackageArray.length - 1] = "@pm@"; + selectedPackages.add("@pm@"); ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder() - .setContext(parent).setOutputUri(contentUri).setPackages(selectedPackageArray).build(); + .setContext(parent).setOutputUri(contentUri).setPackages(selectedPackages).build(); boolean success = initializeBackupTransport(backupConfiguration); if (!success) { @@ -73,7 +73,7 @@ class CreateBackupActivityController { PopupWindow popupWindow = buildPopupWindow(parent); BackupObserver backupObserver = new BackupObserver(parent, popupWindow); - BackupSession backupSession = backupManager.backup(backupObserver, selectedPackageArray); + BackupSession backupSession = backupManager.backup(backupObserver, selectedPackages); View popupWindowButton = popupWindow.getContentView().findViewById(R.id.popup_cancel_button); diff --git a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivity.java b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivity.java index bbeffb35..a52ff4a3 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivity.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivity.java @@ -6,11 +6,10 @@ import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; - import com.stevesoltys.backup.R; -import java.util.LinkedList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; public class RestoreBackupActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener { @@ -18,7 +17,7 @@ public class RestoreBackupActivity extends Activity implements View.OnClickListe private ListView packageListView; - private List selectedPackageList; + private Set selectedPackageList; private Uri contentUri; @@ -30,7 +29,7 @@ public class RestoreBackupActivity extends Activity implements View.OnClickListe findViewById(R.id.restore_confirm_button).setOnClickListener(this); packageListView = findViewById(R.id.restore_package_list); - selectedPackageList = new LinkedList<>(); + selectedPackageList = new HashSet<>(); contentUri = getIntent().getData(); controller = new RestoreBackupActivityController(); diff --git a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivityController.java b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivityController.java index 23ed4dd2..ff672521 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivityController.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivityController.java @@ -6,6 +6,7 @@ import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.ParcelFileDescriptor; +import android.os.RemoteException; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; @@ -33,6 +34,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.util.LinkedList; import java.util.List; +import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -89,12 +91,10 @@ class RestoreBackupActivityController { return results; } - void restorePackages(List selectedPackages, Uri contentUri, Activity parent) { + void restorePackages(Set selectedPackages, Uri contentUri, Activity parent) { try { - String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size()]); - ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder(). - setContext(parent).setOutputUri(contentUri).setPackages(selectedPackageArray).build(); + setContext(parent).setOutputUri(contentUri).setPackages(selectedPackages).build(); boolean success = initializeBackupTransport(backupConfiguration); if(!success) { @@ -103,16 +103,14 @@ class RestoreBackupActivityController { } PopupWindow popupWindow = buildPopupWindow(parent); - RestoreObserver restoreObserver = new RestoreObserver(parent, popupWindow, selectedPackageArray.length); - RestoreSession restoreSession = backupManager.restore(restoreObserver, selectedPackageArray); + RestoreObserver restoreObserver = new RestoreObserver(parent, popupWindow, selectedPackages.size()); + RestoreSession restoreSession = backupManager.restore(restoreObserver, selectedPackages); View popupWindowButton = popupWindow.getContentView().findViewById(R.id.popup_cancel_button); - if (popupWindowButton != null) { popupWindowButton.setOnClickListener(new RestorePopupWindowListener(restoreSession)); } - - } catch (Exception e) { + } catch (RemoteException e) { Log.e(TAG, "Error while running restore: ", e); } } diff --git a/app/src/main/java/com/stevesoltys/backup/session/BackupManagerController.java b/app/src/main/java/com/stevesoltys/backup/session/BackupManagerController.java index 9f858263..295a5664 100644 --- a/app/src/main/java/com/stevesoltys/backup/session/BackupManagerController.java +++ b/app/src/main/java/com/stevesoltys/backup/session/BackupManagerController.java @@ -5,7 +5,6 @@ import android.content.pm.IPackageManager; import android.content.pm.PackageInfo; import android.os.RemoteException; import android.os.ServiceManager; - import com.stevesoltys.backup.session.backup.BackupSession; import com.stevesoltys.backup.session.backup.BackupSessionObserver; import com.stevesoltys.backup.session.restore.RestoreSession; @@ -13,6 +12,7 @@ import com.stevesoltys.backup.session.restore.RestoreSessionObserver; import java.util.ArrayList; import java.util.List; +import java.util.Set; import static android.os.UserHandle.USER_SYSTEM; @@ -32,7 +32,7 @@ public class BackupManagerController { packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); } - public BackupSession backup(BackupSessionObserver observer, String... packages) throws RemoteException { + public BackupSession backup(BackupSessionObserver observer, Set packages) throws RemoteException { if (!BACKUP_TRANSPORT.equals(backupManager.getCurrentTransport())) { backupManager.selectBackupTransport(BACKUP_TRANSPORT); @@ -43,7 +43,7 @@ public class BackupManagerController { return backupSession; } - public RestoreSession restore(RestoreSessionObserver observer, String... packages) throws RemoteException { + public RestoreSession restore(RestoreSessionObserver observer, Set packages) throws RemoteException { if (!BACKUP_TRANSPORT.equals(backupManager.getCurrentTransport())) { backupManager.selectBackupTransport(BACKUP_TRANSPORT); 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 8bc95cae..a61e96b1 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 @@ -6,6 +6,8 @@ import android.app.backup.IBackupManager; import android.app.backup.IBackupObserver; import android.os.RemoteException; +import java.util.Set; + import static android.app.backup.BackupManager.FLAG_NON_INCREMENTAL_BACKUP; /** @@ -17,16 +19,18 @@ public class BackupSession extends IBackupObserver.Stub { private final BackupSessionObserver backupSessionObserver; - private final String[] packages; + private final Set packages; - public BackupSession(IBackupManager backupManager, BackupSessionObserver backupSessionObserver, String... packages) { + public BackupSession(IBackupManager backupManager, BackupSessionObserver backupSessionObserver, + Set packages) { this.backupManager = backupManager; this.backupSessionObserver = backupSessionObserver; this.packages = packages; } public void start() throws RemoteException { - backupManager.requestBackup(packages, this, null, FLAG_NON_INCREMENTAL_BACKUP); + String [] selectedPackageArray = packages.toArray(new String[packages.size()]); + backupManager.requestBackup(selectedPackageArray, this, null, FLAG_NON_INCREMENTAL_BACKUP); } public void stop(BackupResult result) throws RemoteException { 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 c5958478..03161ea8 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 @@ -1,12 +1,10 @@ package com.stevesoltys.backup.session.restore; -import android.app.backup.BackupManager; -import android.app.backup.IBackupManager; -import android.app.backup.IRestoreObserver; -import android.app.backup.IRestoreSession; -import android.app.backup.RestoreSet; +import android.app.backup.*; import android.os.RemoteException; +import java.util.Set; + /** * @author Steve Soltys */ @@ -16,11 +14,11 @@ public class RestoreSession extends IRestoreObserver.Stub { private final RestoreSessionObserver observer; - private final String[] packages; + private final Set packages; private IRestoreSession restoreSession; - public RestoreSession(IBackupManager backupManager, RestoreSessionObserver observer, String... packages) { + public RestoreSession(IBackupManager backupManager, RestoreSessionObserver observer, Set packages) { this.backupManager = backupManager; this.observer = observer; this.packages = packages; @@ -28,7 +26,7 @@ public class RestoreSession extends IRestoreObserver.Stub { public void start() throws RemoteException { - if (restoreSession != null || packages.length == 0) { + if (restoreSession != null || packages.isEmpty()) { observer.restoreSessionCompleted(RestoreResult.FAILURE); return; } @@ -63,7 +61,8 @@ public class RestoreSession extends IRestoreObserver.Stub { public void restoreSetsAvailable(RestoreSet[] restoreSets) throws RemoteException { if (restoreSets.length > 0) { RestoreSet restoreSet = restoreSets[0]; - int result = restoreSession.restoreSome(restoreSet.token, this, null, packages); + String[] packageArray = packages.toArray(new String[packages.size()]); + int result = restoreSession.restoreSome(restoreSet.token, this, null, packageArray); if (result != BackupManager.SUCCESS) { stop(RestoreResult.FAILURE); @@ -88,6 +87,7 @@ public class RestoreSession extends IRestoreObserver.Stub { } else if (result == BackupManager.ERROR_BACKUP_CANCELLED) { stop(RestoreResult.CANCELLED); + } else { stop(RestoreResult.FAILURE); } diff --git a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfiguration.java b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfiguration.java index 522c74c8..3825b498 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfiguration.java +++ b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfiguration.java @@ -3,6 +3,8 @@ package com.stevesoltys.backup.transport.component.provider; import android.content.Context; import android.net.Uri; +import java.util.Set; + /** * @author Steve Soltys */ @@ -18,9 +20,9 @@ public class ContentProviderBackupConfiguration { private final long backupSizeQuota; - private final String[] packages; + private final Set packages; - ContentProviderBackupConfiguration(Context context, Uri uri, String[] packages, long backupSizeQuota) { + ContentProviderBackupConfiguration(Context context, Uri uri, Set packages, long backupSizeQuota) { this.context = context; this.uri = uri; this.packages = packages; @@ -40,7 +42,7 @@ public class ContentProviderBackupConfiguration { } public int getPackageCount() { - return packages.length; + return packages.size(); } } diff --git a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfigurationBuilder.java b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfigurationBuilder.java index 7e7b8575..88041ea2 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfigurationBuilder.java +++ b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfigurationBuilder.java @@ -3,6 +3,8 @@ package com.stevesoltys.backup.transport.component.provider; import android.content.Context; import android.net.Uri; +import java.util.Set; + /** * @author Steve Soltys */ @@ -12,7 +14,7 @@ public class ContentProviderBackupConfigurationBuilder { private Uri outputUri; - private String[] packages; + private Set packages; private long backupSizeQuota = Long.MAX_VALUE; @@ -41,7 +43,7 @@ public class ContentProviderBackupConfigurationBuilder { return this; } - public ContentProviderBackupConfigurationBuilder setPackages(String[] packages) { + public ContentProviderBackupConfigurationBuilder setPackages(Set packages) { this.packages = packages; return this; }