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