mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
add a switch to 'settings / notifications' to disable battery optimisations
This commit is contained in:
@@ -482,6 +482,8 @@
|
||||
<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>
|
||||
<string name="pref_ignore_battery">Fast notifications in background</string>
|
||||
<string name="pref_ignore_battery_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>
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user