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 runsTwentyOne() {
42 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
43 }
44
45 private static boolean runsTwentyFour() {
46 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
47 }
48
49 public static boolean runsTwentySix() {
50 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
51 }
52
53 public static boolean twentyEight() {
54 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
55 }
56
57 private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
58 return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
59 }
60
61 private static SharedPreferences getPreferences(final Context context) {
62 return PreferenceManager.getDefaultSharedPreferences(context);
63 }
64
65 private static boolean targetsTwentySix(Context context) {
66 try {
67 final PackageManager packageManager = context.getPackageManager();
68 final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
69 return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
70 } catch (PackageManager.NameNotFoundException | RuntimeException e) {
71 return true; //when in doubt…
72 }
73 }
74
75 private static boolean targetsTwentyFour(Context context) {
76 try {
77 final PackageManager packageManager = context.getPackageManager();
78 final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
79 return applicationInfo == null || applicationInfo.targetSdkVersion >= 24;
80 } catch (PackageManager.NameNotFoundException | RuntimeException e) {
81 return true; //when in doubt…
82 }
83 }
84
85 public static boolean runsAndTargetsTwentySix(Context context) {
86 return runsTwentySix() && targetsTwentySix(context);
87 }
88
89 public static boolean runsAndTargetsTwentyFour(Context context) {
90 return runsTwentyFour() && targetsTwentyFour(context);
91 }
92
93 public static boolean keepForegroundService(Context context) {
94 return runsAndTargetsTwentySix(context) || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
95 }
96
97 public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
98 List<PreferenceCategory> categories = Arrays.asList(
99 (PreferenceCategory) settingsFragment.findPreference("notification_category"),
100 (PreferenceCategory) settingsFragment.findPreference("advanced"));
101 for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
102 Preference preference = settingsFragment.findPreference(key);
103 if (preference != null) {
104 for (PreferenceCategory category : categories) {
105 if (category != null) {
106 category.removePreference(preference);
107 }
108 }
109 }
110 }
111 if (Compatibility.runsTwentySix()) {
112 if (targetsTwentySix(settingsFragment.getContext())) {
113 Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
114 if (preference != null) {
115 for (PreferenceCategory category : categories) {
116 if (category != null) {
117 category.removePreference(preference);
118 }
119 }
120 }
121 }
122 }
123 }
124
125 public static void startService(Context context, Intent intent) {
126 try {
127 if (Compatibility.runsAndTargetsTwentySix(context)) {
128 intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
129 ContextCompat.startForegroundService(context, intent);
130 } else {
131 context.startService(intent);
132 }
133 } catch (RuntimeException e) {
134 Log.d(Config.LOGTAG, context.getClass().getSimpleName()+" was unable to start service");
135 }
136 }
137}