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