From b19cd7c0c9bfa2f6c292d41bbe2721ad5bf20beb Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 1 Feb 2023 15:14:37 +0100 Subject: [PATCH] Add settings --- .../mastodon/jobs/FetchHomeWorker.java | 49 +++++++++---------- .../settings/FragmentHomeCacheSettings.java | 43 ++++++++-------- .../res/layouts/mastodon/values/strings.xml | 5 +- app/src/main/res/values/strings.xml | 6 +++ app/src/main/res/xml/pref_home_cache.xml | 2 +- 5 files changed, 53 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/mastodon/jobs/FetchHomeWorker.java b/app/src/main/java/app/fedilab/android/mastodon/jobs/FetchHomeWorker.java index f4c6a849..c57fca9b 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/jobs/FetchHomeWorker.java +++ b/app/src/main/java/app/fedilab/android/mastodon/jobs/FetchHomeWorker.java @@ -90,17 +90,13 @@ public class FetchHomeWorker extends Worker { @Override public ListenableFuture getForegroundInfoAsync() { if (Build.VERSION.SDK_INT >= 26) { - String channelName = "Notifications"; - String channelDescription = "Fetched notifications"; - NotificationChannel notifChannel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_LOW); - notifChannel.setDescription(channelDescription); - notifChannel.setSound(null, null); - notifChannel.setShowBadge(false); - notificationManager.createNotificationChannel(notifChannel); - if (notificationManager.getNotificationChannel("notifications") != null) { - notificationManager.deleteNotificationChannel("notifications"); - } - + String channelName = "Fetch Home"; + String channelDescription = "Fetch home messages"; + NotificationChannel fetchHomeChannel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_LOW); + fetchHomeChannel.setDescription(channelDescription); + fetchHomeChannel.setSound(null, null); + fetchHomeChannel.setShowBadge(false); + notificationManager.createNotificationChannel(fetchHomeChannel); } NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID); notificationBuilder.setSmallIcon(R.drawable.ic_notification) @@ -115,20 +111,20 @@ public class FetchHomeWorker extends Worker { @NonNull private ForegroundInfo createForegroundInfo() { if (Build.VERSION.SDK_INT >= 26) { - String channelName = "Notifications"; - String channelDescription = "Fetched notifications"; - NotificationChannel notifChannel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_LOW); - notifChannel.setSound(null, null); - notifChannel.setShowBadge(false); - notifChannel.setDescription(channelDescription); - notificationManager.createNotificationChannel(notifChannel); + String channelName = "Fetch Home"; + String channelDescription = "Fetch home messages"; + NotificationChannel fetchHomeChannel = new NotificationChannel(CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_LOW); + fetchHomeChannel.setSound(null, null); + fetchHomeChannel.setShowBadge(false); + fetchHomeChannel.setDescription(channelDescription); + notificationManager.createNotificationChannel(fetchHomeChannel); } NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID); notificationBuilder.setSmallIcon(R.drawable.ic_notification) .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_launcher_foreground)) - .setContentTitle(getApplicationContext().getString(R.string.notifications)) - .setContentText(getApplicationContext().getString(R.string.fetch_notifications)) + .setContentTitle(getApplicationContext().getString(R.string.fetch_home_messages)) + .setContentText(getApplicationContext().getString(R.string.set_fetch_home)) .setDefaults(NotificationCompat.DEFAULT_ALL) .setSilent(true) .setPriority(Notification.PRIORITY_LOW); @@ -183,12 +179,7 @@ public class FetchHomeWorker extends Worker { statusCache.type = Timeline.TimeLineEnum.HOME; statusCache.status_id = status.id; try { - int inserted = statusCacheDAO.insertOrUpdate(statusCache, Timeline.TimeLineEnum.HOME.getValue()); - //We reached already cached messages - if (inserted == 0) { - canContinue = false; - break; - } + statusCacheDAO.insertOrUpdate(statusCache, Timeline.TimeLineEnum.HOME.getValue()); } catch (DBException e) { e.printStackTrace(); } @@ -206,6 +197,12 @@ public class FetchHomeWorker extends Worker { canContinue = false; } } + //Pause between calls (1 second) + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } call++; } diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/settings/FragmentHomeCacheSettings.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/settings/FragmentHomeCacheSettings.java index 3b1489a4..78a88960 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/settings/FragmentHomeCacheSettings.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/settings/FragmentHomeCacheSettings.java @@ -23,7 +23,6 @@ import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceManager; import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreference; -import androidx.preference.SwitchPreferenceCompat; import androidx.work.WorkManager; import app.fedilab.android.R; @@ -53,7 +52,7 @@ public class FragmentHomeCacheSettings extends PreferenceFragmentCompat implemen return; } SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity()); - SwitchPreferenceCompat SET_FETCH_HOME = findPreference(getString(R.string.SET_FETCH_HOME)); + SwitchPreference SET_FETCH_HOME = findPreference(getString(R.string.SET_FETCH_HOME)); if (SET_FETCH_HOME != null) { boolean checked = sharedpreferences.getBoolean(getString(R.string.SET_FETCH_HOME) + MainActivity.currentUserID + MainActivity.currentInstance, false); SET_FETCH_HOME.setChecked(checked); @@ -69,27 +68,29 @@ public class FragmentHomeCacheSettings extends PreferenceFragmentCompat implemen @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - - if (key.compareToIgnoreCase(getString(R.string.SET_FETCH_HOME)) == 0) { - SharedPreferences.Editor editor = sharedPreferences.edit(); - SwitchPreference SET_FETCH_HOME = findPreference(getString(R.string.SET_FETCH_HOME)); - if (SET_FETCH_HOME != null) { - editor.putBoolean(getString(R.string.SET_FETCH_HOME) + MainActivity.currentUserID + MainActivity.currentInstance, SET_FETCH_HOME.isChecked()); - editor.commit(); - if (SET_FETCH_HOME.isChecked()) { - FetchHomeWorker.setRepeatHome(requireActivity(), MainActivity.currentAccount); - } else { - WorkManager.getInstance(requireActivity()).cancelAllWorkByTag(Helper.WORKER_REFRESH_HOME + MainActivity.currentUserID + MainActivity.currentInstance); + if (getActivity() != null) { + SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(requireActivity()); + if (key.compareToIgnoreCase(getString(R.string.SET_FETCH_HOME)) == 0) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + SwitchPreference SET_FETCH_HOME = findPreference(getString(R.string.SET_FETCH_HOME)); + if (SET_FETCH_HOME != null) { + editor.putBoolean(getString(R.string.SET_FETCH_HOME) + MainActivity.currentUserID + MainActivity.currentInstance, SET_FETCH_HOME.isChecked()); + editor.commit(); + if (SET_FETCH_HOME.isChecked()) { + FetchHomeWorker.setRepeatHome(requireActivity(), MainActivity.currentAccount); + } else { + WorkManager.getInstance(requireActivity()).cancelAllWorkByTag(Helper.WORKER_REFRESH_HOME + MainActivity.currentUserID + MainActivity.currentInstance); + } } } - } - if (key.compareToIgnoreCase(getString(R.string.SET_FETCH_HOME_DELAY_VALUE)) == 0) { - ListPreference SET_FETCH_HOME_DELAY_VALUE = findPreference(getString(R.string.SET_FETCH_HOME_DELAY_VALUE)); - if (SET_FETCH_HOME_DELAY_VALUE != null) { - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putString(getString(R.string.SET_FETCH_HOME_DELAY_VALUE) + MainActivity.currentUserID + MainActivity.currentInstance, SET_FETCH_HOME_DELAY_VALUE.getValue()); - editor.commit(); - FetchHomeWorker.setRepeatHome(requireActivity(), MainActivity.currentAccount); + if (key.compareToIgnoreCase(getString(R.string.SET_FETCH_HOME_DELAY_VALUE)) == 0) { + ListPreference SET_FETCH_HOME_DELAY_VALUE = findPreference(getString(R.string.SET_FETCH_HOME_DELAY_VALUE)); + if (SET_FETCH_HOME_DELAY_VALUE != null) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(getString(R.string.SET_FETCH_HOME_DELAY_VALUE) + MainActivity.currentUserID + MainActivity.currentInstance, SET_FETCH_HOME_DELAY_VALUE.getValue()); + editor.commit(); + FetchHomeWorker.setRepeatHome(requireActivity(), MainActivity.currentAccount); + } } } } diff --git a/app/src/main/res/layouts/mastodon/values/strings.xml b/app/src/main/res/layouts/mastodon/values/strings.xml index cc30206f..0d2c4cc4 100644 --- a/app/src/main/res/layouts/mastodon/values/strings.xml +++ b/app/src/main/res/layouts/mastodon/values/strings.xml @@ -1,7 +1,4 @@ - Fetch Home every - Home fetch time - Automatically fetch home messages - Home cache + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 92c5c292..10af09e3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2236,4 +2236,10 @@ Compose Your Peertube is too old and cannot be supported by the app. Two factor authentication token + + Fetch Home every + Home fetch time + Automatically fetch home messages + Home cache + Fetch home messages \ No newline at end of file diff --git a/app/src/main/res/xml/pref_home_cache.xml b/app/src/main/res/xml/pref_home_cache.xml index 212449c1..0cd45df4 100644 --- a/app/src/main/res/xml/pref_home_cache.xml +++ b/app/src/main/res/xml/pref_home_cache.xml @@ -4,7 +4,7 @@ android:layout_width="match_parent" android:layout_height="match_parent"> -