Compatibility.java

  1package eu.siacs.conversations.utils;
  2
  3import android.annotation.SuppressLint;
  4import android.content.Context;
  5import android.content.Intent;
  6import android.content.SharedPreferences;
  7import android.content.pm.ApplicationInfo;
  8import android.content.pm.PackageManager;
  9import android.os.Build;
 10import android.preference.Preference;
 11import android.preference.PreferenceCategory;
 12import android.preference.PreferenceManager;
 13import android.support.annotation.BoolRes;
 14import android.support.v4.content.ContextCompat;
 15import android.util.Log;
 16
 17import java.util.Arrays;
 18import java.util.Collections;
 19import java.util.List;
 20
 21import eu.siacs.conversations.Config;
 22import eu.siacs.conversations.R;
 23import eu.siacs.conversations.ui.SettingsActivity;
 24import eu.siacs.conversations.ui.SettingsFragment;
 25
 26import static eu.siacs.conversations.services.EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE;
 27
 28public class Compatibility {
 29
 30    private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList(
 31            "led",
 32            "notification_ringtone",
 33            "notification_headsup",
 34            "vibrate_on_notification",
 35            "call_ringtone"
 36    );
 37    private static final List<String> UNUESD_SETTINGS_PRE_TWENTYSIX = Arrays.asList(
 38            "message_notification_settings",
 39            "call_notification_settings"
 40    );
 41
 42
 43    public static boolean hasStoragePermission(Context context) {
 44        return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
 45    }
 46
 47    public static boolean runsTwentyOne() {
 48        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
 49    }
 50
 51    private static boolean runsTwentyFour() {
 52        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
 53    }
 54
 55    public static boolean runsTwentySix() {
 56        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
 57    }
 58
 59    public static boolean twentyEight() {
 60        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
 61    }
 62
 63    private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
 64        return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
 65    }
 66
 67    private static SharedPreferences getPreferences(final Context context) {
 68        return PreferenceManager.getDefaultSharedPreferences(context);
 69    }
 70
 71    private static boolean targetsTwentySix(Context context) {
 72        try {
 73            final PackageManager packageManager = context.getPackageManager();
 74            final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
 75            return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
 76        } catch (PackageManager.NameNotFoundException | RuntimeException e) {
 77            return true; //when in doubt…
 78        }
 79    }
 80
 81    private static boolean targetsTwentyFour(Context context) {
 82        try {
 83            final PackageManager packageManager = context.getPackageManager();
 84            final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
 85            return applicationInfo == null || applicationInfo.targetSdkVersion >= 24;
 86        } catch (PackageManager.NameNotFoundException | RuntimeException e) {
 87            return true; //when in doubt…
 88        }
 89    }
 90
 91    public static boolean runsAndTargetsTwentySix(Context context) {
 92        return runsTwentySix() && targetsTwentySix(context);
 93    }
 94
 95    public static boolean runsAndTargetsTwentyFour(Context context) {
 96        return runsTwentyFour() && targetsTwentyFour(context);
 97    }
 98
 99    public static boolean keepForegroundService(Context context) {
100        return runsAndTargetsTwentySix(context) || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
101    }
102
103    public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
104        List<PreferenceCategory> categories = Arrays.asList(
105                (PreferenceCategory) settingsFragment.findPreference("notification_category"),
106                (PreferenceCategory) settingsFragment.findPreference("advanced"));
107        for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
108            Preference preference = settingsFragment.findPreference(key);
109            if (preference != null) {
110                for (PreferenceCategory category : categories) {
111                    if (category != null) {
112                        category.removePreference(preference);
113                    }
114                }
115            }
116        }
117        if (Compatibility.runsTwentySix()) {
118            if (targetsTwentySix(settingsFragment.getContext())) {
119                Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
120                if (preference != null) {
121                    for (PreferenceCategory category : categories) {
122                        if (category != null) {
123                            category.removePreference(preference);
124                        }
125                    }
126                }
127            }
128        }
129    }
130
131    public static void startService(Context context, Intent intent) {
132        try {
133            if (Compatibility.runsAndTargetsTwentySix(context)) {
134                intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
135                ContextCompat.startForegroundService(context, intent);
136            } else {
137                context.startService(intent);
138            }
139        } catch (RuntimeException e) {
140            Log.d(Config.LOGTAG, context.getClass().getSimpleName() + " was unable to start service");
141        }
142    }
143
144
145    @SuppressLint("UnsupportedChromeOsCameraSystemFeature")
146    public static boolean hasFeatureCamera(final Context context) {
147        final PackageManager packageManager = context.getPackageManager();
148        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
149            return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
150        } else {
151            return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
152        }
153    }
154}