From 34755f05ffba36aa3fdb896a98b26cd7279aa308 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Tue, 1 Sep 2020 00:46:07 +0200 Subject: [PATCH 1/2] add a switch to 'settings / notifications' to disable battery optimisations --- res/values/strings.xml | 2 + res/xml/preferences_notifications.xml | 7 +++ .../NotificationsPreferenceFragment.java | 56 +++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/res/values/strings.xml b/res/values/strings.xml index f2e7f6015..3b7921e21 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -482,6 +482,8 @@ Default color Custom image Custom color + Fast notifications in background + Uses a background connection to your server and requires ignored battery optimizations. Reliable background connection Requires a permanent notification diff --git a/res/xml/preferences_notifications.xml b/res/xml/preferences_notifications.xml index 3971ff3bb..113418b22 100644 --- a/res/xml/preferences_notifications.xml +++ b/res/xml/preferences_notifications.xml @@ -7,6 +7,13 @@ android:title="@string/pref_notifications" android:defaultValue="true" /> + + { + requestToggleIgnoreBatteryOptimizations(); + return true; + }); + CheckBoxPreference reliableService = this.findPreference("pref_reliable_service"); reliableService.setOnPreferenceChangeListener((preference, newValue) -> { @@ -96,6 +110,9 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme public void onResume() { super.onResume(); ((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.pref_notifications); + + // upate ignoreBattery in onResume() to reflects changes done in the system settings + ignoreBattery.setChecked(isIgnoringBatteryOptimizations()); } @Override @@ -133,6 +150,45 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme } } + private boolean needsIgnoreBatteryOptimizations() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; + } + + private boolean isIgnoringBatteryOptimizations() { + if (!needsIgnoreBatteryOptimizations()) { + return true; + } + PowerManager pm = (PowerManager)getActivity().getSystemService(Context.POWER_SERVICE); + if(pm.isIgnoringBatteryOptimizations(getActivity().getPackageName())) { + return true; + } + return false; + } + + private void requestToggleIgnoreBatteryOptimizations() { + Context context = getActivity(); + boolean openManualSettings = true; + + try { + if (needsIgnoreBatteryOptimizations() + && !isIgnoringBatteryOptimizations() + && ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_GRANTED) { + Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:" + context.getPackageName())); + context.startActivity(intent); + openManualSettings = false; + } + } catch (Exception e) { + e.printStackTrace(); + } + + if (openManualSettings && needsIgnoreBatteryOptimizations()) { + // fire ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS if ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS fails + // or if isIgnoringBatteryOptimizations() is already true (there is no intent to re-enable battery optimizations) + Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); + context.startActivity(intent); + } + } + private void initializeRingtoneSummary(Preference pref) { RingtoneSummaryListener listener = (RingtoneSummaryListener) pref.getOnPreferenceChangeListener(); Uri uri = Prefs.getNotificationRingtone(getContext()); From fe25b81d4f8786413d057a6eb56eda7e5cdfde35 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 6 Sep 2020 13:43:26 +0200 Subject: [PATCH 2/2] reword, sort android strings to corresponding section --- res/values/strings.xml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 3b7921e21..9372738dc 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -482,12 +482,6 @@ Default color Custom image Custom color - Fast notifications in background - Uses a background connection to your server and requires ignored battery optimizations. - - Reliable background connection - Requires a permanent notification - Delete old messages @@ -626,9 +620,6 @@ To send audio messages, go to the app settings, select \"Permissions\", and enable \"Microphone\". To recieve or send files, go to the app settings, select \"Permissions\", and enable \"Storage\". To attach a location, go to the app settings, select \"Permissions\", and enable \"Location\". - Tap here to receive messages while Delta Chat is in the background. - Delta Chat uses few resources and takes care not to drain your battery. - You already allowed Delta Chat to receive messages in the background.\n\nIf messages still do not arrive in background, please also check your system settings. Draw anywhere to blur @@ -715,6 +706,7 @@ Message actions Background preview + Delta Chat uses your camera to take and send photos and videos and to scan QR codes. Delta Chat uses your contacts to show a list of email addresses you can write to. Delta Chat has no server, your contacts are not sent anywhere. @@ -724,5 +716,13 @@ + Background notifications + Uses a background connection to your server and requires ignored battery optimizations. + + Reliable background connection + Requires a permanent notification + Tap here to receive messages while Delta Chat is in the background. + Delta Chat uses few resources and takes care not to drain your battery. + You already allowed Delta Chat to receive messages in the background.\n\nIf messages still do not arrive in background, please also check your system settings.