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 { defaultConfig {
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 28 targetSdkVersion 25
versionCode 285 versionCode 285
versionName "2.3.0-beta.2" versionName "2.3.0-beta.2"
archivesBaseName += "-$versionName" archivesBaseName += "-$versionName"

View file

@ -8,6 +8,7 @@ import android.support.v4.content.ContextCompat;
import android.util.Log; import android.util.Log;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.Compatibility;
public class EventReceiver extends BroadcastReceiver { public class EventReceiver extends BroadcastReceiver {
@ -24,7 +25,11 @@ public class EventReceiver extends BroadcastReceiver {
final String action = originalIntent.getAction(); final String action = originalIntent.getAction();
if (action.equals("ui") || hasEnabledAccounts(context)) { if (action.equals("ui") || hasEnabledAccounts(context)) {
try { try {
if (Compatibility.runsAndTargetsTwentySix(context)) {
ContextCompat.startForegroundService(context, intentForService); ContextCompat.startForegroundService(context, intentForService);
} else {
context.startService(intentForService);
}
} catch (RuntimeException e) { } catch (RuntimeException e) {
Log.d(Config.LOGTAG,"EventReceiver was unable to start service"); Log.d(Config.LOGTAG,"EventReceiver was unable to start service");
} }

View file

@ -831,7 +831,7 @@ public class NotificationService {
public Notification createForegroundNotification() { public Notification createForegroundNotification() {
final Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService); final Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service)); 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(); List<Account> accounts = mXmppConnectionService.getAccounts();
int enabled = 0; int enabled = 0;
int connected = 0; int connected = 0;
@ -852,7 +852,7 @@ public class NotificationService {
mBuilder.setPriority(Notification.PRIORITY_LOW); mBuilder.setPriority(Notification.PRIORITY_LOW);
mBuilder.setSmallIcon(R.drawable.ic_link_white_24dp); mBuilder.setSmallIcon(R.drawable.ic_link_white_24dp);
if (Compatibility.twentySix()) { if (Compatibility.runsTwentySix()) {
mBuilder.setChannelId("foreground"); mBuilder.setChannelId("foreground");
} }
@ -907,7 +907,7 @@ public class NotificationService {
145, 145,
new Intent(mXmppConnectionService, ManageAccountActivity.class), new Intent(mXmppConnectionService, ManageAccountActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT)); PendingIntent.FLAG_UPDATE_CURRENT));
if (Compatibility.twentySix()) { if (Compatibility.runsTwentySix()) {
mBuilder.setChannelId("error"); mBuilder.setChannelId("error");
} }
notify(ERROR_NOTIFICATION_ID, mBuilder.build()); notify(ERROR_NOTIFICATION_ID, mBuilder.build());
@ -920,7 +920,7 @@ public class NotificationService {
mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp); mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp);
mBuilder.setContentIntent(createContentIntent(message.getConversation())); mBuilder.setContentIntent(createContentIntent(message.getConversation()));
mBuilder.setOngoing(true); mBuilder.setOngoing(true);
if (Compatibility.twentySix()) { if (Compatibility.runsTwentySix()) {
mBuilder.setChannelId("compression"); mBuilder.setChannelId("compression");
} }
Notification notification = mBuilder.build(); 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.OnAvatarPublication;
import eu.siacs.conversations.ui.interfaces.OnMediaLoaded; import eu.siacs.conversations.ui.interfaces.OnMediaLoaded;
import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable; 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.Compatibility;
import eu.siacs.conversations.utils.ConversationsFileObserver; import eu.siacs.conversations.utils.ConversationsFileObserver;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
@ -960,7 +959,7 @@ public class XmppConnectionService extends Service {
Resolver.init(this); Resolver.init(this);
this.mRandom = new SecureRandom(); this.mRandom = new SecureRandom();
updateMemorizingTrustmanager(); updateMemorizingTrustmanager();
if (Compatibility.twentySix()) { if (Compatibility.runsTwentySix()) {
mNotificationService.initializeChannels(); mNotificationService.initializeChannels();
} }
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); 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.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
import android.preference.PreferenceGroup;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.BoolRes; import android.support.annotation.BoolRes;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
@ -22,7 +22,6 @@ import eu.siacs.conversations.ui.SettingsFragment;
public class Compatibility { public class Compatibility {
private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList( private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList(
SettingsActivity.KEEP_FOREGROUND_SERVICE,
"led", "led",
"notification_ringtone", "notification_ringtone",
"notification_headsup", "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; 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; return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
} }
@ -50,15 +49,29 @@ public class Compatibility {
return PreferenceManager.getDefaultSharedPreferences(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 == 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) { 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) { public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
List<PreferenceCategory> categories = Arrays.asList( List<PreferenceCategory> categories = Arrays.asList(
(PreferenceCategory) settingsFragment.findPreference("notification_category"), (PreferenceCategory) settingsFragment.findPreference("notification_category"),
(PreferenceCategory) settingsFragment.findPreference("other_expert_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); Preference preference = settingsFragment.findPreference(key);
if (preference != null) { if (preference != null) {
for (PreferenceCategory category : categories) { 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);
}
}
}
}
}
} }
} }