From e27a8b308ff7f7ac63d3023b9ff79c999cbd8985 Mon Sep 17 00:00:00 2001 From: Steve Soltys Date: Fri, 22 Sep 2017 00:10:30 -0400 Subject: [PATCH] Switch to builder pattern for ContentProviderBackupConfiguration --- .../CreateBackupActivityController.java | 8 ++-- .../RestoreBackupActivityController.java | 4 +- .../ContentProviderBackupConfiguration.java | 8 ++-- ...entProviderBackupConfigurationBuilder.java | 43 +++++++++++++++++-- 4 files changed, 49 insertions(+), 14 deletions(-) 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 ce4d80fc..695e00d4 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 @@ -61,11 +61,11 @@ class CreateBackupActivityController { String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size() + 1]); selectedPackageArray[selectedPackageArray.length - 1] = "@pm@"; - ContentProviderBackupConfiguration backupConfiguration = ContentProviderBackupConfigurationBuilder. - buildDefaultConfiguration(parent, contentUri, selectedPackageArray.length); + ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder() + .setContext(parent).setOutputUri(contentUri).setPackages(selectedPackageArray).build(); boolean success = initializeBackupTransport(backupConfiguration); - if(!success) { + if (!success) { Toast.makeText(parent, R.string.backup_in_progress, Toast.LENGTH_LONG).show(); return; } @@ -88,7 +88,7 @@ class CreateBackupActivityController { private boolean initializeBackupTransport(ContentProviderBackupConfiguration configuration) { ConfigurableBackupTransport backupTransport = ConfigurableBackupTransportService.getBackupTransport(); - if(backupTransport.getBackupComponent() != null || backupTransport.getRestoreComponent() != null) { + if (backupTransport.getBackupComponent() != null || backupTransport.getRestoreComponent() != null) { return false; } 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 b8239141..fd861f51 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 @@ -93,8 +93,8 @@ class RestoreBackupActivityController { try { String[] selectedPackageArray = selectedPackages.toArray(new String[selectedPackages.size()]); - ContentProviderBackupConfiguration backupConfiguration = ContentProviderBackupConfigurationBuilder. - buildDefaultConfiguration(parent, contentUri, selectedPackageArray.length); + ContentProviderBackupConfiguration backupConfiguration = new ContentProviderBackupConfigurationBuilder(). + setContext(parent).setOutputUri(contentUri).setPackages(selectedPackageArray).build(); boolean success = initializeBackupTransport(backupConfiguration); if(!success) { diff --git a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfiguration.java b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfiguration.java index 92e30ab6..522c74c8 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfiguration.java +++ b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfiguration.java @@ -18,13 +18,13 @@ public class ContentProviderBackupConfiguration { 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.uri = uri; + this.packages = packages; this.backupSizeQuota = backupSizeQuota; - this.packageCount = packageCount; } public Context getContext() { @@ -40,7 +40,7 @@ public class ContentProviderBackupConfiguration { } public int getPackageCount() { - return packageCount; + return packages.length; } } diff --git a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfigurationBuilder.java b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfigurationBuilder.java index f7e12b95..7e7b8575 100644 --- a/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfigurationBuilder.java +++ b/app/src/main/java/com/stevesoltys/backup/transport/component/provider/ContentProviderBackupConfigurationBuilder.java @@ -8,11 +8,46 @@ import android.net.Uri; */ 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, - int packageCount) { - return new ContentProviderBackupConfiguration(context, outputUri, DEFAULT_BACKUP_SIZE_QUOTA, packageCount); + private Uri outputUri; + + 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; + } }