From 249b36f987479dde95d72c84cde6affeebed831c Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 2 Aug 2022 11:29:40 +0200 Subject: [PATCH] Fix issue #285 - Allow to set the fetch time for delayed. --- .../fedilab/android/helper/PushHelper.java | 33 +++++++++++-------- .../FragmentNotificationsSettings.java | 18 ++++++++++ app/src/main/res/values/strings.xml | 23 ++++++++++++- app/src/main/res/xml/pref_notifications.xml | 11 +++++++ 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/helper/PushHelper.java b/app/src/main/java/app/fedilab/android/helper/PushHelper.java index bcc7907b2..fd541ef49 100644 --- a/app/src/main/java/app/fedilab/android/helper/PushHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/PushHelper.java @@ -84,19 +84,7 @@ public class PushHelper { WorkManager.getInstance(context).cancelAllWorkByTag(Helper.WORKER_REFRESH_NOTIFICATION); break; case "REPEAT_NOTIFICATIONS": - new Thread(() -> { - List accounts = new Account(context).getPushNotificationAccounts(); - if (accounts != null) { - for (BaseAccount account : accounts) { - ((Activity) context).runOnUiThread(() -> { - UnifiedPush.unregisterApp(context, account.user_id + "@" + account.instance); - }); - } - } - }).start(); - new PeriodicWorkRequest.Builder(NotificationsWorker.class, 20, TimeUnit.MINUTES) - .addTag(Helper.WORKER_REFRESH_NOTIFICATION) - .build(); + setRepeat(context); break; case "NO_NOTIFICATIONS": WorkManager.getInstance(context).cancelAllWorkByTag(Helper.WORKER_REFRESH_NOTIFICATION); @@ -112,6 +100,25 @@ public class PushHelper { } } + public static void setRepeat(Context context) { + WorkManager.getInstance(context).cancelAllWorkByTag(Helper.WORKER_REFRESH_NOTIFICATION); + new Thread(() -> { + List accounts = new Account(context).getPushNotificationAccounts(); + if (accounts != null) { + for (BaseAccount account : accounts) { + ((Activity) context).runOnUiThread(() -> { + UnifiedPush.unregisterApp(context, account.user_id + "@" + account.instance); + }); + } + } + }).start(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String value = prefs.getString(context.getString(R.string.SET_NOTIFICATION_DELAY_VALUE), "15"); + new PeriodicWorkRequest.Builder(NotificationsWorker.class, Long.parseLong(value), TimeUnit.MINUTES) + .addTag(Helper.WORKER_REFRESH_NOTIFICATION) + .build(); + } + private static void registerAppWithDialog(Context context, List accounts) { if (accounts == null) { diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/settings/FragmentNotificationsSettings.java b/app/src/main/java/app/fedilab/android/ui/fragment/settings/FragmentNotificationsSettings.java index 58fb82ba5..04c575ebc 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/settings/FragmentNotificationsSettings.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/settings/FragmentNotificationsSettings.java @@ -92,7 +92,21 @@ public class FragmentNotificationsSettings extends PreferenceFragmentCompat impl if (notification_time_slot != null) { preferenceScreen.removePreference(notification_time_slot); } + ListPreference SET_NOTIFICATION_DELAY_VALUE = findPreference("SET_NOTIFICATION_DELAY_VALUE"); + if (SET_NOTIFICATION_DELAY_VALUE != null) { + preferenceScreen.removePreferenceRecursively("SET_NOTIFICATION_DELAY_VALUE"); + } return; + } else if (SET_NOTIFICATION_TYPE != null && SET_NOTIFICATION_TYPE.getValue().equals(notificationValues[1])) { + ListPreference SET_NOTIFICATION_DELAY_VALUE = findPreference(getString(R.string.SET_NOTIFICATION_DELAY_VALUE)); + if (SET_NOTIFICATION_DELAY_VALUE != null) { + SET_NOTIFICATION_DELAY_VALUE.getContext().setTheme(Helper.dialogStyle()); + } + } else { + ListPreference SET_NOTIFICATION_DELAY_VALUE = findPreference("SET_NOTIFICATION_DELAY_VALUE"); + if (SET_NOTIFICATION_DELAY_VALUE != null) { + preferenceScreen.removePreferenceRecursively("SET_NOTIFICATION_DELAY_VALUE"); + } } Preference button_mention = findPreference("button_mention"); @@ -178,6 +192,10 @@ public class FragmentNotificationsSettings extends PreferenceFragmentCompat impl createPref(); PushHelper.startStreaming(requireActivity()); } + if (key.compareToIgnoreCase(getString(R.string.SET_NOTIFICATION_DELAY_VALUE)) == 0) { + createPref(); + PushHelper.setRepeat(requireActivity()); + } if (key.compareToIgnoreCase(getString(R.string.SET_LED_COLOUR_VAL)) == 0) { try { int value = Integer.parseInt(key); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 784fa7273..65ca96432 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -738,12 +738,20 @@ No accounts found for this list! Scheduled - + Push notifications Fetch at fixed times No notifications + + 15 minutes + 30 minutes + 1 hour + 2 hours + 6 hours + 12 hours + SET_COOKIES SET_PROXY_ENABLED @@ -758,11 +766,21 @@ SET_CUSTOM_USER_AGENT SET_NSFW_TIMEOUT SET_NOTIFICATION_TYPE + SET_NOTIFICATION_DELAY_VALUE + PUSH_NOTIFICATIONS REPEAT_NOTIFICATIONS NO_NOTIFICATIONS + + 15 + 30 + 60 + 120 + 360 + 720 + SET_PICTURE_COMPRESSED SET_VIDEO_COMPRESSED SET_FORWARD_TAGS_IN_REPLY @@ -994,6 +1012,9 @@ The app failed to get a token Media cannot be uploaded! Open draft + Set the delay between each new fetch + Fetch notifications every: + Notifications fetch time diff --git a/app/src/main/res/xml/pref_notifications.xml b/app/src/main/res/xml/pref_notifications.xml index ccf60c47a..42a17cdfc 100644 --- a/app/src/main/res/xml/pref_notifications.xml +++ b/app/src/main/res/xml/pref_notifications.xml @@ -15,6 +15,17 @@ app:dialogTitle="@string/type_of_notifications" app:iconSpaceReserved="false" app:useSimpleSummaryProvider="true" /> + +