From 722f9f0e6b18b7a2a6cc960dbab421f3ac575f85 Mon Sep 17 00:00:00 2001 From: Jens Date: Mon, 15 Mar 2021 23:06:37 +0100 Subject: [PATCH] News opt-in done. --- .../jens/automation2/ActivityMainScreen.java | 87 ++++++++++--------- .../jens/automation2/ActivityPermissions.java | 2 +- .../com/jens/automation2/Miscellaneous.java | 18 ++-- .../java/com/jens/automation2/Settings.java | 40 +++++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 99 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/ActivityMainScreen.java b/app/src/main/java/com/jens/automation2/ActivityMainScreen.java index c95ac99c..aaba214a 100644 --- a/app/src/main/java/com/jens/automation2/ActivityMainScreen.java +++ b/app/src/main/java/com/jens/automation2/ActivityMainScreen.java @@ -418,7 +418,34 @@ public class ActivityMainScreen extends ActivityGeneric Miscellaneous.logEvent("i", "ActivityMainScreen", "Activity not running. No need to update.", 5); if(activityMainScreenInstance != null) - activityMainScreenInstance.checkForNews(); + { + if(!Settings.hasBeenDone(Settings.constNewsOptInDone)) + newsOptIn(); + else + activityMainScreenInstance.checkForNews(); + + Settings.considerDone(Settings.constNewsOptInDone); + Settings.writeSettings(Miscellaneous.getAnyContext()); + } + } + + static void newsOptIn() + { + AlertDialog.Builder builder = new AlertDialog.Builder(Miscellaneous.getAnyContext()); + builder.setMessage(Miscellaneous.getAnyContext().getResources().getString(R.string.newsOptIn)); + builder.setPositiveButton(Miscellaneous.getAnyContext().getResources().getString(R.string.yes), new DialogInterface.OnClickListener() + { + @Override + public void onClick(DialogInterface dialog, int which) + { + Settings.displayNewsOnMainScreen = true; + Settings.writeSettings(Miscellaneous.getAnyContext()); + activityMainScreenInstance.checkForNews(); + } + }); + builder.setNegativeButton(Miscellaneous.getAnyContext().getResources().getString(R.string.no), null); + + builder.create().show(); } @Override @@ -572,57 +599,31 @@ public class ActivityMainScreen extends ActivityGeneric synchronized void checkForNews() { - News.AsyncTaskDownloadNews dnTask = new News.AsyncTaskDownloadNews(); - dnTask.execute(ActivityMainScreen.this); - -// StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); -// StrictMode.setThreadPolicy(policy); - -// Calendar now = Calendar.getInstance(); - -// if (true || Settings.lastNewsPolltime == -1 || now.getTimeInMillis() >= Settings.lastNewsPolltime + (long)(Settings.pollNewsEveryXDays * 24 * 60 * 60 * 1000)) -// { -// ArrayList newsToDisplay; -// -//// if(Settings.lastNewsPolltime == -1) -// newsToDisplay = News.downloadNews(ActivityMainScreen.this); -//// else -//// newsToDisplay = News.downloadNews(ActivityMainScreen.this, Miscellaneous.calendarFromLong(Settings.lastNewsPolltime)); -// -// if (newsToDisplay.size() > 0) -// { -// activityMainScreenInstance.tvMainScreenNote3.setText(newsToDisplay.get(0).toString()); -// activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.VISIBLE); -// } -// else -// { -// activityMainScreenInstance.tvMainScreenNote3.setText(""); -// activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.GONE); -// } -// } + if(Settings.displayNewsOnMainScreen) + { + News.AsyncTaskDownloadNews dnTask = new News.AsyncTaskDownloadNews(); + dnTask.execute(ActivityMainScreen.this); + } } public void processNewsResult(ArrayList newsToDisplay) { - if(Settings.displayNewsOnMainScreen) + try { - try + if (newsToDisplay.size() > 0) { - if (newsToDisplay.size() > 0) - { - activityMainScreenInstance.tvMainScreenNote3.setText(HtmlCompat.fromHtml(newsToDisplay.get(0).toStringHtml(), 0)); - activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.VISIBLE); - } - else - { - activityMainScreenInstance.tvMainScreenNote3.setText(""); - activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.GONE); - } + activityMainScreenInstance.tvMainScreenNote3.setText(HtmlCompat.fromHtml(newsToDisplay.get(0).toStringHtml(), 0)); + activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.VISIBLE); } - catch(Exception e) + else { - Miscellaneous.logEvent("e", "Error displaying news", Log.getStackTraceString(e), 3); + activityMainScreenInstance.tvMainScreenNote3.setText(""); + activityMainScreenInstance.tvMainScreenNote3.setVisibility(View.GONE); } } + catch(Exception e) + { + Miscellaneous.logEvent("e", "Error displaying news", Log.getStackTraceString(e), 3); + } } } \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index 1992ab27..0f258113 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -977,7 +977,7 @@ public class ActivityPermissions extends Activity and simply disable features while keeping the notification alive. The user may dismiss it anyway. */ - Miscellaneous.logEvent("w", "Denied permissions", getResources().getString(R.string.theFollowingPermissionsHaveBeenDenied) + Miscellaneous.explode(deniedPermissions), 3); + Miscellaneous.logEvent("w", "Denied permissions", getResources().getString(R.string.theFollowingPermissionsHaveBeenDenied) + Miscellaneous.explode(", ", deniedPermissions), 3); // this.finish(); } else diff --git a/app/src/main/java/com/jens/automation2/Miscellaneous.java b/app/src/main/java/com/jens/automation2/Miscellaneous.java index 9efb7128..07079e27 100644 --- a/app/src/main/java/com/jens/automation2/Miscellaneous.java +++ b/app/src/main/java/com/jens/automation2/Miscellaneous.java @@ -864,13 +864,21 @@ public class Miscellaneous extends Service return builder; } - public static String explode(ArrayList arrayList) + public static String explode(String glue, ArrayList arrayList) { - StringBuilder builder = new StringBuilder(); - for(String s : arrayList) - builder.append(s); + if(arrayList != null) + { + StringBuilder builder = new StringBuilder(); + for (String s : arrayList) + builder.append(s + glue); - return builder.toString(); + if (builder.length() > 0) + builder.delete(builder.length() - glue.length(), builder.length()); + + return builder.toString(); + } + else + return ""; } public static boolean isGooglePlayInstalled(Context context) diff --git a/app/src/main/java/com/jens/automation2/Settings.java b/app/src/main/java/com/jens/automation2/Settings.java index 8cc4da82..8e7cfc68 100644 --- a/app/src/main/java/com/jens/automation2/Settings.java +++ b/app/src/main/java/com/jens/automation2/Settings.java @@ -5,6 +5,7 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; +import java.util.ArrayList; import java.util.Map; import java.util.Set; @@ -16,6 +17,8 @@ public class Settings implements SharedPreferences public static final int newsDisplayForXDays = 3; public static final String folderName = "Automation"; + public static final String constNewsOptInDone ="newsOptInDone"; + public static long minimumDistanceChangeForGpsUpdate; public static long minimumDistanceChangeForNetworkUpdate; public static long satisfactoryAccuracyGps; @@ -62,6 +65,8 @@ public class Settings implements SharedPreferences public static boolean noticeAndroid10WifiShown; public static long lastNewsPolltime; + public static ArrayList whatHasBeenDone; + /* Generic settings valid for all installations and not changable */ @@ -250,6 +255,16 @@ public class Settings implements SharedPreferences noticeAndroid10WifiShown = prefs.getBoolean("noticeAndroid10WifiShown", false); lastNewsPolltime = prefs.getLong("lastNewsPolltime", default_lastNewsPolltime); + + String whbdString = prefs.getString("whatHasBeenDone", ""); + if(whbdString != null && whbdString.length() > 0) + { + whatHasBeenDone = new ArrayList<>(); + for(String s : whbdString.split(";")) + { + whatHasBeenDone.add(s); + } + } } catch(Exception e) { @@ -261,6 +276,26 @@ public class Settings implements SharedPreferences initializeSettings(context, false); } } + + public static void considerDone(String key) + { + if(whatHasBeenDone == null) + whatHasBeenDone = new ArrayList<>(); + + if(!whatHasBeenDone.contains(key)) + whatHasBeenDone.add(key); + } + + public static boolean hasBeenDone(String key) + { + if(whatHasBeenDone != null) + { + if(whatHasBeenDone.contains(key)) + return true; + } + + return false; + } /**Makes sure a settings has a valid setting. If not it will assign a reasonable default setting to it. * If force settings will be initialized even if the user has set something.**/ @@ -410,6 +445,9 @@ public class Settings implements SharedPreferences if(!prefs.contains("lastNewsPolltime") | force) editor.putLong("lastNewsPolltime", default_lastNewsPolltime); + + if(!prefs.contains("whatHasBeenDone") | force) + editor.putString("whatHasBeenDone", ""); editor.commit(); @@ -480,6 +518,8 @@ public class Settings implements SharedPreferences editor.putLong("lastNewsPolltime", lastNewsPolltime); + editor.putString("whatHasBeenDone", Miscellaneous.explode(";", whatHasBeenDone)); + if(lastActivePoi == null) editor.putString("lastActivePoi", "null"); else diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 13518f13..78e0024e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -606,4 +606,5 @@ Display application news on main screen Announcements about this app only, we\'re probably talking about 1-2 per year, not more. Automation now uses another path to store your files. All your Automation-files have been moved here: \"%s\". The external storage permission is not required anymore; you can revoke it. It will be removed in a future version. + Would you like to receive (only important) news about this app on the main screen? Those are downloaded from the developer\'s website. There will be no intrusive notification, just a text on the main screen when you open the app. \ No newline at end of file