1package eu.siacs.conversations;
2
3import android.content.Context;
4import android.content.SharedPreferences;
5import android.net.Uri;
6import android.os.Environment;
7import androidx.annotation.BoolRes;
8import androidx.annotation.IntegerRes;
9import androidx.annotation.NonNull;
10import androidx.preference.PreferenceManager;
11import com.google.common.base.Joiner;
12import com.google.common.base.Splitter;
13import com.google.common.base.Strings;
14import eu.siacs.conversations.persistance.FileBackend;
15import eu.siacs.conversations.services.QuickConversationsService;
16import eu.siacs.conversations.utils.Compatibility;
17import java.security.SecureRandom;
18import java.util.Optional;
19
20public class AppSettings {
21
22 public static final String KEEP_FOREGROUND_SERVICE = "enable_foreground_service";
23 public static final String AWAY_WHEN_SCREEN_IS_OFF = "away_when_screen_off";
24 public static final String TREAT_VIBRATE_AS_SILENT = "treat_vibrate_as_silent";
25 public static final String DND_ON_SILENT_MODE = "dnd_on_silent_mode";
26 public static final String MANUALLY_CHANGE_PRESENCE = "manually_change_presence";
27 public static final String BLIND_TRUST_BEFORE_VERIFICATION = "btbv";
28 public static final String AUTOMATIC_MESSAGE_DELETION = "automatic_message_deletion";
29 public static final String BROADCAST_LAST_ACTIVITY = "last_activity";
30 public static final String SEND_CHAT_STATES = "chat_states";
31 public static final String THEME = "theme";
32 public static final String DYNAMIC_COLORS = "dynamic_colors";
33 public static final String SHOW_DYNAMIC_TAGS = "show_dynamic_tags";
34 public static final String OMEMO = "omemo";
35 public static final String ALLOW_SCREENSHOTS = "allow_screenshots";
36 public static final String RINGTONE = "call_ringtone";
37
38 public static final String CONFIRM_MESSAGES = "confirm_messages";
39 public static final String ALLOW_MESSAGE_CORRECTION = "allow_message_correction";
40
41 public static final String TRUST_SYSTEM_CA_STORE = "trust_system_ca_store";
42 public static final String REQUIRE_CHANNEL_BINDING = "channel_binding_required";
43 public static final String NOTIFICATION_RINGTONE = "notification_ringtone";
44 public static final String NOTIFICATION_HEADS_UP = "notification_headsup";
45 public static final String NOTIFICATION_VIBRATE = "vibrate_on_notification";
46 public static final String NOTIFICATION_LED = "led";
47 public static final String SHOW_CONNECTION_OPTIONS = "show_connection_options";
48 public static final String USE_TOR = "use_tor";
49 public static final String CHANNEL_DISCOVERY_METHOD = "channel_discovery_method";
50 public static final String SEND_CRASH_REPORTS = "send_crash_reports";
51 public static final String COLORFUL_CHAT_BUBBLES = "use_green_background";
52 public static final String LARGE_FONT = "large_font";
53 public static final String SHOW_LINK_PREVIEWS = "show_link_previews";
54 public static final String SHOW_AVATARS = "show_avatars";
55 public static final String CALL_INTEGRATION = "call_integration";
56 public static final String ALIGN_START = "align_start";
57 public static final String BACKUP_LOCATION = "backup_location";
58 public static final String AUTO_ACCEPT_FILE_SIZE = "auto_accept_file_size";
59
60 private static final String ACCEPT_INVITES_FROM_STRANGERS = "accept_invites_from_strangers";
61 private static final String NOTIFICATIONS_FROM_STRANGERS = "notifications_from_strangers";
62 private static final String INSTALLATION_ID = "im.conversations.android.install_id";
63
64 private static final String EXTERNAL_STORAGE_AUTHORITY =
65 "com.android.externalstorage.documents";
66
67 private final Context context;
68
69 public AppSettings(final Context context) {
70 this.context = context;
71 }
72
73 public Uri getRingtone() {
74 final SharedPreferences sharedPreferences =
75 PreferenceManager.getDefaultSharedPreferences(context);
76 final String incomingCallRingtone =
77 sharedPreferences.getString(
78 RINGTONE, context.getString(R.string.incoming_call_ringtone));
79 return Strings.isNullOrEmpty(incomingCallRingtone) ? null : Uri.parse(incomingCallRingtone);
80 }
81
82 public void setRingtone(final Uri uri) {
83 final SharedPreferences sharedPreferences =
84 PreferenceManager.getDefaultSharedPreferences(context);
85 sharedPreferences.edit().putString(RINGTONE, uri == null ? null : uri.toString()).apply();
86 }
87
88 public Uri getNotificationTone() {
89 final SharedPreferences sharedPreferences =
90 PreferenceManager.getDefaultSharedPreferences(context);
91 final String incomingCallRingtone =
92 sharedPreferences.getString(
93 NOTIFICATION_RINGTONE, context.getString(R.string.notification_ringtone));
94 return Strings.isNullOrEmpty(incomingCallRingtone) ? null : Uri.parse(incomingCallRingtone);
95 }
96
97 public void setNotificationTone(final Uri uri) {
98 final SharedPreferences sharedPreferences =
99 PreferenceManager.getDefaultSharedPreferences(context);
100 sharedPreferences
101 .edit()
102 .putString(NOTIFICATION_RINGTONE, uri == null ? null : uri.toString())
103 .apply();
104 }
105
106 public boolean isBTBVEnabled() {
107 return getBooleanPreference(BLIND_TRUST_BEFORE_VERIFICATION, R.bool.btbv);
108 }
109
110 public boolean isTrustSystemCAStore() {
111 return getBooleanPreference(TRUST_SYSTEM_CA_STORE, R.bool.trust_system_ca_store);
112 }
113
114 public boolean isAllowScreenshots() {
115 return getBooleanPreference(ALLOW_SCREENSHOTS, R.bool.allow_screenshots);
116 }
117
118 public boolean isColorfulChatBubbles() {
119 return getBooleanPreference(COLORFUL_CHAT_BUBBLES, R.bool.use_green_background);
120 }
121
122 public boolean isLargeFont() {
123 return getBooleanPreference(LARGE_FONT, R.bool.large_font);
124 }
125
126 public boolean showLinkPreviews() {
127 return getBooleanPreference(SHOW_LINK_PREVIEWS, R.bool.show_link_previews);
128 }
129
130 public boolean isShowAvatars() {
131 return getBooleanPreference(SHOW_AVATARS, R.bool.show_avatars);
132 }
133
134 public boolean isCallIntegration() {
135 return getBooleanPreference(CALL_INTEGRATION, R.bool.call_integration);
136 }
137
138 public boolean isAlignStart() {
139 return getBooleanPreference(ALIGN_START, R.bool.align_start);
140 }
141
142 public boolean isConfirmMessages() {
143 return getBooleanPreference(CONFIRM_MESSAGES, R.bool.confirm_messages);
144 }
145
146 public boolean isAllowMessageCorrection() {
147 return getBooleanPreference(ALLOW_MESSAGE_CORRECTION, R.bool.allow_message_correction);
148 }
149
150 public boolean isBroadcastLastActivity() {
151 return getBooleanPreference(BROADCAST_LAST_ACTIVITY, R.bool.last_activity);
152 }
153
154 public boolean isUserManagedAvailability() {
155 return getBooleanPreference(MANUALLY_CHANGE_PRESENCE, R.bool.manually_change_presence);
156 }
157
158 public boolean isAutomaticAvailability() {
159 return !isUserManagedAvailability();
160 }
161
162 public boolean isDndOnSilentMode() {
163 return getBooleanPreference(AppSettings.DND_ON_SILENT_MODE, R.bool.dnd_on_silent_mode);
164 }
165
166 public boolean isTreatVibrateAsSilent() {
167 return getBooleanPreference(
168 AppSettings.TREAT_VIBRATE_AS_SILENT, R.bool.treat_vibrate_as_silent);
169 }
170
171 public boolean isAwayWhenScreenLocked() {
172 return getBooleanPreference(
173 AppSettings.AWAY_WHEN_SCREEN_IS_OFF, R.bool.away_when_screen_off);
174 }
175
176 public boolean isUseTor() {
177 return getBooleanPreference(USE_TOR, R.bool.use_tor);
178 }
179
180 public boolean isSendChatStates() {
181 return getBooleanPreference(SEND_CHAT_STATES, R.bool.chat_states);
182 }
183
184 public boolean isExtendedConnectionOptions() {
185 return getBooleanPreference(
186 AppSettings.SHOW_CONNECTION_OPTIONS, R.bool.show_connection_options);
187 }
188
189 public boolean isAcceptInvitesFromStrangers() {
190 return true;
191 }
192
193 public boolean isNotificationsFromStrangers() {
194 return getBooleanPreference(
195 NOTIFICATIONS_FROM_STRANGERS, R.bool.notifications_from_strangers);
196 }
197
198 public boolean isKeepForegroundService() {
199 return Compatibility.twentySix()
200 || getBooleanPreference(KEEP_FOREGROUND_SERVICE, R.bool.enable_foreground_service);
201 }
202
203 private boolean getBooleanPreference(@NonNull final String name, @BoolRes final int res) {
204 final SharedPreferences sharedPreferences =
205 PreferenceManager.getDefaultSharedPreferences(context);
206 return sharedPreferences.getBoolean(name, context.getResources().getBoolean(res));
207 }
208
209 private long getLongPreference(final String name, @IntegerRes final int res) {
210 final long defaultValue = context.getResources().getInteger(res);
211 final SharedPreferences sharedPreferences =
212 PreferenceManager.getDefaultSharedPreferences(context);
213 try {
214 return Long.parseLong(sharedPreferences.getString(name, String.valueOf(defaultValue)));
215 } catch (final NumberFormatException e) {
216 return defaultValue;
217 }
218 }
219
220 public String getOmemo() {
221 final SharedPreferences sharedPreferences =
222 PreferenceManager.getDefaultSharedPreferences(context);
223 return sharedPreferences.getString(
224 OMEMO, context.getString(R.string.omemo_setting_default));
225 }
226
227 public Uri getBackupLocation() {
228 final SharedPreferences sharedPreferences =
229 PreferenceManager.getDefaultSharedPreferences(context);
230 final String location = sharedPreferences.getString(BACKUP_LOCATION, null);
231 if (Strings.isNullOrEmpty(location)) {
232 final var directory = FileBackend.getBackupDirectory(context);
233 return Uri.fromFile(directory);
234 }
235 return Uri.parse(location);
236 }
237
238 public String getBackupLocationAsPath() {
239 return asPath(getBackupLocation());
240 }
241
242 public static String asPath(final Uri uri) {
243 final var scheme = uri.getScheme();
244 final var path = uri.getPath();
245 if (path == null) {
246 return uri.toString();
247 }
248 if ("file".equalsIgnoreCase(scheme)) {
249 return path;
250 } else if ("content".equalsIgnoreCase(scheme)) {
251 if (EXTERNAL_STORAGE_AUTHORITY.equalsIgnoreCase(uri.getAuthority())) {
252 final var parts = Splitter.on(':').limit(2).splitToList(path);
253 if (parts.size() == 2 && "/tree/primary".equals(parts.get(0))) {
254 return Joiner.on('/')
255 .join(Environment.getExternalStorageDirectory(), parts.get(1));
256 }
257 }
258 }
259 return uri.toString();
260 }
261
262 public void setBackupLocation(final Uri uri) {
263 final SharedPreferences sharedPreferences =
264 PreferenceManager.getDefaultSharedPreferences(context);
265 sharedPreferences
266 .edit()
267 .putString(BACKUP_LOCATION, uri == null ? "" : uri.toString())
268 .apply();
269 }
270
271 public boolean isSendCrashReports() {
272 return getBooleanPreference(SEND_CRASH_REPORTS, R.bool.send_crash_reports);
273 }
274
275 public void setSendCrashReports(boolean value) {
276 final SharedPreferences sharedPreferences =
277 PreferenceManager.getDefaultSharedPreferences(context);
278 sharedPreferences.edit().putBoolean(SEND_CRASH_REPORTS, value).apply();
279 }
280
281 public boolean isRequireChannelBinding() {
282 return getBooleanPreference(REQUIRE_CHANNEL_BINDING, R.bool.require_channel_binding);
283 }
284
285 public synchronized long getInstallationId() {
286 final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
287 final long existing = sharedPreferences.getLong(INSTALLATION_ID, 0);
288 if (existing != 0) {
289 return existing;
290 }
291 final var secureRandom = new SecureRandom();
292 final var installationId = secureRandom.nextLong();
293 sharedPreferences.edit().putLong(INSTALLATION_ID, installationId).apply();
294 return installationId;
295 }
296
297 public Optional<Long> getAutoAcceptFileSize() {
298 final long autoAcceptFileSize =
299 getLongPreference(AUTO_ACCEPT_FILE_SIZE, R.integer.auto_accept_filesize);
300 return autoAcceptFileSize <= 0 ? Optional.empty() : Optional.of(autoAcceptFileSize);
301 }
302
303 public synchronized void resetInstallationId() {
304 final var secureRandom = new SecureRandom();
305 final var installationId = secureRandom.nextLong();
306 PreferenceManager.getDefaultSharedPreferences(context)
307 .edit()
308 .putLong(INSTALLATION_ID, installationId)
309 .apply();
310 }
311}