From e6987978c87d0d42edf32e1d33185bbcb2d2057e Mon Sep 17 00:00:00 2001 From: Javier Santos Date: Tue, 21 Mar 2017 17:11:29 +0100 Subject: [PATCH] Revert "Remove unnecessary class" This reverts commit d172d9a398dd070042e02b899375e8c86af68ee6. --- .../appupdater/AppUpdaterUtils.java | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 library/src/main/java/com/github/javiersantos/appupdater/AppUpdaterUtils.java diff --git a/library/src/main/java/com/github/javiersantos/appupdater/AppUpdaterUtils.java b/library/src/main/java/com/github/javiersantos/appupdater/AppUpdaterUtils.java new file mode 100644 index 00000000..e8dd29a2 --- /dev/null +++ b/library/src/main/java/com/github/javiersantos/appupdater/AppUpdaterUtils.java @@ -0,0 +1,165 @@ +package com.github.javiersantos.appupdater; + +import android.content.Context; +import android.support.annotation.NonNull; + +import com.github.javiersantos.appupdater.enums.AppUpdaterError; +import com.github.javiersantos.appupdater.enums.UpdateFrom; +import com.github.javiersantos.appupdater.objects.GitHub; +import com.github.javiersantos.appupdater.objects.Update; + +public class AppUpdaterUtils { + private Context context; + private UpdateListener updateListener; + private AppUpdaterListener appUpdaterListener; + private UpdateFrom updateFrom; + private GitHub gitHub; + private String xmlOrJSONUrl; + private UtilsAsync.LatestAppVersion latestAppVersion; + + public interface UpdateListener { + /** + * onSuccess method called after it is successful + * onFailed method called if it can't retrieve the latest version + * + * @param update object with the latest update information: version and url to download + * @see com.github.javiersantos.appupdater.objects.Update + * @param isUpdateAvailable compare installed version with the latest one + */ + void onSuccess(Update update, Boolean isUpdateAvailable); + + void onFailed(AppUpdaterError error); + } + + @Deprecated + public interface AppUpdaterListener { + /** + * onSuccess method called after it is successful + * onFailed method called if it can't retrieve the latest version + * + * @param latestVersion available in the provided source + * @param isUpdateAvailable compare installed version with the latest one + */ + void onSuccess(String latestVersion, Boolean isUpdateAvailable); + + void onFailed(AppUpdaterError error); + } + + public AppUpdaterUtils(Context context) { + this.context = context; + this.updateFrom = UpdateFrom.GOOGLE_PLAY; + } + + /** + * Set the source where the latest update can be found. Default: GOOGLE_PLAY. + * + * @param updateFrom source where the latest update is uploaded. If GITHUB is selected, .setGitHubAndRepo method is required. + * @return this + * @see com.github.javiersantos.appupdater.enums.UpdateFrom + * @see Additional documentation + */ + public AppUpdaterUtils setUpdateFrom(UpdateFrom updateFrom) { + this.updateFrom = updateFrom; + return this; + } + + /** + * Set the user and repo where the releases are uploaded. You must upload your updates as a release in order to work properly tagging them as vX.X.X or X.X.X. + * + * @param user GitHub user + * @param repo GitHub repository + * @return this + */ + public AppUpdaterUtils setGitHubUserAndRepo(String user, String repo) { + this.gitHub = new GitHub(user, repo); + return this; + } + + /** + * Set the url to the xml with the latest version info. + * + * @param xmlUrl file + * @return this + */ + public AppUpdaterUtils setUpdateXML(@NonNull String xmlUrl) { + this.xmlOrJSONUrl = xmlUrl; + return this; + } + + /** + * Set the url to the xml with the latest version info. + * + * @param jsonUrl file + * @return this + */ + public AppUpdaterUtils setUpdateJSON(@NonNull String jsonUrl) { + this.xmlOrJSONUrl = jsonUrl; + return this; + } + + + /** + * Method to set the AppUpdaterListener for the AppUpdaterUtils actions + * + * @param appUpdaterListener the listener to be notified + * @return this + * @see com.github.javiersantos.appupdater.AppUpdaterUtils.AppUpdaterListener + * @deprecated + */ + public AppUpdaterUtils withListener(AppUpdaterListener appUpdaterListener) { + this.appUpdaterListener = appUpdaterListener; + return this; + } + + /** + * Method to set the UpdateListener for the AppUpdaterUtils actions + * + * @param updateListener the listener to be notified + * @return this + * @see com.github.javiersantos.appupdater.AppUpdaterUtils.UpdateListener + */ + public AppUpdaterUtils withListener(UpdateListener updateListener) { + this.updateListener = updateListener; + return this; + } + + /** + * Execute AppUpdaterUtils in background. + */ + public void start() { + latestAppVersion = new UtilsAsync.LatestAppVersion(context, true, updateFrom, gitHub, xmlOrJSONUrl, new AppUpdater.LibraryListener() { + @Override + public void onSuccess(Update update) { + if (updateListener != null) { + updateListener.onSuccess(update, UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), update.getLatestVersion())); + } else if (appUpdaterListener != null) { + appUpdaterListener.onSuccess(update.getLatestVersion(), UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), update.getLatestVersion())); + } else { + throw new RuntimeException("You must provide a listener for the AppUpdaterUtils"); + } + } + + @Override + public void onFailed(AppUpdaterError error) { + if (updateListener != null) { + updateListener.onFailed(error); + } else if (appUpdaterListener != null) { + appUpdaterListener.onFailed(error); + } else { + throw new RuntimeException("You must provide a listener for the AppUpdaterUtils"); + } + } + }); + + latestAppVersion.execute(); + } + + /** + * Stops the execution of AppUpdater. + */ + public void stop() { + if (latestAppVersion != null && !latestAppVersion.isCancelled()) { + latestAppVersion.cancel(true); + } + } +}