From 023750be6e245877f9c08578efda89a778319b1a Mon Sep 17 00:00:00 2001 From: Steve Soltys Date: Thu, 21 Feb 2019 22:41:54 -0500 Subject: [PATCH] Add loading popup when fetching packages for backup or restore --- .../activity/backup/CreateBackupActivity.java | 3 +- .../CreateBackupActivityController.java | 64 +++++++++++------- .../restore/RestoreBackupActivity.java | 3 +- .../RestoreBackupActivityController.java | 65 ++++++++++++------- app/src/main/res/values/strings.xml | 2 + 5 files changed, 90 insertions(+), 47 deletions(-) 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 382469b9..40a6fc00 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 @@ -2,6 +2,7 @@ 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; @@ -49,7 +50,7 @@ public class CreateBackupActivity extends Activity implements View.OnClickListen contentUri = getIntent().getData(); controller = new CreateBackupActivityController(); - controller.populatePackageList(packageListView, this); + AsyncTask.execute(() -> controller.populatePackageList(packageListView, CreateBackupActivity.this)); } @Override 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 57de2aaf..7389b835 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 @@ -14,6 +14,7 @@ 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; @@ -26,10 +27,10 @@ import com.stevesoltys.backup.transport.component.provider.ContentProviderBackup import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; import com.stevesoltys.backup.transport.component.provider.ContentProviderRestoreComponent; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; /** * @author Steve Soltys @@ -38,7 +39,9 @@ class CreateBackupActivityController { private static final String TAG = CreateBackupActivityController.class.getName(); - private static final Set IGNORED_PACKAGES = Collections.singleton("com.android.providers.downloads.ui"); + private static final Set IGNORED_PACKAGES = Sets.newArraySet( + "com.android.providers.downloads.ui", "com.android.providers.media" + ); private final BackupManagerController backupManager; @@ -47,6 +50,17 @@ class CreateBackupActivityController { } void populatePackageList(ListView packageListView, CreateBackupActivity parent) { + AtomicReference popupWindow = new AtomicReference<>(); + + parent.runOnUiThread(() -> { + popupWindow.set(showLoadingPopupWindow(parent)); + TextView textView = popupWindow.get().getContentView().findViewById(R.id.popup_text_view); + textView.setText(R.string.loading_packages); + + View popupWindowButton = popupWindow.get().getContentView().findViewById(R.id.popup_cancel_button); + popupWindowButton.setOnClickListener(view -> parent.finish()); + }); + List eligiblePackageList = new LinkedList<>(); try { @@ -57,10 +71,29 @@ class CreateBackupActivityController { Log.e(TAG, "Error while obtaining package list: ", e); } + parent.runOnUiThread(() -> { + if (popupWindow.get() != null) { + popupWindow.get().dismiss(); + } - packageListView.setOnItemClickListener(parent); - packageListView.setAdapter(new ArrayAdapter<>(parent, R.layout.checked_list_item, eligiblePackageList)); - packageListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); + packageListView.setOnItemClickListener(parent); + packageListView.setAdapter(new ArrayAdapter<>(parent, R.layout.checked_list_item, eligiblePackageList)); + packageListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); + }); + } + + 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) { @@ -68,8 +101,8 @@ class CreateBackupActivityController { passwordTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); new AlertDialog.Builder(parent) - .setMessage("Please enter a password.\n" + - "You'll need this to restore your backup, so write it down!") + .setTitle("Enter a password") + .setMessage("You'll need this to restore your backup, so write it down!") .setView(passwordTextView) .setPositiveButton("Set password", (dialog, button) -> @@ -86,7 +119,7 @@ class CreateBackupActivityController { passwordTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); new AlertDialog.Builder(parent) - .setMessage("Please confirm your password.") + .setTitle("Confirm password") .setView(passwordTextView) .setPositiveButton("Confirm", (dialog, button) -> { @@ -128,7 +161,7 @@ class CreateBackupActivityController { return; } - PopupWindow popupWindow = buildStatusPopupWindow(parent); + PopupWindow popupWindow = showLoadingPopupWindow(parent); BackupObserver backupObserver = new BackupObserver(parent, popupWindow); BackupSession backupSession = backupManager.backup(backupObserver, selectedPackages); @@ -155,17 +188,4 @@ class CreateBackupActivityController { backupTransport.initialize(backupComponent, restoreComponent); return true; } - - private PopupWindow buildStatusPopupWindow(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); - return popupWindow; - } } 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 aa53437d..e16f792f 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 @@ -2,6 +2,7 @@ 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; @@ -49,7 +50,7 @@ public class RestoreBackupActivity extends Activity implements View.OnClickListe contentUri = getIntent().getData(); controller = new RestoreBackupActivityController(); - controller.populatePackageList(packageListView, contentUri, this); + AsyncTask.execute(() -> controller.populatePackageList(packageListView, contentUri, this)); } @Override 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 3d0e8d01..223c0879 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 @@ -22,9 +22,9 @@ 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.ContentProviderBackupComponent; import com.stevesoltys.backup.transport.component.provider.ContentProviderRestoreComponent; import libcore.io.IoUtils; @@ -34,6 +34,7 @@ import java.io.IOException; import java.util.LinkedList; import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -51,7 +52,19 @@ class RestoreBackupActivityController { } void populatePackageList(ListView packageListView, Uri contentUri, RestoreBackupActivity parent) { + AtomicReference popupWindow = new AtomicReference<>(); + + parent.runOnUiThread(() -> { + popupWindow.set(showLoadingPopupWindow(parent)); + TextView textView = popupWindow.get().getContentView().findViewById(R.id.popup_text_view); + textView.setText(R.string.loading_backup); + + View popupWindowButton = popupWindow.get().getContentView().findViewById(R.id.popup_cancel_button); + popupWindowButton.setOnClickListener(view -> parent.finish()); + }); + List eligiblePackageList = new LinkedList<>(); + try { eligiblePackageList.addAll(getEligiblePackages(contentUri, parent)); @@ -59,9 +72,29 @@ class RestoreBackupActivityController { Log.e(TAG, "Error while obtaining package list: ", e); } - packageListView.setOnItemClickListener(parent); - packageListView.setAdapter(new ArrayAdapter<>(parent, R.layout.checked_list_item, eligiblePackageList)); - packageListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); + parent.runOnUiThread(() -> { + if (popupWindow.get() != null) { + popupWindow.get().dismiss(); + } + + packageListView.setOnItemClickListener(parent); + packageListView.setAdapter(new ArrayAdapter<>(parent, R.layout.checked_list_item, eligiblePackageList)); + packageListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); + }); + } + + 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 { @@ -93,8 +126,8 @@ class RestoreBackupActivityController { passwordTextView.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); new AlertDialog.Builder(parent) - .setMessage("Please enter a password.\n" + - "If you didn't enter one while creating the backup, you can leave this blank.") + .setTitle("Enter a password") + .setMessage("If you didn't enter one while creating the backup, you can leave this blank.") .setView(passwordTextView) .setPositiveButton("Confirm", (dialog, button) -> @@ -103,7 +136,6 @@ class RestoreBackupActivityController { .show(); } - private void restorePackages(Set selectedPackages, Uri contentUri, Activity parent, String password) { try { ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder(). @@ -115,12 +147,12 @@ class RestoreBackupActivityController { boolean success = initializeBackupTransport(backupConfiguration); - if(!success) { + if (!success) { Toast.makeText(parent, R.string.restore_in_progress, Toast.LENGTH_LONG).show(); return; } - PopupWindow popupWindow = buildPopupWindow(parent); + PopupWindow popupWindow = showLoadingPopupWindow(parent); RestoreObserver restoreObserver = new RestoreObserver(parent, popupWindow, selectedPackages.size()); RestoreSession restoreSession = backupManager.restore(restoreObserver, selectedPackages); @@ -136,7 +168,7 @@ class RestoreBackupActivityController { private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); - if(backupTransport.isActive()) { + if (backupTransport.isActive()) { return false; } @@ -145,17 +177,4 @@ class RestoreBackupActivityController { backupTransport.initialize(backupComponent, restoreComponent); return true; } - - private PopupWindow buildPopupWindow(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); - return popupWindow; - } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8e512a59..3be75a51 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -19,5 +19,7 @@ Cancel Select all + Loading backup… + Loading packages…