Fix custom emoji packs

Author:    tehcneko <chsqwyx@gmail.com>
This commit is contained in:
tehcneko 2023-01-21 20:50:51 +08:00 committed by xtaodada
parent 59cfedd355
commit 9a02f92c84
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659

View File

@ -304,24 +304,25 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
} }
private Typeface getSelectedTypeface() { private Typeface getSelectedTypeface() {
return getEmojiCustomPacksInfo() EmojiPackBase pack = getEmojiCustomPacksInfo()
.parallelStream() .parallelStream()
.filter(emojiPackInfo -> emojiPackInfo.packId.equals(emojiPack)) .filter(emojiPackInfo -> emojiPackInfo.packId.equals(emojiPack))
.map(emojiPackInfo -> {
Typeface typeface;
if (!typefaceCache.containsKey(emojiPackInfo.packId)) {
File emojiFile = new File(emojiPackInfo.fileLocation);
if (!emojiFile.exists()) {
return null;
}
typefaceCache.put(emojiPackInfo.packId, typeface = Typeface.createFromFile(emojiFile));
} else {
typeface = typefaceCache.get(emojiPackInfo.packId);
}
return typeface;
})
.findFirst() .findFirst()
.orElse(null); .orElse(null);
if (pack == null) {
return null;
}
Typeface typeface;
if (!typefaceCache.containsKey(pack.packId)) {
File emojiFile = new File(pack.fileLocation);
if (!emojiFile.exists()) {
return null;
}
typefaceCache.put(pack.packId, typeface = Typeface.createFromFile(emojiFile));
} else {
typeface = typefaceCache.get(pack.packId);
}
return typeface;
} }
public String getSelectedPackName() { public String getSelectedPackName() {
@ -348,10 +349,6 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
? emojiPack : "default"; ? emojiPack : "default";
} }
public boolean loadedPackInfo() {
return emojiPacksInfo.parallelStream().anyMatch(e -> e instanceof EmojiPackInfo);
}
public void loadEmojisInfo(EmojiPacksLoadedListener listener) { public void loadEmojisInfo(EmojiPacksLoadedListener listener) {
if (loadingPack) { if (loadingPack) {
return; return;
@ -367,13 +364,13 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
} }
public void loadEmojiPackInfo() { public void loadEmojiPackInfo() {
String list = preferences.getString("emoji_packs", ""); String list = preferences.getString("emoji_packs_v2", "");
if (!TextUtils.isEmpty(list)) { if (!TextUtils.isEmpty(list)) {
byte[] bytes = Base64.decode(list, Base64.DEFAULT); byte[] bytes = Base64.decode(list, Base64.DEFAULT);
SerializedData data = new SerializedData(bytes); SerializedData data = new SerializedData(bytes);
int count = data.readInt32(false); int count = data.readInt32(false);
for (int a = 0; a < count; a++) { for (int a = 0; a < count; a++) {
emojiPacksInfo.add(data.readBool(false) ? EmojiPackInfo.deserialize(data) : EmojiPackBase.deserialize(data)); emojiPacksInfo.add(EmojiPackInfo.deserialize(data));
} }
data.cleanup(); data.cleanup();
} }
@ -671,18 +668,17 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
} }
private void checkAccount() { private void checkAccount() {
if (Thread.currentThread() != ApplicationLoader.applicationHandler.getLooper().getThread()) { AndroidUtilities.runOnUIThread(() -> {
return; if (currentAccount != UserConfig.selectedAccount) {
} NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.fileLoaded);
if (currentAccount != UserConfig.selectedAccount) { NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.fileLoadProgressChanged);
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.fileLoaded); NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.fileLoadFailed);
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.fileLoadProgressChanged); }
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.fileLoadFailed); currentAccount = UserConfig.selectedAccount;
} NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.fileLoaded);
currentAccount = UserConfig.selectedAccount; NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.fileLoadProgressChanged);
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.fileLoaded); NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.fileLoadFailed);
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.fileLoadProgressChanged); });
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.fileLoadFailed);
} }
private void getNewVersionMessagesCallback(Delegate delegate, ArrayList<EmojiPackInfo> packs, TLObject response) { private void getNewVersionMessagesCallback(Delegate delegate, ArrayList<EmojiPackInfo> packs, TLObject response) {
@ -700,15 +696,21 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
SerializedData serializedData = new SerializedData(); SerializedData serializedData = new SerializedData();
serializedData.writeInt32(packs.size()); serializedData.writeInt32(packs.size());
for (EmojiPackInfo pack : packs) { for (EmojiPackInfo pack : packs) {
pack.fileDocument = documents.get(pack.fileId); TLRPC.Document file = documents.get(pack.fileId);
pack.previewDocument = documents.get(pack.previewId); if (file != null) {
if (pack.fileDocument != null) { pack.flags |= 1;
pack.fileSize = pack.fileDocument.size; pack.fileDocument = file;
pack.fileLocation = getFileLoader().getPathToAttach(pack.fileDocument).getAbsolutePath(); pack.fileSize = file.size;
pack.fileLocation = getFileLoader().getPathToAttach(file).getAbsolutePath();
}
TLRPC.Document preview = documents.get(pack.previewId);
if (preview != null) {
pack.flags |= 2;
pack.previewDocument = preview;
} }
pack.serializeToStream(serializedData); pack.serializeToStream(serializedData);
} }
preferences.edit().putString("emoji_packs", Base64.encodeToString(serializedData.toByteArray(), Base64.NO_WRAP)).apply(); preferences.edit().putString("emoji_packs_v2", Base64.encodeToString(serializedData.toByteArray(), Base64.NO_WRAP | Base64.NO_PADDING)).apply();
serializedData.cleanup(); serializedData.cleanup();
AndroidUtilities.runOnUIThread(() -> { AndroidUtilities.runOnUIThread(() -> {
@ -789,8 +791,8 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
} }
int reason = (Integer) args[1]; int reason = (Integer) args[1];
if (reason == 0) { if (reason == 0) {
EmojiHelper.getInstance().load((res, error) -> { EmojiHelper.getInstance().load((res, error) -> AndroidUtilities.runOnUIThread(() -> {
EmojiHelper.EmojiPackInfo newPack = EmojiHelper.getInstance().getEmojiPackInfo(pack.getPackId()); EmojiPackInfo newPack = EmojiHelper.getInstance().getEmojiPackInfo(pack.getPackId());
if (newPack == null) { if (newPack == null) {
return; return;
} }
@ -798,7 +800,7 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
loadingEmojiPacks.remove(pack.getFileLocation()); loadingEmojiPacks.remove(pack.getFileLocation());
downloadPack(newPack, params[0], true); downloadPack(newPack, params[0], true);
} }
}); }));
} }
} else { } else {
installDownloadedEmoji(pack, params[0]); installDownloadedEmoji(pack, params[0]);
@ -871,7 +873,7 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
protected String packName; protected String packName;
protected String packId; protected String packId;
protected String fileLocation; protected String fileLocation;
protected String preview; private String preview;
protected long fileSize; protected long fileSize;
public EmojiPackBase() { public EmojiPackBase() {
@ -886,16 +888,6 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
this.fileSize = fileSize; this.fileSize = fileSize;
} }
public static EmojiPackBase deserialize(AbstractSerializedData stream) {
EmojiPackBase pack = new EmojiPackBase();
pack.packId = stream.readString(false);
pack.packName = stream.readString(false);
pack.fileLocation = stream.readString(false);
pack.preview = stream.readString(false);
pack.fileSize = stream.readInt64(false);
return pack;
}
public void loadFromFile(File file) { public void loadFromFile(File file) {
String fileName = file.getName(); String fileName = file.getName();
packName = fileName; packName = fileName;
@ -927,18 +919,10 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
public Long getFileSize() { public Long getFileSize() {
return fileSize; return fileSize;
} }
public void serializeToStream(AbstractSerializedData serializedData) {
serializedData.writeBool(false);
serializedData.writeString(packId);
serializedData.writeString(packName);
serializedData.writeString(fileLocation);
serializedData.writeString(preview);
serializedData.writeInt64(fileSize);
}
} }
public static class EmojiPackInfo extends EmojiPackBase { public static class EmojiPackInfo extends EmojiPackBase {
private int flags;
private int previewId; private int previewId;
private int fileId; private int fileId;
private int packVersion; private int packVersion;
@ -956,34 +940,6 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
public EmojiPackInfo() { public EmojiPackInfo() {
} }
public static EmojiPackInfo deserialize(AbstractSerializedData stream) {
EmojiPackInfo pack = new EmojiPackInfo();
pack.packId = stream.readString(false);
pack.packName = stream.readString(false);
pack.fileLocation = stream.readString(false);
pack.preview = stream.readString(false);
pack.fileSize = stream.readInt64(false);
pack.fileId = stream.readInt32(false);
pack.previewId = stream.readInt32(false);
pack.packVersion = stream.readInt32(false);
if (stream.readBool(false)) {
pack.previewDocument = TLRPC.Document.TLdeserialize(stream, stream.readInt32(false), false);
}
if (stream.readBool(false)) {
pack.fileDocument = TLRPC.Document.TLdeserialize(stream, stream.readInt32(false), false);
}
return pack;
}
public int getFileId() {
return fileId;
}
public int getPreviewId() {
return previewId;
}
public TLRPC.Document getPreviewDocument() { public TLRPC.Document getPreviewDocument() {
return previewDocument; return previewDocument;
} }
@ -996,24 +952,40 @@ public class EmojiHelper extends BaseRemoteHelper implements NotificationCenter.
return packVersion; return packVersion;
} }
@Override public static EmojiPackInfo deserialize(AbstractSerializedData stream) {
EmojiPackInfo pack = new EmojiPackInfo();
pack.flags = stream.readInt32(false);
pack.packId = stream.readString(false);
pack.packName = stream.readString(false);
pack.fileId = stream.readInt32(false);
pack.previewId = stream.readInt32(false);
pack.packVersion = stream.readInt32(false);
if ((pack.flags & 1) != 0) {
pack.previewDocument = TLRPC.Document.TLdeserialize(stream, stream.readInt32(false), false);
pack.fileSize = stream.readInt64(false);
pack.fileLocation = stream.readString(false);
}
if ((pack.flags & 2) != 0) {
pack.fileDocument = TLRPC.Document.TLdeserialize(stream, stream.readInt32(false), false);
}
return pack;
}
public void serializeToStream(AbstractSerializedData serializedData) { public void serializeToStream(AbstractSerializedData serializedData) {
serializedData.writeBool(true); serializedData.writeInt32(flags);
serializedData.writeString(packId); serializedData.writeString(packId);
serializedData.writeString(packName); serializedData.writeString(packName);
serializedData.writeString(fileLocation);
serializedData.writeString(preview == null ? "" : preview);
serializedData.writeInt64(fileSize);
serializedData.writeInt32(fileId); serializedData.writeInt32(fileId);
serializedData.writeInt32(previewId); serializedData.writeInt32(previewId);
serializedData.writeInt32(packVersion); serializedData.writeInt32(packVersion);
if (previewDocument != null) { if ((flags & 1) != 0) {
serializedData.writeBool(true);
previewDocument.serializeToStream(serializedData); previewDocument.serializeToStream(serializedData);
serializedData.writeInt64(fileSize);
serializedData.writeString(fileLocation);
} }
if (fileDocument != null) { if ((flags & 2) != 0) {
serializedData.writeBool(true);
fileDocument.serializeToStream(serializedData); fileDocument.serializeToStream(serializedData);
} }
} }