| 1 | package tw.nekomimi.nkmr; |
| 2 | |
| 3 | import static tw.nekomimi.nekogram.NekoConfig.TITLE_TYPE_TEXT; |
| 4 | |
| 5 | import android.annotation.SuppressLint; |
| 6 | import android.app.Activity; |
| 7 | import android.content.Context; |
| 8 | import android.content.SharedPreferences; |
| 9 | |
| 10 | import org.telegram.messenger.ApplicationLoader; |
| 11 | import org.telegram.messenger.BuildVars; |
| 12 | import org.telegram.messenger.FileLog; |
| 13 | |
| 14 | import java.io.ByteArrayInputStream; |
| 15 | import java.io.ByteArrayOutputStream; |
| 16 | import java.io.ObjectInputStream; |
| 17 | import java.io.ObjectOutputStream; |
| 18 | import java.util.ArrayList; |
| 19 | import android.util.Base64; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | @SuppressLint("ApplySharedPref") |
| 25 | public class NekomuraConfig { |
| 26 | private static final int configTypeBool = 0; |
| 27 | private static final int configTypeInt = 1; |
| 28 | private static final int configTypeString = 2; |
| 29 | private static final int configTypeSetInt = 3; |
| 30 | private static final int configTypeMapIntInt = 4; |
| 31 | private static final int configTypeLong = 5; |
| 32 | private static final int configTypeFloat = 6; |
| 33 | |
| 34 | public static class ConfigItem { |
| 35 | private String key; |
| 36 | private int type; |
| 37 | |
| 38 | private Object defaultValue; |
| 39 | private Object value; |
| 40 | private boolean forceDefault;//针对某些人的垃圾选项,应该不允许改变默认值 |
| 41 | |
| 42 | private ConfigItem(String k, int t, Object d) { |
| 43 | key = k; |
| 44 | type = t; |
| 45 | defaultValue = d; |
| 46 | } |
| 47 | |
| 48 | public String getKey() { |
| 49 | return key; |
| 50 | } |
| 51 | |
| 52 | // 读配置 |
| 53 | |
| 54 | public boolean Bool() { |
| 55 | return (boolean) value; |
| 56 | } |
| 57 | |
| 58 | public int Int() { |
| 59 | return (int) value; |
| 60 | } |
| 61 | |
| 62 | public Long Long() { |
| 63 | return (Long) value; |
| 64 | } |
| 65 | |
| 66 | public Float Float() { |
| 67 | return (Float) value; |
| 68 | } |
| 69 | |
| 70 | public String String() { |
| 71 | return value.toString(); |
| 72 | } |
| 73 | |
| 74 | public HashSet<Integer> SetInt() { |
| 75 | return (HashSet<Integer>) value; |
| 76 | } |
| 77 | |
| 78 | public HashMap<Integer, Integer> MapIntInt() { |
| 79 | return (HashMap<Integer, Integer>) value; |
| 80 | } |
| 81 | |
| 82 | public boolean SetIntContains(Integer v) { |
| 83 | return ((HashSet<Integer>) value).contains(v); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | public void changed(Object o) { |
| 88 | value = o; |
| 89 | } |
| 90 | |
| 91 | //写配置 |
| 92 | //这里没有检查类型哦 |
| 93 | |
| 94 | public boolean toggleConfigBool() { |
| 95 | value = !this.Bool(); |
| 96 | if (forceDefault) { |
| 97 | value = defaultValue; |
| 98 | } |
| 99 | saveConfig(this); |
| 100 | return this.Bool();//返回toggle之后的 |
| 101 | } |
| 102 | |
| 103 | public void setConfigBool(boolean v) { |
| 104 | value = v; |
| 105 | if (forceDefault) { |
| 106 | value = defaultValue; |
| 107 | } |
| 108 | saveConfig(this); |
| 109 | } |
| 110 | |
| 111 | public void setConfigInt(int v) { |
| 112 | if (forceDefault) { |
| 113 | value = defaultValue; |
| 114 | } |
| 115 | value = v; |
| 116 | saveConfig(this); |
| 117 | } |
| 118 | |
| 119 | public void setConfigLong(Long v) { |
| 120 | if (forceDefault) { |
| 121 | value = defaultValue; |
| 122 | } |
| 123 | value = v; |
| 124 | saveConfig(this); |
| 125 | } |
| 126 | |
| 127 | public void setConfigFloat(Float v) { |
| 128 | if (forceDefault) { |
| 129 | value = defaultValue; |
| 130 | } |
| 131 | value = v; |
| 132 | saveConfig(this); |
| 133 | } |
| 134 | |
| 135 | public void setConfigString(String v) { |
| 136 | value = v; |
| 137 | if (forceDefault) { |
| 138 | value = defaultValue; |
| 139 | } |
| 140 | saveConfig(this); |
| 141 | } |
| 142 | |
| 143 | public void setConfigSetInt(HashSet<Integer> v) { |
| 144 | value = v; |
| 145 | if (forceDefault) { |
| 146 | value = defaultValue; |
| 147 | } |
| 148 | saveConfig(this); |
| 149 | } |
| 150 | |
| 151 | public void setConfigMapInt(HashMap<Integer, Integer> v) { |
| 152 | value = v; |
| 153 | saveConfig(this); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private static boolean configLoaded; |
| 158 | private static final Object sync = new Object(); |
| 159 | private static final ArrayList<ConfigItem> configs = new ArrayList<>(); |
| 160 | |
| 161 | // Configs |
| 162 | public static ConfigItem migrate = addConfig("NekoConfigMigrate", configTypeBool, false); |
| 163 | public static ConfigItem largeAvatarInDrawer = addConfig("largeAvatarInDrawer", configTypeInt, 0); // 0:TG Default 1:NekoX Default 2:Large Avatar |
| 164 | public static ConfigItem unreadBadgeOnBackButton = addConfig("unreadBadgeOnBackButton", configTypeBool, false); |
| 165 | |
| 166 | //From NekoConfig |
| 167 | public static ConfigItem useIPv6 = addConfig("useIPv6", configTypeBool, false); |
| 168 | public static ConfigItem hidePhone = addConfig("hidePhone", configTypeBool, true); |
| 169 | public static ConfigItem ignoreBlocked = addConfig("ignoreBlocked", configTypeBool, false); |
| 170 | public static ConfigItem forceTablet = addConfig("forceTablet", configTypeInt, 0); |
| 171 | public static ConfigItem inappCamera = addConfig("inappCamera", configTypeBool, false); // fake |
| 172 | public static ConfigItem smoothKeyboard = addConfig("smoothKeyboard", configTypeBool, false);// fake |
| 173 | |
| 174 | public static ConfigItem typeface = addConfig("typeface_b", configTypeBool, false); |
| 175 | public static ConfigItem nameOrder = addConfig("nameOrder", configTypeInt, 1); |
| 176 | public static ConfigItem mapPreviewProvider = addConfig("mapPreviewProvider", configTypeInt, 0); |
| 177 | public static ConfigItem transparentStatusBar = addConfig("transparentStatusBar", configTypeBool, false); |
| 178 | public static ConfigItem residentNotification = addConfig("residentNotification", configTypeBool, false); |
| 179 | public static ConfigItem hideProxySponsorChannel = addConfig("hideProxySponsorChannel", configTypeBool, false); |
| 180 | public static ConfigItem showAddToSavedMessages = addConfig("showAddToSavedMessages", configTypeBool, true); |
| 181 | public static ConfigItem showReport = addConfig("showReport", configTypeBool, true); |
| 182 | public static ConfigItem showViewHistory = addConfig("showViewHistory", configTypeBool, true); |
| 183 | public static ConfigItem showAdminActions = addConfig("showAdminActions", configTypeBool, true); |
| 184 | public static ConfigItem showChangePermissions = addConfig("showChangePermissions", configTypeBool, true); |
| 185 | public static ConfigItem showDeleteDownloadedFile = addConfig("showDeleteDownloadedFile", configTypeBool, true); |
| 186 | public static ConfigItem showMessageDetails = addConfig("showMessageDetails", configTypeBool, false); |
| 187 | public static ConfigItem showTranslate = addConfig("showTranslate", configTypeBool, true); |
| 188 | public static ConfigItem showRepeat = addConfig("showRepeat", configTypeBool, false); |
| 189 | public static ConfigItem showMessageHide = addConfig("showMessageHide", configTypeBool, false); |
| 190 | |
| 191 | public static ConfigItem eventType = addConfig("eventType", configTypeInt, 0); |
| 192 | public static ConfigItem actionBarDecoration = addConfig("actionBarDecoration", configTypeInt, 0); |
| 193 | public static ConfigItem newYear = addConfig("newYear", configTypeBool, false); |
| 194 | public static ConfigItem stickerSize = addConfig("stickerSize", configTypeFloat, 14.0f); |
| 195 | public static ConfigItem unlimitedFavedStickers = addConfig("unlimitedFavedStickers", configTypeBool, false); |
| 196 | public static ConfigItem unlimitedPinnedDialogs = addConfig("unlimitedPinnedDialogs", configTypeBool, false); |
| 197 | public static ConfigItem translationProvider = addConfig("translationProvider", configTypeInt, 1); |
| 198 | public static ConfigItem disablePhotoSideAction = addConfig("disablePhotoSideAction", configTypeBool, true); |
| 199 | public static ConfigItem openArchiveOnPull = addConfig("openArchiveOnPull", configTypeBool, false); |
| 200 | public static ConfigItem hideKeyboardOnChatScroll = addConfig("hideKeyboardOnChatScroll", configTypeBool, false); |
| 201 | public static ConfigItem avatarBackgroundBlur = addConfig("avatarBackgroundBlur", configTypeBool, false); |
| 202 | public static ConfigItem avatarBackgroundDarken = addConfig("avatarBackgroundDarken", configTypeBool, false); |
| 203 | public static ConfigItem useSystemEmoji = addConfig("useSystemEmoji", configTypeBool, false); |
| 204 | public static ConfigItem showTabsOnForward = addConfig("showTabsOnForward", configTypeBool, false); |
| 205 | public static ConfigItem rearVideoMessages = addConfig("rearVideoMessages", configTypeBool, false); |
| 206 | public static ConfigItem hideAllTab = addConfig("hideAllTab", configTypeBool, false); |
| 207 | public static ConfigItem pressTitleToOpenAllChats = addConfig("pressTitleToOpenAllChats", configTypeBool, false); |
| 208 | |
| 209 | public static ConfigItem disableChatAction = addConfig("disable_chat_action", configTypeBool, false); |
| 210 | public static ConfigItem sortByUnread = addConfig("sort_by_unread", configTypeBool, false); |
| 211 | public static ConfigItem sortByUnmuted = addConfig("sort_by_unmuted", configTypeBool, true); |
| 212 | public static ConfigItem sortByUser = addConfig("sort_by_user", configTypeBool, true); |
| 213 | public static ConfigItem sortByContacts = addConfig("sort_by_contacts", configTypeBool, true); |
| 214 | |
| 215 | public static ConfigItem disableUndo = addConfig("disable_undo", configTypeBool, false); |
| 216 | |
| 217 | public static ConfigItem filterUsers = addConfig("filter_users", configTypeBool, true); |
| 218 | public static ConfigItem filterContacts = addConfig("filter_contacts", configTypeBool, true); |
| 219 | public static ConfigItem filterGroups = addConfig("filter_groups", configTypeBool, true); |
| 220 | public static ConfigItem filterChannels = addConfig("filter_channels", configTypeBool, true); |
| 221 | public static ConfigItem filterBots = addConfig("filter_bots", configTypeBool, true); |
| 222 | public static ConfigItem filterAdmins = addConfig("filter_admins", configTypeBool, true); |
| 223 | public static ConfigItem filterUnmuted = addConfig("filter_unmuted", configTypeBool, true); |
| 224 | public static ConfigItem filterUnread = addConfig("filter_unread", configTypeBool, true); |
| 225 | public static ConfigItem filterUnmutedAndUnread = addConfig("filter_unmuted_and_unread", configTypeBool, true); |
| 226 | |
| 227 | public static ConfigItem disableSystemAccount = addConfig("disable_system_account", configTypeBool, false); |
| 228 | public static ConfigItem disableProxyWhenVpnEnabled = addConfig("disable_proxy_when_vpn_enabled", configTypeBool, false); |
| 229 | public static ConfigItem skipOpenLinkConfirm = addConfig("skip_open_link_confirm", configTypeBool, false); |
| 230 | |
| 231 | public static ConfigItem ignoreMutedCount = addConfig("ignore_muted_count", configTypeBool, true); |
| 232 | public static ConfigItem useDefaultTheme = addConfig("use_default_theme", configTypeBool, false); |
| 233 | public static ConfigItem showIdAndDc = addConfig("show_id_and_dc", configTypeBool, false); |
| 234 | |
| 235 | public static ConfigItem googleCloudTranslateKey = addConfig("google_cloud_translate_key", configTypeString, ""); |
| 236 | public static ConfigItem cachePath = addConfig("cache_path", configTypeString, ""); |
| 237 | |
| 238 | public static ConfigItem translateToLang = addConfig("trans_to_lang", configTypeString, ""); |
| 239 | public static ConfigItem translateInputLang = addConfig("trans_input_to_lang", configTypeString, "en"); |
| 240 | |
| 241 | public static ConfigItem ccToLang = addConfig("opencc_to_lang", configTypeString, ""); |
| 242 | public static ConfigItem ccInputLang = addConfig("opencc_input_to_lang", configTypeString, ""); |
| 243 | |
| 244 | public static ConfigItem tabsTitleType = addConfig("tabsTitleType", configTypeInt, TITLE_TYPE_TEXT); |
| 245 | public static ConfigItem confirmAVMessage = addConfig("confirmAVMessage", configTypeBool, false); |
| 246 | public static ConfigItem askBeforeCall = addConfig("askBeforeCall", configTypeBool, false); |
| 247 | public static ConfigItem disableNumberRounding = addConfig("disableNumberRounding", configTypeBool, false); |
| 248 | |
| 249 | public static ConfigItem useSystemDNS = addConfig("useSystemDNS", configTypeBool, false); |
| 250 | public static ConfigItem customDoH = addConfig("customDoH", configTypeString, ""); |
| 251 | public static ConfigItem hideProxyByDefault = addConfig("hide_proxy_by_default", configTypeBool, false); |
| 252 | public static ConfigItem useProxyItem = addConfig("use_proxy_item", configTypeBool, true); |
| 253 | |
| 254 | public static ConfigItem disableAppBarShadow = addConfig("disableAppBarShadow", configTypeBool, false); |
| 255 | public static ConfigItem mediaPreview = addConfig("mediaPreview", configTypeBool, true); |
| 256 | |
| 257 | public static ConfigItem proxyAutoSwitch = addConfig("proxy_auto_switch", configTypeBool, false); |
| 258 | |
| 259 | public static ConfigItem usePersianCalendar = addConfig("persian_calendar", configTypeBool, false); |
| 260 | public static ConfigItem displayPersianCalendarByLatin = addConfig("displayPersianCalendarByLatin", configTypeBool, false); |
| 261 | public static ConfigItem openPGPApp = addConfig("openPGPApp", configTypeString, ""); |
| 262 | public static ConfigItem openPGPKeyId = addConfig("openPGPKeyId", configTypeLong, 0L); |
| 263 | |
| 264 | public static ConfigItem disableVibration = addConfig("disableVibration", configTypeBool, false); |
| 265 | public static ConfigItem autoPauseVideo = addConfig("autoPauseVideo", configTypeBool, false); |
| 266 | public static ConfigItem disableProximityEvents = addConfig("disableProximityEvents", configTypeBool, false); |
| 267 | |
| 268 | public static ConfigItem ignoreContentRestrictions = addConfig("ignoreContentRestrictions", configTypeBool, !BuildVars.isPlay); |
| 269 | public static ConfigItem useChatAttachMediaMenu = addConfig("useChatAttachMediaMenu", configTypeBool, true); |
| 270 | public static ConfigItem disableLinkPreviewByDefault = addConfig("disableLinkPreviewByDefault", configTypeBool, false); |
| 271 | public static ConfigItem sendCommentAfterForward = addConfig("sendCommentAfterForward", configTypeBool, true); |
| 272 | public static ConfigItem increaseVoiceMessageQuality = addConfig("increaseVoiceMessageQuality", configTypeBool, true); |
| 273 | public static ConfigItem acceptSecretChat = addConfig("acceptSecretChat", configTypeBool, true); |
| 274 | public static ConfigItem disableTrending = addConfig("disableTrending", configTypeBool, true); |
| 275 | public static ConfigItem dontSendGreetingSticker = addConfig("dontSendGreetingSticker", configTypeBool, false); |
| 276 | public static ConfigItem hideTimeForSticker = addConfig("hideTimeForSticker", configTypeBool, false); |
| 277 | public static ConfigItem takeGIFasVideo = addConfig("takeGIFasVideo", configTypeBool, false); |
| 278 | public static ConfigItem maxRecentStickerCount = addConfig("maxRecentStickerCount", configTypeInt, 20); |
| 279 | public static ConfigItem disableSwipeToNext = addConfig("disableSwipeToNext", configTypeBool, true); |
| 280 | public static ConfigItem disableRemoteEmojiInteractions = addConfig("disableRemoteEmojiInteractions", configTypeBool, true); |
| 281 | public static ConfigItem disableChoosingSticker = addConfig("disableChoosingSticker", configTypeBool, false); |
| 282 | |
| 283 | public static ConfigItem disableAutoDownloadingWin32Executable = addConfig("disableAutoDownloadingWin32Executable", configTypeBool, true); |
| 284 | public static ConfigItem disableAutoDownloadingArchive = addConfig("disableAutoDownloadingArchive", configTypeBool, true); |
| 285 | |
| 286 | public static ConfigItem enableStickerPin = addConfig("enableStickerPin", configTypeBool, false); |
| 287 | public static ConfigItem useMediaStreamInVoip = addConfig("useMediaStreamInVoip", configTypeBool, false); |
| 288 | public static ConfigItem customAudioBitrate = addConfig("customAudioBitrate", configTypeInt, 32); |
| 289 | public static ConfigItem disableGroupVoipAudioProcessing = addConfig("disableGroupVoipAudioProcessing", configTypeBool, false); |
| 290 | |
| 291 | |
| 292 | static { |
| 293 | loadConfig(false); |
| 294 | } |
| 295 | |
| 296 | public static ConfigItem addConfig(String k, int t, Object d) { |
| 297 | ConfigItem a = new ConfigItem(k, t, d); |
| 298 | configs.add(a); |
| 299 | return a; |
| 300 | } |
| 301 | |
| 302 | public static ConfigItem findOne(String key) { |
| 303 | for (int i = 0; i < configs.size(); i++) { |
| 304 | ConfigItem o = configs.get(i); |
| 305 | if (key.equals(o.key)) { |
| 306 | return o; |
| 307 | } |
| 308 | } |
| 309 | return null; |
| 310 | } |
| 311 | |
| 312 | public static void loadConfig(boolean force) { |
| 313 | synchronized (sync) { |
| 314 | if (configLoaded && !force) { |
| 315 | return; |
| 316 | } |
| 317 | SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nkmrcfg", Activity.MODE_PRIVATE); |
| 318 | for (int i = 0; i < configs.size(); i++) { |
| 319 | ConfigItem o = configs.get(i); |
| 320 | |
| 321 | if (o.forceDefault) { |
| 322 | o.value = o.defaultValue; |
| 323 | continue; |
| 324 | } |
| 325 | |
| 326 | if (o.type == configTypeBool) { |
| 327 | o.value = preferences.getBoolean(o.key, (boolean) o.defaultValue); |
| 328 | } |
| 329 | if (o.type == configTypeInt) { |
| 330 | o.value = preferences.getInt(o.key, (int) o.defaultValue); |
| 331 | } |
| 332 | if (o.type == configTypeLong) { |
| 333 | o.value = preferences.getLong(o.key, (Long) o.defaultValue); |
| 334 | } |
| 335 | if (o.type == configTypeFloat) { |
| 336 | o.value = preferences.getFloat(o.key, (Float) o.defaultValue); |
| 337 | } |
| 338 | if (o.type == configTypeString) { |
| 339 | o.value = preferences.getString(o.key, (String) o.defaultValue); |
| 340 | } |
| 341 | if (o.type == configTypeSetInt) { |
| 342 | Set<String> ss = preferences.getStringSet(o.key, new HashSet<>()); |
| 343 | HashSet<Integer> si = new HashSet<>(); |
| 344 | for (String s : ss) { |
| 345 | si.add(Integer.parseInt(s)); |
| 346 | } |
| 347 | o.value = si; |
| 348 | } |
| 349 | if (o.type == configTypeMapIntInt) { |
| 350 | String cv = preferences.getString(o.key, ""); |
| 351 | // Log.e("NC", String.format("Getting pref %s val %s", o.key, cv)); |
| 352 | if (cv.length() == 0) { |
| 353 | o.value = new HashMap<Integer, Integer>(); |
| 354 | } else { |
| 355 | try { |
| 356 | byte[] data = Base64.decode(cv, Base64.DEFAULT); |
| 357 | ObjectInputStream ois = new ObjectInputStream( |
| 358 | new ByteArrayInputStream(data)); |
| 359 | o.value = (HashMap<Integer, Integer>) ois.readObject(); |
| 360 | if (o.value == null) { |
| 361 | o.value = new HashMap<Integer, Integer>(); |
| 362 | } |
| 363 | ois.close(); |
| 364 | } catch (Exception e) { |
| 365 | o.value = new HashMap<Integer, Integer>(); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | configLoaded = true; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | public static void saveConfig(ConfigItem item) { |
| 375 | synchronized (sync) { |
| 376 | try { |
| 377 | SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nkmrcfg", Context.MODE_PRIVATE); |
| 378 | SharedPreferences.Editor editor = preferences.edit(); |
| 379 | for (int i = 0; i < configs.size(); i++) { |
| 380 | ConfigItem o = configs.get(i); |
| 381 | if (item != null && !item.getKey().equals(o.getKey())) continue; |
| 382 | |
| 383 | if (o.type == configTypeBool) { |
| 384 | editor.putBoolean(o.key, (boolean) o.value); |
| 385 | } |
| 386 | if (o.type == configTypeInt) { |
| 387 | editor.putInt(o.key, (int) o.value); |
| 388 | } |
| 389 | if (o.type == configTypeLong) { |
| 390 | editor.putLong(o.key, (Long) o.value); |
| 391 | } |
| 392 | if (o.type == configTypeFloat) { |
| 393 | editor.putFloat(o.key, (Float) o.value); |
| 394 | } |
| 395 | if (o.type == configTypeString) { |
| 396 | editor.putString(o.key, o.value.toString()); |
| 397 | } |
| 398 | if (o.type == configTypeSetInt) { |
| 399 | HashSet<String> ss = new HashSet<>(); |
| 400 | for (Integer n : (Set<Integer>) o.value) { |
| 401 | ss.add(Integer.toString(n)); |
| 402 | } |
| 403 | editor.putStringSet(o.key, ss); |
| 404 | } |
| 405 | if (o.type == configTypeMapIntInt) { |
| 406 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 407 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 408 | oos.writeObject(o.value); |
| 409 | oos.close(); |
| 410 | editor.putString(o.key, Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT)); |
| 411 | } |
| 412 | } |
| 413 | editor.commit(); |
| 414 | } catch (Exception e) { |
| 415 | FileLog.e(e); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |