added update dialog
This commit is contained in:
parent
0c0d4b9bb1
commit
71975e98f4
3 changed files with 68 additions and 18 deletions
|
@ -1,20 +1,25 @@
|
||||||
package me.brysonsteck.wiimmfiwatcher;
|
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.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
import androidx.annotation.RequiresApi;
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.room.Room;
|
import androidx.room.Room;
|
||||||
|
|
||||||
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||||
|
|
||||||
import me.brysonsteck.wiimmfiwatcher.database.AppDatabase;
|
import me.brysonsteck.wiimmfiwatcher.database.AppDatabase;
|
||||||
import me.brysonsteck.wiimmfiwatcher.fragments.AboutFragment;
|
import me.brysonsteck.wiimmfiwatcher.fragments.AboutFragment;
|
||||||
import me.brysonsteck.wiimmfiwatcher.fragments.WatchCodeFragment;
|
import me.brysonsteck.wiimmfiwatcher.fragments.WatchCodeFragment;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
AppDatabase database;
|
AppDatabase database;
|
||||||
|
final MaterialAlertDialogBuilder[] dialog = new MaterialAlertDialogBuilder[1];
|
||||||
|
boolean shownUpdate = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -52,20 +57,56 @@ public class MainActivity extends AppCompatActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
new Thread(() -> {
|
final String[] newestRelease = {""};
|
||||||
Updater updater = new Updater();
|
final boolean[] outdated = {false};
|
||||||
updater.compareRelease(BuildConfig.VERSION_NAME);
|
Thread thread = new Thread() {
|
||||||
if (updater.isOutdated()) {
|
public void run() {
|
||||||
System.out.println("---------------------------------------------------------------");
|
Updater updater = new Updater();
|
||||||
System.out.println("\tA newer version of Wiimmfi Watcher is available! (" + updater.getNewestRelease() + ")");
|
updater.compareRelease(BuildConfig.VERSION_NAME);
|
||||||
System.out.println("\tView the release notes and the source code here: " + updater.getGithubRelease());
|
if (updater.isOutdated()) {
|
||||||
System.out.println("\t---------------------------------------------------------------");
|
System.out.println("---------------------------------------------------------------");
|
||||||
} else {
|
System.out.println("\tA newer version of Wiimmfi Watcher is available! (" + updater.getNewestRelease() + ")");
|
||||||
System.out.println("---------------------------------------------------------------");
|
System.out.println("\tView the release notes and the source code here: " + updater.getGithubRelease());
|
||||||
System.out.println("\t\t" + updater.getNewestRelease() + " is the latest release of Wiimmfi Watcher.");
|
System.out.println("\t---------------------------------------------------------------");
|
||||||
System.out.println("\t\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
|
@Override
|
||||||
|
|
|
@ -22,8 +22,12 @@ public class Updater {
|
||||||
String json = urlReader();
|
String json = urlReader();
|
||||||
JsonElement root = new JsonParser().parse(json);
|
JsonElement root = new JsonParser().parse(json);
|
||||||
JsonObject rootObj = root.getAsJsonObject();
|
JsonObject rootObj = root.getAsJsonObject();
|
||||||
newestRelease = rootObj.get("current-release").getAsString();
|
JsonElement softwareElement = rootObj.getAsJsonObject("wiimmfi-watcher");
|
||||||
githubRelease = rootObj.get("github-release").getAsString();
|
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) {
|
} catch (IOException e) {
|
||||||
System.out.println("An error has occurred while attempting to check for updates.");
|
System.out.println("An error has occurred while attempting to check for updates.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -31,7 +35,7 @@ public class Updater {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String urlReader() throws IOException {
|
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();
|
URLConnection connection = website.openConnection();
|
||||||
BufferedReader in = new BufferedReader(
|
BufferedReader in = new BufferedReader(
|
||||||
new InputStreamReader(
|
new InputStreamReader(
|
||||||
|
|
|
@ -37,4 +37,9 @@
|
||||||
<string name="header_null_error">This player is not online, not inside a room or does not exist. Click the refresh button to try again, or click on the back button to enter a different friend code.</string>
|
<string name="header_null_error">This player is not online, not inside a room or does not exist. Click the refresh button to try again, or click on the back button to enter a different friend code.</string>
|
||||||
<string name="jsoup_error">Whoops! Wiimmfi Watcher was unable to connect to the Wiimmfi servers. This could be that you are not connected to the internet, but it could be something else. Here was the error:\n\n%1$s\n\nIf the error is along the lines of \"Unable to resolve host\" or \"Timeout\", you are probably having internet issues. Make sure you are connected to the internet then click the refresh button or press back to watch a new friend code.\n\nIf the error is something other than that or if the error persists, make sure that Wiimmfi\'s website is currently running. Otherwise, please screenshot this screen and submit a bug report by clicking the About icon on the main page.</string>
|
<string name="jsoup_error">Whoops! Wiimmfi Watcher was unable to connect to the Wiimmfi servers. This could be that you are not connected to the internet, but it could be something else. Here was the error:\n\n%1$s\n\nIf the error is along the lines of \"Unable to resolve host\" or \"Timeout\", you are probably having internet issues. Make sure you are connected to the internet then click the refresh button or press back to watch a new friend code.\n\nIf the error is something other than that or if the error persists, make sure that Wiimmfi\'s website is currently running. Otherwise, please screenshot this screen and submit a bug report by clicking the About icon on the main page.</string>
|
||||||
<string name="locating_text">Locating %1$s…</string>
|
<string name="locating_text">Locating %1$s…</string>
|
||||||
|
|
||||||
|
<string name="update_title">A new update is available!</string>
|
||||||
|
<string name="update_message">A new version of Wiimmfi Watcher is available on the Play Store (version %1$s)! You can download it by pressing \"Update\".</string>
|
||||||
|
<string name="update_positive">Update</string>
|
||||||
|
<string name="update_negative">Later</string>
|
||||||
</resources>
|
</resources>
|
Reference in a new issue