Merge pull request #1608 from deltachat/notify-bg-switch

add switch to enable/disable battery optimizations
This commit is contained in:
bjoern
2020-09-06 13:44:10 +02:00
committed by GitHub
3 changed files with 72 additions and 7 deletions
+9 -7
View File
@@ -482,10 +482,6 @@
<string name="pref_background_default_color">Default color</string>
<string name="pref_background_custom_image">Custom image</string>
<string name="pref_background_custom_color">Custom color</string>
<!-- disabling "Reliable service" will hide a the maybe annoying permanent-notification with the drawback that new-message-notifications get potentially unreliable -->
<string name="pref_reliable_service">Reliable background connection</string>
<string name="pref_reliable_service_explain">Requires a permanent notification</string>
<!-- automatically delete message -->
<string name="delete_old_messages">Delete old messages</string>
@@ -624,9 +620,6 @@
<string name="perm_explain_access_to_mic_denied">To send audio messages, go to the app settings, select \"Permissions\", and enable \"Microphone\".</string>
<string name="perm_explain_access_to_storage_denied">To recieve or send files, go to the app settings, select \"Permissions\", and enable \"Storage\".</string>
<string name="perm_explain_access_to_location_denied">To attach a location, go to the app settings, select \"Permissions\", and enable \"Location\".</string>
<string name="perm_enable_bg_reminder_title">Tap here to receive messages while Delta Chat is in the background.</string>
<string name="perm_enable_bg_reminder_text">Delta Chat uses few resources and takes care not to drain your battery.</string>
<string name="perm_enable_bg_already_done">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.</string>
<!-- ImageEditorHud -->
<string name="ImageEditorHud_draw_anywhere_to_blur">Draw anywhere to blur</string>
@@ -713,6 +706,7 @@
<string name="a11y_message_context_menu_btn_label">Message actions</string>
<string name="a11y_background_preview_label">Background preview</string>
<!-- iOS permissions, copy from "deltachat-ios/Info.plist", which is used on missing translations in "deltachat-ios/LANG.lproj/InfoPlist.strings" -->
<string name="InfoPlist_NSCameraUsageDescription">Delta Chat uses your camera to take and send photos and videos and to scan QR codes.</string>
<string name="InfoPlist_NSContactsUsageDescription">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.</string>
@@ -722,5 +716,13 @@
<!-- android specific strings, developers: please take care to remove strings that are no longer used! -->
<string name="pref_background_notifications">Background notifications</string>
<string name="pref_background_notifications_explain">Uses a background connection to your server and requires ignored battery optimizations.</string>
<!-- disabling "Reliable service" will hide a the maybe annoying permanent-notification with the drawback that new-message-notifications get potentially unreliable -->
<string name="pref_reliable_service">Reliable background connection</string>
<string name="pref_reliable_service_explain">Requires a permanent notification</string>
<string name="perm_enable_bg_reminder_title">Tap here to receive messages while Delta Chat is in the background.</string>
<string name="perm_enable_bg_reminder_text">Delta Chat uses few resources and takes care not to drain your battery.</string>
<string name="perm_enable_bg_already_done">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.</string>
</resources>
+7
View File
@@ -7,6 +7,13 @@
android:title="@string/pref_notifications"
android:defaultValue="true" />
<org.thoughtcrime.securesms.components.SwitchPreferenceCompat
android:dependency="pref_key_enable_notifications"
android:defaultValue="false"
android:key="pref_ignore_battery_optimizations"
android:title="@string/pref_ignore_battery"
android:summary="@string/pref_ignore_battery_explain"/>
<org.thoughtcrime.securesms.components.SwitchPreferenceCompat
android:dependency="pref_key_enable_notifications"
android:defaultValue="false"
@@ -1,13 +1,18 @@
package org.thoughtcrime.securesms.preferences;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.preference.CheckBoxPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
@@ -26,6 +31,8 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
private static final String TAG = NotificationsPreferenceFragment.class.getSimpleName();
private static final int REQUEST_CODE_NOTIFICATION_SELECTED = 1;
private CheckBoxPreference ignoreBattery;
@Override
public void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
@@ -61,6 +68,13 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
initializeRingtoneSummary(findPreference(Prefs.RINGTONE_PREF));
ignoreBattery = this.findPreference("pref_ignore_battery_optimizations");
ignoreBattery.setVisible(needsIgnoreBatteryOptimizations());
ignoreBattery.setOnPreferenceChangeListener((preference, newValue) -> {
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());