From facb7029b9ab2f5fefe854a5c2a2c7e1afe1063c Mon Sep 17 00:00:00 2001 From: Steve Soltys Date: Mon, 9 Oct 2017 20:02:41 -0400 Subject: [PATCH] Add initialization functions to ConfigurableBackupTransport --- .../activity/backup/BackupObserver.java | 6 +-- .../CreateBackupActivityController.java | 10 +++-- .../RestoreBackupActivityController.java | 13 +++--- .../activity/restore/RestoreObserver.java | 5 +-- .../ConfigurableBackupTransport.java | 43 ++++++++++++------- 5 files changed, 44 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupObserver.java b/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupObserver.java index 1fc5ee90..1fa12e1a 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupObserver.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/backup/BackupObserver.java @@ -6,7 +6,6 @@ import android.widget.PopupWindow; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; - import com.stevesoltys.backup.R; import com.stevesoltys.backup.session.backup.BackupResult; import com.stevesoltys.backup.session.backup.BackupSession; @@ -63,12 +62,11 @@ class BackupObserver implements BackupSessionObserver { public void backupSessionCompleted(BackupSession backupSession, BackupResult backupResult) { ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); - if (backupTransport.getRestoreComponent() == null || backupTransport.getBackupComponent() == null) { + if (!backupTransport.isActive()) { return; } - backupTransport.setBackupComponent(null); - backupTransport.setRestoreComponent(null); + backupTransport.reset(); context.runOnUiThread(() -> { if (backupResult == BackupResult.SUCCESS) { 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 695e00d4..685adcc8 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 @@ -15,12 +15,13 @@ import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.Toast; - 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.ContentProviderBackupConfiguration; import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; import com.stevesoltys.backup.transport.component.provider.backup.ContentProviderBackupComponent; @@ -88,12 +89,13 @@ class CreateBackupActivityController { private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); - if (backupTransport.getBackupComponent() != null || backupTransport.getRestoreComponent() != null) { + if (backupTransport.isActive()) { return false; } - backupTransport.setBackupComponent(new ContentProviderBackupComponent(configuration)); - backupTransport.setRestoreComponent(new ContentProviderRestoreComponent(configuration)); + 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/RestoreBackupActivityController.java b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreBackupActivityController.java index fd861f51..23ed4dd2 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 @@ -15,16 +15,18 @@ import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.Toast; - 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.ContentProviderBackupConfiguration; import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; import com.stevesoltys.backup.transport.component.provider.backup.ContentProviderBackupComponent; import com.stevesoltys.backup.transport.component.provider.restore.ContentProviderRestoreComponent; +import libcore.io.IoUtils; import java.io.File; import java.io.FileInputStream; @@ -34,8 +36,6 @@ import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import libcore.io.IoUtils; - import static com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration.FULL_BACKUP_DIRECTORY; /** @@ -120,12 +120,13 @@ class RestoreBackupActivityController { private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); - if(backupTransport.getBackupComponent() != null || backupTransport.getRestoreComponent() != null) { + if(backupTransport.isActive()) { return false; } - backupTransport.setBackupComponent(new ContentProviderBackupComponent(configuration)); - backupTransport.setRestoreComponent(new ContentProviderRestoreComponent(configuration)); + 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/RestoreObserver.java b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreObserver.java index df536b4b..9fbe0bc8 100644 --- a/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreObserver.java +++ b/app/src/main/java/com/stevesoltys/backup/activity/restore/RestoreObserver.java @@ -55,12 +55,11 @@ class RestoreObserver implements RestoreSessionObserver { public void restoreSessionCompleted(RestoreResult restoreResult) { ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); - if(backupTransport.getRestoreComponent() == null || backupTransport.getBackupComponent() == null) { + if(!backupTransport.isActive()) { return; } - backupTransport.setBackupComponent(null); - backupTransport.setRestoreComponent(null); + backupTransport.reset(); context.runOnUiThread(() -> { if (restoreResult == RestoreResult.SUCCESS) { diff --git a/app/src/main/java/com/stevesoltys/backup/transport/ConfigurableBackupTransport.java b/app/src/main/java/com/stevesoltys/backup/transport/ConfigurableBackupTransport.java index 7051774c..675a9647 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/ConfigurableBackupTransport.java +++ b/app/src/main/java/com/stevesoltys/backup/transport/ConfigurableBackupTransport.java @@ -6,6 +6,7 @@ import android.app.backup.RestoreSet; import android.content.pm.PackageInfo; import android.os.ParcelFileDescriptor; +import com.android.internal.util.Preconditions; import com.stevesoltys.backup.transport.component.BackupComponent; import com.stevesoltys.backup.transport.component.RestoreComponent; @@ -26,6 +27,32 @@ public class ConfigurableBackupTransport extends BackupTransport { restoreComponent = null; } + public void initialize(BackupComponent backupComponent, RestoreComponent restoreComponent) { + Preconditions.checkNotNull(backupComponent); + Preconditions.checkNotNull(restoreComponent); + Preconditions.checkState(!isActive()); + + this.restoreComponent = restoreComponent; + this.backupComponent = backupComponent; + } + + public void reset() { + backupComponent = null; + restoreComponent = null; + } + + public boolean isActive() { + return backupComponent != null && restoreComponent != null; + } + + public BackupComponent getBackupComponent() { + return backupComponent; + } + + public RestoreComponent getRestoreComponent() { + return restoreComponent; + } + @Override public String transportDirName() { return TRANSPORT_DIRECTORY_NAME; @@ -141,20 +168,4 @@ public class ConfigurableBackupTransport extends BackupTransport { public void finishRestore() { restoreComponent.finishRestore(); } - - public BackupComponent getBackupComponent() { - return backupComponent; - } - - public void setBackupComponent(BackupComponent backupComponent) { - this.backupComponent = backupComponent; - } - - public RestoreComponent getRestoreComponent() { - return restoreComponent; - } - - public void setRestoreComponent(RestoreComponent restoreComponent) { - this.restoreComponent = restoreComponent; - } }