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 bcc7907b..fd541ef4 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<BaseAccount> 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<BaseAccount> 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<BaseAccount> 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 58fb82ba..04c575eb 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 784fa727..65ca9643 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -738,12 +738,20 @@ <string name="no_account_in_list">No accounts found for this list!</string> <string name="scheduled">Scheduled</string> - <string-array name="set_notification_type_value" > + <string-array name="set_notification_type_value"> <item>Push notifications</item> <item>Fetch at fixed times</item> <item>No notifications</item> </string-array> + <string-array name="set_notification_delay_values"> + <item>15 minutes</item> + <item>30 minutes</item> + <item>1 hour</item> + <item>2 hours</item> + <item>6 hours</item> + <item>12 hours</item> + </string-array> <string name="SET_COOKIES" translatable="false">SET_COOKIES</string> <string name="SET_PROXY_ENABLED" translatable="false">SET_PROXY_ENABLED</string> @@ -758,11 +766,21 @@ <string name="SET_CUSTOM_USER_AGENT" translatable="false">SET_CUSTOM_USER_AGENT</string> <string name="SET_NSFW_TIMEOUT" translatable="false">SET_NSFW_TIMEOUT</string> <string name="SET_NOTIFICATION_TYPE" translatable="false">SET_NOTIFICATION_TYPE</string> + <string name="SET_NOTIFICATION_DELAY_VALUE" translatable="false">SET_NOTIFICATION_DELAY_VALUE</string> + <string-array name="SET_NOTIFICATION_TYPE_VALUE" translatable="false"> <item>PUSH_NOTIFICATIONS</item> <item>REPEAT_NOTIFICATIONS</item> <item>NO_NOTIFICATIONS</item> </string-array> + <string-array name="SET_NOTIFICATION_DELAYS_VALUES" translatable="false"> + <item>15</item> + <item>30</item> + <item>60</item> + <item>120</item> + <item>360</item> + <item>720</item> + </string-array> <string name="SET_PICTURE_COMPRESSED" translatable="false">SET_PICTURE_COMPRESSED</string> <string name="SET_VIDEO_COMPRESSED" translatable="false">SET_VIDEO_COMPRESSED</string> <string name="SET_FORWARD_TAGS_IN_REPLY" translatable="false">SET_FORWARD_TAGS_IN_REPLY</string> @@ -994,6 +1012,9 @@ <string name="toast_token">The app failed to get a token</string> <string name="media_cannot_be_uploaded">Media cannot be uploaded!</string> <string name="open_draft">Open draft</string> + <string name="set_push_notifications_delay">Set the delay between each new fetch</string> + <string name="refresh_every">Fetch notifications every:</string> + <string name="type_of_notifications_delay_title">Notifications fetch time</string> <string-array name="photo_editor_emoji" translatable="false"> <!-- Smiles --> diff --git a/app/src/main/res/xml/pref_notifications.xml b/app/src/main/res/xml/pref_notifications.xml index ccf60c47..42a17cdf 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" /> + + <ListPreference + app:defaultValue="15" + app:dialogTitle="@string/refresh_every" + app:entries="@array/set_notification_delay_values" + app:entryValues="@array/SET_NOTIFICATION_DELAYS_VALUES" + app:iconSpaceReserved="false" + app:key="@string/SET_NOTIFICATION_DELAY_VALUE" + app:summary="@string/set_push_notifications_delay" + app:title="@string/type_of_notifications_delay_title" + app:useSimpleSummaryProvider="true" /> </PreferenceCategory>