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