1package eu.siacs.conversations.utils;
2
3import android.content.Context;
4import android.content.Intent;
5import android.content.SharedPreferences;
6import android.content.pm.ApplicationInfo;
7import android.content.pm.PackageManager;
8import android.os.Build;
9import android.preference.Preference;
10import android.preference.PreferenceCategory;
11import android.preference.PreferenceManager;
12import android.support.annotation.BoolRes;
13import android.support.v4.content.ContextCompat;
14import android.util.Log;
15
16import java.util.Arrays;
17import java.util.Collections;
18import java.util.List;
19
20import eu.siacs.conversations.Config;
21import eu.siacs.conversations.R;
22import eu.siacs.conversations.ui.SettingsActivity;
23import eu.siacs.conversations.ui.SettingsFragment;
24
25import static eu.siacs.conversations.services.EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE;
26
27public class Compatibility {
28
29 private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList(
30 "led",
31 "notification_ringtone",
32 "notification_headsup",
33 "vibrate_on_notification");
34 private static final List<String> UNUESD_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings");
35
36
37 public static boolean hasStoragePermission(Context context) {
38 return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
39 }
40
41 public static boolean runsTwentySix() {
42 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
43 }
44
45 public static boolean twentyEight() {
46 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
47 }
48
49 private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
50 return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
51 }
52
53 private static SharedPreferences getPreferences(final Context context) {
54 return PreferenceManager.getDefaultSharedPreferences(context);
55 }
56
57 private static boolean targetsTwentySix(Context context) {
58 try {
59 final PackageManager packageManager = context.getPackageManager();
60 final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
61 return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
62 } catch (PackageManager.NameNotFoundException | RuntimeException e) {
63 return true; //when in doubt…
64 }
65 }
66
67 public static boolean runsAndTargetsTwentySix(Context context) {
68 return runsTwentySix() && targetsTwentySix(context);
69 }
70
71 public static boolean keepForegroundService(Context context) {
72 return runsAndTargetsTwentySix(context) || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
73 }
74
75 public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
76 List<PreferenceCategory> categories = Arrays.asList(
77 (PreferenceCategory) settingsFragment.findPreference("notification_category"),
78 (PreferenceCategory) settingsFragment.findPreference("other_expert_category"));
79 for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
80 Preference preference = settingsFragment.findPreference(key);
81 if (preference != null) {
82 for (PreferenceCategory category : categories) {
83 if (category != null) {
84 category.removePreference(preference);
85 }
86 }
87 }
88 }
89 if (Compatibility.runsTwentySix()) {
90 if (targetsTwentySix(settingsFragment.getContext())) {
91 Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
92 if (preference != null) {
93 for (PreferenceCategory category : categories) {
94 if (category != null) {
95 category.removePreference(preference);
96 }
97 }
98 }
99 }
100 }
101 }
102
103 public static void startService(Context context, Intent intent) {
104 try {
105 if (Compatibility.runsAndTargetsTwentySix(context)) {
106 intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
107 ContextCompat.startForegroundService(context, intent);
108 } else {
109 context.startService(intent);
110 }
111 } catch (RuntimeException e) {
112 Log.d(Config.LOGTAG, context.getClass().getSimpleName()+" was unable to start service");
113 }
114 }
115}