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    private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
36        return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
37    }
38
39    private static SharedPreferences getPreferences(final Context context) {
40        return PreferenceManager.getDefaultSharedPreferences(context);
41    }
42
43    public static boolean keepForegroundService(Context context) {
44        return twentySix() || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
45    }
46
47    public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
48        List<PreferenceCategory> categories = Arrays.asList(
49                (PreferenceCategory) settingsFragment.findPreference("notification_category"),
50                (PreferenceCategory) settingsFragment.findPreference("other_expert_category"));
51        for (String key : (twentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
52            Preference preference = settingsFragment.findPreference(key);
53            if (preference != null) {
54                for (PreferenceCategory category : categories) {
55                    if (category != null) {
56                        category.removePreference(preference);
57                    }
58                }
59            }
60        }
61    }
62}