blob: f459964cd40f764c66ee9fdf0a1bd1b7de49a70f [file] [log] [blame]
Alexandre Lision5da46fa2017-08-09 10:25:50 -04001/*
Adrien Béraude552f692020-11-03 14:30:52 -05002 * Copyright (C) 2004-2020 Savoir-faire Linux Inc.
Alexandre Lision5da46fa2017-08-09 10:25:50 -04003 *
4 * Author: Thibault Wittemberg <thibault.wittemberg@savoirfairelinux.com>
5 * Author: Adrien Beraud <adrien.beraud@savoirfairelinux.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21package cx.ring.services;
22
23import android.content.Context;
24import android.content.SharedPreferences;
Adrien Béraud37fb52e2020-10-02 19:24:31 -040025import android.os.Build;
26import android.text.TextUtils;
27
Adrien Béraud8bc95102018-08-25 22:34:51 -040028import androidx.annotation.NonNull;
Adrien Béraud91f88742019-09-09 00:46:46 -040029import androidx.appcompat.app.AppCompatDelegate;
Adrien Béraud6c41ddd2019-10-03 21:01:54 -040030import androidx.preference.PreferenceManager;
Adrien Béraud4d6c0682019-08-24 21:47:19 -040031
Alexandre Lision5da46fa2017-08-09 10:25:50 -040032import java.util.HashMap;
33import java.util.HashSet;
34import java.util.Map;
35import java.util.Set;
36
37import javax.inject.Inject;
38
Adrien Béraud4d6c0682019-08-24 21:47:19 -040039import cx.ring.R;
Adrien Béraude4f901a2019-10-25 17:26:30 -040040import cx.ring.application.JamiApplication;
Alexandre Lision5da46fa2017-08-09 10:25:50 -040041import cx.ring.model.Settings;
Adrien Béraud693b6882019-01-27 14:51:11 -050042import cx.ring.utils.DeviceUtils;
Alexandre Lision5da46fa2017-08-09 10:25:50 -040043import cx.ring.utils.NetworkUtils;
44
45public class SharedPreferencesServiceImpl extends PreferencesService {
46
Adrien Béraud11d92cc2019-12-20 00:12:00 -050047 public static final String PREFS_SETTINGS = "ring_settings";
48 private static final String PREFS_REQUESTS = "ring_requests";
Adrien Béraud91f88742019-09-09 00:46:46 -040049 public static final String PREFS_THEME = "theme";
Adrien Béraud4dc01f92019-10-21 11:41:50 -040050 public static final String PREFS_VIDEO = "videoPrefs";
Adrien Béraud11d92cc2019-12-20 00:12:00 -050051 public static final String PREFS_ACCOUNT = "account_";
52
53 private static final String PREF_PUSH_NOTIFICATIONS = "push_notifs";
54 private static final String PREF_PERSISTENT_NOTIFICATION = "persistent_notif";
Amirhossein4ae37ef2020-11-30 23:26:27 -050055 private static final String PREF_SHOW_TYPING = "persistent_typing";
Amirhossein85b3f2e2020-12-01 13:05:43 -050056 private static final String PREF_SHOW_READ = "persistent_read";
Amirhossein7bb77aa2020-12-18 16:30:44 -050057 private static final String PREF_BLOCK_RECORD = "persistent_block_record";
Adrien Béraud11d92cc2019-12-20 00:12:00 -050058 private static final String PREF_HW_ENCODING = "video_hwenc";
59 public static final String PREF_BITRATE = "video_bitrate";
60 public static final String PREF_RESOLUTION = "video_resolution";
61 private static final String PREF_SYSTEM_CONTACTS = "system_contacts";
62 private static final String PREF_PLACE_CALLS = "place_calls";
63 private static final String PREF_ON_STARTUP = "on_startup";
Adrien Béraud91f88742019-09-09 00:46:46 -040064 public static final String PREF_DARK_MODE= "darkMode";
Adrien Béraud11d92cc2019-12-20 00:12:00 -050065 private static final String PREF_ACCEPT_IN_MAX_SIZE = "acceptIncomingFilesMaxSize";
ayounesc6c2c012019-11-11 11:09:51 -050066 public static final String PREF_PLUGINS = "plugins";
Loïc Siret0202fbf2017-09-28 16:34:28 -040067 private final Map<String, Set<String>> mNotifiedRequests = new HashMap<>();
Adrien Béraudace37a22018-06-22 14:58:36 -040068
Alexandre Lision5da46fa2017-08-09 10:25:50 -040069 @Inject
70 protected Context mContext;
71
Alexandre Lision5da46fa2017-08-09 10:25:50 -040072 @Override
Adrien Béraudace37a22018-06-22 14:58:36 -040073 protected void saveSettings(Settings settings) {
Adrien Béraud4d6c0682019-08-24 21:47:19 -040074 SharedPreferences appPrefs = getPreferences();
Alexandre Lision5da46fa2017-08-09 10:25:50 -040075 SharedPreferences.Editor edit = appPrefs.edit();
76 edit.clear();
Adrien Béraud11d92cc2019-12-20 00:12:00 -050077 edit.putBoolean(PREF_SYSTEM_CONTACTS, settings.isAllowSystemContacts());
78 edit.putBoolean(PREF_PLACE_CALLS, settings.isAllowPlaceSystemCalls());
Adrien Béraude591bdd2020-05-10 22:31:16 -040079 edit.putBoolean(PREF_ON_STARTUP, settings.isAllowOnStartup());
Adrien Béraud11d92cc2019-12-20 00:12:00 -050080 edit.putBoolean(PREF_PUSH_NOTIFICATIONS, settings.isAllowPushNotifications());
81 edit.putBoolean(PREF_PERSISTENT_NOTIFICATION, settings.isAllowPersistentNotification());
Amirhossein4ae37ef2020-11-30 23:26:27 -050082 edit.putBoolean(PREF_SHOW_TYPING, settings.isAllowTypingIndicator());
Amirhossein85b3f2e2020-12-01 13:05:43 -050083 edit.putBoolean(PREF_SHOW_READ, settings.isAllowReadIndicator());
Amirhossein7bb77aa2020-12-18 16:30:44 -050084 edit.putBoolean(PREF_BLOCK_RECORD, settings.isRecordingBlocked());
Alexandre Lision5da46fa2017-08-09 10:25:50 -040085 edit.apply();
Alexandre Lision5da46fa2017-08-09 10:25:50 -040086 }
87
88 @Override
Adrien Béraudace37a22018-06-22 14:58:36 -040089 protected Settings loadSettings() {
Adrien Béraud4d6c0682019-08-24 21:47:19 -040090 SharedPreferences appPrefs = getPreferences();
Adrien Béraudace37a22018-06-22 14:58:36 -040091 Settings settings = getUserSettings();
92 if (settings == null) {
93 settings = new Settings();
Alexandre Lision5da46fa2017-08-09 10:25:50 -040094 }
Adrien Béraud11d92cc2019-12-20 00:12:00 -050095 settings.setAllowSystemContacts(appPrefs.getBoolean(PREF_SYSTEM_CONTACTS, false));
96 settings.setAllowPlaceSystemCalls(appPrefs.getBoolean(PREF_PLACE_CALLS, false));
97 settings.setAllowRingOnStartup(appPrefs.getBoolean(PREF_ON_STARTUP, true));
98 settings.setAllowPushNotifications(appPrefs.getBoolean(PREF_PUSH_NOTIFICATIONS, false));
99 settings.setAllowPersistentNotification(appPrefs.getBoolean(PREF_PERSISTENT_NOTIFICATION, false));
Amirhossein4ae37ef2020-11-30 23:26:27 -0500100 settings.setAllowTypingIndicator(appPrefs.getBoolean(PREF_SHOW_TYPING, true));
Amirhossein85b3f2e2020-12-01 13:05:43 -0500101 settings.setAllowReadIndicator(appPrefs.getBoolean(PREF_SHOW_READ, true));
Amirhossein7bb77aa2020-12-18 16:30:44 -0500102 settings.setBlockRecordIndicator(appPrefs.getBoolean(PREF_BLOCK_RECORD, false));
Adrien Béraudace37a22018-06-22 14:58:36 -0400103 return settings;
Alexandre Lision5da46fa2017-08-09 10:25:50 -0400104 }
105
106 private void saveRequests(String accountId, Set<String> requests) {
Adrien Béraud11d92cc2019-12-20 00:12:00 -0500107 SharedPreferences preferences = mContext.getSharedPreferences(PREFS_REQUESTS, Context.MODE_PRIVATE);
Adrien Béraud4dc01f92019-10-21 11:41:50 -0400108 SharedPreferences.Editor edit = preferences.edit();
Alexandre Lision5da46fa2017-08-09 10:25:50 -0400109 edit.putStringSet(accountId, requests);
110 edit.apply();
111 }
112
113 @Override
114 public void saveRequestPreferences(String accountId, String contactId) {
115 Set<String> requests = loadRequestsPreferences(accountId);
116 requests.add(contactId);
117 saveRequests(accountId, requests);
118 }
119
120 @Override
121 @NonNull
122 public Set<String> loadRequestsPreferences(@NonNull String accountId) {
123 Set<String> requests = mNotifiedRequests.get(accountId);
124 if (requests == null) {
Adrien Béraud11d92cc2019-12-20 00:12:00 -0500125 SharedPreferences preferences = mContext.getSharedPreferences(PREFS_REQUESTS, Context.MODE_PRIVATE);
Pierre Duchemin45707ca2018-01-15 15:05:23 -0500126 requests = new HashSet<>(preferences.getStringSet(accountId, new HashSet<>()));
Alexandre Lision5da46fa2017-08-09 10:25:50 -0400127 mNotifiedRequests.put(accountId, requests);
128 }
129 return requests;
130 }
131
132 @Override
133 public void removeRequestPreferences(String accountId, String contactId) {
134 Set<String> requests = loadRequestsPreferences(accountId);
135 requests.remove(contactId);
136 saveRequests(accountId, requests);
137 }
138
139 @Override
140 public boolean hasNetworkConnected() {
Adrien Béraud91f88742019-09-09 00:46:46 -0400141 return NetworkUtils.isConnectivityAllowed(mContext);
Alexandre Lision5da46fa2017-08-09 10:25:50 -0400142 }
143
Adrien Béraude2ea70d2018-02-18 14:54:00 +0100144 @Override
145 public boolean isPushAllowed() {
Adrien Béraude4f901a2019-10-25 17:26:30 -0400146 String token = JamiApplication.getInstance().getPushToken();
Adrien Béraudace37a22018-06-22 14:58:36 -0400147 return getSettings().isAllowPushNotifications() && !TextUtils.isEmpty(token) /*&& NetworkUtils.isPushAllowed(mContext, getSettings().isAllowMobileData())*/;
Adrien Béraude2ea70d2018-02-18 14:54:00 +0100148 }
Adrien Béraud4d6c0682019-08-24 21:47:19 -0400149
150 @Override
151 public int getResolution() {
Adrien Béraud11d92cc2019-12-20 00:12:00 -0500152 return Integer.parseInt(getVideoPreferences().getString(PREF_RESOLUTION,
Adrien Béraud4d6c0682019-08-24 21:47:19 -0400153 DeviceUtils.isTv(mContext)
154 ? mContext.getString(R.string.video_resolution_default_tv)
155 : mContext.getString(R.string.video_resolution_default)));
156 }
157
158 @Override
159 public int getBitrate() {
Adrien Béraud11d92cc2019-12-20 00:12:00 -0500160 return Integer.parseInt(getVideoPreferences().getString(PREF_BITRATE, mContext.getString(R.string.video_bitrate_default)));
Adrien Béraud4d6c0682019-08-24 21:47:19 -0400161 }
162
163 @Override
164 public boolean isHardwareAccelerationEnabled() {
Adrien Béraud11d92cc2019-12-20 00:12:00 -0500165 return getVideoPreferences().getBoolean(PREF_HW_ENCODING, true);
Adrien Béraud4d6c0682019-08-24 21:47:19 -0400166 }
167
Adrien Béraud91f88742019-09-09 00:46:46 -0400168 @Override
169 public void setDarkMode(boolean enabled) {
170 SharedPreferences.Editor edit = getThemePreferences().edit();
Adrien Béraud4dc01f92019-10-21 11:41:50 -0400171 edit.putBoolean(PREF_DARK_MODE, enabled)
172 .apply();
Adrien Béraud91f88742019-09-09 00:46:46 -0400173 applyDarkMode(enabled);
174 }
175
176 @Override
177 public boolean getDarkMode() {
178 return getThemePreferences().getBoolean(PREF_DARK_MODE, false);
179 }
180
181 @Override
182 public void loadDarkMode() {
183 applyDarkMode(getDarkMode());
184 }
185
Adrien Béraud11d92cc2019-12-20 00:12:00 -0500186 @Override
187 public int getMaxFileAutoAccept(String accountId) {
188 return mContext.getSharedPreferences(PREFS_ACCOUNT+accountId, Context.MODE_PRIVATE)
189 .getInt(PREF_ACCEPT_IN_MAX_SIZE, 30) * 1024 * 1024;
ayounesc6c2c012019-11-11 11:09:51 -0500190 }
Adrien Béraud11d92cc2019-12-20 00:12:00 -0500191
Adrien Béraud91f88742019-09-09 00:46:46 -0400192 private void applyDarkMode(boolean enabled) {
193 AppCompatDelegate.setDefaultNightMode(
194 enabled ? AppCompatDelegate.MODE_NIGHT_YES
195 : Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
196 ? AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
197 : AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
198 }
199
Adrien Béraud4d6c0682019-08-24 21:47:19 -0400200 private SharedPreferences getPreferences() {
Adrien Béraud11d92cc2019-12-20 00:12:00 -0500201 return mContext.getSharedPreferences(PREFS_SETTINGS, Context.MODE_PRIVATE);
Adrien Béraud4d6c0682019-08-24 21:47:19 -0400202 }
203
204 private SharedPreferences getVideoPreferences() {
Adrien Béraud4dc01f92019-10-21 11:41:50 -0400205 return mContext.getSharedPreferences(PREFS_VIDEO, Context.MODE_PRIVATE);
Adrien Béraud4d6c0682019-08-24 21:47:19 -0400206 }
Adrien Béraud91f88742019-09-09 00:46:46 -0400207
208 private SharedPreferences getThemePreferences() {
Adrien Béraud6c41ddd2019-10-03 21:01:54 -0400209 return PreferenceManager.getDefaultSharedPreferences(mContext);
Adrien Béraud91f88742019-09-09 00:46:46 -0400210 }
Alexandre Lision5da46fa2017-08-09 10:25:50 -0400211}