1package eu.siacs.conversations;
2
3import android.content.Context;
4import android.content.SharedPreferences;
5import android.net.Uri;
6
7import androidx.annotation.BoolRes;
8import androidx.annotation.NonNull;
9import androidx.preference.PreferenceManager;
10
11import com.google.common.base.Strings;
12
13import java.security.SecureRandom;
14
15public class AppSettings {
16
17 public static final String KEEP_FOREGROUND_SERVICE = "enable_foreground_service";
18 public static final String AWAY_WHEN_SCREEN_IS_OFF = "away_when_screen_off";
19 public static final String TREAT_VIBRATE_AS_SILENT = "treat_vibrate_as_silent";
20 public static final String DND_ON_SILENT_MODE = "dnd_on_silent_mode";
21 public static final String MANUALLY_CHANGE_PRESENCE = "manually_change_presence";
22 public static final String BLIND_TRUST_BEFORE_VERIFICATION = "btbv";
23 public static final String AUTOMATIC_MESSAGE_DELETION = "automatic_message_deletion";
24 public static final String BROADCAST_LAST_ACTIVITY = "last_activity";
25 public static final String THEME = "theme";
26 public static final String DYNAMIC_COLORS = "dynamic_colors";
27 public static final String SHOW_DYNAMIC_TAGS = "show_dynamic_tags";
28 public static final String OMEMO = "omemo";
29 public static final String ALLOW_SCREENSHOTS = "allow_screenshots";
30 public static final String RINGTONE = "call_ringtone";
31 public static final String BTBV = "btbv";
32
33 public static final String CONFIRM_MESSAGES = "confirm_messages";
34 public static final String ALLOW_MESSAGE_CORRECTION = "allow_message_correction";
35
36 public static final String TRUST_SYSTEM_CA_STORE = "trust_system_ca_store";
37 public static final String REQUIRE_CHANNEL_BINDING = "channel_binding_required";
38 public static final String NOTIFICATION_RINGTONE = "notification_ringtone";
39 public static final String NOTIFICATION_HEADS_UP = "notification_headsup";
40 public static final String NOTIFICATION_VIBRATE = "vibrate_on_notification";
41 public static final String NOTIFICATION_LED = "led";
42 public static final String SHOW_CONNECTION_OPTIONS = "show_connection_options";
43 public static final String USE_TOR = "use_tor";
44 public static final String CHANNEL_DISCOVERY_METHOD = "channel_discovery_method";
45 public static final String SEND_CRASH_REPORTS = "send_crash_reports";
46 public static final String COLORFUL_CHAT_BUBBLES = "use_green_background";
47 public static final String LARGE_FONT = "large_font";
48 public static final String SHOW_LINK_PREVIEWS = "show_link_previews";
49
50 private static final String ACCEPT_INVITES_FROM_STRANGERS = "accept_invites_from_strangers";
51 private static final String INSTALLATION_ID = "im.conversations.android.install_id";
52
53 private final Context context;
54
55 public AppSettings(final Context context) {
56 this.context = context;
57 }
58
59 public Uri getRingtone() {
60 final SharedPreferences sharedPreferences =
61 PreferenceManager.getDefaultSharedPreferences(context);
62 final String incomingCallRingtone =
63 sharedPreferences.getString(
64 RINGTONE, context.getString(R.string.incoming_call_ringtone));
65 return Strings.isNullOrEmpty(incomingCallRingtone) ? null : Uri.parse(incomingCallRingtone);
66 }
67
68 public void setRingtone(final Uri uri) {
69 final SharedPreferences sharedPreferences =
70 PreferenceManager.getDefaultSharedPreferences(context);
71 sharedPreferences.edit().putString(RINGTONE, uri == null ? null : uri.toString()).apply();
72 }
73
74 public Uri getNotificationTone() {
75 final SharedPreferences sharedPreferences =
76 PreferenceManager.getDefaultSharedPreferences(context);
77 final String incomingCallRingtone =
78 sharedPreferences.getString(
79 NOTIFICATION_RINGTONE, context.getString(R.string.notification_ringtone));
80 return Strings.isNullOrEmpty(incomingCallRingtone) ? null : Uri.parse(incomingCallRingtone);
81 }
82
83 public void setNotificationTone(final Uri uri) {
84 final SharedPreferences sharedPreferences =
85 PreferenceManager.getDefaultSharedPreferences(context);
86 sharedPreferences
87 .edit()
88 .putString(NOTIFICATION_RINGTONE, uri == null ? null : uri.toString())
89 .apply();
90 }
91
92 public boolean isBTBVEnabled() {
93 return getBooleanPreference(BTBV, R.bool.btbv);
94 }
95
96 public boolean isTrustSystemCAStore() {
97 return getBooleanPreference(TRUST_SYSTEM_CA_STORE, R.bool.trust_system_ca_store);
98 }
99
100 public boolean isAllowScreenshots() {
101 return getBooleanPreference(ALLOW_SCREENSHOTS, R.bool.allow_screenshots);
102 }
103
104 public boolean isColorfulChatBubbles() {
105 return getBooleanPreference(COLORFUL_CHAT_BUBBLES, R.bool.use_green_background);
106 }
107
108 public boolean isLargeFont() {
109 return getBooleanPreference(LARGE_FONT, R.bool.large_font);
110 }
111
112 public boolean showLinkPreviews() {
113 return getBooleanPreference(SHOW_LINK_PREVIEWS, R.bool.show_link_previews);
114 }
115
116 public boolean isUseTor() {
117 return getBooleanPreference(USE_TOR, R.bool.use_tor);
118 }
119
120 private boolean getBooleanPreference(@NonNull final String name, @BoolRes int res) {
121 final SharedPreferences sharedPreferences =
122 PreferenceManager.getDefaultSharedPreferences(context);
123 return sharedPreferences.getBoolean(name, context.getResources().getBoolean(res));
124 }
125
126 public String getOmemo() {
127 final SharedPreferences sharedPreferences =
128 PreferenceManager.getDefaultSharedPreferences(context);
129 return sharedPreferences.getString(
130 OMEMO, context.getString(R.string.omemo_setting_default));
131 }
132
133 public boolean isSendCrashReports() {
134 return getBooleanPreference(SEND_CRASH_REPORTS, R.bool.send_crash_reports);
135 }
136
137 public void setSendCrashReports(boolean value) {
138 final SharedPreferences sharedPreferences =
139 PreferenceManager.getDefaultSharedPreferences(context);
140 sharedPreferences.edit().putBoolean(SEND_CRASH_REPORTS, value).apply();
141 }
142
143 public boolean isRequireChannelBinding() {
144 return getBooleanPreference(REQUIRE_CHANNEL_BINDING, R.bool.require_channel_binding);
145 }
146
147 public synchronized long getInstallationId() {
148 final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
149 final long existing = sharedPreferences.getLong(INSTALLATION_ID, 0);
150 if (existing != 0) {
151 return existing;
152 }
153 final var secureRandom = new SecureRandom();
154 final var installationId = secureRandom.nextLong();
155 sharedPreferences.edit().putLong(INSTALLATION_ID, installationId).apply();
156 return installationId;
157 }
158
159 public synchronized void resetInstallationId() {
160 final var secureRandom = new SecureRandom();
161 final var installationId = secureRandom.nextLong();
162 PreferenceManager.getDefaultSharedPreferences(context)
163 .edit()
164 .putLong(INSTALLATION_ID, installationId)
165 .apply();
166 }
167}