diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-09-27 19:45:25 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-09-27 19:45:25 +0200 |
commit | 59dd2bb1c9b2be4c6b8802fbe0ee48085881d74d (patch) | |
tree | ef4ba57451d409f99653ea537aa4f6f021500bca /src/main/java/de/pixart/messenger/utils/Compatibility.java | |
parent | c8d159adca8258c88e91c8decc41fe1b90afc765 (diff) |
behave nicely (no foreground service by default) if app is built with targetSdk <26
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/Compatibility.java | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/Compatibility.java b/src/main/java/de/pixart/messenger/utils/Compatibility.java index 3ebc7c4f6..6d5f35bc4 100644 --- a/src/main/java/de/pixart/messenger/utils/Compatibility.java +++ b/src/main/java/de/pixart/messenger/utils/Compatibility.java @@ -2,6 +2,7 @@ package de.pixart.messenger.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; @@ -26,13 +27,13 @@ public class Compatibility { "notification_ringtone", "notification_headsup", "vibrate_on_notification"); - private static final List<String> UNUESD_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings"); + private static final List<String> UNUSED_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings"); public static boolean hasStoragePermission(Context context) { 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; } @@ -48,8 +49,22 @@ 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.SHOW_FOREGROUND_SERVICE, R.bool.show_foreground_service); + return runsTwentySix() || getBooleanPreference(context, SettingsActivity.SHOW_FOREGROUND_SERVICE, R.bool.show_foreground_service); } public static void removeUnusedPreferences(SettingsFragment settingsFragment) { @@ -57,7 +72,7 @@ public class Compatibility { (PreferenceScreen) settingsFragment.findPreference("notifications")); List<PreferenceCategory> categories = Arrays.asList( (PreferenceCategory) settingsFragment.findPreference("general")); - for (String key : (twentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) { + for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUSED_SETTINGS_PRE_TWENTYSIX)) { Preference preference = settingsFragment.findPreference(key); if (preference != null) { for (PreferenceScreen screen : screens) { @@ -72,5 +87,17 @@ public class Compatibility { } } } + if (Compatibility.runsTwentySix()) { + if (targetsTwentySix(settingsFragment.getContext())) { + Preference preference = settingsFragment.findPreference(SettingsActivity.SHOW_FOREGROUND_SERVICE); + if (preference != null) { + for (PreferenceCategory category : categories) { + if (category != null) { + category.removePreference(preference); + } + } + } + } + } } }
\ No newline at end of file |