Compatibility.java

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