1package eu.siacs.conversations.utils;
2
3import android.content.Context;
4import android.content.SharedPreferences;
5import android.content.pm.PackageManager;
6import android.os.Build;
7import android.preference.Preference;
8import android.preference.PreferenceCategory;
9import android.preference.PreferenceGroup;
10import android.preference.PreferenceManager;
11import android.support.annotation.BoolRes;
12import android.support.v4.content.ContextCompat;
13
14import java.util.Arrays;
15import java.util.Collections;
16import java.util.List;
17
18import eu.siacs.conversations.R;
19import eu.siacs.conversations.ui.SettingsActivity;
20import eu.siacs.conversations.ui.SettingsFragment;
21
22public class Compatibility {
23
24 private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList(
25 SettingsActivity.KEEP_FOREGROUND_SERVICE,
26 "led",
27 "notification_ringtone",
28 "notification_headsup",
29 "vibrate_on_notification");
30 private static final List<String> UNUESD_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings");
31
32
33 public static boolean hasStoragePermission(Context context) {
34 return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
35 }
36
37 public static boolean twentySix() {
38 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
39 }
40
41 public static boolean twentyTwo() {
42 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
43 }
44
45 private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
46 return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
47 }
48
49 private static SharedPreferences getPreferences(final Context context) {
50 return PreferenceManager.getDefaultSharedPreferences(context);
51 }
52
53 public static boolean keepForegroundService(Context context) {
54 return twentySix() || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
55 }
56
57 public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
58 List<PreferenceCategory> categories = Arrays.asList(
59 (PreferenceCategory) settingsFragment.findPreference("notification_category"),
60 (PreferenceCategory) settingsFragment.findPreference("other_expert_category"));
61 for (String key : (twentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
62 Preference preference = settingsFragment.findPreference(key);
63 if (preference != null) {
64 for (PreferenceCategory category : categories) {
65 if (category != null) {
66 category.removePreference(preference);
67 }
68 }
69 }
70 }
71 }
72}