Add initialization functions to ConfigurableBackupTransport

This commit is contained in:
Steve Soltys 2017-10-09 20:02:41 -04:00
parent 0a978c37e8
commit facb7029b9
5 changed files with 44 additions and 33 deletions

View file

@ -6,7 +6,6 @@ import android.widget.PopupWindow;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.stevesoltys.backup.R; import com.stevesoltys.backup.R;
import com.stevesoltys.backup.session.backup.BackupResult; import com.stevesoltys.backup.session.backup.BackupResult;
import com.stevesoltys.backup.session.backup.BackupSession; import com.stevesoltys.backup.session.backup.BackupSession;
@ -63,12 +62,11 @@ class BackupObserver implements BackupSessionObserver {
public void backupSessionCompleted(BackupSession backupSession, BackupResult backupResult) { public void backupSessionCompleted(BackupSession backupSession, BackupResult backupResult) {
ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport();
if (backupTransport.getRestoreComponent() == null || backupTransport.getBackupComponent() == null) { if (!backupTransport.isActive()) {
return; return;
} }
backupTransport.setBackupComponent(null); backupTransport.reset();
backupTransport.setRestoreComponent(null);
context.runOnUiThread(() -> { context.runOnUiThread(() -> {
if (backupResult == BackupResult.SUCCESS) { if (backupResult == BackupResult.SUCCESS) {

View file

@ -15,12 +15,13 @@ import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import android.widget.Toast; import android.widget.Toast;
import com.stevesoltys.backup.R; import com.stevesoltys.backup.R;
import com.stevesoltys.backup.session.BackupManagerController; import com.stevesoltys.backup.session.BackupManagerController;
import com.stevesoltys.backup.session.backup.BackupSession; import com.stevesoltys.backup.session.backup.BackupSession;
import com.stevesoltys.backup.transport.ConfigurableBackupTransport; import com.stevesoltys.backup.transport.ConfigurableBackupTransport;
import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; 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.ContentProviderBackupConfiguration;
import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder;
import com.stevesoltys.backup.transport.component.provider.backup.ContentProviderBackupComponent; import com.stevesoltys.backup.transport.component.provider.backup.ContentProviderBackupComponent;
@ -88,12 +89,13 @@ class CreateBackupActivityController {
private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) {
ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport();
if (backupTransport.getBackupComponent() != null || backupTransport.getRestoreComponent() != null) { if (backupTransport.isActive()) {
return false; return false;
} }
backupTransport.setBackupComponent(new ContentProviderBackupComponent(configuration)); BackupComponent backupComponent = new ContentProviderBackupComponent(configuration);
backupTransport.setRestoreComponent(new ContentProviderRestoreComponent(configuration)); RestoreComponent restoreComponent = new ContentProviderRestoreComponent(configuration);
backupTransport.initialize(backupComponent, restoreComponent);
return true; return true;
} }

View file

@ -15,16 +15,18 @@ import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import android.widget.Toast; import android.widget.Toast;
import com.stevesoltys.backup.R; import com.stevesoltys.backup.R;
import com.stevesoltys.backup.session.BackupManagerController; import com.stevesoltys.backup.session.BackupManagerController;
import com.stevesoltys.backup.session.restore.RestoreSession; import com.stevesoltys.backup.session.restore.RestoreSession;
import com.stevesoltys.backup.transport.ConfigurableBackupTransport; import com.stevesoltys.backup.transport.ConfigurableBackupTransport;
import com.stevesoltys.backup.transport.ConfigurableBackupTransportService; 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.ContentProviderBackupConfiguration;
import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder; import com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfigurationBuilder;
import com.stevesoltys.backup.transport.component.provider.backup.ContentProviderBackupComponent; import com.stevesoltys.backup.transport.component.provider.backup.ContentProviderBackupComponent;
import com.stevesoltys.backup.transport.component.provider.restore.ContentProviderRestoreComponent; import com.stevesoltys.backup.transport.component.provider.restore.ContentProviderRestoreComponent;
import libcore.io.IoUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -34,8 +36,6 @@ import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import libcore.io.IoUtils;
import static com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration.FULL_BACKUP_DIRECTORY; import static com.stevesoltys.backup.transport.component.provider.ContentProviderBackupConfiguration.FULL_BACKUP_DIRECTORY;
/** /**
@ -120,12 +120,13 @@ class RestoreBackupActivityController {
private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) {
ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport();
if(backupTransport.getBackupComponent() != null || backupTransport.getRestoreComponent() != null) { if(backupTransport.isActive()) {
return false; return false;
} }
backupTransport.setBackupComponent(new ContentProviderBackupComponent(configuration)); BackupComponent backupComponent = new ContentProviderBackupComponent(configuration);
backupTransport.setRestoreComponent(new ContentProviderRestoreComponent(configuration)); RestoreComponent restoreComponent = new ContentProviderRestoreComponent(configuration);
backupTransport.initialize(backupComponent, restoreComponent);
return true; return true;
} }

View file

@ -55,12 +55,11 @@ class RestoreObserver implements RestoreSessionObserver {
public void restoreSessionCompleted(RestoreResult restoreResult) { public void restoreSessionCompleted(RestoreResult restoreResult) {
ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport();
if(backupTransport.getRestoreComponent() == null || backupTransport.getBackupComponent() == null) { if(!backupTransport.isActive()) {
return; return;
} }
backupTransport.setBackupComponent(null); backupTransport.reset();
backupTransport.setRestoreComponent(null);
context.runOnUiThread(() -> { context.runOnUiThread(() -> {
if (restoreResult == RestoreResult.SUCCESS) { if (restoreResult == RestoreResult.SUCCESS) {

View file

@ -6,6 +6,7 @@ import android.app.backup.RestoreSet;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import com.android.internal.util.Preconditions;
import com.stevesoltys.backup.transport.component.BackupComponent; import com.stevesoltys.backup.transport.component.BackupComponent;
import com.stevesoltys.backup.transport.component.RestoreComponent; import com.stevesoltys.backup.transport.component.RestoreComponent;
@ -26,6 +27,32 @@ public class ConfigurableBackupTransport extends BackupTransport {
restoreComponent = null; 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 @Override
public String transportDirName() { public String transportDirName() {
return TRANSPORT_DIRECTORY_NAME; return TRANSPORT_DIRECTORY_NAME;
@ -141,20 +168,4 @@ public class ConfigurableBackupTransport extends BackupTransport {
public void finishRestore() { public void finishRestore() {
restoreComponent.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;
}
} }