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).
|
|
|
|
*
|
2016-01-11 18:19:48 +01:00
|
|
|
* Copyright Nikolai Kudashov, 2013-2016.
|
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.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
2015-09-24 22:52:02 +02:00
|
|
|
import org.telegram.messenger.NotificationCenter;
|
|
|
|
import org.telegram.messenger.query.StickersQuery;
|
|
|
|
import org.telegram.messenger.support.widget.RecyclerView;
|
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;
|
2015-01-02 23:15:07 +01:00
|
|
|
import org.telegram.ui.Cells.StickerCell;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
2016-05-25 23:49:47 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
2015-01-02 23:15:07 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
public class StickersAdapter extends RecyclerView.Adapter implements NotificationCenter.NotificationCenterDelegate {
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
private ArrayList<TLRPC.Document> stickers;
|
|
|
|
private ArrayList<String> stickersToLoad = new ArrayList<>();
|
|
|
|
private StickersAdapterDelegate delegate;
|
|
|
|
private String lastSticker;
|
|
|
|
private boolean visible;
|
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
public interface StickersAdapterDelegate {
|
|
|
|
void needChangePanelVisibility(boolean show);
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private class Holder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
public Holder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public StickersAdapter(Context context, StickersAdapterDelegate delegate) {
|
|
|
|
mContext = context;
|
|
|
|
this.delegate = delegate;
|
2016-10-11 13:57:01 +02:00
|
|
|
StickersQuery.checkStickers(StickersQuery.TYPE_IMAGE);
|
|
|
|
StickersQuery.checkStickers(StickersQuery.TYPE_MASK);
|
2015-01-02 23:15:07 +01:00
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileDidLoaded);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileDidFailedLoad);
|
|
|
|
}
|
|
|
|
|
2015-06-29 19:12:11 +02:00
|
|
|
public void onDestroy() {
|
2015-01-02 23:15:07 +01:00
|
|
|
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.FileDidLoaded);
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.FileDidFailedLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void didReceivedNotification(int id, final Object... args) {
|
|
|
|
if (id == NotificationCenter.FileDidLoaded || id == NotificationCenter.FileDidFailedLoad) {
|
2015-05-21 23:27:27 +02:00
|
|
|
if (stickers != null && !stickers.isEmpty() && !stickersToLoad.isEmpty() && visible) {
|
|
|
|
String fileName = (String) args[0];
|
|
|
|
stickersToLoad.remove(fileName);
|
|
|
|
if (stickersToLoad.isEmpty()) {
|
|
|
|
delegate.needChangePanelVisibility(stickers != null && !stickers.isEmpty() && stickersToLoad.isEmpty());
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
2015-05-21 23:27:27 +02:00
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkStickerFilesExistAndDownload() {
|
|
|
|
if (stickers == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stickersToLoad.clear();
|
|
|
|
int size = Math.min(10, stickers.size());
|
|
|
|
for (int a = 0; a < size; a++) {
|
|
|
|
TLRPC.Document document = stickers.get(a);
|
2015-05-21 23:27:27 +02:00
|
|
|
File f = FileLoader.getPathToAttach(document.thumb, "webp", true);
|
2015-01-02 23:15:07 +01:00
|
|
|
if (!f.exists()) {
|
2015-05-21 23:27:27 +02:00
|
|
|
stickersToLoad.add(FileLoader.getAttachFileName(document.thumb, "webp"));
|
|
|
|
FileLoader.getInstance().loadFile(document.thumb.location, "webp", 0, true);
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return stickersToLoad.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadStikersForEmoji(CharSequence emoji) {
|
2015-10-29 18:10:07 +01:00
|
|
|
boolean search = emoji != null && emoji.length() > 0 && emoji.length() <= 14;
|
2015-01-02 23:15:07 +01:00
|
|
|
if (search) {
|
2015-10-29 18:10:07 +01:00
|
|
|
int length = emoji.length();
|
|
|
|
for (int a = 0; a < length; a++) {
|
2016-10-11 13:57:01 +02:00
|
|
|
if (a < length - 1 && (emoji.charAt(a) == 0xD83C && emoji.charAt(a + 1) >= 0xDFFB && emoji.charAt(a + 1) <= 0xDFFF || emoji.charAt(a) == 0x200D && (emoji.charAt(a + 1) == 0x2640 || emoji.charAt(a + 1) == 0x2642))) {
|
2015-10-29 18:10:07 +01:00
|
|
|
emoji = TextUtils.concat(emoji.subSequence(0, a), emoji.subSequence(a + 2, emoji.length()));
|
2016-10-11 13:57:01 +02:00
|
|
|
length -= 2;
|
|
|
|
a--;
|
2015-10-29 18:10:07 +01:00
|
|
|
} else if (emoji.charAt(a) == 0xfe0f) {
|
|
|
|
emoji = TextUtils.concat(emoji.subSequence(0, a), emoji.subSequence(a + 1, emoji.length()));
|
|
|
|
length--;
|
2016-10-11 13:57:01 +02:00
|
|
|
a--;
|
2015-10-29 18:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
lastSticker = emoji.toString();
|
2015-05-21 23:27:27 +02:00
|
|
|
HashMap<String, ArrayList<TLRPC.Document>> allStickers = StickersQuery.getAllStickers();
|
2015-01-02 23:15:07 +01:00
|
|
|
if (allStickers != null) {
|
|
|
|
ArrayList<TLRPC.Document> newStickers = allStickers.get(lastSticker);
|
|
|
|
if (stickers != null && newStickers == null) {
|
|
|
|
if (visible) {
|
|
|
|
delegate.needChangePanelVisibility(false);
|
|
|
|
visible = false;
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-25 23:49:47 +02:00
|
|
|
stickers = newStickers != null && !newStickers.isEmpty() ? new ArrayList<>(newStickers) : null;
|
|
|
|
if (stickers != null) {
|
2016-10-11 13:57:01 +02:00
|
|
|
final ArrayList<TLRPC.Document> recentStickers = StickersQuery.getRecentStickersNoCopy(StickersQuery.TYPE_IMAGE);
|
|
|
|
if (!recentStickers.isEmpty()) {
|
|
|
|
Collections.sort(stickers, new Comparator<TLRPC.Document>() {
|
|
|
|
private int getIndex(long id) {
|
|
|
|
for (int a = 0; a < recentStickers.size(); a++) {
|
|
|
|
if (recentStickers.get(a).id == id) {
|
|
|
|
return a;
|
|
|
|
}
|
2016-05-25 23:49:47 +02:00
|
|
|
}
|
2016-10-11 13:57:01 +02:00
|
|
|
return -1;
|
2016-05-25 23:49:47 +02:00
|
|
|
}
|
2016-10-11 13:57:01 +02:00
|
|
|
|
2016-05-25 23:49:47 +02:00
|
|
|
@Override
|
|
|
|
public int compare(TLRPC.Document lhs, TLRPC.Document rhs) {
|
2016-10-11 13:57:01 +02:00
|
|
|
int idx1 = getIndex(lhs.id);
|
|
|
|
int idx2 = getIndex(rhs.id);
|
2016-05-25 23:49:47 +02:00
|
|
|
if (idx1 > idx2) {
|
|
|
|
return -1;
|
|
|
|
} else if (idx1 < idx2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
checkStickerFilesExistAndDownload();
|
|
|
|
delegate.needChangePanelVisibility(stickers != null && !stickers.isEmpty() && stickersToLoad.isEmpty());
|
|
|
|
notifyDataSetChanged();
|
|
|
|
visible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!search) {
|
|
|
|
if (visible && stickers != null) {
|
|
|
|
visible = false;
|
|
|
|
delegate.needChangePanelVisibility(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearStickers() {
|
|
|
|
lastSticker = null;
|
|
|
|
stickers = null;
|
|
|
|
stickersToLoad.clear();
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return stickers != null ? stickers.size() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TLRPC.Document getItem(int i) {
|
|
|
|
return stickers != null && i >= 0 && i < stickers.size() ? stickers.get(i) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int i) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
|
|
|
|
StickerCell view = new StickerCell(mContext);
|
|
|
|
return new Holder(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
|
|
|
|
int side = 0;
|
|
|
|
if (i == 0) {
|
|
|
|
if (stickers.size() == 1) {
|
|
|
|
side = 2;
|
|
|
|
} else {
|
|
|
|
side = -1;
|
|
|
|
}
|
|
|
|
} else if (i == stickers.size() - 1) {
|
|
|
|
side = 1;
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
((StickerCell) viewHolder.itemView).setSticker(stickers.get(i), side);
|
2015-01-02 23:15:07 +01:00
|
|
|
}
|
|
|
|
}
|