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