Compatibility.java

  1package eu.siacs.conversations.utils;
  2
  3import static eu.siacs.conversations.services.EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE;
  4
  5import android.annotation.SuppressLint;
  6import android.app.ActivityOptions;
  7import android.content.Context;
  8import android.content.Intent;
  9import android.content.SharedPreferences;
 10import android.content.pm.ApplicationInfo;
 11import android.content.pm.PackageManager;
 12import android.net.ConnectivityManager;
 13import android.os.Build;
 14import android.os.Bundle;
 15import android.preference.Preference;
 16import android.preference.PreferenceCategory;
 17import android.preference.PreferenceManager;
 18import android.util.Log;
 19
 20import androidx.annotation.BoolRes;
 21import androidx.annotation.NonNull;
 22import androidx.annotation.RequiresApi;
 23import androidx.core.content.ContextCompat;
 24
 25import eu.siacs.conversations.Config;
 26import eu.siacs.conversations.R;
 27import eu.siacs.conversations.ui.SettingsActivity;
 28import eu.siacs.conversations.ui.SettingsFragment;
 29
 30import java.util.Arrays;
 31import java.util.Collections;
 32import java.util.List;
 33
 34public class Compatibility {
 35
 36    private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX =
 37            Arrays.asList(
 38                    "led",
 39                    "notification_ringtone",
 40                    "notification_headsup",
 41                    "vibrate_on_notification");
 42    private static final List<String> UNUSED_SETTINGS_PRE_TWENTYSIX =
 43            Collections.singletonList("message_notification_settings");
 44
 45    public static boolean hasStoragePermission(final Context context) {
 46        return Build.VERSION.SDK_INT < Build.VERSION_CODES.M
 47                || Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
 48                || ContextCompat.checkSelfPermission(
 49                                context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
 50                        == PackageManager.PERMISSION_GRANTED;
 51    }
 52
 53    public static boolean s() {
 54        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
 55    }
 56
 57    private static boolean runsTwentyFour() {
 58        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
 59    }
 60
 61    public static boolean runsTwentySix() {
 62        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
 63    }
 64
 65    public static boolean twentyEight() {
 66        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
 67    }
 68
 69    private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
 70        return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
 71    }
 72
 73    private static SharedPreferences getPreferences(final Context context) {
 74        return PreferenceManager.getDefaultSharedPreferences(context);
 75    }
 76
 77    private static boolean targetsTwentySix(Context context) {
 78        try {
 79            final PackageManager packageManager = context.getPackageManager();
 80            final ApplicationInfo applicationInfo =
 81                    packageManager.getApplicationInfo(context.getPackageName(), 0);
 82            return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
 83        } catch (PackageManager.NameNotFoundException | RuntimeException e) {
 84            return true; // when in doubt…
 85        }
 86    }
 87
 88    private static boolean targetsTwentyFour(Context context) {
 89        try {
 90            final PackageManager packageManager = context.getPackageManager();
 91            final ApplicationInfo applicationInfo =
 92                    packageManager.getApplicationInfo(context.getPackageName(), 0);
 93            return applicationInfo == null || applicationInfo.targetSdkVersion >= 24;
 94        } catch (PackageManager.NameNotFoundException | RuntimeException e) {
 95            return true; // when in doubt…
 96        }
 97    }
 98
 99    public static boolean runsAndTargetsTwentySix(Context context) {
100        return runsTwentySix() && targetsTwentySix(context);
101    }
102
103    public static boolean runsAndTargetsTwentyFour(Context context) {
104        return runsTwentyFour() && targetsTwentyFour(context);
105    }
106
107    public static boolean keepForegroundService(Context context) {
108        return runsAndTargetsTwentySix(context)
109                || getBooleanPreference(
110                        context,
111                        SettingsActivity.KEEP_FOREGROUND_SERVICE,
112                        R.bool.enable_foreground_service);
113    }
114
115    public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
116        List<PreferenceCategory> categories =
117                Arrays.asList(
118                        (PreferenceCategory)
119                                settingsFragment.findPreference("notification_category"),
120                        (PreferenceCategory) settingsFragment.findPreference("advanced"));
121        for (String key :
122                (runsTwentySix()
123                        ? UNUSED_SETTINGS_POST_TWENTYSIX
124                        : UNUSED_SETTINGS_PRE_TWENTYSIX)) {
125            Preference preference = settingsFragment.findPreference(key);
126            if (preference != null) {
127                for (PreferenceCategory category : categories) {
128                    if (category != null) {
129                        category.removePreference(preference);
130                    }
131                }
132            }
133        }
134        if (Compatibility.runsTwentySix()) {
135            if (targetsTwentySix(settingsFragment.getContext())) {
136                Preference preference =
137                        settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
138                if (preference != null) {
139                    for (PreferenceCategory category : categories) {
140                        if (category != null) {
141                            category.removePreference(preference);
142                        }
143                    }
144                }
145            }
146        }
147
148        try {
149            Class.forName("io.sentry.Sentry");
150            Preference preference = settingsFragment.findPreference("never_send");
151            if (preference != null) {
152                for (PreferenceCategory category : categories) {
153                    if (category != null) {
154                        category.removePreference(preference);
155                    }
156                }
157            }
158        } catch (final ClassNotFoundException e) { }
159    }
160
161    public static void startService(Context context, Intent intent) {
162        try {
163            if (Compatibility.runsAndTargetsTwentySix(context)) {
164                intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
165                context.startForegroundService(intent);
166            } else {
167                context.startService(intent);
168            }
169        } catch (RuntimeException e) {
170            Log.d(
171                    Config.LOGTAG,
172                    context.getClass().getSimpleName() + " was unable to start service");
173        }
174    }
175
176    @SuppressLint("UnsupportedChromeOsCameraSystemFeature")
177    public static boolean hasFeatureCamera(final Context context) {
178        final PackageManager packageManager = context.getPackageManager();
179        return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
180    }
181
182    @RequiresApi(api = Build.VERSION_CODES.N)
183    public static int getRestrictBackgroundStatus(
184            @NonNull final ConnectivityManager connectivityManager) {
185        try {
186            return connectivityManager.getRestrictBackgroundStatus();
187        } catch (final Exception e) {
188            Log.d(
189                    Config.LOGTAG,
190                    "platform bug detected. Unable to get restrict background status",
191                    e);
192            return ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
193        }
194    }
195
196    public static Bundle pgpStartIntentSenderOptions() {
197        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
198            return ActivityOptions.makeBasic()
199                    .setPendingIntentBackgroundActivityStartMode(
200                            ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
201                    .toBundle();
202        } else {
203            return null;
204        }
205    }
206}