1package eu.siacs.conversations.utils;
2
3import android.content.Context;
4import android.content.Intent;
5import android.content.SharedPreferences;
6import android.content.pm.ApplicationInfo;
7import android.content.pm.PackageManager;
8import android.os.Build;
9import android.preference.Preference;
10import android.preference.PreferenceCategory;
11import android.preference.PreferenceManager;
12import android.support.annotation.BoolRes;
13import android.support.v4.content.ContextCompat;
14import android.util.Log;
15
16import java.util.Arrays;
17import java.util.Collections;
18import java.util.List;
19
20import eu.siacs.conversations.Config;
21import eu.siacs.conversations.R;
22import eu.siacs.conversations.ui.SettingsActivity;
23import eu.siacs.conversations.ui.SettingsFragment;
24
25import static eu.siacs.conversations.services.EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE;
26
27public class Compatibility {
28
29 private static final List<String> UNUSED_SETTINGS_POST_TWENTYSIX = Arrays.asList(
30 "led",
31 "notification_ringtone",
32 "notification_headsup",
33 "vibrate_on_notification");
34 private static final List<String> UNUESD_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings");
35
36
37 public static boolean hasStoragePermission(Context context) {
38 return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
39 }
40
41 public static boolean runsTwentySix() {
42 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
43 }
44
45 public static boolean runsTwentyFour() {
46 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
47 }
48
49 public static boolean twentyEight() {
50 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
51 }
52
53 private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
54 return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
55 }
56
57 private static SharedPreferences getPreferences(final Context context) {
58 return PreferenceManager.getDefaultSharedPreferences(context);
59 }
60
61 private static boolean targetsTwentySix(Context context) {
62 try {
63 final PackageManager packageManager = context.getPackageManager();
64 final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
65 return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
66 } catch (PackageManager.NameNotFoundException | RuntimeException e) {
67 return true; //when in doubt…
68 }
69 }
70
71 private static boolean targetsTwentyFour(Context context) {
72 try {
73 final PackageManager packageManager = context.getPackageManager();
74 final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
75 return applicationInfo == null || applicationInfo.targetSdkVersion >= 24;
76 } catch (PackageManager.NameNotFoundException | RuntimeException e) {
77 return true; //when in doubt…
78 }
79 }
80
81 public static boolean runsAndTargetsTwentySix(Context context) {
82 return runsTwentySix() && targetsTwentySix(context);
83 }
84
85 public static boolean runsAndTargetsTwentyFour(Context context) {
86 return runsTwentyFour() && targetsTwentyFour(context);
87 }
88
89 public static boolean keepForegroundService(Context context) {
90 return runsAndTargetsTwentySix(context) || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
91 }
92
93 public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
94 List<PreferenceCategory> categories = Arrays.asList(
95 (PreferenceCategory) settingsFragment.findPreference("notification_category"),
96 (PreferenceCategory) settingsFragment.findPreference("advanced"));
97 for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
98 Preference preference = settingsFragment.findPreference(key);
99 if (preference != null) {
100 for (PreferenceCategory category : categories) {
101 if (category != null) {
102 category.removePreference(preference);
103 }
104 }
105 }
106 }
107 if (Compatibility.runsTwentySix()) {
108 if (targetsTwentySix(settingsFragment.getContext())) {
109 Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
110 if (preference != null) {
111 for (PreferenceCategory category : categories) {
112 if (category != null) {
113 category.removePreference(preference);
114 }
115 }
116 }
117 }
118 }
119 }
120
121 public static void startService(Context context, Intent intent) {
122 try {
123 if (Compatibility.runsAndTargetsTwentySix(context)) {
124 intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
125 ContextCompat.startForegroundService(context, intent);
126 } else {
127 context.startService(intent);
128 }
129 } catch (RuntimeException e) {
130 Log.d(Config.LOGTAG, context.getClass().getSimpleName()+" was unable to start service");
131 }
132 }
133}