Clean up package and class structure significantly
1. Add service layer for backup / restore operations. 2. Reduce amount of duplicate code in activity classes.
This commit is contained in:
parent
0b5cc1a798
commit
828f257426
14 changed files with 313 additions and 307 deletions
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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> 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<String> 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<String> 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<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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> 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<String> getEligiblePackages(Uri contentUri, Activity context) throws IOException {
|
||||
List<String> 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<String> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<String> getEligiblePackages() throws RemoteException {
|
||||
List<String> results = new ArrayList<>();
|
||||
List<PackageInfo> 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;
|
||||
}
|
||||
}
|
|
@ -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<String> packages) throws RemoteException {
|
||||
|
@ -61,20 +72,4 @@ public class BackupManagerController {
|
|||
restoreSession.start();
|
||||
return restoreSession;
|
||||
}
|
||||
|
||||
public List<String> getEligiblePackages() throws RemoteException {
|
||||
List<String> results = new ArrayList<>();
|
||||
List<PackageInfo> 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;
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -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<String> 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() {
|
||||
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.stevesoltys.backup.activity.restore;
|
||||
package com.stevesoltys.backup.service.restore;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.widget.PopupWindow;
|
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue