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