AppSettings.java

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