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