You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
4.1 KiB
Java
129 lines
4.1 KiB
Java
|
11 years ago
|
package org.thoughtcrime.securesms.components.emoji;
|
||
|
|
|
||
|
11 years ago
|
import android.annotation.TargetApi;
|
||
|
11 years ago
|
import android.content.Context;
|
||
|
|
import android.graphics.drawable.Drawable;
|
||
|
11 years ago
|
import android.os.Build.VERSION_CODES;
|
||
|
|
import android.util.AttributeSet;
|
||
|
11 years ago
|
import android.view.LayoutInflater;
|
||
|
|
import android.view.View;
|
||
|
|
import android.view.ViewGroup;
|
||
|
|
import android.widget.AbsListView;
|
||
|
|
import android.widget.AdapterView;
|
||
|
|
import android.widget.AdapterView.OnItemClickListener;
|
||
|
|
import android.widget.BaseAdapter;
|
||
|
11 years ago
|
import android.widget.FrameLayout;
|
||
|
11 years ago
|
import android.widget.GridView;
|
||
|
|
import android.widget.ImageView;
|
||
|
|
|
||
|
|
import org.thoughtcrime.securesms.R;
|
||
|
|
|
||
|
11 years ago
|
public class EmojiPageView extends FrameLayout {
|
||
|
|
private static final String TAG = EmojiPageView.class.getSimpleName();
|
||
|
11 years ago
|
|
||
|
|
private EmojiPageModel model;
|
||
|
|
private EmojiSelectionListener listener;
|
||
|
11 years ago
|
private GridView grid;
|
||
|
11 years ago
|
|
||
|
11 years ago
|
public EmojiPageView(Context context) {
|
||
|
|
super(context);
|
||
|
|
init();
|
||
|
|
}
|
||
|
|
|
||
|
|
public EmojiPageView(Context context, AttributeSet attrs) {
|
||
|
|
super(context, attrs);
|
||
|
|
init();
|
||
|
|
}
|
||
|
|
|
||
|
|
public EmojiPageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||
|
|
super(context, attrs, defStyleAttr);
|
||
|
|
init();
|
||
|
|
}
|
||
|
|
|
||
|
|
@TargetApi(VERSION_CODES.LOLLIPOP)
|
||
|
|
public EmojiPageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||
|
|
super(context, attrs, defStyleAttr, defStyleRes);
|
||
|
|
init();
|
||
|
11 years ago
|
}
|
||
|
|
|
||
|
11 years ago
|
public void onSelected() {
|
||
|
|
if (model.isDynamic() && grid != null && grid.getAdapter() != null) {
|
||
|
|
((EmojiGridAdapter)grid.getAdapter()).notifyDataSetChanged();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
11 years ago
|
private void init() {
|
||
|
|
final View view = LayoutInflater.from(getContext()).inflate(R.layout.emoji_grid_layout, this, true);
|
||
|
11 years ago
|
grid = (GridView) view.findViewById(R.id.emoji);
|
||
|
11 years ago
|
grid.setColumnWidth(getResources().getDimensionPixelSize(R.dimen.emoji_drawer_size) + 2 * getResources().getDimensionPixelSize(R.dimen.emoji_drawer_item_padding));
|
||
|
|
grid.setOnItemClickListener(new OnItemClickListener() {
|
||
|
|
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||
|
|
if (listener != null) listener.onEmojiSelected((Integer)view.getTag());
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
public void setModel(EmojiPageModel model) {
|
||
|
|
this.model = model;
|
||
|
11 years ago
|
grid.setAdapter(new EmojiGridAdapter(getContext(), model));
|
||
|
11 years ago
|
}
|
||
|
|
|
||
|
|
public void setEmojiSelectedListener(EmojiSelectionListener listener) {
|
||
|
|
this.listener = listener;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static class EmojiGridAdapter extends BaseAdapter {
|
||
|
|
|
||
|
|
protected final Context context;
|
||
|
|
private final int emojiSize;
|
||
|
|
private final EmojiPageModel model;
|
||
|
|
|
||
|
|
public EmojiGridAdapter(Context context, EmojiPageModel model) {
|
||
|
|
this.context = context;
|
||
|
|
this.emojiSize = (int) context.getResources().getDimension(R.dimen.emoji_drawer_size);
|
||
|
|
this.model = model;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override public int getCount() {
|
||
|
|
return model.getCodePoints().length;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Object getItem(int position) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public long getItemId(int position) {
|
||
|
|
return position;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public View getView(final int position, final View convertView, final ViewGroup parent) {
|
||
|
|
final ImageView view;
|
||
|
|
final int pad = context.getResources().getDimensionPixelSize(R.dimen.emoji_drawer_item_padding);
|
||
|
|
if (convertView != null && convertView instanceof ImageView) {
|
||
|
|
view = (ImageView)convertView;
|
||
|
|
} else {
|
||
|
|
ImageView imageView = new ImageView(context);
|
||
|
|
imageView.setPadding(pad, pad, pad, pad);
|
||
|
|
imageView.setLayoutParams(new AbsListView.LayoutParams(emojiSize + 2 * pad, emojiSize + 2 * pad));
|
||
|
|
view = imageView;
|
||
|
|
}
|
||
|
|
|
||
|
|
final Integer unicodeTag = model.getCodePoints()[position];
|
||
|
|
final EmojiProvider provider = EmojiProvider.getInstance(context);
|
||
|
11 years ago
|
final Drawable drawable = provider.getEmojiDrawable(unicodeTag, EmojiProvider.EMOJI_FULL);
|
||
|
11 years ago
|
|
||
|
|
view.setImageDrawable(drawable);
|
||
|
|
view.setPadding(pad, pad, pad, pad);
|
||
|
|
view.setTag(unicodeTag);
|
||
|
|
return view;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public interface EmojiSelectionListener {
|
||
|
|
void onEmojiSelected(int emojiCode);
|
||
|
|
}
|
||
|
|
}
|