diff --git a/res/values/strings.xml b/res/values/strings.xml
index f2e7f6015..9372738dc 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -482,10 +482,6 @@
Default color
Custom image
Custom color
-
- Reliable background connection
- Requires a permanent notification
-
Delete old messages
@@ -624,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
@@ -713,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.
@@ -722,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.
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());