From eaa56c5eff6a9cae89030ff21dcd4b2d8986bbd7 Mon Sep 17 00:00:00 2001 From: Javier Santos Date: Tue, 23 Feb 2016 16:58:43 +0100 Subject: [PATCH] #6 Custom title, description and buttons for the dialog --- README.md | 17 ++- .../javiersantos/appupdater/AppUpdater.java | 143 +++++++++++++++++- .../appupdater/AppUpdaterUtils.java | 13 +- .../javiersantos/appupdater/UtilsAsync.java | 55 +------ .../javiersantos/appupdater/UtilsDisplay.java | 6 +- 5 files changed, 169 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 21050649..b25657d0 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ repositories { And add the library to your module **build.gradle**: ```Javascript dependencies { - compile 'com.github.javiersantos:AppUpdater:1.1' + compile 'com.github.javiersantos:AppUpdater:1.2' } ``` @@ -62,12 +62,14 @@ Use the builder and add following: .setDisplay(Display.SNACKBAR) .setDisplay(Display.NOTIFICATION) ``` + ```Java // (Optional) Provide a duration for the Snackbars. // Default: Duration.NORMAL .setDuration(Duration.NORMAL) .setDuration(Duration.INDEFINITE) ``` + ```Java // (Optional) Provide a source for the updates. // Default: UpdateFrom.GOOGLE_PLAY @@ -76,21 +78,34 @@ Use the builder and add following: .setUpdateFrom(UpdateFrom.AMAZON) .setUpdateFrom(UpdateFrom.FDROID) ``` + ```Java // (Required for GITHUB, optional otherwise) Provide the GitHub user and repo where releases are available. .setGitHubUserAndRepo("javiersantos", "AppUpdater") ``` + ```Java // (Optional) Updates will be displayed only every X times the app ascertains that a new update is available. // Default: 1 (Always) .showEvery(5) ``` + ```Java // (Optional) Show dialog, snackbar or notification although there aren't updates. // Default: false .showAppUpdated(true) ``` +```Java +// Customize the dialog title, description and buttons +.setDialogTitleWhenUpdateAvailable("Update available") +.setDialogDescriptionWhenUpdateAvailable("Check out the latest version available of my app!") +.setDialogButtonUpdate("Update now?") +.setDialogButtonDoNotShowAgain("Huh, not interested") +.setDialogTitleWhenUpdateNotAvailable("Update not available") +.setDialogDescriptionWhenUpdateNotAvailable("No update available. Check for updates again later!") +``` + ## Other features ### Get the latest update and compare with the installed one (asynchronous) ```Java diff --git a/library/src/main/java/com/github/javiersantos/appupdater/AppUpdater.java b/library/src/main/java/com/github/javiersantos/appupdater/AppUpdater.java index 20b6d612..3dcead5a 100644 --- a/library/src/main/java/com/github/javiersantos/appupdater/AppUpdater.java +++ b/library/src/main/java/com/github/javiersantos/appupdater/AppUpdater.java @@ -1,6 +1,7 @@ package com.github.javiersantos.appupdater; import android.content.Context; +import android.support.annotation.NonNull; import com.github.javiersantos.appupdater.enums.Display; import com.github.javiersantos.appupdater.enums.Duration; @@ -9,20 +10,30 @@ public class AppUpdater { private Context context; + private LibraryPreferences libraryPreferences; private Display display; private UpdateFrom updateFrom; private Duration duration; private GitHub gitHub; private Integer showEvery; private Boolean showAppUpdated; + private String titleUpdate, descriptionUpdate, btnUpdate, btnDisable; // Update available + private String titleNoUpdate, descriptionNoUpdate; // Update not available public AppUpdater(Context context) { this.context = context; + this.libraryPreferences = new LibraryPreferences(context); this.display = Display.DIALOG; this.updateFrom = UpdateFrom.GOOGLE_PLAY; this.duration = Duration.NORMAL; this.showEvery = 1; this.showAppUpdated = false; + + // Dialog + this.titleUpdate = context.getResources().getString(R.string.appupdater_update_available); + this.titleNoUpdate = context.getResources().getString(R.string.appupdater_update_not_available); + this.btnUpdate = context.getResources().getString(R.string.appupdater_btn_update); + this.btnDisable = context.getResources().getString(R.string.appupdater_btn_disable); } /** @@ -68,7 +79,7 @@ public AppUpdater setDuration(Duration duration) { * @param repo GitHub repository * @return this */ - public AppUpdater setGitHubUserAndRepo(String user, String repo) { + public AppUpdater setGitHubUserAndRepo(@NonNull String user, @NonNull String repo) { this.gitHub = new GitHub(user, repo); return this; } @@ -95,6 +106,72 @@ public AppUpdater showAppUpdated(Boolean res) { return this; } + /** + * Set a custom title for the dialog when an update is available. + * + * @param title for the dialog + * @return this + */ + public AppUpdater setDialogTitleWhenUpdateAvailable(@NonNull String title) { + this.titleUpdate = title; + return this; + } + + /** + * Set a custom description for the dialog when an update is available. + * + * @param description for the dialog + * @return this + */ + public AppUpdater setDialogDescriptionWhenUpdateAvailable(@NonNull String description) { + this.descriptionUpdate = description; + return this; + } + + /** + * Set a custom title for the dialog when no update is available. + * + * @param title for the dialog + * @return this + */ + public AppUpdater setDialogTitleWhenUpdateNotAvailable(@NonNull String title) { + this.titleNoUpdate = title; + return this; + } + + /** + * Set a custom description for the dialog when no update is available. + * + * @param description for the dialog + * @return this + */ + public AppUpdater setDialogDescriptionWhenUpdateNotAvailable(@NonNull String description) { + this.descriptionNoUpdate = description; + return this; + } + + /** + * Set a custom "Update" button text when a new update is available. + * + * @param text for the update button + * @return this + */ + public AppUpdater setDialogButtonUpdate(@NonNull String text) { + this.btnUpdate = text; + return this; + } + + /** + * Set a custom "Don't show again" button text when a new update is available. + * + * @param text for the disable button + * @return this + */ + public AppUpdater setDialogButtonDoNotShowAgain(@NonNull String text) { + this.btnDisable = text; + return this; + } + /** * Execute AppUpdater in background. * @@ -102,20 +179,70 @@ public AppUpdater showAppUpdated(Boolean res) { * @deprecated use {@link #start()} instead */ public AppUpdater init() { - UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, showEvery, showAppUpdated, updateFrom, display, duration, gitHub, null); - latestAppVersion.execute(); + start(); return this; } /** * Execute AppUpdater in background. - * - * @return this */ - public AppUpdater start() { - UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, showEvery, showAppUpdated, updateFrom, display, duration, gitHub, null); + public void start() { + UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, false, updateFrom, gitHub, new LibraryListener() { + @Override + public void onSuccess(String version) { + if (UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), version)) { + Integer successfulChecks = libraryPreferences.getSuccessfulChecks(); + if (UtilsLibrary.isAbleToShow(successfulChecks, showEvery)) { + switch (display) { + case DIALOG: + UtilsDisplay.showUpdateAvailableDialog(context, titleUpdate, getDescriptionUpdate(context, version), btnUpdate, btnDisable, updateFrom, gitHub); + break; + case SNACKBAR: + UtilsDisplay.showUpdateAvailableSnackbar(context, String.format(context.getResources().getString(R.string.appupdater_update_available_description_snackbar), UtilsLibrary.getAppInstalledVersion(context)), UtilsLibrary.getDurationEnumToBoolean(duration), updateFrom, gitHub); + break; + case NOTIFICATION: + UtilsDisplay.showUpdateAvailableNotification(context, context.getResources().getString(R.string.appupdater_update_available), String.format(context.getResources().getString(R.string.appupdater_update_available_description_notification), version, UtilsLibrary.getAppName(context)), updateFrom, gitHub); + break; + } + } + libraryPreferences.setSuccessfulChecks(successfulChecks + 1); + } else if (showAppUpdated) { + switch (display) { + case DIALOG: + UtilsDisplay.showUpdateNotAvailableDialog(context, titleNoUpdate, getDescriptionNoUpdate(context)); + break; + case SNACKBAR: + UtilsDisplay.showUpdateNotAvailableSnackbar(context, context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getDurationEnumToBoolean(duration)); + break; + case NOTIFICATION: + UtilsDisplay.showUpdateNotAvailableNotification(context, context.getResources().getString(R.string.appupdater_update_not_available), String.format(context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getAppName(context))); + break; + } + } + } + }); + latestAppVersion.execute(); - return this; + } + + interface LibraryListener { + void onSuccess(String version); + } + + private String getDescriptionUpdate(Context context, String version) { + if (descriptionUpdate == null) { + return String.format(context.getResources().getString(R.string.appupdater_update_available_description_dialog), version, UtilsLibrary.getAppName(context)); + } else { + return descriptionUpdate; + } + } + + private String getDescriptionNoUpdate(Context context) { + if (descriptionNoUpdate == null) { + return String.format(context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getAppName(context)); + } else { + return descriptionNoUpdate; + } } } diff --git a/library/src/main/java/com/github/javiersantos/appupdater/AppUpdaterUtils.java b/library/src/main/java/com/github/javiersantos/appupdater/AppUpdaterUtils.java index cb4f115a..e10e4034 100644 --- a/library/src/main/java/com/github/javiersantos/appupdater/AppUpdaterUtils.java +++ b/library/src/main/java/com/github/javiersantos/appupdater/AppUpdaterUtils.java @@ -64,13 +64,16 @@ public AppUpdaterUtils withListener(AppUpdaterListener appUpdaterListener) { /** * Execute AppUpdaterUtils in background. - * - * @return this */ - public AppUpdaterUtils start() { - UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, null, null, updateFrom, null, null, gitHub, appUpdaterListener); + public void start() { + UtilsAsync.LatestAppVersion latestAppVersion = new UtilsAsync.LatestAppVersion(context, true, updateFrom, gitHub, new AppUpdater.LibraryListener() { + @Override + public void onSuccess(String version) { + appUpdaterListener.onSuccess(version, UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), version)); + } + }); + latestAppVersion.execute(); - return this; } } diff --git a/library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java b/library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java index a06ff610..3afac7dc 100644 --- a/library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java +++ b/library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java @@ -3,8 +3,6 @@ import android.content.Context; import android.os.AsyncTask; -import com.github.javiersantos.appupdater.enums.Display; -import com.github.javiersantos.appupdater.enums.Duration; import com.github.javiersantos.appupdater.enums.UpdateFrom; import com.github.javiersantos.appupdater.objects.GitHub; @@ -13,24 +11,18 @@ class UtilsAsync { static class LatestAppVersion extends AsyncTask { private Context context; private LibraryPreferences libraryPreferences; - private Integer showEvery; - private Boolean showAppUpdated; + private Boolean fromUtils; private UpdateFrom updateFrom; - private Display display; - private Duration duration; private GitHub gitHub; - private AppUpdaterUtils.AppUpdaterListener appUpdaterListener; + private AppUpdater.LibraryListener listener; - public LatestAppVersion(Context context, Integer showEvery, Boolean showAppUpdated, UpdateFrom updateFrom, Display display, Duration duration, GitHub gitHub, AppUpdaterUtils.AppUpdaterListener appUpdaterListener) { + public LatestAppVersion(Context context, Boolean fromUtils, UpdateFrom updateFrom, GitHub gitHub, AppUpdater.LibraryListener libraryListener) { this.context = context; this.libraryPreferences = new LibraryPreferences(context); - this.showEvery = showEvery; - this.showAppUpdated = showAppUpdated; + this.fromUtils = fromUtils; this.updateFrom = updateFrom; - this.display = display; - this.duration = duration; this.gitHub = gitHub; - this.appUpdaterListener = appUpdaterListener; + this.listener = libraryListener; } @Override @@ -38,7 +30,7 @@ protected void onPreExecute() { super.onPreExecute(); if (UtilsLibrary.isNetworkAvailable(context)) { - if (appUpdaterListener == null && !libraryPreferences.getAppUpdaterShow()) { cancel(true); } + if (!fromUtils && !libraryPreferences.getAppUpdaterShow()) { cancel(true); } else { if (updateFrom == UpdateFrom.GITHUB) { if (!GitHub.isGitHubValid(gitHub)) { cancel(true); } @@ -55,40 +47,7 @@ protected String doInBackground(Void... voids) { @Override protected void onPostExecute(String version) { super.onPostExecute(version); - - if (appUpdaterListener == null) { - if (UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), version)) { - Integer successfulChecks = libraryPreferences.getSuccessfulChecks(); - if (UtilsLibrary.isAbleToShow(successfulChecks, showEvery)) { - switch (display) { - case DIALOG: - UtilsDisplay.showUpdateAvailableDialog(context, context.getResources().getString(R.string.appupdater_update_available), String.format(context.getResources().getString(R.string.appupdater_update_available_description_dialog), version, UtilsLibrary.getAppName(context)), updateFrom, gitHub); - break; - case SNACKBAR: - UtilsDisplay.showUpdateAvailableSnackbar(context, String.format(context.getResources().getString(R.string.appupdater_update_available_description_snackbar), UtilsLibrary.getAppInstalledVersion(context)), UtilsLibrary.getDurationEnumToBoolean(duration), updateFrom, gitHub); - break; - case NOTIFICATION: - UtilsDisplay.showUpdateAvailableNotification(context, context.getResources().getString(R.string.appupdater_update_available), String.format(context.getResources().getString(R.string.appupdater_update_available_description_notification), version, UtilsLibrary.getAppName(context)), updateFrom, gitHub); - break; - } - } - libraryPreferences.setSuccessfulChecks(successfulChecks + 1); - } else if (showAppUpdated) { - switch (display) { - case DIALOG: - UtilsDisplay.showUpdateNotAvailableDialog(context, context.getResources().getString(R.string.appupdater_update_not_available), String.format(context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getAppName(context))); - break; - case SNACKBAR: - UtilsDisplay.showUpdateNotAvailableSnackbar(context, context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getDurationEnumToBoolean(duration)); - break; - case NOTIFICATION: - UtilsDisplay.showUpdateNotAvailableNotification(context, context.getResources().getString(R.string.appupdater_update_not_available), String.format(context.getResources().getString(R.string.appupdater_update_not_available_description), UtilsLibrary.getAppName(context))); - break; - } - } - } else { - appUpdaterListener.onSuccess(version, UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), version)); - } + listener.onSuccess(version); } } diff --git a/library/src/main/java/com/github/javiersantos/appupdater/UtilsDisplay.java b/library/src/main/java/com/github/javiersantos/appupdater/UtilsDisplay.java index e42e6f02..42108d9c 100644 --- a/library/src/main/java/com/github/javiersantos/appupdater/UtilsDisplay.java +++ b/library/src/main/java/com/github/javiersantos/appupdater/UtilsDisplay.java @@ -18,15 +18,15 @@ class UtilsDisplay { - static void showUpdateAvailableDialog(final Context context, String title, String content, final UpdateFrom updateFrom, final GitHub gitHub) { + static void showUpdateAvailableDialog(final Context context, String title, String content, String btnPositive, String btnNeutral, final UpdateFrom updateFrom, final GitHub gitHub) { final LibraryPreferences libraryPreferences = new LibraryPreferences(context); MaterialDialog materialDialog = new MaterialDialog.Builder(context) .title(title) .content(content) - .positiveText(context.getResources().getString(R.string.appupdater_btn_update)) + .positiveText(btnPositive) .negativeText(context.getResources().getString(android.R.string.cancel)) - .neutralText(context.getResources().getString(R.string.appupdater_btn_disable)) + .neutralText(btnNeutral) .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) {