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