show permantent-notification unconditionally and add a switch to disable it

This commit is contained in:
B. Petersen
2020-04-28 15:16:50 +02:00
parent 1e345a40c4
commit 69d05ae0c7
7 changed files with 49 additions and 14 deletions
+4
View File
@@ -468,6 +468,10 @@
<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 service</string>
<string name="pref_reliable_service_explain">Requires a permanent notification</string>
<!-- automatically delete message -->
<string name="autodel_title">Auto-delete messages</string>
+6
View File
@@ -58,4 +58,10 @@
android:entries="@array/pref_notification_priority_entries"
android:entryValues="@array/pref_notification_priority_values"/>
<org.thoughtcrime.securesms.components.SwitchPreferenceCompat
android:defaultValue="true"
android:key="pref_reliable_service"
android:title="@string/pref_reliable_service"
android:summary="@string/pref_reliable_service_explain"/>
</PreferenceScreen>
@@ -40,6 +40,7 @@ import com.b44t.messenger.DcContext;
import org.thoughtcrime.securesms.connect.DcHelper;
import org.thoughtcrime.securesms.database.NoExternalStorageException;
import org.thoughtcrime.securesms.util.Prefs;
import org.thoughtcrime.securesms.util.Scrubber;
import org.thoughtcrime.securesms.util.StorageUtil;
@@ -240,6 +241,8 @@ public class LogViewFragment extends Fragment {
builder.append("ignoreBatteryOptimizations=").append(
powerManager.isIgnoringBatteryOptimizations(context.getPackageName())).append("\n");
}
builder.append("reliableService=").append(
Prefs.reliableService(context)).append("\n");
} catch (Exception e) {
builder.append("Unknown\n");
}
@@ -30,6 +30,7 @@ import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.model.ThreadRecord;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.Prefs;
import org.thoughtcrime.securesms.util.Util;
import java.io.File;
@@ -81,7 +82,7 @@ public class ApplicationDcContext extends DcContext {
BroadcastReceiver networkStateReceiver = new NetworkStateReceiver();
context.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
KeepAliveService.startSelf(context);
KeepAliveService.maybeStartSelf(context);
}
public void setStockTranslations() {
@@ -10,32 +10,29 @@ import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import androidx.core.app.NotificationCompat;
import android.util.Log;
import org.thoughtcrime.securesms.ConversationListActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.util.Prefs;
public class KeepAliveService extends Service {
static KeepAliveService s_this = null;
public static void maybeStartSelf(Context context) {
// note, that unfortunately, the check for isIgnoringBatteryOptimizations() is not sufficient,
// this checks only stock-android settings, several os have additional "optimizers" that ignore this setting.
// therefore, the most reliable way to not get killed is a permanent-foreground-notification.
if(Prefs.reliableService(context)) {
startSelf(context);
}
}
public static void startSelf(Context context)
{
try {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
if(powerManager.isIgnoringBatteryOptimizations(context.getPackageName())) {
return; // fine, the user has disabled the battery optimisations for us
}
}
else {
return; // android <= lollipop does not have a doze mode
}
// if we're here, we're on a system with doze mode and battery optimisations enabled.
// add foreground notification to stay alive.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// the started service has to call startForeground() within 5 seconds,
// see https://developer.android.com/about/versions/oreo/android-8.0-changes
@@ -10,12 +10,14 @@ import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import androidx.annotation.Nullable;
import androidx.preference.CheckBoxPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import android.text.TextUtils;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.connect.KeepAliveService;
import org.thoughtcrime.securesms.notifications.MessageNotifierCompat;
import org.thoughtcrime.securesms.util.Prefs;
@@ -64,6 +66,19 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme
initializeListSummary((ListPreference) findPreference(Prefs.NOTIFICATION_PRIORITY_PREF));
initializeRingtoneSummary(findPreference(Prefs.RINGTONE_PREF));
CheckBoxPreference reliableService = this.findPreference("pref_reliable_service");
reliableService.setOnPreferenceChangeListener((preference, newValue) -> {
Context context = getContext();
boolean enabled = (Boolean) newValue;
if (enabled) {
KeepAliveService.startSelf(context);
} else {
context.stopService(new Intent(context, KeepAliveService.class));
}
return true;
});
}
@Override
@@ -237,6 +237,15 @@ public class Prefs {
return result==null? null : Uri.parse(result);
}
public static boolean reliableService(Context context) {
try {
return getBooleanPreference(context, "pref_reliable_service", true);
}
catch(Exception e) {
return false;
}
}
// vibrate
public static boolean isNotificationVibrateEnabled(Context context) {