2018-09-05 19:37:05 +00:00
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
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;
|
2018-09-05 19:37:05 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceCategory;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.support.annotation.BoolRes;
|
2018-09-17 19:24:25 +00:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2018-09-05 19:37:05 +00:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.ui.SettingsActivity;
|
|
|
|
import eu.siacs.conversations.ui.SettingsFragment;
|
|
|
|
|
|
|
|
public class Compatibility {
|
|
|
|
|
|
|
|
private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList(
|
|
|
|
"led",
|
|
|
|
"notification_ringtone",
|
|
|
|
"notification_headsup",
|
|
|
|
"vibrate_on_notification");
|
|
|
|
private static final List<String> UNUESD_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings");
|
|
|
|
|
|
|
|
|
2018-09-17 19:24:25 +00:00
|
|
|
public static boolean hasStoragePermission(Context context) {
|
|
|
|
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
|
|
|
|
}
|
|
|
|
|
2018-09-18 09:33:18 +00:00
|
|
|
public static boolean runsTwentySix() {
|
2018-09-05 19:37:05 +00:00
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
|
|
|
|
}
|
|
|
|
|
2018-09-17 15:47:36 +00:00
|
|
|
public static boolean twentyTwo() {
|
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
|
|
|
|
return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
return true; //when in doubt…
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean runsAndTargetsTwentySix(Context context) {
|
|
|
|
return runsTwentySix() && targetsTwentySix(context);
|
|
|
|
}
|
|
|
|
|
2018-09-05 19:37:05 +00:00
|
|
|
public static boolean keepForegroundService(Context context) {
|
2018-09-18 09:33:18 +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) {
|
|
|
|
List<PreferenceCategory> categories = Arrays.asList(
|
|
|
|
(PreferenceCategory) settingsFragment.findPreference("notification_category"),
|
|
|
|
(PreferenceCategory) settingsFragment.findPreference("other_expert_category"));
|
2018-09-18 09:33:18 +00:00
|
|
|
for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_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())) {
|
|
|
|
Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
|
|
|
|
if (preference != null) {
|
|
|
|
for (PreferenceCategory category : categories) {
|
|
|
|
if (category != null) {
|
|
|
|
category.removePreference(preference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 19:37:05 +00:00
|
|
|
}
|
|
|
|
}
|