fix: remote emoji pack

This commit is contained in:
xtaodada 2024-06-25 21:30:49 +08:00
parent 000c6e4b6a
commit 9c075fa4f4
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 36 additions and 57 deletions

View File

@ -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 @@ public class Emoji {
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 @@ public class Emoji {
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;

View File

@ -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);
});
}
}

View File

@ -55,13 +55,13 @@ import java.util.Objects;
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 class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
}
private Typeface getSelectedTypeface() {
EmojiPackBase pack = getEmojiCustomPacksInfo()
EmojiPackBase pack = getEmojiPacksInfoAll()
.parallelStream()
.filter(emojiPackInfo -> emojiPackInfo.packId.equals(emojiPack))
.findFirst()
@ -406,12 +406,21 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
.collect(Collectors.toCollection(ArrayList::new));
}
public ArrayList<EmojiPackBase> getEmojiPacksInfoAll() {
ArrayList<EmojiPackInfo> emojiPacksInfo = getEmojiPacksInfo();
ArrayList<EmojiPackBase> emojiCustomPacksInfo = getEmojiCustomPacksInfo();
ArrayList<EmojiPackBase> 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<File> getAllVersions(String emojiID) {
@ -475,7 +484,18 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
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 class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
}
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 {

View File

@ -918,6 +918,7 @@ public class NekoChatSettingsActivity extends BaseNekoXSettingsActivity implemen
@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 class NekoChatSettingsActivity extends BaseNekoXSettingsActivity implemen
} 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 class NekoChatSettingsActivity extends BaseNekoXSettingsActivity implemen
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;
}