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 "call_ringtone"
35 );
36 private static final List<String> UNUESD_SETTINGS_PRE_TWENTYSIX = Collections.singletonList("more_notification_settings");
37
38
39 public static boolean hasStoragePermission(Context context) {
40 return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
41 }
42
43 public static boolean runsTwentyOne() {
44 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
45 }
46
47 private static boolean runsTwentyFour() {
48 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
49 }
50
51 public static boolean runsTwentySix() {
52 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
53 }
54
55 public static boolean twentyEight() {
56 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
57 }
58
59 private static boolean getBooleanPreference(Context context, String name, @BoolRes int res) {
60 return getPreferences(context).getBoolean(name, context.getResources().getBoolean(res));
61 }
62
63 private static SharedPreferences getPreferences(final Context context) {
64 return PreferenceManager.getDefaultSharedPreferences(context);
65 }
66
67 private static boolean targetsTwentySix(Context context) {
68 try {
69 final PackageManager packageManager = context.getPackageManager();
70 final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
71 return applicationInfo == null || applicationInfo.targetSdkVersion >= 26;
72 } catch (PackageManager.NameNotFoundException | RuntimeException e) {
73 return true; //when in doubt…
74 }
75 }
76
77 private static boolean targetsTwentyFour(Context context) {
78 try {
79 final PackageManager packageManager = context.getPackageManager();
80 final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
81 return applicationInfo == null || applicationInfo.targetSdkVersion >= 24;
82 } catch (PackageManager.NameNotFoundException | RuntimeException e) {
83 return true; //when in doubt…
84 }
85 }
86
87 public static boolean runsAndTargetsTwentySix(Context context) {
88 return runsTwentySix() && targetsTwentySix(context);
89 }
90
91 public static boolean runsAndTargetsTwentyFour(Context context) {
92 return runsTwentyFour() && targetsTwentyFour(context);
93 }
94
95 public static boolean keepForegroundService(Context context) {
96 return runsAndTargetsTwentySix(context) || getBooleanPreference(context, SettingsActivity.KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
97 }
98
99 public static void removeUnusedPreferences(SettingsFragment settingsFragment) {
100 List<PreferenceCategory> categories = Arrays.asList(
101 (PreferenceCategory) settingsFragment.findPreference("notification_category"),
102 (PreferenceCategory) settingsFragment.findPreference("advanced"));
103 for (String key : (runsTwentySix() ? UNUSED_SETTINGS_POST_TWENTYSIX : UNUESD_SETTINGS_PRE_TWENTYSIX)) {
104 Preference preference = settingsFragment.findPreference(key);
105 if (preference != null) {
106 for (PreferenceCategory category : categories) {
107 if (category != null) {
108 category.removePreference(preference);
109 }
110 }
111 }
112 }
113 if (Compatibility.runsTwentySix()) {
114 if (targetsTwentySix(settingsFragment.getContext())) {
115 Preference preference = settingsFragment.findPreference(SettingsActivity.KEEP_FOREGROUND_SERVICE);
116 if (preference != null) {
117 for (PreferenceCategory category : categories) {
118 if (category != null) {
119 category.removePreference(preference);
120 }
121 }
122 }
123 }
124 }
125 }
126
127 public static void startService(Context context, Intent intent) {
128 try {
129 if (Compatibility.runsAndTargetsTwentySix(context)) {
130 intent.putExtra(EXTRA_NEEDS_FOREGROUND_SERVICE, true);
131 ContextCompat.startForegroundService(context, intent);
132 } else {
133 context.startService(intent);
134 }
135 } catch (RuntimeException e) {
136 Log.d(Config.LOGTAG, context.getClass().getSimpleName()+" was unable to start service");
137 }
138 }
139}