2018-09-05 19:37:05 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
2022-07-10 10:34:02 +00:00
|
|
|
import static eu.siacs.conversations.services.EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE;
|
|
|
|
|
2020-06-21 13:40:51 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2018-09-05 19:37:05 +00:00
|
|
|
import android.content.Context;
|
2019-01-13 08:42:44 +00:00
|
|
|
import android.content.Intent;
|
2018-09-05 19:37:05 +00:00
|
|
|
import android.content.SharedPreferences;
|
2018-09-18 09:33:18 +00:00
|
|
|
import android.content.pm.ApplicationInfo;
|
2018-09-17 19:24:25 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2022-09-03 10:33:27 +00:00
|
|
|
import android.net.ConnectivityManager;
|
2018-09-05 19:37:05 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceCategory;
|
|
|
|
import android.preference.PreferenceManager;
|
2021-01-23 08:25:34 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2021-01-18 17:26:46 +00:00
|
|
|
import androidx.annotation.BoolRes;
|
2022-09-03 10:33:27 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.RequiresApi;
|
2021-01-18 17:26:46 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
2018-09-05 19:37:05 +00:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
2021-02-18 19:55:29 +00:00
|
|
|
import java.util.Collections;
|
2018-09-05 19:37:05 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-01-13 08:42:44 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2018-09-05 19:37:05 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.ui.SettingsActivity;
|
|
|
|
import eu.siacs.conversations.ui.SettingsFragment;
|
|
|
|
|
|
|
|
public class Compatibility {
|
|
|
|
|
2022-07-10 10:34:02 +00:00
|
|
|
private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX =
|
|
|
|
Arrays.asList(
|
|
|
|
"led",
|
|
|
|
"notification_ringtone",
|
|
|
|
"notification_headsup",
|
|
|
|
"vibrate_on_notification");
|
2022-08-09 12:34:39 +00:00
|
|
|
private static final List<String> UNUSED_SETTINGS_PRE_TWENTYSIX =
|
2022-07-10 10:34:02 +00:00
|
|
|
Collections.singletonList("message_notification_settings");
|
2018-09-05 19:37:05 +00:00
|
|
|
|
2023-05-07 08:13:14 +00:00
|
|
|
public static boolean hasStoragePermission(final Context context) {
|
|
|
|
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
|
2022-07-10 10:34:02 +00:00
|
|
|
|| ContextCompat.checkSelfPermission(
|
|
|
|
context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
|
|
|
== PackageManager.PERMISSION_GRANTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean s() {
|
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
|
2018-09-17 19:24:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 16:41:54 +00:00
|
|
|
private static boolean runsTwentyFour() {
|
2019-02-15 12:17:49 +00:00
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
|
|
|
|
}
|
|
|
|
|
2020-02-11 16:41:54 +00:00
|
|
|
public static boolean runsTwentySix() {
|
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
|
|
|
|
}
|
|
|
|
|
2018-10-11 02:36:32 +00:00
|
|
|
public static boolean twentyEight() {
|
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
|
2018-09-17 15:47:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-05 19:37:05 +00:00
|
|
|
private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
|
|
|
|
return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static SharedPreferences getPreferences(final Context context) {
|
|
|
|
return PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
}
|
|
|
|
|
2018-09-18 09:33:18 +00:00
|
|
|
private static boolean targetsTwentySix(Context context) {
|
|
|
|
try {
|
|
|
|
final PackageManager packageManager = context.getPackageManager();
|
2022-07-10 10:34:02 +00:00
|
|
|
final ApplicationInfo applicationInfo =
|
|
|
|
packageManager.getApplicationInfo(context.getPackageName(), 0);
|
2018-09-18 09:33:18 +00:00
|
|
|
return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
|
2018-11-22 09:06:56 +00:00
|
|
|
} catch (PackageManager.NameNotFoundException | RuntimeException e) {
|
2022-07-10 10:34:02 +00:00
|
|
|
return true; // when in doubt…
|
2018-09-18 09:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 12:17:49 +00:00
|
|
|
private static boolean targetsTwentyFour(Context context) {
|
|
|
|
try {
|
|
|
|
final PackageManager packageManager = context.getPackageManager();
|
2022-07-10 10:34:02 +00:00
|
|
|
final ApplicationInfo applicationInfo =
|
|
|
|
packageManager.getApplicationInfo(context.getPackageName(), 0);
|
2019-02-15 12:17:49 +00:00
|
|
|
return applicationInfo == null || applicationInfo.targetSdkVersion >= 24;
|
|
|
|
} catch (PackageManager.NameNotFoundException | RuntimeException e) {
|
2022-07-10 10:34:02 +00:00
|
|
|
return true; // when in doubt…
|
2019-02-15 12:17:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 09:33:18 +00:00
|
|
|
public static boolean runsAndTargetsTwentySix(Context context) {
|
|
|
|
return runsTwentySix() && targetsTwentySix(context);
|
|
|
|
}
|
|
|
|
|
2019-02-15 12:17:49 +00:00
|
|
|
public static boolean runsAndTargetsTwentyFour(Context context) {
|
|
|
|
return runsTwentyFour() && targetsTwentyFour(context);
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:37:05 +00:00
|
|
|
public static boolean keepForegroundService(Context context) {
|
2022-07-10 10:34:02 +00:00
|
|
|
return runsAndTargetsTwentySix(context)
|
|
|
|
|| getBooleanPreference(
|
|
|
|
context,
|
|
|
|
SettingsActivity.KEEP_FOREGROUND_SERVICE,
|
|
|
|
R.bool.enable_foreground_service);
|
2018-09-05 19:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
|
2022-07-10 10:34:02 +00:00
|
|
|
List<PreferenceCategory> categories =
|
|
|
|
Arrays.asList(
|
|
|
|
(PreferenceCategory)
|
|
|
|
settingsFragment.findPreference("notification_category"),
|
|
|
|
(PreferenceCategory) settingsFragment.findPreference("advanced"));
|
|
|
|
for (String key :
|
|
|
|
(runsTwentySix()
|
|
|
|
? UNUSED_SETTINGS_POST_TWENTYSIX
|
2022-08-09 12:34:39 +00:00
|
|
|
: UNUSED_SETTINGS_PRE_TWENTYSIX)) {
|
2018-09-05 19:37:05 +00:00
|
|
|
Preference preference = settingsFragment.findPreference(key);
|
|
|
|
if (preference != null) {
|
|
|
|
for (PreferenceCategory category : categories) {
|
|
|
|
if (category != null) {
|
|
|
|
category.removePreference(preference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 09:33:18 +00:00
|
|
|
if (Compatibility.runsTwentySix()) {
|
|
|
|
if (targetsTwentySix(settingsFragment.getContext())) {
|
2022-07-10 10:34:02 +00:00
|
|
|
Preference preference =
|
|
|
|
settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
|
2018-09-18 09:33:18 +00:00
|
|
|
if (preference != null) {
|
|
|
|
for (PreferenceCategory category : categories) {
|
|
|
|
if (category != null) {
|
|
|
|
category.removePreference(preference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 19:37:05 +00:00
|
|
|
}
|
2019-01-13 08:42:44 +00:00
|
|
|
|
|
|
|
public static void startService(Context context, Intent intent) {
|
|
|
|
try {
|
|
|
|
if (Compatibility.runsAndTargetsTwentySix(context)) {
|
|
|
|
intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
|
|
|
|
ContextCompat.startForegroundService(context, intent);
|
|
|
|
} else {
|
|
|
|
context.startService(intent);
|
|
|
|
}
|
|
|
|
} catch (RuntimeException e) {
|
2022-07-10 10:34:02 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
context.getClass().getSimpleName() + " was unable to start service");
|
2019-01-13 08:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-21 13:40:51 +00:00
|
|
|
|
|
|
|
@SuppressLint("UnsupportedChromeOsCameraSystemFeature")
|
|
|
|
public static boolean hasFeatureCamera(final Context context) {
|
|
|
|
final PackageManager packageManager = context.getPackageManager();
|
2022-09-03 10:33:27 +00:00
|
|
|
return packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.N)
|
|
|
|
public static int getRestrictBackgroundStatus(
|
|
|
|
@NonNull final ConnectivityManager connectivityManager) {
|
|
|
|
try {
|
|
|
|
return connectivityManager.getRestrictBackgroundStatus();
|
|
|
|
} catch (final Exception e) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"platform bug detected. Unable to get restrict background status",
|
|
|
|
e);
|
|
|
|
return ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
|
2020-06-21 13:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-05 19:37:05 +00:00
|
|
|
}
|