From 6ede6c7f850b3401c978edc5ef764c4167388464 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 18 Sep 2018 11:33:18 +0200 Subject: [PATCH] behave nicely (no foreground service by default) if app is built with targetSdk <26 --- build.gradle | 2 +- .../conversations/services/EventReceiver.java | 7 +++- .../services/NotificationService.java | 8 ++--- .../services/XmppConnectionService.java | 3 +- .../conversations/utils/Compatibility.java | 35 ++++++++++++++++--- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/build.gradle b/build.gradle index e9dee750a..8b403ef0f 100644 --- a/build.gradle +++ b/build.gradle @@ -64,7 +64,7 @@ android { defaultConfig { minSdkVersion 19 - targetSdkVersion 28 + targetSdkVersion 25 versionCode 285 versionName "2.3.0-beta.2" archivesBaseName += "-$versionName" diff --git a/src/main/java/eu/siacs/conversations/services/EventReceiver.java b/src/main/java/eu/siacs/conversations/services/EventReceiver.java index fe8f469a5..608b1fd72 100644 --- a/src/main/java/eu/siacs/conversations/services/EventReceiver.java +++ b/src/main/java/eu/siacs/conversations/services/EventReceiver.java @@ -8,6 +8,7 @@ import android.support.v4.content.ContextCompat; import android.util.Log; import eu.siacs.conversations.Config; +import eu.siacs.conversations.utils.Compatibility; public class EventReceiver extends BroadcastReceiver { @@ -24,7 +25,11 @@ public class EventReceiver extends BroadcastReceiver { final String action = originalIntent.getAction(); if (action.equals("ui") || hasEnabledAccounts(context)) { try { - ContextCompat.startForegroundService(context, intentForService); + if (Compatibility.runsAndTargetsTwentySix(context)) { + ContextCompat.startForegroundService(context, intentForService); + } else { + context.startService(intentForService); + } } catch (RuntimeException e) { Log.d(Config.LOGTAG,"EventReceiver was unable to start service"); } diff --git a/src/main/java/eu/siacs/conversations/services/NotificationService.java b/src/main/java/eu/siacs/conversations/services/NotificationService.java index 5bf345ea6..d800f1685 100644 --- a/src/main/java/eu/siacs/conversations/services/NotificationService.java +++ b/src/main/java/eu/siacs/conversations/services/NotificationService.java @@ -831,7 +831,7 @@ public class NotificationService { public Notification createForegroundNotification() { final Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService); mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service)); - if (Compatibility.twentySix() || Config.SHOW_CONNECTED_ACCOUNTS) { + if (Compatibility.runsAndTargetsTwentySix(mXmppConnectionService) || Config.SHOW_CONNECTED_ACCOUNTS) { List accounts = mXmppConnectionService.getAccounts(); int enabled = 0; int connected = 0; @@ -852,7 +852,7 @@ public class NotificationService { mBuilder.setPriority(Notification.PRIORITY_LOW); mBuilder.setSmallIcon(R.drawable.ic_link_white_24dp); - if (Compatibility.twentySix()) { + if (Compatibility.runsTwentySix()) { mBuilder.setChannelId("foreground"); } @@ -907,7 +907,7 @@ public class NotificationService { 145, new Intent(mXmppConnectionService, ManageAccountActivity.class), PendingIntent.FLAG_UPDATE_CURRENT)); - if (Compatibility.twentySix()) { + if (Compatibility.runsTwentySix()) { mBuilder.setChannelId("error"); } notify(ERROR_NOTIFICATION_ID, mBuilder.build()); @@ -920,7 +920,7 @@ public class NotificationService { mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp); mBuilder.setContentIntent(createContentIntent(message.getConversation())); mBuilder.setOngoing(true); - if (Compatibility.twentySix()) { + if (Compatibility.runsTwentySix()) { mBuilder.setChannelId("compression"); } Notification notification = mBuilder.build(); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 69a8e1abb..3c7dabbee 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -108,7 +108,6 @@ import eu.siacs.conversations.ui.UiCallback; import eu.siacs.conversations.ui.interfaces.OnAvatarPublication; import eu.siacs.conversations.ui.interfaces.OnMediaLoaded; import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable; -import eu.siacs.conversations.ui.util.Attachment; import eu.siacs.conversations.utils.Compatibility; import eu.siacs.conversations.utils.ConversationsFileObserver; import eu.siacs.conversations.utils.CryptoHelper; @@ -960,7 +959,7 @@ public class XmppConnectionService extends Service { Resolver.init(this); this.mRandom = new SecureRandom(); updateMemorizingTrustmanager(); - if (Compatibility.twentySix()) { + if (Compatibility.runsTwentySix()) { mNotificationService.initializeChannels(); } final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); diff --git a/src/main/java/eu/siacs/conversations/utils/Compatibility.java b/src/main/java/eu/siacs/conversations/utils/Compatibility.java index 487886bea..6eca0c74c 100644 --- a/src/main/java/eu/siacs/conversations/utils/Compatibility.java +++ b/src/main/java/eu/siacs/conversations/utils/Compatibility.java @@ -2,11 +2,11 @@ package eu.siacs.conversations.utils; import android.content.Context; import android.content.SharedPreferences; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.os.Build; import android.preference.Preference; import android.preference.PreferenceCategory; -import android.preference.PreferenceGroup; import android.preference.PreferenceManager; import android.support.annotation.BoolRes; import android.support.v4.content.ContextCompat; @@ -22,7 +22,6 @@ import eu.siacs.conversations.ui.SettingsFragment; public class Compatibility { private static final List UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList( - SettingsActivity.KEEP_FOREGROUND_SERVICE, "led", "notification_ringtone", "notification_headsup", @@ -34,7 +33,7 @@ public class Compatibility { return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; } - public static boolean twentySix() { + public static boolean runsTwentySix() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; } @@ -50,15 +49,29 @@ public class Compatibility { 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 == null || applicationInfo.targetSdkVersion >= 26; + } catch (PackageManager.NameNotFoundException e) { + return true; //when in doubt… + } + } + + public static boolean runsAndTargetsTwentySix(Context context) { + return runsTwentySix() && targetsTwentySix(context); + } + public static boolean keepForegroundService(Context context) { - return twentySix() || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service); + return runsAndTargetsTwentySix(context) || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service); } public static void removeUnusedPreferences(SettingsFragment settingsFragment) { List categories = Arrays.asList( (PreferenceCategory) settingsFragment.findPreference("notification_category"), (PreferenceCategory) settingsFragment.findPreference("other_expert_category")); - for (String key : (twentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) { + for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) { Preference preference = settingsFragment.findPreference(key); if (preference != null) { for (PreferenceCategory category : categories) { @@ -68,5 +81,17 @@ public class Compatibility { } } } + if (Compatibility.runsTwentySix()) { + if (targetsTwentySix(settingsFragment.getContext())) { + Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE); + if (preference != null) { + for (PreferenceCategory category : categories) { + if (category != null) { + category.removePreference(preference); + } + } + } + } + } } }