From a655cab7aefb281e710ba6363729a9705805817b Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 28 Feb 2023 16:10:24 +0100 Subject: Request remove battery optimization --- app/src/main/AndroidManifest.xml | 1 + .../android/mastodon/jobs/FetchHomeWorker.java | 17 +++++++++++++- .../settings/FragmentNotificationsSettings.java | 26 ++++++++++++++++++++++ app/src/main/res/values/strings.xml | 3 +++ app/src/main/res/xml/pref_notifications.xml | 5 +++++ .../metadata/android/en/changelogs/482.txt | 3 ++- 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 399f8770b..4b06ef7cc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + 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 5b2e899a6..422f1e99e 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 @@ -169,6 +169,8 @@ public class FetchHomeWorker extends Worker { int call = 0; String max_id = null; MastodonTimelinesService mastodonTimelinesService = init(account.instance); + int insertValue = 0; + StatusCache lastStatusCache = null; while (canContinue && call < max_calls) { Call> homeCall = mastodonTimelinesService.getHome(account.token, max_id, null, null, status_per_page, null); if (homeCall != null) { @@ -184,12 +186,14 @@ public class FetchHomeWorker extends Worker { statusCache.status = status; statusCache.type = Timeline.TimeLineEnum.HOME; statusCache.status_id = status.id; + lastStatusCache = statusCache; try { - statusCacheDAO.insertOrUpdate(statusCache, Timeline.TimeLineEnum.HOME.getValue()); + insertValue = statusCacheDAO.insertOrUpdate(statusCache, Timeline.TimeLineEnum.HOME.getValue()); } catch (DBException e) { e.printStackTrace(); } } + Pagination pagination = MastodonHelper.getPagination(homeResponse.headers()); if (pagination.max_id != null) { max_id = pagination.max_id; @@ -211,6 +215,17 @@ public class FetchHomeWorker extends Worker { } call++; } + //insertValue is for last status and equals zero if updated or 1 if inserted + if (lastStatusCache != null && insertValue == 1) { //Last inserted message was not in cache. + StatusCache statusCacheDAO = new StatusCache(getApplicationContext()); + lastStatusCache.status.isFetchMore = true; + lastStatusCache.status.positionFetchMore = Status.PositionFetchMore.TOP; + try { + statusCacheDAO.updateIfExists(lastStatusCache); + } catch (DBException e) { + throw new RuntimeException(e); + } + } } } diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/settings/FragmentNotificationsSettings.java b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/settings/FragmentNotificationsSettings.java index be4b82288..a665adc93 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/settings/FragmentNotificationsSettings.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/fragment/settings/FragmentNotificationsSettings.java @@ -14,12 +14,16 @@ package app.fedilab.android.mastodon.ui.fragment.settings; * You should have received a copy of the GNU General Public License along with Fedilab; if not, * see . */ +import static android.content.Context.POWER_SERVICE; + import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.PowerManager; import android.provider.Settings; import androidx.annotation.NonNull; @@ -121,6 +125,28 @@ public class FragmentNotificationsSettings extends PreferenceFragmentCompat impl } } + Preference SET_KEY_IGNORE_BATTERY_OPTIMIZATIONS = findPreference(getString(R.string.SET_KEY_IGNORE_BATTERY_OPTIMIZATIONS)); + if (SET_KEY_IGNORE_BATTERY_OPTIMIZATIONS != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + PowerManager pm = (PowerManager) requireActivity().getSystemService(POWER_SERVICE); + String packageName = requireActivity().getPackageName(); + if (!pm.isIgnoringBatteryOptimizations(packageName)) { + SET_KEY_IGNORE_BATTERY_OPTIMIZATIONS.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(); + String packageName1 = requireActivity().getPackageName(); + intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + intent.setData(Uri.parse("package:" + packageName1)); + startActivity(intent); + return false; + }); + } else { + preferenceScreen.removePreferenceRecursively(getString(R.string.SET_KEY_IGNORE_BATTERY_OPTIMIZATIONS)); + } + } else { + preferenceScreen.removePreferenceRecursively(getString(R.string.SET_KEY_IGNORE_BATTERY_OPTIMIZATIONS)); + } + } + Preference button_mention = findPreference("button_mention"); assert button_mention != null; button_mention.setOnPreferenceClickListener(preference -> { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 33c2dfcea..a7955120e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -840,6 +840,8 @@ SET_NOTIFICATION_TYPE SET_LOAD_MEDIA_TYPE SET_NOTIFICATION_DELAY_VALUE + SET_KEY_IGNORE_BATTERY_OPTIMIZATIONS + SET_PUSH_DISTRIBUTOR PUSH_NOTIFICATIONS @@ -1923,4 +1925,5 @@ Warn only If there are missing media a dialog will be displayed with the ability to send the message without media description Send anyway + Ignore battery optimizations \ No newline at end of file diff --git a/app/src/main/res/xml/pref_notifications.xml b/app/src/main/res/xml/pref_notifications.xml index 3dea8a05b..7ae5fcfc5 100644 --- a/app/src/main/res/xml/pref_notifications.xml +++ b/app/src/main/res/xml/pref_notifications.xml @@ -33,6 +33,11 @@ app:key="@string/SET_PUSH_DISTRIBUTOR" app:title="@string/push_distributors" app:useSimpleSummaryProvider="true" /> + + diff --git a/src/fdroid/fastlane/metadata/android/en/changelogs/482.txt b/src/fdroid/fastlane/metadata/android/en/changelogs/482.txt index 020553135..755e5e4ec 100644 --- a/src/fdroid/fastlane/metadata/android/en/changelogs/482.txt +++ b/src/fdroid/fastlane/metadata/android/en/changelogs/482.txt @@ -1,10 +1,11 @@ Added: - Settings compose: display a dialog to warn if there are missing media description (default disabled) - +- Settings > Notification: disable battery optimization Changed: - Fixed: +- Fix an issue with cache and fetch more - Cache view with large fonts - Bad behaviors with truncated messages \ No newline at end of file -- cgit v1.2.3