behave nicely (no foreground service by default) if app is built with targetSdk <26

This commit is contained in:
Daniel Gultsch 2018-09-18 11:33:18 +02:00
parent 88dc7bae1c
commit 6ede6c7f85
5 changed files with 42 additions and 13 deletions

View File

@ -64,7 +64,7 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 25
versionCode 285
versionName "2.3.0-beta.2"
archivesBaseName += "-$versionName"

View File

@ -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");
}

View File

@ -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<Account> 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();

View File

@ -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);

View File

@ -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<String> 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<PreferenceCategory> 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);
}
}
}
}
}
}
}