Switch to builder pattern for ContentProviderBackupConfiguration

This commit is contained in:
Steve Soltys 2017-09-22 00:10:30 -04:00
parent 2497a94e4c
commit e27a8b308f
4 changed files with 49 additions and 14 deletions

View file

@ -61,11 +61,11 @@ class CreateBackupActivityController {
String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size() + 1]); String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size() + 1]);
selectedPackageArray[selectedPackageArray.length - 1] = "@pm@"; selectedPackageArray[selectedPackageArray.length - 1] = "@pm@";
ContentProviderBackupConfiguration backupConfiguration = ContentProviderBackupConfigurationBuilder. ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder()
buildDefaultConfiguration(parent, contentUri, selectedPackageArray.length); .setContext(parent).setOutputUri(contentUri).setPackages(selectedPackageArray).build();
boolean success = initializeBackupTransport(backupConfiguration); boolean success = initializeBackupTransport(backupConfiguration);
if(!success) { if (!success) {
Toast.makeText(parent, R.string.backup_in_progress, Toast.LENGTH_LONG).show(); Toast.makeText(parent, R.string.backup_in_progress, Toast.LENGTH_LONG).show();
return; return;
} }
@ -88,7 +88,7 @@ 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.getBackupComponent() != null || backupTransport.getRestoreComponent() != null) {
return false; return false;
} }

View file

@ -93,8 +93,8 @@ class RestoreBackupActivityController {
try { try {
String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size()]); String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size()]);
ContentProviderBackupConfiguration backupConfiguration = ContentProviderBackupConfigurationBuilder. ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder().
buildDefaultConfiguration(parent, contentUri, selectedPackageArray.length); setContext(parent).setOutputUri(contentUri).setPackages(selectedPackageArray).build();
boolean success = initializeBackupTransport(backupConfiguration); boolean success = initializeBackupTransport(backupConfiguration);
if(!success) { if(!success) {

View file

@ -18,13 +18,13 @@ public class ContentProviderBackupConfiguration {
private final long backupSizeQuota; private final long backupSizeQuota;
private final int packageCount; private final String[] packages;
public ContentProviderBackupConfiguration(Context context, Uri uri, long backupSizeQuota, int packageCount) { ContentProviderBackupConfiguration(Context context, Uri uri, String[] packages, long backupSizeQuota) {
this.context = context; this.context = context;
this.uri = uri; this.uri = uri;
this.packages = packages;
this.backupSizeQuota = backupSizeQuota; this.backupSizeQuota = backupSizeQuota;
this.packageCount = packageCount;
} }
public Context getContext() { public Context getContext() {
@ -40,7 +40,7 @@ public class ContentProviderBackupConfiguration {
} }
public int getPackageCount() { public int getPackageCount() {
return packageCount; return packages.length;
} }
} }

View file

@ -8,11 +8,46 @@ import android.net.Uri;
*/ */
public class ContentProviderBackupConfigurationBuilder { public class ContentProviderBackupConfigurationBuilder {
private static final long DEFAULT_BACKUP_SIZE_QUOTA = Long.MAX_VALUE; private Context context;
public static ContentProviderBackupConfiguration buildDefaultConfiguration(Context context, Uri outputUri, private Uri outputUri;
int packageCount) {
return new ContentProviderBackupConfiguration(context, outputUri, DEFAULT_BACKUP_SIZE_QUOTA, packageCount); private String[] packages;
private long backupSizeQuota = Long.MAX_VALUE;
public ContentProviderBackupConfiguration build() {
if(context == null) {
throw new IllegalArgumentException("Context must be set.");
} else if (outputUri == null) {
throw new IllegalArgumentException("Output URI must be set.");
} else if(packages == null) {
throw new IllegalArgumentException("Package list must be set.");
}
return new ContentProviderBackupConfiguration(context, outputUri, packages, backupSizeQuota);
} }
public ContentProviderBackupConfigurationBuilder setContext(Context context) {
this.context = context;
return this;
}
public ContentProviderBackupConfigurationBuilder setOutputUri(Uri outputUri) {
this.outputUri = outputUri;
return this;
}
public ContentProviderBackupConfigurationBuilder setPackages(String[] packages) {
this.packages = packages;
return this;
}
public ContentProviderBackupConfigurationBuilder setBackupSizeQuota(long backupSizeQuota) {
this.backupSizeQuota = backupSizeQuota;
return this;
}
} }