From 828f257426471dccef321b0d59820b74a87df39b Mon Sep 17 00:00:00 2001 From: Steve Soltys Date: Fri, 1 Mar 2019 23:34:04 -0500 Subject: [PATCH] Clean up package and class structure significantly 1. Add service layer for backup / restore operations. 2. Reduce amount of duplicate code in activity classes. --- .../backup/activity/PackageListActivity.java | 50 ++++++++++ .../backup/activity/PopupWindowUtil.java | 32 +++++++ .../backup/BackupPopupWindowListener.java | 18 ++-- .../activity/backup/CreateBackupActivity.java | 54 +---------- .../CreateBackupActivityController.java | 92 ++----------------- .../restore/RestoreBackupActivity.java | 54 +---------- .../RestoreBackupActivityController.java | 86 ++--------------- .../restore/RestorePopupWindowListener.java | 4 +- .../backup/service/PackageService.java | 42 +++++++++ .../TransportService.java} | 51 +++++----- .../backup/BackupObserver.java | 9 +- .../backup/service/backup/BackupService.java | 67 ++++++++++++++ .../restore/RestoreObserver.java | 2 +- .../service/restore/RestoreService.java | 59 ++++++++++++ 14 files changed, 313 insertions(+), 307 deletions(-) create mode 100644 app/src/main/java/com/stevesoltys/backup/activity/PackageListActivity.java create mode 100644 app/src/main/java/com/stevesoltys/backup/activity/PopupWindowUtil.java create mode 100644 app/src/main/java/com/stevesoltys/backup/service/PackageService.java rename app/src/main/java/com/stevesoltys/backup/{session/BackupManagerController.java => service/TransportService.java} (59%) rename app/src/main/java/com/stevesoltys/backup/{activity => service}/backup/BackupObserver.java (92%) create mode 100644 app/src/main/java/com/stevesoltys/backup/service/backup/BackupService.java rename app/src/main/java/com/stevesoltys/backup/{activity => service}/restore/RestoreObserver.java (98%) create mode 100644 app/src/main/java/com/stevesoltys/backup/service/restore/RestoreService.java diff --git a/app/src/main/java/com/stevesoltys/backup/activity/PackageListActivity.java b/app/src/main/java/com/stevesoltys/backup/activity/PackageListActivity.java new file mode 100644 index 00000000..fd488e17 --- /dev/null +++ b/app/src/main/java/com/stevesoltys/backup/activity/PackageListActivity.java @@ -0,0 +1,50 @@ +package com.stevesoltys.backup.activity; + +import android.app.Activity; +import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ListView; +import com.stevesoltys.backup.R; + +import java.util.Set; +import java.util.stream.IntStream; + +/** + * @author Steve Soltys + */ +public abstract class PackageListActivity extends Activity implements AdapterView.OnItemClickListener { + + protected ListView packageListView; + + protected Set selectedPackageList; + + public void onItemClick(AdapterView parent, View view, int position, long id) { + String clickedPackage = (String) packageListView.getItemAtPosition(position); + + if (!selectedPackageList.remove(clickedPackage)) { + selectedPackageList.add(clickedPackage); + packageListView.setItemChecked(position, true); + + } else { + packageListView.setItemChecked(position, false); + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + + if (item.getItemId() == R.id.action_select_all) { + + IntStream.range(0, packageListView.getCount()) + .forEach(position -> { + selectedPackageList.add((String) packageListView.getItemAtPosition(position)); + packageListView.setItemChecked(position, true); + }); + + return true; + } + + return super.onOptionsItemSelected(item); + } +} diff --git a/app/src/main/java/com/stevesoltys/backup/activity/PopupWindowUtil.java b/app/src/main/java/com/stevesoltys/backup/activity/PopupWindowUtil.java new file mode 100644 index 00000000..b4b6bdc6 --- /dev/null +++ b/app/src/main/java/com/stevesoltys/backup/activity/PopupWindowUtil.java @@ -0,0 +1,32 @@ +package com.stevesoltys.backup.activity; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.PopupWindow; +import com.stevesoltys.backup.R; + +/** + * @author Steve Soltys + */ +public class PopupWindowUtil { + + public static PopupWindow showLoadingPopupWindow(Activity parent) { + LayoutInflater inflater = (LayoutInflater) parent.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + ViewGroup popupViewGroup = parent.findViewById(R.id.popup_layout); + View popupView = inflater.inflate(R.layout.progress_popup_window, popupViewGroup); + + PopupWindow popupWindow = new PopupWindow(popupView, 750, 350, true); + popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); + popupWindow.setElevation(10); + popupWindow.setFocusable(false); + popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); + popupWindow.setOutsideTouchable(false); + return popupWindow; + } +} diff --git a/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupPopupWindowListener.java b/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupPopupWindowListener.java index aa702dfd..14ea5be8 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupPopupWindowListener.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupPopupWindowListener.java @@ -4,7 +4,6 @@ import android.os.RemoteException; import android.util.Log; import android.view.View; import android.widget.Button; - import com.stevesoltys.backup.R; import com.stevesoltys.backup.session.backup.BackupResult; import com.stevesoltys.backup.session.backup.BackupSession; @@ -12,7 +11,7 @@ import com.stevesoltys.backup.session.backup.BackupSession; /** * @author Steve Soltys */ -class BackupPopupWindowListener implements Button.OnClickListener { +public class BackupPopupWindowListener implements Button.OnClickListener { private static final String TAG = BackupPopupWindowListener.class.getName(); @@ -26,16 +25,13 @@ class BackupPopupWindowListener implements Button.OnClickListener { public void onClick(View view) { int viewId = view.getId(); - switch (viewId) { + if (viewId == R.id.popup_cancel_button) { + try { + backupSession.stop(BackupResult.CANCELLED); - case R.id.popup_cancel_button: - try { - backupSession.stop(BackupResult.CANCELLED); - - } catch (RemoteException e) { - Log.e(TAG, "Error cancelling backup session: ", e); - } - break; + } catch (RemoteException e) { + Log.e(TAG, "Error cancelling backup session: ", e); + } } } } 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 40a6fc00..c3c1bb0a 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 @@ -1,40 +1,28 @@ package com.stevesoltys.backup.activity.backup; -import android.app.Activity; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; -import android.view.MenuItem; import android.view.View; -import android.widget.AdapterView; -import android.widget.ListView; import com.stevesoltys.backup.R; +import com.stevesoltys.backup.activity.PackageListActivity; import java.util.HashSet; -import java.util.Set; -import java.util.stream.IntStream; -public class CreateBackupActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener { +public class CreateBackupActivity extends PackageListActivity implements View.OnClickListener { private CreateBackupActivityController controller; - private ListView packageListView; - - private Set selectedPackageList; - private Uri contentUri; @Override public void onClick(View view) { int viewId = view.getId(); - switch (viewId) { - - case R.id.create_confirm_button: - controller.showEnterPasswordAlert(selectedPackageList, contentUri, this); - break; + if (viewId == R.id.create_confirm_button) { + controller.showEnterPasswordAlert(selectedPackageList, contentUri, this); } } @@ -59,38 +47,4 @@ public class CreateBackupActivity extends Activity implements View.OnClickListen inflater.inflate(R.menu.backup_menu, menu); return true; } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - - case R.id.action_select_all: - - IntStream.range(0, packageListView.getCount()) - .forEach(position -> { - selectedPackageList.add((String) packageListView.getItemAtPosition(position)); - packageListView.setItemChecked(position, true); - }); - - return true; - - default: - return super.onOptionsItemSelected(item); - - } - } - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - String clickedPackage = (String) packageListView.getItemAtPosition(position); - - if (!selectedPackageList.remove(clickedPackage)) { - selectedPackageList.add(clickedPackage); - packageListView.setItemChecked(position, true); - - } else { - packageListView.setItemChecked(position, false); - } - } } 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 80a72ac7..1ebc4e4c 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 @@ -2,30 +2,17 @@ package com.stevesoltys.backup.activity.backup; import android.app.Activity; import android.app.AlertDialog; -import android.content.Context; -import android.graphics.Color; -import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.RemoteException; import android.text.InputType; import android.util.Log; -import android.view.Gravity; -import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; import android.widget.*; import com.google.android.collect.Sets; import com.stevesoltys.backup.R; -import com.stevesoltys.backup.session.BackupManagerController; -import com.stevesoltys.backup.session.backup.BackupSession; -import com.stevesoltys.backup.transport.ConfigurableBackupTransport; -import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; -import com.stevesoltys.backup.transport.component.BackupComponent; -import com.stevesoltys.backup.transport.component.RestoreComponent; -import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupComponent; -import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration; -import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; -import com.stevesoltys.backup.transport.component.provider.ContentProviderRestoreComponent; +import com.stevesoltys.backup.activity.PopupWindowUtil; +import com.stevesoltys.backup.service.backup.BackupService; +import com.stevesoltys.backup.service.PackageService; import java.util.LinkedList; import java.util.List; @@ -44,17 +31,15 @@ class CreateBackupActivityController { "com.stevesoltys.backup" ); - private final BackupManagerController backupManager; + private final BackupService backupService = new BackupService(); - CreateBackupActivityController() { - backupManager = new BackupManagerController(); - } + private final PackageService packageService = new PackageService(); void populatePackageList(ListView packageListView, CreateBackupActivity parent) { AtomicReference popupWindow = new AtomicReference<>(); parent.runOnUiThread(() -> { - popupWindow.set(showLoadingPopupWindow(parent)); + popupWindow.set(PopupWindowUtil.showLoadingPopupWindow(parent)); TextView textView = popupWindow.get().getContentView().findViewById(R.id.popup_text_view); textView.setText(R.string.loading_packages); @@ -65,7 +50,7 @@ class CreateBackupActivityController { List eligiblePackageList = new LinkedList<>(); try { - eligiblePackageList.addAll(backupManager.getEligiblePackages()); + eligiblePackageList.addAll(packageService.getEligiblePackages()); eligiblePackageList.removeAll(IGNORED_PACKAGES); } catch (RemoteException e) { @@ -83,20 +68,6 @@ class CreateBackupActivityController { }); } - private PopupWindow showLoadingPopupWindow(Activity parent) { - LayoutInflater inflater = (LayoutInflater) parent.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - ViewGroup popupViewGroup = parent.findViewById(R.id.popup_layout); - View popupView = inflater.inflate(R.layout.progress_popup_window, popupViewGroup); - - PopupWindow popupWindow = new PopupWindow(popupView, 750, 350, true); - popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); - popupWindow.setElevation(10); - popupWindow.setFocusable(false); - popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); - popupWindow.setOutsideTouchable(false); - return popupWindow; - } - void showEnterPasswordAlert(Set selectedPackages, Uri contentUri, Activity parent) { final EditText passwordTextView = new EditText(parent); passwordTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); @@ -127,7 +98,7 @@ class CreateBackupActivityController { String password = passwordTextView.getText().toString(); if (originalPassword.equals(password)) { - backupPackages(selectedPackages, contentUri, parent, password); + backupService.backupPackageData(selectedPackages, contentUri, parent, password); } else { new AlertDialog.Builder(parent) @@ -142,51 +113,4 @@ class CreateBackupActivityController { .setNegativeButton("Cancel", (dialog, button) -> dialog.cancel()) .show(); } - - private void backupPackages(Set selectedPackages, Uri contentUri, Activity parent, - String selectedPassword) { - try { - selectedPackages.add("@pm@"); - - ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder() - .setContext(parent) - .setOutputUri(contentUri) - .setPackages(selectedPackages) - .setPassword(selectedPassword) - .build(); - - boolean success = initializeBackupTransport(backupConfiguration); - - if (!success) { - Toast.makeText(parent, R.string.backup_in_progress, Toast.LENGTH_LONG).show(); - return; - } - - PopupWindow popupWindow = showLoadingPopupWindow(parent); - BackupObserver backupObserver = new BackupObserver(parent, popupWindow); - BackupSession backupSession = backupManager.backup(backupObserver, selectedPackages); - - View popupWindowButton = popupWindow.getContentView().findViewById(R.id.popup_cancel_button); - popupWindowButton.setOnClickListener(new BackupPopupWindowListener(backupSession)); - - TextView textView = popupWindow.getContentView().findViewById(R.id.popup_text_view); - textView.setText(R.string.initializing); - - } catch (Exception e) { - Log.e(TAG, "Error while running backup: ", e); - } - } - - private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { - ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); - - if (backupTransport.isActive()) { - return false; - } - - BackupComponent backupComponent = new ContentProviderBackupComponent(configuration); - RestoreComponent restoreComponent = new ContentProviderRestoreComponent(configuration); - backupTransport.initialize(backupComponent, restoreComponent); - return true; - } } 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 e16f792f..4d206f57 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 @@ -1,40 +1,28 @@ package com.stevesoltys.backup.activity.restore; -import android.app.Activity; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; -import android.view.MenuItem; import android.view.View; -import android.widget.AdapterView; -import android.widget.ListView; import com.stevesoltys.backup.R; +import com.stevesoltys.backup.activity.PackageListActivity; import java.util.HashSet; -import java.util.Set; -import java.util.stream.IntStream; -public class RestoreBackupActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener { +public class RestoreBackupActivity extends PackageListActivity implements View.OnClickListener { private RestoreBackupActivityController controller; - private ListView packageListView; - - private Set selectedPackageList; - private Uri contentUri; @Override public void onClick(View view) { int viewId = view.getId(); - switch (viewId) { - - case R.id.restore_confirm_button: - controller.showEnterPasswordAlert(selectedPackageList, contentUri, this); - break; + if (viewId == R.id.restore_confirm_button) { + controller.showEnterPasswordAlert(selectedPackageList, contentUri, this); } } @@ -59,38 +47,4 @@ public class RestoreBackupActivity extends Activity implements View.OnClickListe inflater.inflate(R.menu.backup_menu, menu); return true; } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - - switch (item.getItemId()) { - - case R.id.action_select_all: - - IntStream.range(0, packageListView.getCount()) - .forEach(position -> { - selectedPackageList.add((String) packageListView.getItemAtPosition(position)); - packageListView.setItemChecked(position, true); - }); - - return true; - - default: - return super.onOptionsItemSelected(item); - - } - } - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - String clickedPackage = (String) packageListView.getItemAtPosition(position); - - if (!selectedPackageList.remove(clickedPackage)) { - selectedPackageList.add(clickedPackage); - packageListView.setItemChecked(position, true); - - } else { - packageListView.setItemChecked(position, false); - } - } } 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 223c0879..6370d7e7 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 @@ -2,30 +2,16 @@ package com.stevesoltys.backup.activity.restore; import android.app.Activity; import android.app.AlertDialog; -import android.content.Context; -import android.graphics.Color; -import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.ParcelFileDescriptor; -import android.os.RemoteException; import android.text.InputType; import android.util.Log; -import android.view.Gravity; -import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; import android.widget.*; import com.stevesoltys.backup.R; -import com.stevesoltys.backup.session.BackupManagerController; -import com.stevesoltys.backup.session.restore.RestoreSession; -import com.stevesoltys.backup.transport.ConfigurableBackupTransport; -import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; -import com.stevesoltys.backup.transport.component.BackupComponent; -import com.stevesoltys.backup.transport.component.RestoreComponent; -import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupComponent; -import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration; +import com.stevesoltys.backup.activity.PopupWindowUtil; +import com.stevesoltys.backup.service.restore.RestoreService; import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; -import com.stevesoltys.backup.transport.component.provider.ContentProviderRestoreComponent; import libcore.io.IoUtils; import java.io.File; @@ -45,17 +31,13 @@ class RestoreBackupActivityController { private static final String TAG = RestoreBackupActivityController.class.getName(); - private final BackupManagerController backupManager; - - RestoreBackupActivityController() { - backupManager = new BackupManagerController(); - } + private final RestoreService restoreService = new RestoreService(); void populatePackageList(ListView packageListView, Uri contentUri, RestoreBackupActivity parent) { AtomicReference popupWindow = new AtomicReference<>(); parent.runOnUiThread(() -> { - popupWindow.set(showLoadingPopupWindow(parent)); + popupWindow.set(PopupWindowUtil.showLoadingPopupWindow(parent)); TextView textView = popupWindow.get().getContentView().findViewById(R.id.popup_text_view); textView.setText(R.string.loading_backup); @@ -83,20 +65,6 @@ class RestoreBackupActivityController { }); } - private PopupWindow showLoadingPopupWindow(Activity parent) { - LayoutInflater inflater = (LayoutInflater) parent.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - ViewGroup popupViewGroup = parent.findViewById(R.id.popup_layout); - View popupView = inflater.inflate(R.layout.progress_popup_window, popupViewGroup); - - PopupWindow popupWindow = new PopupWindow(popupView, 750, 350, true); - popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); - popupWindow.setElevation(10); - popupWindow.setFocusable(false); - popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); - popupWindow.setOutsideTouchable(false); - return popupWindow; - } - private List getEligiblePackages(Uri contentUri, Activity context) throws IOException { List results = new LinkedList<>(); @@ -131,50 +99,10 @@ class RestoreBackupActivityController { .setView(passwordTextView) .setPositiveButton("Confirm", (dialog, button) -> - restorePackages(selectedPackages, contentUri, parent, passwordTextView.getText().toString())) + restoreService.restorePackages(selectedPackages, contentUri, parent, + passwordTextView.getText().toString())) + .setNegativeButton("Cancel", (dialog, button) -> dialog.cancel()) .show(); } - - private void restorePackages(Set selectedPackages, Uri contentUri, Activity parent, String password) { - try { - ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder(). - setContext(parent) - .setOutputUri(contentUri) - .setPackages(selectedPackages) - .setPassword(password) - .build(); - - boolean success = initializeBackupTransport(backupConfiguration); - - if (!success) { - Toast.makeText(parent, R.string.restore_in_progress, Toast.LENGTH_LONG).show(); - return; - } - - PopupWindow popupWindow = showLoadingPopupWindow(parent); - 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 (RemoteException e) { - Log.e(TAG, "Error while running restore: ", e); - } - } - - private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { - ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); - - if (backupTransport.isActive()) { - return false; - } - - BackupComponent backupComponent = new ContentProviderBackupComponent(configuration); - RestoreComponent restoreComponent = new ContentProviderRestoreComponent(configuration); - backupTransport.initialize(backupComponent, restoreComponent); - return true; - } } diff --git a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestorePopupWindowListener.java b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestorePopupWindowListener.java index 69397dfc..7e6c04f9 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestorePopupWindowListener.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestorePopupWindowListener.java @@ -12,13 +12,13 @@ import com.stevesoltys.backup.session.restore.RestoreSession; /** * @author Steve Soltys */ -class RestorePopupWindowListener implements Button.OnClickListener { +public class RestorePopupWindowListener implements Button.OnClickListener { private static final String TAG = RestorePopupWindowListener.class.getName(); private final RestoreSession restoreSession; - RestorePopupWindowListener(RestoreSession restoreSession) { + public RestorePopupWindowListener(RestoreSession restoreSession) { this.restoreSession = restoreSession; } diff --git a/app/src/main/java/com/stevesoltys/backup/service/PackageService.java b/app/src/main/java/com/stevesoltys/backup/service/PackageService.java new file mode 100644 index 00000000..e64f958d --- /dev/null +++ b/app/src/main/java/com/stevesoltys/backup/service/PackageService.java @@ -0,0 +1,42 @@ +package com.stevesoltys.backup.service; + +import android.app.backup.IBackupManager; +import android.content.pm.IPackageManager; +import android.content.pm.PackageInfo; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.os.UserHandle; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Steve Soltys + */ +public class PackageService { + + private final IBackupManager backupManager; + + private final IPackageManager packageManager; + + 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<>(); + List packages = packageManager.getInstalledPackages(0, UserHandle.USER_SYSTEM).getList(); + + if (packages != null) { + for (PackageInfo packageInfo : packages) { + + if (backupManager.isAppEligibleForBackup(packageInfo.packageName)) { + results.add(packageInfo.packageName); + } + } + } + + return results; + } +} diff --git a/app/src/main/java/com/stevesoltys/backup/session/BackupManagerController.java b/app/src/main/java/com/stevesoltys/backup/service/TransportService.java similarity index 59% rename from app/src/main/java/com/stevesoltys/backup/session/BackupManagerController.java rename to app/src/main/java/com/stevesoltys/backup/service/TransportService.java index edc884d9..934005b2 100644 --- a/app/src/main/java/com/stevesoltys/backup/session/BackupManagerController.java +++ b/app/src/main/java/com/stevesoltys/backup/service/TransportService.java @@ -1,35 +1,46 @@ -package com.stevesoltys.backup.session; +package com.stevesoltys.backup.service; import android.app.backup.IBackupManager; -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; import com.stevesoltys.backup.session.restore.RestoreSessionObserver; +import com.stevesoltys.backup.transport.ConfigurableBackupTransport; +import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; +import com.stevesoltys.backup.transport.component.BackupComponent; +import com.stevesoltys.backup.transport.component.RestoreComponent; +import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupComponent; +import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration; +import com.stevesoltys.backup.transport.component.provider.ContentProviderRestoreComponent; -import java.util.ArrayList; -import java.util.List; import java.util.Set; -import static android.os.UserHandle.USER_SYSTEM; - /** * @author Steve Soltys */ -public class BackupManagerController { +public class TransportService { private static final String BACKUP_TRANSPORT = "com.stevesoltys.backup.transport.ConfigurableBackupTransport"; private final IBackupManager backupManager; - private final IPackageManager packageManager; - - public BackupManagerController() { + public TransportService() { backupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup")); - packageManager = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); + } + + public boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { + ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); + + if (backupTransport.isActive()) { + return false; + } + + BackupComponent backupComponent = new ContentProviderBackupComponent(configuration); + RestoreComponent restoreComponent = new ContentProviderRestoreComponent(configuration); + backupTransport.initialize(backupComponent, restoreComponent); + return true; } public BackupSession backup(BackupSessionObserver observer, Set packages) throws RemoteException { @@ -61,20 +72,4 @@ public class BackupManagerController { restoreSession.start(); return restoreSession; } - - public List getEligiblePackages() throws RemoteException { - List results = new ArrayList<>(); - List packages = packageManager.getInstalledPackages(0, USER_SYSTEM).getList(); - - if (packages != null) { - for (PackageInfo packageInfo : packages) { - - if (backupManager.isAppEligibleForBackup(packageInfo.packageName)) { - results.add(packageInfo.packageName); - } - } - } - - return results; - } } diff --git a/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupObserver.java b/app/src/main/java/com/stevesoltys/backup/service/backup/BackupObserver.java similarity index 92% rename from app/src/main/java/com/stevesoltys/backup/activity/backup/BackupObserver.java rename to app/src/main/java/com/stevesoltys/backup/service/backup/BackupObserver.java index 1fa12e1a..abd2d075 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupObserver.java +++ b/app/src/main/java/com/stevesoltys/backup/service/backup/BackupObserver.java @@ -1,4 +1,4 @@ -package com.stevesoltys.backup.activity.backup; +package com.stevesoltys.backup.service.backup; import android.app.Activity; import android.app.backup.BackupProgress; @@ -13,6 +13,8 @@ import com.stevesoltys.backup.session.backup.BackupSessionObserver; import com.stevesoltys.backup.transport.ConfigurableBackupTransport; import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; +import java.net.URI; + /** * @author Steve Soltys */ @@ -22,9 +24,12 @@ class BackupObserver implements BackupSessionObserver { private final PopupWindow popupWindow; - BackupObserver(Activity context, PopupWindow popupWindow) { + private final URI contentUri; + + BackupObserver(Activity context, PopupWindow popupWindow, URI contentUri) { this.context = context; this.popupWindow = popupWindow; + this.contentUri = contentUri; } @Override diff --git a/app/src/main/java/com/stevesoltys/backup/service/backup/BackupService.java b/app/src/main/java/com/stevesoltys/backup/service/backup/BackupService.java new file mode 100644 index 00000000..dc5604e7 --- /dev/null +++ b/app/src/main/java/com/stevesoltys/backup/service/backup/BackupService.java @@ -0,0 +1,67 @@ +package com.stevesoltys.backup.service.backup; + +import android.app.Activity; +import android.net.Uri; +import android.util.Log; +import android.view.View; +import android.widget.PopupWindow; +import android.widget.TextView; +import android.widget.Toast; +import com.stevesoltys.backup.R; +import com.stevesoltys.backup.activity.backup.BackupPopupWindowListener; +import com.stevesoltys.backup.activity.PopupWindowUtil; +import com.stevesoltys.backup.service.TransportService; +import com.stevesoltys.backup.session.backup.BackupSession; +import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration; +import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; + +import java.net.URI; +import java.util.Set; + +/** + * @author Steve Soltys + */ +public class BackupService { + + private static final String TAG = BackupService.class.getName(); + + private final TransportService transportService = new TransportService(); + + public void backupPackageData(Set selectedPackages, Uri contentUri, Activity parent, + String selectedPassword) { + try { + selectedPackages.add("@pm@"); + + ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder() + .setContext(parent) + .setOutputUri(contentUri) + .setPackages(selectedPackages) + .setPassword(selectedPassword) + .build(); + + boolean success = transportService.initializeBackupTransport(backupConfiguration); + + if (!success) { + Toast.makeText(parent, R.string.backup_in_progress, Toast.LENGTH_LONG).show(); + return; + } + + PopupWindow popupWindow = PopupWindowUtil.showLoadingPopupWindow(parent); + BackupObserver backupObserver = new BackupObserver(parent, popupWindow, new URI(contentUri.getPath())); + BackupSession backupSession = transportService.backup(backupObserver, selectedPackages); + + View popupWindowButton = popupWindow.getContentView().findViewById(R.id.popup_cancel_button); + popupWindowButton.setOnClickListener(new BackupPopupWindowListener(backupSession)); + + TextView textView = popupWindow.getContentView().findViewById(R.id.popup_text_view); + textView.setText(R.string.initializing); + + } catch (Exception e) { + Log.e(TAG, "Error while running backup: ", e); + } + } + + public void backupPackages() { + + } +} diff --git a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreObserver.java b/app/src/main/java/com/stevesoltys/backup/service/restore/RestoreObserver.java similarity index 98% rename from app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreObserver.java rename to app/src/main/java/com/stevesoltys/backup/service/restore/RestoreObserver.java index 7b7d4809..69caa690 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreObserver.java +++ b/app/src/main/java/com/stevesoltys/backup/service/restore/RestoreObserver.java @@ -1,4 +1,4 @@ -package com.stevesoltys.backup.activity.restore; +package com.stevesoltys.backup.service.restore; import android.app.Activity; import android.widget.PopupWindow; diff --git a/app/src/main/java/com/stevesoltys/backup/service/restore/RestoreService.java b/app/src/main/java/com/stevesoltys/backup/service/restore/RestoreService.java new file mode 100644 index 00000000..06e15c0b --- /dev/null +++ b/app/src/main/java/com/stevesoltys/backup/service/restore/RestoreService.java @@ -0,0 +1,59 @@ +package com.stevesoltys.backup.service.restore; + +import android.app.Activity; +import android.net.Uri; +import android.os.RemoteException; +import android.util.Log; +import android.view.View; +import android.widget.PopupWindow; +import android.widget.Toast; +import com.stevesoltys.backup.R; +import com.stevesoltys.backup.activity.PopupWindowUtil; +import com.stevesoltys.backup.activity.restore.RestorePopupWindowListener; +import com.stevesoltys.backup.service.TransportService; +import com.stevesoltys.backup.session.restore.RestoreSession; +import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration; +import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; + +import java.util.Set; + +/** + * @author Steve Soltys + */ +public class RestoreService { + + private static final String TAG = RestoreService.class.getName(); + + private final TransportService transportService = new TransportService(); + + public void restorePackages(Set selectedPackages, Uri contentUri, Activity parent, String password) { + try { + ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder(). + setContext(parent) + .setOutputUri(contentUri) + .setPackages(selectedPackages) + .setPassword(password) + .build(); + + boolean success = transportService.initializeBackupTransport(backupConfiguration); + + if (!success) { + Toast.makeText(parent, R.string.restore_in_progress, Toast.LENGTH_LONG).show(); + return; + } + + PopupWindow popupWindow = PopupWindowUtil.showLoadingPopupWindow(parent); + RestoreObserver restoreObserver = new RestoreObserver(parent, popupWindow, selectedPackages.size()); + RestoreSession restoreSession = transportService.restore(restoreObserver, selectedPackages); + + View popupWindowButton = popupWindow.getContentView().findViewById(R.id.popup_cancel_button); + + if (popupWindowButton != null) { + popupWindowButton.setOnClickListener(new RestorePopupWindowListener(restoreSession)); + } + + } catch (RemoteException e) { + Log.e(TAG, "Error while running restore: ", e); + } + } +}