NekoX/TMessagesProj/src/main/java/org/telegram/ui/Adapters/StickersAdapter.java

216 lines
7.6 KiB
Java
Raw Normal View History

2015-01-02 23:15:07 +01:00
/*
* This is the source code of Telegram for Android v. 2.0.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2015-01-02 23:15:07 +01:00
*/
package org.telegram.ui.Adapters;
import android.content.Context;
2015-10-29 18:10:07 +01:00
import android.text.TextUtils;
2015-01-02 23:15:07 +01:00
import android.view.ViewGroup;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2019-07-18 15:01:39 +02:00
import org.telegram.messenger.MediaDataController;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.Emoji;
2019-05-14 14:08:05 +02:00
import org.telegram.messenger.ImageLocation;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.NotificationCenter;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.UserConfig;
2015-01-02 23:15:07 +01:00
import org.telegram.messenger.FileLoader;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLRPC;
2019-05-14 14:08:05 +02:00
import org.telegram.ui.Cells.EmojiReplacementCell;
2017-03-31 01:58:05 +02:00
import org.telegram.ui.Components.RecyclerListView;
2015-01-02 23:15:07 +01:00
import java.io.File;
import java.util.ArrayList;
2019-05-14 14:08:05 +02:00
import java.util.Arrays;
2015-01-02 23:15:07 +01:00
2019-05-14 14:08:05 +02:00
import androidx.recyclerview.widget.RecyclerView;
2017-03-31 01:58:05 +02:00
public class StickersAdapter extends RecyclerListView.SelectionAdapter implements NotificationCenter.NotificationCenterDelegate {
2015-01-02 23:15:07 +01:00
2018-07-30 04:07:02 +02:00
private int currentAccount = UserConfig.selectedAccount;
2015-01-02 23:15:07 +01:00
private Context mContext;
2019-07-18 15:01:39 +02:00
private ArrayList<MediaDataController.KeywordResult> keywordResults;
2015-01-02 23:15:07 +01:00
private StickersAdapterDelegate delegate;
2021-08-31 21:06:39 +02:00
2015-01-02 23:15:07 +01:00
private boolean visible;
2021-08-31 21:06:39 +02:00
private String lastSearch;
2019-05-14 14:08:05 +02:00
private String[] lastSearchKeyboardLanguage;
private Runnable searchRunnable;
2015-01-02 23:15:07 +01:00
public interface StickersAdapterDelegate {
void needChangePanelVisibility(boolean show);
2015-01-02 23:15:07 +01:00
}
public StickersAdapter(Context context, StickersAdapterDelegate delegate) {
mContext = context;
this.delegate = delegate;
2019-07-18 15:01:39 +02:00
MediaDataController.getInstance(currentAccount).checkStickers(MediaDataController.TYPE_IMAGE);
MediaDataController.getInstance(currentAccount).checkStickers(MediaDataController.TYPE_MASK);
2019-05-14 14:08:05 +02:00
NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.newEmojiSuggestionsAvailable);
2015-01-02 23:15:07 +01:00
}
2015-06-29 19:12:11 +02:00
public void onDestroy() {
2019-05-14 14:08:05 +02:00
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.newEmojiSuggestionsAvailable);
2015-01-02 23:15:07 +01:00
}
@Override
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, final Object... args) {
2021-08-31 21:06:39 +02:00
if (id == NotificationCenter.newEmojiSuggestionsAvailable) {
if ((keywordResults == null || keywordResults.isEmpty()) && !TextUtils.isEmpty(lastSearch) && getItemCount() == 0) {
2019-05-14 14:08:05 +02:00
searchEmojiByKeyword();
}
2015-01-02 23:15:07 +01:00
}
}
2019-05-14 14:08:05 +02:00
public void hide() {
2021-08-31 21:06:39 +02:00
if (visible && keywordResults != null && !keywordResults.isEmpty()) {
2019-05-14 14:08:05 +02:00
visible = false;
delegate.needChangePanelVisibility(false);
2018-07-30 04:07:02 +02:00
}
2019-05-14 14:08:05 +02:00
}
private void cancelEmojiSearch() {
if (searchRunnable != null) {
AndroidUtilities.cancelRunOnUIThread(searchRunnable);
searchRunnable = null;
}
}
private void searchEmojiByKeyword() {
String[] newLanguage = AndroidUtilities.getCurrentKeyboardLanguage();
if (!Arrays.equals(newLanguage, lastSearchKeyboardLanguage)) {
2019-07-18 15:01:39 +02:00
MediaDataController.getInstance(currentAccount).fetchNewEmojiKeywords(newLanguage);
2019-05-14 14:08:05 +02:00
}
lastSearchKeyboardLanguage = newLanguage;
2021-08-31 21:06:39 +02:00
String query = lastSearch;
2019-05-14 14:08:05 +02:00
cancelEmojiSearch();
2019-07-18 15:01:39 +02:00
searchRunnable = () -> MediaDataController.getInstance(currentAccount).getEmojiSuggestions(lastSearchKeyboardLanguage, query, true, (param, alias) -> {
2021-08-31 21:06:39 +02:00
if (query.equals(lastSearch)) {
2019-05-14 14:08:05 +02:00
if (!param.isEmpty()) {
keywordResults = param;
2015-10-29 18:10:07 +01:00
}
2019-05-14 14:08:05 +02:00
notifyDataSetChanged();
delegate.needChangePanelVisibility(visible = !param.isEmpty());
2015-10-29 18:10:07 +01:00
}
2019-05-14 14:08:05 +02:00
});
if (keywordResults == null || keywordResults.isEmpty()) {
AndroidUtilities.runOnUIThread(searchRunnable, 1000);
} else {
searchRunnable.run();
}
}
2021-08-31 21:06:39 +02:00
public void searchEmojiByKeyword(CharSequence emoji) {
2019-05-14 14:08:05 +02:00
boolean searchEmoji = emoji != null && emoji.length() > 0 && emoji.length() <= 14;
2020-12-23 08:48:30 +01:00
String originalEmoji = "";
if (searchEmoji) {
originalEmoji = emoji.toString();
int length = emoji.length();
for (int a = 0; a < length; a++) {
char ch = emoji.charAt(a);
char nch = a < length - 1 ? emoji.charAt(a + 1) : 0;
if (a < length - 1 && ch == 0xD83C && nch >= 0xDFFB && nch <= 0xDFFF) {
emoji = TextUtils.concat(emoji.subSequence(0, a), emoji.subSequence(a + 2, emoji.length()));
length -= 2;
a--;
} else if (ch == 0xfe0f) {
emoji = TextUtils.concat(emoji.subSequence(0, a), emoji.subSequence(a + 1, emoji.length()));
length--;
a--;
}
2019-05-14 14:08:05 +02:00
}
}
2021-08-31 21:06:39 +02:00
lastSearch = emoji.toString().trim();
boolean isValidEmoji = searchEmoji && (Emoji.isValidEmoji(originalEmoji) || Emoji.isValidEmoji(lastSearch));
2019-08-22 01:53:26 +02:00
if (isValidEmoji) {
TLRPC.Document animatedSticker = MediaDataController.getInstance(currentAccount).getEmojiAnimatedSticker(emoji);
if (animatedSticker != null) {
ArrayList<TLRPC.TL_messages_stickerSet> sets = MediaDataController.getInstance(currentAccount).getStickerSets(MediaDataController.TYPE_EMOJI);
File f = FileLoader.getPathToAttach(animatedSticker, true);
if (!f.exists()) {
FileLoader.getInstance(currentAccount).loadFile(ImageLocation.getForDocument(animatedSticker), sets.get(0), null, 1, 1);
}
}
}
2018-07-30 04:07:02 +02:00
2021-08-31 21:06:39 +02:00
if (visible && (keywordResults == null || keywordResults.isEmpty())) {
visible = false;
delegate.needChangePanelVisibility(false);
2019-05-14 14:08:05 +02:00
notifyDataSetChanged();
2021-08-31 21:06:39 +02:00
}
if (!isValidEmoji) {
searchEmojiByKeyword();
} else {
clearSearch();
2019-05-14 14:08:05 +02:00
delegate.needChangePanelVisibility(false);
2015-01-02 23:15:07 +01:00
}
}
2021-08-31 21:06:39 +02:00
public void clearSearch() {
lastSearch = null;
2019-05-14 14:08:05 +02:00
keywordResults = null;
2015-01-02 23:15:07 +01:00
notifyDataSetChanged();
}
2020-12-23 08:48:30 +01:00
public String getQuery() {
2021-08-31 21:06:39 +02:00
return lastSearch;
2020-12-23 08:48:30 +01:00
}
2019-05-14 14:08:05 +02:00
public boolean isShowingKeywords() {
return keywordResults != null && !keywordResults.isEmpty();
}
2015-01-02 23:15:07 +01:00
@Override
public int getItemCount() {
2019-05-14 14:08:05 +02:00
if (keywordResults != null && !keywordResults.isEmpty()) {
return keywordResults.size();
}
2021-08-31 21:06:39 +02:00
return 0;
2015-01-02 23:15:07 +01:00
}
2019-05-14 14:08:05 +02:00
public Object getItem(int i) {
if (keywordResults != null && !keywordResults.isEmpty()) {
2020-07-26 10:03:38 +02:00
return i >= 0 && i < keywordResults.size() ? keywordResults.get(i).emoji : null;
2019-05-14 14:08:05 +02:00
}
2021-08-31 21:06:39 +02:00
return null;
2019-01-23 18:03:33 +01:00
}
2015-01-02 23:15:07 +01:00
@Override
2017-03-31 01:58:05 +02:00
public boolean isEnabled(RecyclerView.ViewHolder holder) {
2019-05-14 14:08:05 +02:00
return false;
2015-01-02 23:15:07 +01:00
}
@Override
2019-05-14 14:08:05 +02:00
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
2021-08-31 21:06:39 +02:00
return new RecyclerListView.Holder(new EmojiReplacementCell(mContext));
2015-01-02 23:15:07 +01:00
}
@Override
2019-05-14 14:08:05 +02:00
public int getItemViewType(int position) {
return 0;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
2021-08-31 21:06:39 +02:00
int side = 0;
if (position == 0) {
if (keywordResults.size() == 1) {
side = 2;
} else {
side = -1;
2015-01-02 23:15:07 +01:00
}
2021-08-31 21:06:39 +02:00
} else if (position == keywordResults.size() - 1) {
side = 1;
2015-01-02 23:15:07 +01:00
}
2021-08-31 21:06:39 +02:00
EmojiReplacementCell cell = (EmojiReplacementCell) holder.itemView;
cell.setEmoji(keywordResults.get(position).emoji, side);
2015-01-02 23:15:07 +01:00
}
}