| 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("AvatarAsBackground", 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 | // TODO sort |
| 168 | public static ConfigItem useIPv6 = addConfig("IPv6", configTypeBool, false); |
| 169 | public static ConfigItem hidePhone = addConfig("HidePhone", configTypeBool, true); |
| 170 | public static ConfigItem ignoreBlocked = addConfig("IgnoreBlocked", configTypeBool, false); |
| 171 | public static ConfigItem tabletMode = addConfig("TabletMode", configTypeInt, 0); |
| 172 | public static ConfigItem inappCamera = addConfig("DebugMenuEnableCamera", configTypeBool, false); // fake |
| 173 | public static ConfigItem smoothKeyboard = addConfig("DebugMenuEnableSmoothKeyboard", configTypeBool, false);// fake |
| 174 |
| 175 | public static ConfigItem typeface = addConfig("TypefaceUseDefault", configTypeBool, false); |
| 176 | public static ConfigItem nameOrder = addConfig("NameOrder", configTypeInt, 1); |
| 177 | public static ConfigItem mapPreviewProvider = addConfig("MapPreviewProvider", configTypeInt, 0); |
| 178 | public static ConfigItem transparentStatusBar = addConfig("TransparentStatusBar", configTypeBool, false); |
| 179 | public static ConfigItem residentNotification = addConfig("EnableResidentNotification", configTypeBool, false); |
| 180 | public static ConfigItem hideProxySponsorChannel = addConfig("HideProxySponsorChannel", configTypeBool, false); |
| 181 | public static ConfigItem showAddToSavedMessages = addConfig("showAddToSavedMessages", configTypeBool, true); |
| 182 | public static ConfigItem showReport = addConfig("showReport", configTypeBool, true); |
| 183 | public static ConfigItem showViewHistory = addConfig("showViewHistory", configTypeBool, true); |
| 184 | public static ConfigItem showAdminActions = addConfig("showAdminActions", configTypeBool, true); |
| 185 | public static ConfigItem showChangePermissions = addConfig("showChangePermissions", configTypeBool, true); |
| 186 | public static ConfigItem showDeleteDownloadedFile = addConfig("showDeleteDownloadedFile", configTypeBool, true); |
| 187 | public static ConfigItem showMessageDetails = addConfig("showMessageDetails", configTypeBool, false); |
| 188 | public static ConfigItem showTranslate = addConfig("showTranslate", configTypeBool, true); |
| 189 | public static ConfigItem showRepeat = addConfig("showRepeat", configTypeBool, false); |
| 190 | public static ConfigItem showMessageHide = addConfig("showMessageHide", configTypeBool, false); |
| 191 |
| 192 | public static ConfigItem eventType = addConfig("eventType", configTypeInt, 0); |
| 193 | public static ConfigItem actionBarDecoration = addConfig("ActionBarDecoration", configTypeInt, 0); |
| 194 | public static ConfigItem newYear = addConfig("ChristmasHat", configTypeBool, false); |
| 195 | public static ConfigItem stickerSize = addConfig("stickerSize", configTypeFloat, 14.0f); |
| 196 | public static ConfigItem unlimitedFavedStickers = addConfig("UnlimitedFavoredStickers", configTypeBool, false); |
| 197 | public static ConfigItem unlimitedPinnedDialogs = addConfig("UnlimitedPinnedDialogs", configTypeBool, false); |
| 198 | public static ConfigItem translationProvider = addConfig("translationProvider", configTypeInt, 1); |
| 199 | public static ConfigItem disablePhotoSideAction = addConfig("DisablePhotoViewerSideAction", configTypeBool, true); |
| 200 | public static ConfigItem openArchiveOnPull = addConfig("OpenArchiveOnPull", configTypeBool, false); |
| 201 | public static ConfigItem hideKeyboardOnChatScroll = addConfig("HideKeyboardOnChatScroll", configTypeBool, false); |
| 202 | public static ConfigItem avatarBackgroundBlur = addConfig("BlurAvatarBackground", configTypeBool, false); |
| 203 | public static ConfigItem avatarBackgroundDarken = addConfig("DarkenAvatarBackground", configTypeBool, false); |
| 204 | public static ConfigItem useSystemEmoji = addConfig("EmojiUseDefault", configTypeBool, false); |
| 205 | public static ConfigItem showTabsOnForward = addConfig("ShowTabsOnForward", configTypeBool, false); |
| 206 | public static ConfigItem rearVideoMessages = addConfig("RearVideoMessages", configTypeBool, false); |
| 207 | public static ConfigItem hideAllTab = addConfig("HideAllTab", configTypeBool, false); |
| 208 | public static ConfigItem pressTitleToOpenAllChats = addConfig("pressTitleToOpenAllChats", configTypeBool, false); |
| 209 |
| 210 | public static ConfigItem disableChatAction = addConfig("DisableChatAction", configTypeBool, false); |
| 211 | public static ConfigItem sortByUnread = addConfig("sort_by_unread", configTypeBool, false); |
| 212 | public static ConfigItem sortByUnmuted = addConfig("sort_by_unmuted", configTypeBool, true); |
| 213 | public static ConfigItem sortByUser = addConfig("sort_by_user", configTypeBool, true); |
| 214 | public static ConfigItem sortByContacts = addConfig("sort_by_contacts", configTypeBool, true); |
| 215 |
| 216 | public static ConfigItem disableUndo = addConfig("DisableUndo", configTypeBool, false); |
| 217 |
| 218 | public static ConfigItem filterUsers = addConfig("filter_users", configTypeBool, true); |
| 219 | public static ConfigItem filterContacts = addConfig("filter_contacts", configTypeBool, true); |
| 220 | public static ConfigItem filterGroups = addConfig("filter_groups", configTypeBool, true); |
| 221 | public static ConfigItem filterChannels = addConfig("filter_channels", configTypeBool, true); |
| 222 | public static ConfigItem filterBots = addConfig("filter_bots", configTypeBool, true); |
| 223 | public static ConfigItem filterAdmins = addConfig("filter_admins", configTypeBool, true); |
| 224 | public static ConfigItem filterUnmuted = addConfig("filter_unmuted", configTypeBool, true); |
| 225 | public static ConfigItem filterUnread = addConfig("filter_unread", configTypeBool, true); |
| 226 | public static ConfigItem filterUnmutedAndUnread = addConfig("filter_unmuted_and_unread", configTypeBool, true); |
| 227 |
| 228 | public static ConfigItem disableSystemAccount = addConfig("DisableSystemAccount", configTypeBool, false); |
| 229 | public static ConfigItem disableProxyWhenVpnEnabled = addConfig("DisableProxyWhenVpnEnabled", configTypeBool, false); |
| 230 | public static ConfigItem skipOpenLinkConfirm = addConfig("SkipOpenLinkConfirm", configTypeBool, false); |
| 231 |
| 232 | public static ConfigItem ignoreMutedCount = addConfig("IgnoreMutedCount", configTypeBool, true); |
| 233 | public static ConfigItem useDefaultTheme = addConfig("UseDefaultTheme", configTypeBool, false); |
| 234 | public static ConfigItem showIdAndDc = addConfig("ShowIdAndDc", configTypeBool, false); |
| 235 |
| 236 | public static ConfigItem googleCloudTranslateKey = addConfig("GoogleCloudTransKey", configTypeString, ""); |
| 237 | public static ConfigItem cachePath = addConfig("cache_path", configTypeString, ""); |
| 238 |
| 239 | public static ConfigItem translateToLang = addConfig("TransToLang", configTypeString, ""); |
| 240 | public static ConfigItem translateInputLang = addConfig("TransInputToLang", configTypeString, "en"); |
| 241 |
| 242 | public static ConfigItem ccToLang = addConfig("opencc_to_lang", configTypeString, ""); |
| 243 | public static ConfigItem ccInputLang = addConfig("opencc_input_to_lang", configTypeString, ""); |
| 244 |
| 245 | public static ConfigItem tabsTitleType = addConfig("TabTitleType", configTypeInt, TITLE_TYPE_TEXT); |
| 246 | public static ConfigItem confirmAVMessage = addConfig("ConfirmAVMessage", configTypeBool, false); |
| 247 | public static ConfigItem askBeforeCall = addConfig("AskBeforeCalling", configTypeBool, false); |
| 248 | public static ConfigItem disableNumberRounding = addConfig("DisableNumberRounding", configTypeBool, false); |
| 249 |
| 250 | public static ConfigItem useSystemDNS = addConfig("useSystemDNS", configTypeBool, false); |
| 251 | public static ConfigItem customDoH = addConfig("customDoH", configTypeString, ""); |
| 252 | public static ConfigItem hideProxyByDefault = addConfig("HideProxyByDefault", configTypeBool, false); |
| 253 | public static ConfigItem useProxyItem = addConfig("UseProxyItem", configTypeBool, true); |
| 254 |
| 255 | public static ConfigItem disableAppBarShadow = addConfig("DisableAppBarShadow", configTypeBool, false); |
| 256 | public static ConfigItem mediaPreview = addConfig("MediaPreview", configTypeBool, true); |
| 257 |
| 258 | public static ConfigItem proxyAutoSwitch = addConfig("ProxyAutoSwitch", configTypeBool, false); |
| 259 |
| 260 | public static ConfigItem usePersianCalendar = addConfig("UsePersiancalendar", configTypeBool, false); |
| 261 | public static ConfigItem displayPersianCalendarByLatin = addConfig("DisplayPersianCalendarByLatin", configTypeBool, false); |
| 262 | public static ConfigItem openPGPApp = addConfig("OpenPGPApp", configTypeString, ""); |
| 263 | public static ConfigItem openPGPKeyId = addConfig("OpenPGPKey", configTypeLong, 0L); |
| 264 |
| 265 | public static ConfigItem disableVibration = addConfig("DisableVibration", configTypeBool, false); |
| 266 | public static ConfigItem autoPauseVideo = addConfig("AutoPauseVideo", configTypeBool, false); |
| 267 | public static ConfigItem disableProximityEvents = addConfig("DisableProximityEvents", configTypeBool, false); |
| 268 |
| 269 | public static ConfigItem ignoreContentRestrictions = addConfig("ignoreContentRestrictions", configTypeBool, !BuildVars.isPlay); |
| 270 | public static ConfigItem useChatAttachMediaMenu = addConfig("UseChatAttachEnterMenu", configTypeBool, true); |
| 271 | public static ConfigItem disableLinkPreviewByDefault = addConfig("DisableLinkPreviewByDefault", configTypeBool, false); |
| 272 | public static ConfigItem sendCommentAfterForward = addConfig("SendCommentAfterForward", configTypeBool, true); |
| 273 | public static ConfigItem increaseVoiceMessageQuality = addConfig("IncreaseVoiceMessageQuality", configTypeBool, true); |
| 274 | public static ConfigItem acceptSecretChat = addConfig("AcceptSecretChat", configTypeBool, true); |
| 275 | public static ConfigItem disableTrending = addConfig("DisableTrending", configTypeBool, true); |
| 276 | public static ConfigItem dontSendGreetingSticker = addConfig("DontSendGreetingSticker", configTypeBool, false); |
| 277 | public static ConfigItem hideTimeForSticker = addConfig("HideTimeForSticker", configTypeBool, false); |
| 278 | public static ConfigItem takeGIFasVideo = addConfig("TakeGIFasVideo", configTypeBool, false); |
| 279 | public static ConfigItem maxRecentStickerCount = addConfig("maxRecentStickerCount", configTypeInt, 20); |
| 280 | public static ConfigItem disableSwipeToNext = addConfig("disableSwipeToNextChannel", configTypeBool, true); |
| 281 | public static ConfigItem disableRemoteEmojiInteractions = addConfig("disableRemoteEmojiInteractions", configTypeBool, true); |
| 282 | public static ConfigItem disableChoosingSticker = addConfig("disableChoosingSticker", configTypeBool, false); |
| 283 |
| 284 | public static ConfigItem disableAutoDownloadingWin32Executable = addConfig("Win32ExecutableFiles", configTypeBool, true); |
| 285 | public static ConfigItem disableAutoDownloadingArchive = addConfig("ArchiveFiles", configTypeBool, true); |
| 286 |
| 287 | public static ConfigItem enableStickerPin = addConfig("EnableStickerPin", configTypeBool, false); |
| 288 | public static ConfigItem useMediaStreamInVoip = addConfig("UseMediaStreamInVoip", configTypeBool, false); |
| 289 | public static ConfigItem customAudioBitrate = addConfig("customAudioBitrate", configTypeInt, 32); |
| 290 | public static ConfigItem disableGroupVoipAudioProcessing = addConfig("disableGroupVoipAudioProcessing", configTypeBool, false); |
| 291 |
| 292 |
| 293 | static { |
| 294 | loadConfig(false); |
| 295 | } |
| 296 |
| 297 | public static ConfigItem addConfig(String k, int t, Object d) { |
| 298 | ConfigItem a = new ConfigItem(k, t, d); |
| 299 | configs.add(a); |
| 300 | return a; |
| 301 | } |
| 302 |
| 303 | public static ConfigItem findOne(String key) { |
| 304 | for (int i = 0; i < configs.size(); i++) { |
| 305 | ConfigItem o = configs.get(i); |
| 306 | if (key.equals(o.key)) { |
| 307 | return o; |
| 308 | } |
| 309 | } |
| 310 | return null; |
| 311 | } |
| 312 |
| 313 | public static void loadConfig(boolean force) { |
| 314 | synchronized (sync) { |
| 315 | if (configLoaded && !force) { |
| 316 | return; |
| 317 | } |
| 318 | SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nkmrcfg", Activity.MODE_PRIVATE); |
| 319 | for (int i = 0; i < configs.size(); i++) { |
| 320 | ConfigItem o = configs.get(i); |
| 321 |
| 322 | if (o.forceDefault) { |
| 323 | o.value = o.defaultValue; |
| 324 | continue; |
| 325 | } |
| 326 |
| 327 | if (o.type == configTypeBool) { |
| 328 | o.value = preferences.getBoolean(o.key, (boolean) o.defaultValue); |
| 329 | } |
| 330 | if (o.type == configTypeInt) { |
| 331 | o.value = preferences.getInt(o.key, (int) o.defaultValue); |
| 332 | } |
| 333 | if (o.type == configTypeLong) { |
| 334 | o.value = preferences.getLong(o.key, (Long) o.defaultValue); |
| 335 | } |
| 336 | if (o.type == configTypeFloat) { |
| 337 | o.value = preferences.getFloat(o.key, (Float) o.defaultValue); |
| 338 | } |
| 339 | if (o.type == configTypeString) { |
| 340 | o.value = preferences.getString(o.key, (String) o.defaultValue); |
| 341 | } |
| 342 | if (o.type == configTypeSetInt) { |
| 343 | Set<String> ss = preferences.getStringSet(o.key, new HashSet<>()); |
| 344 | HashSet<Integer> si = new HashSet<>(); |
| 345 | for (String s : ss) { |
| 346 | si.add(Integer.parseInt(s)); |
| 347 | } |
| 348 | o.value = si; |
| 349 | } |
| 350 | if (o.type == configTypeMapIntInt) { |
| 351 | String cv = preferences.getString(o.key, ""); |
| 352 | // Log.e("NC", String.format("Getting pref %s val %s", o.key, cv)); |
| 353 | if (cv.length() == 0) { |
| 354 | o.value = new HashMap<Integer, Integer>(); |
| 355 | } else { |
| 356 | try { |
| 357 | byte[] data = Base64.decode(cv, Base64.DEFAULT); |
| 358 | ObjectInputStream ois = new ObjectInputStream( |
| 359 | new ByteArrayInputStream(data)); |
| 360 | o.value = (HashMap<Integer, Integer>) ois.readObject(); |
| 361 | if (o.value == null) { |
| 362 | o.value = new HashMap<Integer, Integer>(); |
| 363 | } |
| 364 | ois.close(); |
| 365 | } catch (Exception e) { |
| 366 | o.value = new HashMap<Integer, Integer>(); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | configLoaded = true; |
| 372 | } |
| 373 | } |
| 374 |
| 375 | public static void saveConfig(ConfigItem item) { |
| 376 | synchronized (sync) { |
| 377 | try { |
| 378 | SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nkmrcfg", Context.MODE_PRIVATE); |
| 379 | SharedPreferences.Editor editor = preferences.edit(); |
| 380 | for (int i = 0; i < configs.size(); i++) { |
| 381 | ConfigItem o = configs.get(i); |
| 382 | if (item != null && !item.getKey().equals(o.getKey())) continue; |
| 383 |
| 384 | if (o.type == configTypeBool) { |
| 385 | editor.putBoolean(o.key, (boolean) o.value); |
| 386 | } |
| 387 | if (o.type == configTypeInt) { |
| 388 | editor.putInt(o.key, (int) o.value); |
| 389 | } |
| 390 | if (o.type == configTypeLong) { |
| 391 | editor.putLong(o.key, (Long) o.value); |
| 392 | } |
| 393 | if (o.type == configTypeFloat) { |
| 394 | editor.putFloat(o.key, (Float) o.value); |
| 395 | } |
| 396 | if (o.type == configTypeString) { |
| 397 | editor.putString(o.key, o.value.toString()); |
| 398 | } |
| 399 | if (o.type == configTypeSetInt) { |
| 400 | HashSet<String> ss = new HashSet<>(); |
| 401 | for (Integer n : (Set<Integer>) o.value) { |
| 402 | ss.add(Integer.toString(n)); |
| 403 | } |
| 404 | editor.putStringSet(o.key, ss); |
| 405 | } |
| 406 | if (o.type == configTypeMapIntInt) { |
| 407 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 408 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 409 | oos.writeObject(o.value); |
| 410 | oos.close(); |
| 411 | editor.putString(o.key, Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT)); |
| 412 | } |
| 413 | } |
| 414 | editor.commit(); |
| 415 | } catch (Exception e) { |
| 416 | FileLog.e(e); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |