| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.content.SharedPreferences; |
| 4 | import android.content.pm.ApplicationInfo; |
| 5 | import android.content.pm.PackageManager; |
| 6 | import android.content.res.Configuration; |
| 7 | import android.graphics.Typeface; |
| 8 | |
| 9 | import org.telegram.messenger.AndroidUtilities; |
| 10 | import org.telegram.messenger.ApplicationLoader; |
| 11 | import org.telegram.messenger.BuildConfig; |
| 12 | import org.telegram.messenger.BuildVars; |
| 13 | import org.telegram.messenger.FileLog; |
| 14 | import org.telegram.messenger.LocaleController; |
| 15 | import org.telegram.messenger.NotificationCenter; |
| 16 | import org.telegram.messenger.R; |
| 17 | import org.telegram.messenger.SharedConfig; |
| 18 | import org.telegram.messenger.UserConfig; |
| 19 | import org.telegram.ui.ActionBar.Theme; |
| 20 | |
| 21 | import java.io.BufferedReader; |
| 22 | import java.io.FileReader; |
| 23 | import java.util.Locale; |
| 24 | import java.util.regex.Matcher; |
| 25 | import java.util.regex.Pattern; |
| 26 | |
| 27 | import cn.hutool.core.util.ArrayUtil; |
| 28 | import cn.hutool.core.util.StrUtil; |
| 29 | import tw.nekomimi.nekogram.database.NitritesKt; |
| 30 | |
| 31 | public class NekoXConfig { |
| 32 | |
| 33 | // public static String FAQ_URL = "https://telegra.ph/NekoX-FAQ-03-31"; |
| 34 | public static String FAQ_URL = "https://github.com/NextAlone/Nagram#faq"; |
| 35 | public static long[] officialChats = { |
| 36 | // 1305127566, // NekoX Updates |
| 37 | // 1151172683, // NekoX Chat |
| 38 | // 1299578049, // NekoX Chat Channel |
| 39 | // 1137038259, // NekoX APKs |
| 40 | 1500637449, // Nagram |
| 41 | 1645699549, // Nagram Updates |
| 42 | 2001739482, // Nagram Tips |
| 43 | }; |
| 44 | |
| 45 | public static long[] developers = { |
| 46 | 896711046, // nekohasekai |
| 47 | 380570774, // Haruhi |
| 48 | 784901712, // NextAlone |
| 49 | 457896977, // Queally |
| 50 | 782954985, // MaiTungTM |
| 51 | 5412523572L, //blxueya |
| 52 | 676660002, // mrwangzhe |
| 53 | }; |
| 54 | |
| 55 | public static final int TITLE_TYPE_TEXT = 0; |
| 56 | public static final int TITLE_TYPE_ICON = 1; |
| 57 | public static final int TITLE_TYPE_MIX = 2; |
| 58 | |
| 59 | private static final String EMOJI_FONT_AOSP = "NotoColorEmoji.ttf"; |
| 60 | |
| 61 | public static boolean loadSystemEmojiFailed = false; |
| 62 | private static Typeface systemEmojiTypeface; |
| 63 | |
| 64 | |
| 65 | public static SharedPreferences preferences = NitritesKt.openMainSharedPreference("nekox_config"); |
| 66 | |
| 67 | public static boolean developerMode = preferences.getBoolean("developer_mode", true); |
| 68 | |
| 69 | public static boolean disableFlagSecure = preferences.getBoolean("disable_flag_secure", true); |
| 70 | public static boolean disableScreenshotDetection = preferences.getBoolean("disable_screenshot_detection", false); |
| 71 | |
| 72 | public static boolean disableStatusUpdate = preferences.getBoolean("disable_status_update", false); |
| 73 | public static boolean keepOnlineStatus = preferences.getBoolean("keepOnlineStatus", false); |
| 74 | |
| 75 | public static int autoUpdateReleaseChannel = preferences.getInt("autoUpdateReleaseChannel", 2); |
| 76 | // public static String ignoredUpdateTag = preferences.getString("ignoredUpdateTag", ""); |
| 77 | // public static long nextUpdateCheck = preferences.getLong("nextUpdateCheckTimestamp", 0); |
| 78 | |
| 79 | public static int customApi = preferences.getInt("custom_api", 0); |
| 80 | public static int customAppId = preferences.getInt("custom_app_id", 0); |
| 81 | public static String customAppHash = preferences.getString("custom_app_hash", ""); |
| 82 | |
| 83 | public static void toggleDeveloperMode() { |
| 84 | preferences.edit().putBoolean("developer_mode", developerMode = !developerMode).apply(); |
| 85 | if (!developerMode) { |
| 86 | preferences.edit() |
| 87 | .putBoolean("disable_flag_secure", disableFlagSecure = false) |
| 88 | .putBoolean("disable_screenshot_detection", disableScreenshotDetection = false) |
| 89 | .putBoolean("disable_status_update", disableStatusUpdate = false) |
| 90 | .apply(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public static void toggleDisableFlagSecure() { |
| 95 | preferences.edit().putBoolean("disable_flag_secure", disableFlagSecure = !disableFlagSecure).apply(); |
| 96 | } |
| 97 | |
| 98 | public static void toggleDisableScreenshotDetection() { |
| 99 | preferences.edit().putBoolean("disable_screenshot_detection", disableScreenshotDetection = !disableScreenshotDetection).apply(); |
| 100 | } |
| 101 | |
| 102 | private static Boolean hasDeveloper = null; |
| 103 | |
| 104 | public static int currentAppId() { |
| 105 | switch (customApi) { |
| 106 | case 0: |
| 107 | return BuildConfig.APP_ID; |
| 108 | case 1: |
| 109 | return BuildVars.OFFICAL_APP_ID; |
| 110 | case 2: |
| 111 | return BuildVars.TGX_APP_ID; |
| 112 | default: |
| 113 | return customAppId; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public static String currentAppHash() { |
| 118 | switch (customApi) { |
| 119 | case 0: |
| 120 | return BuildConfig.APP_HASH; |
| 121 | case 1: |
| 122 | return BuildVars.OFFICAL_APP_HASH; |
| 123 | case 2: |
| 124 | return BuildVars.TGX_APP_HASH; |
| 125 | default: |
| 126 | return customAppHash; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | public static void saveCustomApi() { |
| 131 | preferences.edit() |
| 132 | .putInt("custom_api", customApi) |
| 133 | .putInt("custom_app_id", customAppId) |
| 134 | .putString("custom_app_hash", customAppHash) |
| 135 | .apply(); |
| 136 | } |
| 137 | |
| 138 | public static void toggleDisableStatusUpdate() { |
| 139 | preferences.edit().putBoolean("disable_status_update", disableStatusUpdate = !disableStatusUpdate).apply(); |
| 140 | NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.updateUserStatus, (Object) null); |
| 141 | } |
| 142 | |
| 143 | public static void toggleKeepOnlineStatus() { |
| 144 | preferences.edit().putBoolean("keepOnlineStatus", keepOnlineStatus = !keepOnlineStatus).apply(); |
| 145 | NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.updateUserStatus, (Object) null); |
| 146 | } |
| 147 | |
| 148 | public static void setAutoUpdateReleaseChannel(int channel) { |
| 149 | preferences.edit().putInt("autoUpdateReleaseChannel", autoUpdateReleaseChannel = channel).apply(); |
| 150 | } |
| 151 | |
| 152 | public static boolean isDeveloper() { |
| 153 | if (hasDeveloper != null) |
| 154 | return hasDeveloper; |
| 155 | hasDeveloper = true; // BuildVars.DEBUG_VERSION; |
| 156 | for (int acc : SharedConfig.activeAccounts) { |
| 157 | long myId = UserConfig.getInstance(acc).clientUserId; |
| 158 | if (ArrayUtil.contains(NekoXConfig.developers, myId)) { |
| 159 | hasDeveloper = true; |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | return hasDeveloper; |
| 164 | } |
| 165 | |
| 166 | public static String getOpenPGPAppName() { |
| 167 | if (StrUtil.isNotBlank(NekoConfig.openPGPApp.String())) { |
| 168 | try { |
| 169 | PackageManager manager = ApplicationLoader.applicationContext.getPackageManager(); |
| 170 | ApplicationInfo info = manager.getApplicationInfo(NekoConfig.openPGPApp.String(), PackageManager.GET_META_DATA); |
| 171 | return (String) manager.getApplicationLabel(info); |
| 172 | } catch (PackageManager.NameNotFoundException e) { |
| 173 | NekoConfig.openPGPApp.setConfigString(""); |
| 174 | } |
| 175 | } |
| 176 | return LocaleController.getString("None", R.string.None); |
| 177 | } |
| 178 | |
| 179 | public static String formatLang(String name) { |
| 180 | if (name == null || name.isEmpty()) { |
| 181 | return LocaleController.getString("Default", R.string.Default); |
| 182 | } else { |
| 183 | if (name.contains("-")) { |
| 184 | return new Locale(StrUtil.subBefore(name, "-", false), StrUtil.subAfter(name, "-", false)).getDisplayName(LocaleController.getInstance().currentLocale); |
| 185 | } else { |
| 186 | return new Locale(name).getDisplayName(LocaleController.getInstance().currentLocale); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | public static Typeface getSystemEmojiTypeface() { |
| 192 | if (!loadSystemEmojiFailed && systemEmojiTypeface == null) { |
| 193 | try { |
| 194 | Pattern p = Pattern.compile(">(.*emoji.*)</font>", Pattern.CASE_INSENSITIVE); |
| 195 | BufferedReader br = new BufferedReader(new FileReader("/system/etc/fonts.xml")); |
| 196 | String line; |
| 197 | while ((line = br.readLine()) != null) { |
| 198 | Matcher m = p.matcher(line); |
| 199 | if (m.find()) { |
| 200 | systemEmojiTypeface = Typeface.createFromFile("/system/fonts/" + m.group(1)); |
| 201 | FileLog.d("emoji font file fonts.xml = " + m.group(1)); |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | br.close(); |
| 206 | } catch (Exception e) { |
| 207 | FileLog.e(e); |
| 208 | } |
| 209 | if (systemEmojiTypeface == null) { |
| 210 | try { |
| 211 | systemEmojiTypeface = Typeface.createFromFile("/system/fonts/" + EMOJI_FONT_AOSP); |
| 212 | FileLog.d("emoji font file = " + EMOJI_FONT_AOSP); |
| 213 | } catch (Exception e) { |
| 214 | FileLog.e(e); |
| 215 | loadSystemEmojiFailed = true; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | return systemEmojiTypeface; |
| 220 | } |
| 221 | |
| 222 | public static int getNotificationColor() { |
| 223 | int color = 0; |
| 224 | Configuration configuration = ApplicationLoader.applicationContext.getResources().getConfiguration(); |
| 225 | boolean isDark = (configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; |
| 226 | if (isDark) { |
| 227 | color = 0xffffffff; |
| 228 | } else { |
| 229 | if (Theme.getActiveTheme().hasAccentColors()) { |
| 230 | color = Theme.getActiveTheme().getAccentColor(Theme.getActiveTheme().currentAccentId); |
| 231 | } |
| 232 | if (Theme.getActiveTheme().isDark() || color == 0) { |
| 233 | color = Theme.getColor(Theme.key_actionBarDefault); |
| 234 | } |
| 235 | // too bright |
| 236 | if (AndroidUtilities.computePerceivedBrightness(color) >= 0.721f) { |
| 237 | color = Theme.getColor(Theme.key_windowBackgroundWhiteBlueHeader) | 0xff000000; |
| 238 | } |
| 239 | } |
| 240 | return color; |
| 241 | } |
| 242 | |
| 243 | |
| 244 | public static void setChannelAlias(long channelID, String name) { |
| 245 | preferences.edit().putString(NekoConfig.channelAliasPrefix + channelID, name).apply(); |
| 246 | } |
| 247 | |
| 248 | public static void emptyChannelAlias(long channelID) { |
| 249 | preferences.edit().remove(NekoConfig.channelAliasPrefix + channelID).apply(); |
| 250 | } |
| 251 | |
| 252 | public static String getChannelAlias(long channelID) { |
| 253 | return preferences.getString(NekoConfig.channelAliasPrefix + channelID, null); |
| 254 | } |
| 255 | } |
| 256 | |