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    private static boolean runsTwentyFour() {
 47        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
 48    }
 49
 50    public static boolean runsTwentySix() {
 51        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
 52    }
 53
 54    public static boolean twentyEight() {
 55        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
 56    }
 57
 58    private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
 59        return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
 60    }
 61
 62    private static SharedPreferences getPreferences(final Context context) {
 63        return PreferenceManager.getDefaultSharedPreferences(context);
 64    }
 65
 66    private static boolean targetsTwentySix(Context context) {
 67        try {
 68            final PackageManager packageManager = context.getPackageManager();
 69            final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
 70            return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
 71        } catch (PackageManager.NameNotFoundException | RuntimeException e) {
 72            return true; //when in doubt…
 73        }
 74    }
 75
 76    private static boolean targetsTwentyFour(Context context) {
 77        try {
 78            final PackageManager packageManager = context.getPackageManager();
 79            final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
 80            return applicationInfo == null || applicationInfo.targetSdkVersion >= 24;
 81        } catch (PackageManager.NameNotFoundException | RuntimeException e) {
 82            return true; //when in doubt…
 83        }
 84    }
 85
 86    public static boolean runsAndTargetsTwentySix(Context context) {
 87        return runsTwentySix() && targetsTwentySix(context);
 88    }
 89
 90    public static boolean runsAndTargetsTwentyFour(Context context) {
 91        return runsTwentyFour() && targetsTwentyFour(context);
 92    }
 93
 94    public static boolean keepForegroundService(Context context) {
 95        return runsAndTargetsTwentySix(context) || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
 96    }
 97
 98    public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
 99        List<PreferenceCategory> categories = Arrays.asList(
100                (PreferenceCategory) settingsFragment.findPreference("notification_category"),
101                (PreferenceCategory) settingsFragment.findPreference("advanced"));
102        for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
103            Preference preference = settingsFragment.findPreference(key);
104            if (preference != null) {
105                for (PreferenceCategory category : categories) {
106                    if (category != null) {
107                        category.removePreference(preference);
108                    }
109                }
110            }
111        }
112        if (Compatibility.runsTwentySix()) {
113            if (targetsTwentySix(settingsFragment.getContext())) {
114                Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
115                if (preference != null) {
116                    for (PreferenceCategory category : categories) {
117                        if (category != null) {
118                            category.removePreference(preference);
119                        }
120                    }
121                }
122            }
123        }
124    }
125
126    public static void startService(Context context, Intent intent) {
127        try {
128            if (Compatibility.runsAndTargetsTwentySix(context)) {
129                intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
130                ContextCompat.startForegroundService(context, intent);
131            } else {
132                context.startService(intent);
133            }
134        } catch (RuntimeException e) {
135            Log.d(Config.LOGTAG, context.getClass().getSimpleName() + " was unable to start service");
136        }
137    }
138
139
140    @SuppressLint("UnsupportedChromeOsCameraSystemFeature")
141    public static boolean hasFeatureCamera(final Context context) {
142        final PackageManager packageManager = context.getPackageManager();
143        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
144            return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
145        } else {
146            return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA);
147        }
148    }
149}