From 71975e98f4b2de35af051577d5d11d935b108374 Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Mon, 28 Jun 2021 22:31:33 -0600 Subject: added update dialog --- .../brysonsteck/wiimmfiwatcher/MainActivity.java | 71 +++++++++++++++++----- .../me/brysonsteck/wiimmfiwatcher/Updater.java | 10 ++- 2 files changed, 63 insertions(+), 18 deletions(-) (limited to 'app/src/main/java/me/brysonsteck/wiimmfiwatcher') diff --git a/app/src/main/java/me/brysonsteck/wiimmfiwatcher/MainActivity.java b/app/src/main/java/me/brysonsteck/wiimmfiwatcher/MainActivity.java index 3a42749..e98856b 100644 --- a/app/src/main/java/me/brysonsteck/wiimmfiwatcher/MainActivity.java +++ b/app/src/main/java/me/brysonsteck/wiimmfiwatcher/MainActivity.java @@ -1,20 +1,25 @@ package me.brysonsteck.wiimmfiwatcher; -import android.os.Build; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.WindowManager; -import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import androidx.room.Room; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + import me.brysonsteck.wiimmfiwatcher.database.AppDatabase; import me.brysonsteck.wiimmfiwatcher.fragments.AboutFragment; import me.brysonsteck.wiimmfiwatcher.fragments.WatchCodeFragment; public class MainActivity extends AppCompatActivity { AppDatabase database; + final MaterialAlertDialogBuilder[] dialog = new MaterialAlertDialogBuilder[1]; + boolean shownUpdate = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -52,20 +57,56 @@ public class MainActivity extends AppCompatActivity { @Override protected void onStart() { super.onStart(); - new Thread(() -> { - Updater updater = new Updater(); - updater.compareRelease(BuildConfig.VERSION_NAME); - if (updater.isOutdated()) { - System.out.println("---------------------------------------------------------------"); - System.out.println("\tA newer version of Wiimmfi Watcher is available! (" + updater.getNewestRelease() + ")"); - System.out.println("\tView the release notes and the source code here: " + updater.getGithubRelease()); - System.out.println("\t---------------------------------------------------------------"); - } else { - System.out.println("---------------------------------------------------------------"); - System.out.println("\t\t" + updater.getNewestRelease() + " is the latest release of Wiimmfi Watcher."); - System.out.println("\t\t---------------------------------------------------------------"); + final String[] newestRelease = {""}; + final boolean[] outdated = {false}; + Thread thread = new Thread() { + public void run() { + Updater updater = new Updater(); + updater.compareRelease(BuildConfig.VERSION_NAME); + if (updater.isOutdated()) { + System.out.println("---------------------------------------------------------------"); + System.out.println("\tA newer version of Wiimmfi Watcher is available! (" + updater.getNewestRelease() + ")"); + System.out.println("\tView the release notes and the source code here: " + updater.getGithubRelease()); + System.out.println("\t---------------------------------------------------------------"); + } else { + System.out.println("---------------------------------------------------------------"); + System.out.println("\t\t" + updater.getNewestRelease() + " is the latest release of Wiimmfi Watcher."); + System.out.println("\t\t---------------------------------------------------------------"); + } + newestRelease[0] = updater.getNewestRelease(); + outdated[0] = updater.isOutdated(); } - }).start(); + }; + thread.start(); + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if (outdated[0] && !shownUpdate) { + shownUpdate = true; + final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object + new MaterialAlertDialogBuilder(this) + .setTitle(R.string.update_title) + .setMessage(getResources().getString(R.string.update_message, newestRelease[0])) + .setNegativeButton(getResources().getString(R.string.update_negative), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }) + .setPositiveButton(getResources().getString(R.string.update_positive), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + try { + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); + } catch (android.content.ActivityNotFoundException anfe) { + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); + } + } + }) + .show(); + } + } @Override diff --git a/app/src/main/java/me/brysonsteck/wiimmfiwatcher/Updater.java b/app/src/main/java/me/brysonsteck/wiimmfiwatcher/Updater.java index eaa4d0c..eb3e813 100644 --- a/app/src/main/java/me/brysonsteck/wiimmfiwatcher/Updater.java +++ b/app/src/main/java/me/brysonsteck/wiimmfiwatcher/Updater.java @@ -22,8 +22,12 @@ public class Updater { String json = urlReader(); JsonElement root = new JsonParser().parse(json); JsonObject rootObj = root.getAsJsonObject(); - newestRelease = rootObj.get("current-release").getAsString(); - githubRelease = rootObj.get("github-release").getAsString(); + JsonElement softwareElement = rootObj.getAsJsonObject("wiimmfi-watcher"); + JsonObject softwareObj = softwareElement.getAsJsonObject(); + newestRelease = softwareObj.get("current-release").getAsString(); + newestRelease = newestRelease.replace("\"", ""); + githubRelease = softwareObj.get("github-release").getAsString(); + githubRelease = githubRelease.replace("\"", ""); } catch (IOException e) { System.out.println("An error has occurred while attempting to check for updates."); e.printStackTrace(); @@ -31,7 +35,7 @@ public class Updater { } public String urlReader() throws IOException { - URL website = new URL("https://brysonsteck.net/watcher.json"); + URL website = new URL("https://brysonsteck.net/updates.json"); URLConnection connection = website.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( -- cgit v1.2.3