Switch selected package list data type to Set

This commit is contained in:
Steve Soltys 2017-10-09 20:25:40 -04:00
parent facb7029b9
commit 677b950dea
9 changed files with 48 additions and 44 deletions

View file

@ -6,11 +6,10 @@ import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import com.stevesoltys.backup.R; import com.stevesoltys.backup.R;
import java.util.LinkedList; import java.util.HashSet;
import java.util.List; import java.util.Set;
public class CreateBackupActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener { 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 ListView packageListView;
private List<String> selectedPackageList; private Set<String> selectedPackageList;
private Uri contentUri; private Uri contentUri;
@ -30,7 +29,7 @@ public class CreateBackupActivity extends Activity implements View.OnClickListen
findViewById(R.id.create_confirm_button).setOnClickListener(this); findViewById(R.id.create_confirm_button).setOnClickListener(this);
packageListView = findViewById(R.id.create_package_list); packageListView = findViewById(R.id.create_package_list);
selectedPackageList = new LinkedList<>(); selectedPackageList = new HashSet<>();
contentUri = getIntent().getData(); contentUri = getIntent().getData();
controller = new CreateBackupActivityController(); controller = new CreateBackupActivityController();

View file

@ -29,6 +29,7 @@ import com.stevesoltys.backup.transport.component.provider.restore.ContentProvid
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* @author Steve Soltys * @author Steve Soltys
@ -57,13 +58,12 @@ class CreateBackupActivityController {
packageListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); packageListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
} }
void backupPackages(List<String> selectedPackages, Uri contentUri, Activity parent) { void backupPackages(Set<String> selectedPackages, Uri contentUri, Activity parent) {
try { try {
String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size() + 1]); selectedPackages.add("@pm@");
selectedPackageArray[selectedPackageArray.length - 1] = "@pm@";
ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder() ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder()
.setContext(parent).setOutputUri(contentUri).setPackages(selectedPackageArray).build(); .setContext(parent).setOutputUri(contentUri).setPackages(selectedPackages).build();
boolean success = initializeBackupTransport(backupConfiguration); boolean success = initializeBackupTransport(backupConfiguration);
if (!success) { if (!success) {
@ -73,7 +73,7 @@ class CreateBackupActivityController {
PopupWindow popupWindow = buildPopupWindow(parent); PopupWindow popupWindow = buildPopupWindow(parent);
BackupObserver backupObserver = new BackupObserver(parent, popupWindow); 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); View popupWindowButton = popupWindow.getContentView().findViewById(R.id.popup_cancel_button);

View file

@ -6,11 +6,10 @@ import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ListView; import android.widget.ListView;
import com.stevesoltys.backup.R; import com.stevesoltys.backup.R;
import java.util.LinkedList; import java.util.HashSet;
import java.util.List; import java.util.Set;
public class RestoreBackupActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener { 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 ListView packageListView;
private List<String> selectedPackageList; private Set<String> selectedPackageList;
private Uri contentUri; private Uri contentUri;
@ -30,7 +29,7 @@ public class RestoreBackupActivity extends Activity implements View.OnClickListe
findViewById(R.id.restore_confirm_button).setOnClickListener(this); findViewById(R.id.restore_confirm_button).setOnClickListener(this);
packageListView = findViewById(R.id.restore_package_list); packageListView = findViewById(R.id.restore_package_list);
selectedPackageList = new LinkedList<>(); selectedPackageList = new HashSet<>();
contentUri = getIntent().getData(); contentUri = getIntent().getData();
controller = new RestoreBackupActivityController(); controller = new RestoreBackupActivityController();

View file

@ -6,6 +6,7 @@ import android.graphics.Color;
import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.ColorDrawable;
import android.net.Uri; import android.net.Uri;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -33,6 +34,7 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
@ -89,12 +91,10 @@ class RestoreBackupActivityController {
return results; return results;
} }
void restorePackages(List<String> selectedPackages, Uri contentUri, Activity parent) { void restorePackages(Set<String> selectedPackages, Uri contentUri, Activity parent) {
try { try {
String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size()]);
ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder(). ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder().
setContext(parent).setOutputUri(contentUri).setPackages(selectedPackageArray).build(); setContext(parent).setOutputUri(contentUri).setPackages(selectedPackages).build();
boolean success = initializeBackupTransport(backupConfiguration); boolean success = initializeBackupTransport(backupConfiguration);
if(!success) { if(!success) {
@ -103,16 +103,14 @@ class RestoreBackupActivityController {
} }
PopupWindow popupWindow = buildPopupWindow(parent); PopupWindow popupWindow = buildPopupWindow(parent);
RestoreObserver restoreObserver = new RestoreObserver(parent, popupWindow, selectedPackageArray.length); RestoreObserver restoreObserver = new RestoreObserver(parent, popupWindow, selectedPackages.size());
RestoreSession restoreSession = backupManager.restore(restoreObserver, selectedPackageArray); RestoreSession restoreSession = backupManager.restore(restoreObserver, selectedPackages);
View popupWindowButton = popupWindow.getContentView().findViewById(R.id.popup_cancel_button); View popupWindowButton = popupWindow.getContentView().findViewById(R.id.popup_cancel_button);
if (popupWindowButton != null) { if (popupWindowButton != null) {
popupWindowButton.setOnClickListener(new RestorePopupWindowListener(restoreSession)); popupWindowButton.setOnClickListener(new RestorePopupWindowListener(restoreSession));
} }
} catch (RemoteException e) {
} catch (Exception e) {
Log.e(TAG, "Error while running restore: ", e); Log.e(TAG, "Error while running restore: ", e);
} }
} }

View file

@ -5,7 +5,6 @@ import android.content.pm.IPackageManager;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.ServiceManager; import android.os.ServiceManager;
import com.stevesoltys.backup.session.backup.BackupSession; import com.stevesoltys.backup.session.backup.BackupSession;
import com.stevesoltys.backup.session.backup.BackupSessionObserver; import com.stevesoltys.backup.session.backup.BackupSessionObserver;
import com.stevesoltys.backup.session.restore.RestoreSession; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import static android.os.UserHandle.USER_SYSTEM; import static android.os.UserHandle.USER_SYSTEM;
@ -32,7 +32,7 @@ public class BackupManagerController {
packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
} }
public BackupSession backup(BackupSessionObserver observer, String... packages) throws RemoteException { public BackupSession backup(BackupSessionObserver observer, Set<String> packages) throws RemoteException {
if (!BACKUP_TRANSPORT.equals(backupManager.getCurrentTransport())) { if (!BACKUP_TRANSPORT.equals(backupManager.getCurrentTransport())) {
backupManager.selectBackupTransport(BACKUP_TRANSPORT); backupManager.selectBackupTransport(BACKUP_TRANSPORT);
@ -43,7 +43,7 @@ public class BackupManagerController {
return backupSession; return backupSession;
} }
public RestoreSession restore(RestoreSessionObserver observer, String... packages) throws RemoteException { public RestoreSession restore(RestoreSessionObserver observer, Set<String> packages) throws RemoteException {
if (!BACKUP_TRANSPORT.equals(backupManager.getCurrentTransport())) { if (!BACKUP_TRANSPORT.equals(backupManager.getCurrentTransport())) {
backupManager.selectBackupTransport(BACKUP_TRANSPORT); backupManager.selectBackupTransport(BACKUP_TRANSPORT);

View file

@ -6,6 +6,8 @@ import android.app.backup.IBackupManager;
import android.app.backup.IBackupObserver; import android.app.backup.IBackupObserver;
import android.os.RemoteException; import android.os.RemoteException;
import java.util.Set;
import static android.app.backup.BackupManager.FLAG_NON_INCREMENTAL_BACKUP; 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 BackupSessionObserver backupSessionObserver;
private final String[] packages; private final Set<String> packages;
public BackupSession(IBackupManager backupManager, BackupSessionObserver backupSessionObserver, String... packages) { public BackupSession(IBackupManager backupManager, BackupSessionObserver backupSessionObserver,
Set<String> packages) {
this.backupManager = backupManager; this.backupManager = backupManager;
this.backupSessionObserver = backupSessionObserver; this.backupSessionObserver = backupSessionObserver;
this.packages = packages; this.packages = packages;
} }
public void start() throws RemoteException { 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 { public void stop(BackupResult result) throws RemoteException {

View file

@ -1,12 +1,10 @@
package com.stevesoltys.backup.session.restore; package com.stevesoltys.backup.session.restore;
import android.app.backup.BackupManager; import android.app.backup.*;
import android.app.backup.IBackupManager;
import android.app.backup.IRestoreObserver;
import android.app.backup.IRestoreSession;
import android.app.backup.RestoreSet;
import android.os.RemoteException; import android.os.RemoteException;
import java.util.Set;
/** /**
* @author Steve Soltys * @author Steve Soltys
*/ */
@ -16,11 +14,11 @@ public class RestoreSession extends IRestoreObserver.Stub {
private final RestoreSessionObserver observer; private final RestoreSessionObserver observer;
private final String[] packages; private final Set<String> packages;
private IRestoreSession restoreSession; private IRestoreSession restoreSession;
public RestoreSession(IBackupManager backupManager, RestoreSessionObserver observer, String... packages) { public RestoreSession(IBackupManager backupManager, RestoreSessionObserver observer, Set<String> packages) {
this.backupManager = backupManager; this.backupManager = backupManager;
this.observer = observer; this.observer = observer;
this.packages = packages; this.packages = packages;
@ -28,7 +26,7 @@ public class RestoreSession extends IRestoreObserver.Stub {
public void start() throws RemoteException { public void start() throws RemoteException {
if (restoreSession != null || packages.length == 0) { if (restoreSession != null || packages.isEmpty()) {
observer.restoreSessionCompleted(RestoreResult.FAILURE); observer.restoreSessionCompleted(RestoreResult.FAILURE);
return; return;
} }
@ -63,7 +61,8 @@ 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];
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) { if (result != BackupManager.SUCCESS) {
stop(RestoreResult.FAILURE); stop(RestoreResult.FAILURE);
@ -88,6 +87,7 @@ public class RestoreSession extends IRestoreObserver.Stub {
} else if (result == BackupManager.ERROR_BACKUP_CANCELLED) { } else if (result == BackupManager.ERROR_BACKUP_CANCELLED) {
stop(RestoreResult.CANCELLED); stop(RestoreResult.CANCELLED);
} else { } else {
stop(RestoreResult.FAILURE); stop(RestoreResult.FAILURE);
} }

View file

@ -3,6 +3,8 @@ package com.stevesoltys.backup.transport.component.provider;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import java.util.Set;
/** /**
* @author Steve Soltys * @author Steve Soltys
*/ */
@ -18,9 +20,9 @@ public class ContentProviderBackupConfiguration {
private final long backupSizeQuota; private final long backupSizeQuota;
private final String[] packages; private final Set<String> packages;
ContentProviderBackupConfiguration(Context context, Uri uri, String[] packages, long backupSizeQuota) { ContentProviderBackupConfiguration(Context context, Uri uri, Set<String> packages, long backupSizeQuota) {
this.context = context; this.context = context;
this.uri = uri; this.uri = uri;
this.packages = packages; this.packages = packages;
@ -40,7 +42,7 @@ public class ContentProviderBackupConfiguration {
} }
public int getPackageCount() { public int getPackageCount() {
return packages.length; return packages.size();
} }
} }

View file

@ -3,6 +3,8 @@ package com.stevesoltys.backup.transport.component.provider;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import java.util.Set;
/** /**
* @author Steve Soltys * @author Steve Soltys
*/ */
@ -12,7 +14,7 @@ public class ContentProviderBackupConfigurationBuilder {
private Uri outputUri; private Uri outputUri;
private String[] packages; private Set<String> packages;
private long backupSizeQuota = Long.MAX_VALUE; private long backupSizeQuota = Long.MAX_VALUE;
@ -41,7 +43,7 @@ public class ContentProviderBackupConfigurationBuilder {
return this; return this;
} }
public ContentProviderBackupConfigurationBuilder setPackages(String[] packages) { public ContentProviderBackupConfigurationBuilder setPackages(Set<String> packages) {
this.packages = packages; this.packages = packages;
return this; return this;
} }