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