Compatibility.java

 1package eu.siacs.conversations.utils;
 2
 3import android.content.Context;
 4import android.content.SharedPreferences;
 5import android.content.pm.ApplicationInfo;
 6import android.content.pm.PackageManager;
 7import android.os.Build;
 8import android.preference.Preference;
 9import android.preference.PreferenceCategory;
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            "led",
26            "notification_ringtone",
27            "notification_headsup",
28            "vibrate_on_notification");
29    private static final List<String> UNUESD_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings");
30
31
32    public static boolean hasStoragePermission(Context context) {
33        return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
34    }
35
36    public static boolean runsTwentySix() {
37        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
38    }
39
40    public static boolean twentyEight() {
41        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
42    }
43
44    private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
45        return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
46    }
47
48    private static SharedPreferences getPreferences(final Context context) {
49        return PreferenceManager.getDefaultSharedPreferences(context);
50    }
51
52    private static boolean targetsTwentySix(Context context) {
53        try {
54            final PackageManager packageManager = context.getPackageManager();
55            final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
56            return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
57        } catch (PackageManager.NameNotFoundException | RuntimeException e) {
58            return true; //when in doubt…
59        }
60    }
61
62    public static boolean runsAndTargetsTwentySix(Context context) {
63        return runsTwentySix() && targetsTwentySix(context);
64    }
65
66    public static boolean keepForegroundService(Context context) {
67        return runsAndTargetsTwentySix(context) || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
68    }
69
70    public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
71        List<PreferenceCategory> categories = Arrays.asList(
72                (PreferenceCategory) settingsFragment.findPreference("notification_category"),
73                (PreferenceCategory) settingsFragment.findPreference("other_expert_category"));
74        for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
75            Preference preference = settingsFragment.findPreference(key);
76            if (preference != null) {
77                for (PreferenceCategory category : categories) {
78                    if (category != null) {
79                        category.removePreference(preference);
80                    }
81                }
82            }
83        }
84        if (Compatibility.runsTwentySix()) {
85            if (targetsTwentySix(settingsFragment.getContext())) {
86                Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
87                if (preference != null) {
88                    for (PreferenceCategory category : categories) {
89                        if (category != null) {
90                            category.removePreference(preference);
91                        }
92                    }
93                }
94            }
95        }
96    }
97}