From 7b88e7a61293f616fb0fcd3586a07ca661ec7fdf Mon Sep 17 00:00:00 2001 From: Jens Date: Wed, 9 Jun 2021 22:41:47 +0200 Subject: [PATCH] Automatic update check for APK version. --- .../jens/automation2/ActivityMainScreen.java | 15 ++++++++++++++- .../com/jens/automation2/ActivitySettings.java | 2 +- .../java/com/jens/automation2/AsyncTasks.java | 18 +++++++++++++++--- .../jens/automation2/AutomationService.java | 3 ++- .../java/com/jens/automation2/Settings.java | 2 +- app/src/main/res/layout/activity_settings.xml | 2 +- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/ActivityMainScreen.java b/app/src/main/java/com/jens/automation2/ActivityMainScreen.java index ef430af1..30b24ede 100644 --- a/app/src/main/java/com/jens/automation2/ActivityMainScreen.java +++ b/app/src/main/java/com/jens/automation2/ActivityMainScreen.java @@ -641,6 +641,8 @@ public class ActivityMainScreen extends ActivityGeneric { if(result && !updateNoteDisplayed) { + updateNoteDisplayed = true; + AlertDialog.Builder updateNoteBuilder = new AlertDialog.Builder(ActivityMainScreen.this); updateNoteBuilder.setMessage(getResources().getString(R.string.updateAvailable)); updateNoteBuilder.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() @@ -651,10 +653,21 @@ public class ActivityMainScreen extends ActivityGeneric String url = "https://server47.de/automation/"; Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browserIntent); + + updateNoteDisplayed = false; + } + }); + updateNoteBuilder.setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dialogInterface, int i) + { + updateNoteDisplayed = false; } }); - updateNoteBuilder.setNegativeButton(getResources().getString(R.string.no), null); updateNoteBuilder.show(); } + + AsyncTasks.AsyncTaskUpdateCheck.checkRunning = false; } } \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/ActivitySettings.java b/app/src/main/java/com/jens/automation2/ActivitySettings.java index df8998b7..a8e81a3c 100644 --- a/app/src/main/java/com/jens/automation2/ActivitySettings.java +++ b/app/src/main/java/com/jens/automation2/ActivitySettings.java @@ -20,7 +20,7 @@ public class ActivitySettings extends PreferenceActivity if(BuildConfig.FLAVOR.equals("apkFlavor")) { - chkPrefUpdateCheck = (CheckBoxPreference) findPreference("chkPrefUpdateCheck"); + chkPrefUpdateCheck = (CheckBoxPreference) findPreference("automaticUpdateCheck"); chkPrefUpdateCheck.setEnabled(true); } } diff --git a/app/src/main/java/com/jens/automation2/AsyncTasks.java b/app/src/main/java/com/jens/automation2/AsyncTasks.java index ae71b079..3360e846 100644 --- a/app/src/main/java/com/jens/automation2/AsyncTasks.java +++ b/app/src/main/java/com/jens/automation2/AsyncTasks.java @@ -11,14 +11,26 @@ public class AsyncTasks { public static class AsyncTaskUpdateCheck extends AsyncTask { + public static boolean checkRunning = false; + @Override protected Boolean doInBackground(Context... contexts) { + if(checkRunning) + return false; + else + checkRunning = true; + try { - String result = Miscellaneous.downloadURL("https://server47.de/automation/?action=getLatestVersionCode", null, null); + String result = Miscellaneous.downloadURL("https://server47.de/automation/?action=getLatestVersionCode", null, null).trim(); int latestVersion = Integer.parseInt(result); + // At this point the update check itself has already been successful. + + Settings.lastUpdateCheck = Calendar.getInstance().getTimeInMillis(); + Settings.writeSettings(contexts[0]); + if (latestVersion > BuildConfig.VERSION_CODE) { // There's a new update @@ -42,11 +54,11 @@ public class AsyncTasks } catch (NullPointerException e) { - Miscellaneous.logEvent("e", "NewsDownload", "There was a problem displaying the already downloded news, probably ActivityMainScreen isn't currently shown: " + Log.getStackTraceString(e), 2); + Miscellaneous.logEvent("e", "NewsDownload", "There was a problem displaying the update check result, probably ActivityMainScreen isn't currently shown: " + Log.getStackTraceString(e), 2); } catch (Exception e) { - Miscellaneous.logEvent("e", "NewsDownload", "There was a problem displaying the already downloded news: " + Log.getStackTraceString(e), 2); + Miscellaneous.logEvent("e", "NewsDownload", "There was a problem displaying the update check result: " + Log.getStackTraceString(e), 2); } } } diff --git a/app/src/main/java/com/jens/automation2/AutomationService.java b/app/src/main/java/com/jens/automation2/AutomationService.java index 33a41002..e422c2a2 100644 --- a/app/src/main/java/com/jens/automation2/AutomationService.java +++ b/app/src/main/java/com/jens/automation2/AutomationService.java @@ -258,7 +258,8 @@ public class AutomationService extends Service implements OnInitListener case reloadSettings: Settings.readFromPersistentStorage(this); applySettingsAndRules(); - myLocationProvider.applySettingsAndRules(); + if(myLocationProvider != null) + myLocationProvider.applySettingsAndRules(); break; case updateNotification: this.updateNotification(); diff --git a/app/src/main/java/com/jens/automation2/Settings.java b/app/src/main/java/com/jens/automation2/Settings.java index 706481cb..eb2fbbbb 100644 --- a/app/src/main/java/com/jens/automation2/Settings.java +++ b/app/src/main/java/com/jens/automation2/Settings.java @@ -255,7 +255,7 @@ public class Settings implements SharedPreferences startScreen = Integer.parseInt(prefs.getString("startScreen", String.valueOf(default_startScreen))); executeRulesAndProfilesWithSingleClick = prefs.getBoolean("executeRulesAndProfilesWithSingleClick", default_executeRulesAndProfilesWithSingleClick); - automaticUpdateCheck = prefs.getBoolean("chkPrefUpdateCheck", default_automaticUpdateCheck); + automaticUpdateCheck = prefs.getBoolean("automaticUpdateCheck", default_automaticUpdateCheck); displayNewsOnMainScreen = prefs.getBoolean("displayNewsOnMainScreen", default_displayNewsOnMainScreen); lockSoundChanges = prefs.getBoolean("lockSoundChanges", default_lockSoundChanges); diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 9c6fd073..a651c527 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -57,7 +57,7 @@ android:title="@string/executeRulesAndProfilesWithSingleClickTitle" />