From 9c075fa4f49187c993963a15bda45e5fd61848bb Mon Sep 17 00:00:00 2001 From: xtaodada Date: Tue, 25 Jun 2024 21:30:49 +0800 Subject: [PATCH] fix: remote emoji pack --- .../java/org/telegram/messenger/Emoji.java | 12 ++---- .../nekogram/helpers/UnzipHelper.java | 36 ----------------- .../nekogram/helpers/remote/EmojiHelper.java | 40 ++++++++++++++----- .../settings/NekoChatSettingsActivity.java | 5 ++- 4 files changed, 36 insertions(+), 57 deletions(-) delete mode 100644 TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/UnzipHelper.java diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/Emoji.java b/TMessagesProj/src/main/java/org/telegram/messenger/Emoji.java index 9ab4f33641..74eb0f5819 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/Emoji.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/Emoji.java @@ -86,18 +86,16 @@ public class Emoji { private final static int MAX_RECENT_EMOJI_COUNT = 48; - private static boolean isSelectedCustomEmojiPack; private static File emojiFile; private static boolean isSelectedEmojiPack; private static void reloadCache() { - isSelectedCustomEmojiPack = EmojiHelper.getInstance().isSelectedCustomEmojiPack(); emojiFile = EmojiHelper.getInstance().getCurrentEmojiPackOffline(); isSelectedEmojiPack = !EmojiHelper.getInstance().getEmojiPack().equals("default") && emojiFile != null && emojiFile.exists(); } public static boolean isSelectedCustomPack() { - return isSelectedCustomEmojiPack || isSelectedEmojiPack || NekoConfig.useSystemEmoji.Bool(); + return isSelectedEmojiPack || NekoConfig.useSystemEmoji.Bool(); } public static void reloadEmoji() { @@ -147,7 +145,7 @@ private static void loadEmoji(final byte page, final short page2) { loadingEmoji[page][page2] = true; Utilities.globalQueue.postRunnable(() -> { final Bitmap bitmap; - if (NekoConfig.useSystemEmoji.Bool() || isSelectedCustomEmojiPack) { + if (NekoConfig.useSystemEmoji.Bool() || isSelectedEmojiPack) { int emojiSize = 66; bitmap = Bitmap.createBitmap(emojiSize, emojiSize, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); @@ -160,11 +158,7 @@ private static void loadEmoji(final byte page, final short page2) { emojiSize ); } else { - if (isSelectedEmojiPack) { - bitmap = loadBitmap(emojiFile.getAbsolutePath() + "/" + String.format(Locale.US, "%d_%d.png", page, page2), false); - } else { - bitmap = loadBitmap("emoji/" + String.format(Locale.US, "%d_%d.png", page, page2)); - } + bitmap = loadBitmap("emoji/" + String.format(Locale.US, "%d_%d.png", page, page2)); } if (bitmap != null) { emojiBmp[page][page2] = bitmap; diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/UnzipHelper.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/UnzipHelper.java deleted file mode 100644 index c78707bf55..0000000000 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/UnzipHelper.java +++ /dev/null @@ -1,36 +0,0 @@ -package tw.nekomimi.nekogram.helpers; - -import org.telegram.messenger.AndroidUtilities; -import org.telegram.messenger.DispatchQueue; -import org.telegram.messenger.FileLog; - -import java.io.File; -import java.io.IOException; -import java.util.zip.ZipFile; - -public class UnzipHelper { - private static final DispatchQueue unzipQueue = new DispatchQueue("unzipQueue"); - - public static void unzip(String path, File output, Runnable callback) { - unzipQueue.postRunnable(() -> { - try (var zip = new ZipFile(path)) { - var entries = zip.entries(); - while (entries.hasMoreElements()) { - var entry = entries.nextElement(); - var target = new File(output.getAbsolutePath(), entry.getName()); - if (!entry.isDirectory()) { - var in = zip.getInputStream(entry); - AndroidUtilities.copyFile(in, target); - in.close(); - } else { - //noinspection ResultOfMethodCallIgnored - target.mkdir(); - } - } - } catch (IOException e) { - FileLog.e(e); - } - AndroidUtilities.runOnUIThread(callback); - }); - } -} diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/remote/EmojiHelper.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/remote/EmojiHelper.java index 9a4e0c41b5..3dafeb1b13 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/remote/EmojiHelper.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/helpers/remote/EmojiHelper.java @@ -55,13 +55,13 @@ import java.util.stream.Collectors; import tw.nekomimi.nekogram.NekoConfig; -import tw.nekomimi.nekogram.helpers.UnzipHelper; @SuppressWarnings("ResultOfMethodCallIgnored") public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.NotificationCenterDelegate { - private static final String EMOJI_TAG = "emojiv1"; + private static final String EMOJI_TAG = "emojiv2"; private static final String EMOJI_FONT_AOSP = "NotoColorEmoji.ttf"; - private static final int EMOJI_COUNT = 3538; + private static final String EMOJI_FONT_NAME = "font.ttf"; + private static final int EMOJI_COUNT = 1; private static final String EMOJI_PACKS_FILE_DIR; private static final Runnable invalidateUiRunnable = () -> NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.emojiLoaded); private static final String[] previewEmojis = { @@ -313,7 +313,7 @@ public Typeface getSystemEmojiTypeface() { } private Typeface getSelectedTypeface() { - EmojiPackBase pack = getEmojiCustomPacksInfo() + EmojiPackBase pack = getEmojiPacksInfoAll() .parallelStream() .filter(emojiPackInfo -> emojiPackInfo.packId.equals(emojiPack)) .findFirst() @@ -406,12 +406,21 @@ public ArrayList getEmojiCustomPacksInfo() { .collect(Collectors.toCollection(ArrayList::new)); } + public ArrayList getEmojiPacksInfoAll() { + ArrayList emojiPacksInfo = getEmojiPacksInfo(); + ArrayList emojiCustomPacksInfo = getEmojiCustomPacksInfo(); + ArrayList newList = new ArrayList<>(); + newList.addAll(emojiPacksInfo); + newList.addAll(emojiCustomPacksInfo); + return newList; + } + public boolean isInstalledOldVersion(String emojiID, int version) { - return getAllVersions(emojiID, version).size() > 0; + return !getAllVersions(emojiID, version).isEmpty(); } public boolean isInstalledOffline(String emojiID) { - return getAllVersions(emojiID, -1).size() > 0; + return !getAllVersions(emojiID, -1).isEmpty(); } public ArrayList getAllVersions(String emojiID) { @@ -475,7 +484,18 @@ public boolean isEmojiPackDownloading(EmojiPackInfo pack) { public void installDownloadedEmoji(EmojiPackInfo pack, boolean update) { var emojiDir = EmojiHelper.getEmojiDir(pack.packId, pack.packVersion); emojiDir.mkdir(); - UnzipHelper.unzip(pack.fileLocation, emojiDir, () -> { + File old = new File(pack.fileLocation); + File newFile = new File(emojiDir, EMOJI_FONT_NAME); + if (old.isFile() && old.exists() && old.canRead()) { + try (FileInputStream inputStream = new FileInputStream(old)) { + AndroidUtilities.copyFile(inputStream, newFile); + } catch (IOException e) { + e.printStackTrace(); + } + if (newFile.isFile() && newFile.exists() && newFile.canRead()) { + loadingEmojiPacks.remove(pack.fileLocation); + pack.fileLocation = newFile.toString(); + } if (isPackInstalled(pack)) { if (update) { EmojiHelper.getInstance().deleteOldVersions(pack); @@ -484,10 +504,8 @@ public void installDownloadedEmoji(EmojiPackInfo pack, boolean update) { } reloadEmoji(); } - callProgressChanged(pack, true, 100, pack.fileSize); - loadingEmojiPacks.remove(pack.fileLocation); - }); - callProgressChanged(pack, false, 100, pack.fileSize); + } + callProgressChanged(pack, true, 100, pack.fileSize); } public EmojiPackBase installEmoji(File emojiFile) throws Exception { diff --git a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java index 47d5c348cc..ba8d924d7c 100644 --- a/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java +++ b/TMessagesProj/src/main/java/tw/nekomimi/nekogram/settings/NekoChatSettingsActivity.java @@ -918,6 +918,7 @@ public int getItemViewType(int position) { @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { + View view = holder.itemView; AbstractConfigCell a = cellGroup.rows.get(position); if (a != null) { if (a instanceof ConfigCellCustom) { @@ -929,6 +930,9 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { } else if (position == cellGroup.rows.indexOf(doubleTapActionRow)) { textCell.setTextAndValue(LocaleController.getString("DoubleTapAction", R.string.DoubleTapAction), DoubleTap.doubleTapActionMap.get(NaConfig.INSTANCE.getDoubleTapAction().Int()), true); } + } else if (view instanceof EmojiSetCell) { + EmojiSetCell v1 = (EmojiSetCell) view; + v1.setData(EmojiHelper.getInstance().getCurrentEmojiPackInfo(), false, true); } } else { // Default binds @@ -970,7 +974,6 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType break; case ConfigCellCustom.CUSTOM_ITEM_EmojiSet: view = emojiSetCell = new EmojiSetCell(mContext, false); - emojiSetCell.setData(EmojiHelper.getInstance().getCurrentEmojiPackInfo(), false, true); view.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundWhite)); break; }