Detailed changes
@@ -12,6 +12,7 @@ import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.QuickConversationsService;
+import eu.siacs.conversations.utils.Compatibility;
import java.security.SecureRandom;
public class AppSettings {
@@ -145,6 +146,11 @@ public class AppSettings {
ACCEPT_INVITES_FROM_STRANGERS, R.bool.accept_invites_from_strangers);
}
+ public boolean isKeepForegroundService() {
+ return Compatibility.twentySix()
+ || getBooleanPreference(KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
+ }
+
private boolean getBooleanPreference(@NonNull final String name, @BoolRes int res) {
final SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
@@ -1820,7 +1820,7 @@ public class NotificationService {
.setSmallIcon(connected > 0 ? R.drawable.ic_link_24dp : R.drawable.ic_link_off_24dp)
.setLocalOnly(true);
- if (Compatibility.runsTwentySix()) {
+ if (Compatibility.twentySix()) {
mBuilder.setChannelId("foreground");
mBuilder.addAction(
R.drawable.ic_logout_24dp,
@@ -1970,7 +1970,7 @@ public class NotificationService {
s()
? PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
: PendingIntent.FLAG_UPDATE_CURRENT));
- if (Compatibility.runsTwentySix()) {
+ if (Compatibility.twentySix()) {
mBuilder.setChannelId("error");
}
notify(ERROR_NOTIFICATION_ID, mBuilder.build());
@@ -1995,7 +1995,7 @@ public class NotificationService {
builder.setContentIntent(createContentIntent(message.getConversation()));
}
builder.setOngoing(true);
- if (Compatibility.runsTwentySix()) {
+ if (Compatibility.twentySix()) {
builder.setChannelId("compression");
}
return builder.build();
@@ -1401,11 +1401,11 @@ public class XmppConnectionService extends Service {
@Override
public void onCreate() {
LibIdnXmppStringprep.setup();
- if (Compatibility.runsTwentySix()) {
+ if (Compatibility.twentySix()) {
mNotificationService.initializeChannels();
}
mChannelDiscoveryService.initializeMuclumbusService();
- mForceDuringOnCreate.set(Compatibility.runsAndTargetsTwentySix(this));
+ mForceDuringOnCreate.set(Compatibility.twentySix());
toggleForegroundService();
this.destroyed = false;
OmemoSetting.load(this);
@@ -1657,7 +1657,7 @@ public class XmppConnectionService extends Service {
|| mForceDuringOnCreate.get()
|| ongoingVideoTranscoding
|| ongoing != null
- || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) {
+ || (appSettings.isKeepForegroundService() && hasEnabledAccounts())) {
final Notification notification;
if (ongoing != null) {
notification = this.mNotificationService.getOngoingCallNotification(ongoing);
@@ -1729,14 +1729,14 @@ public class XmppConnectionService extends Service {
public boolean foregroundNotificationNeedsUpdatingWhenErrorStateChanges() {
return !mOngoingVideoTranscoding.get()
&& ongoingCall.get() == null
- && Compatibility.keepForegroundService(this)
+ && appSettings.isKeepForegroundService()
&& hasEnabledAccounts();
}
@Override
public void onTaskRemoved(final Intent rootIntent) {
super.onTaskRemoved(rootIntent);
- if ((Compatibility.keepForegroundService(this) && hasEnabledAccounts())
+ if ((appSettings.isKeepForegroundService() && hasEnabledAccounts())
|| mOngoingVideoTranscoding.get()
|| ongoingCall.get() != null) {
Log.d(Config.LOGTAG, "ignoring onTaskRemoved because foreground service is activated");
@@ -75,7 +75,7 @@ public class NotificationsSettingsFragment extends XmppPreferenceFragment {
|| callIntegration == null) {
throw new IllegalStateException("The preference resource file is missing preferences");
}
- if (Compatibility.runsTwentySix()) {
+ if (Compatibility.twentySix()) {
notificationRingtone.setVisible(false);
notificationHeadsUp.setVisible(false);
notificationVibrate.setVisible(false);
@@ -6,40 +6,30 @@ import android.annotation.SuppressLint;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
-import android.content.SharedPreferences;
-import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
-import android.preference.PreferenceManager;
import android.util.Log;
-
-import androidx.annotation.BoolRes;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
-
-import eu.siacs.conversations.AppSettings;
import eu.siacs.conversations.Config;
-import eu.siacs.conversations.R;
public class Compatibility {
public static boolean hasStoragePermission(final Context context) {
- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU || ContextCompat.checkSelfPermission(
- context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
+ return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
+ || ContextCompat.checkSelfPermission(
+ context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
+ == PackageManager.PERMISSION_GRANTED;
}
public static boolean s() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
}
- private static boolean runsTwentyFour() {
- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
- }
-
- public static boolean runsTwentySix() {
+ public static boolean twentySix() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}
@@ -47,56 +37,9 @@ public class Compatibility {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
}
- private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
- return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
- }
-
- private static SharedPreferences getPreferences(final Context context) {
- return PreferenceManager.getDefaultSharedPreferences(context);
- }
-
- private static boolean targetsTwentySix(Context context) {
- try {
- final PackageManager packageManager = context.getPackageManager();
- final ApplicationInfo applicationInfo =
- packageManager.getApplicationInfo(context.getPackageName(), 0);
- return applicationInfo.targetSdkVersion >= 26;
- } catch (PackageManager.NameNotFoundException | RuntimeException e) {
- return true; // when in doubt…
- }
- }
-
- private static boolean targetsTwentyFour(Context context) {
- try {
- final PackageManager packageManager = context.getPackageManager();
- final ApplicationInfo applicationInfo =
- packageManager.getApplicationInfo(context.getPackageName(), 0);
- return applicationInfo.targetSdkVersion >= 24;
- } catch (PackageManager.NameNotFoundException | RuntimeException e) {
- return true; // when in doubt…
- }
- }
-
- public static boolean runsAndTargetsTwentySix(Context context) {
- return runsTwentySix() && targetsTwentySix(context);
- }
-
- public static boolean runsAndTargetsTwentyFour(Context context) {
- return runsTwentyFour() && targetsTwentyFour(context);
- }
-
- public static boolean keepForegroundService(Context context) {
- return runsAndTargetsTwentySix(context)
- || getBooleanPreference(
- context,
- AppSettings.KEEP_FOREGROUND_SERVICE,
- R.bool.enable_foreground_service);
- }
-
-
public static void startService(final Context context, final Intent intent) {
try {
- if (Compatibility.runsAndTargetsTwentySix(context)) {
+ if (Compatibility.twentySix()) {
intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
ContextCompat.startForegroundService(context, intent);
} else {