2015-05-06 13:53:55 -07:00
|
|
|
package org.thoughtcrime.securesms.components.emoji;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-05-29 10:54:59 -07:00
|
|
|
import android.support.v4.view.PagerAdapter;
|
2015-05-06 13:53:55 -07:00
|
|
|
import android.support.v4.view.ViewPager;
|
2015-05-29 10:54:59 -07:00
|
|
|
import android.util.AttributeSet;
|
2015-05-06 13:53:55 -07:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.KeyEvent;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
2015-05-18 14:33:11 -07:00
|
|
|
import android.widget.ImageView.ScaleType;
|
2015-05-29 10:54:59 -07:00
|
|
|
import android.widget.LinearLayout;
|
2015-05-06 13:53:55 -07:00
|
|
|
|
|
|
|
import com.astuetz.PagerSlidingTabStrip;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-07-13 15:45:14 -07:00
|
|
|
import org.thoughtcrime.securesms.components.KeyboardAwareLinearLayout;
|
2015-05-06 13:53:55 -07:00
|
|
|
import org.thoughtcrime.securesms.components.RepeatableImageKey;
|
|
|
|
import org.thoughtcrime.securesms.components.RepeatableImageKey.KeyEventListener;
|
2015-05-29 10:54:59 -07:00
|
|
|
import org.thoughtcrime.securesms.components.emoji.EmojiPageView.EmojiSelectionListener;
|
2015-05-06 13:53:55 -07:00
|
|
|
import org.thoughtcrime.securesms.util.ResUtil;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-07-13 15:45:14 -07:00
|
|
|
public class EmojiDrawer extends LinearLayout {
|
2015-05-06 13:53:55 -07:00
|
|
|
private static final KeyEvent DELETE_KEY_EVENT = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL);
|
|
|
|
|
2015-07-02 16:47:03 -07:00
|
|
|
private ViewPager pager;
|
|
|
|
private List<EmojiPageModel> models;
|
|
|
|
private PagerSlidingTabStrip strip;
|
|
|
|
private RecentEmojiPageModel recentModel;
|
|
|
|
private EmojiEventListener listener;
|
2015-05-06 13:53:55 -07:00
|
|
|
|
2015-05-29 10:54:59 -07:00
|
|
|
public EmojiDrawer(Context context) {
|
2015-07-02 16:47:03 -07:00
|
|
|
this(context, null);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-29 10:54:59 -07:00
|
|
|
public EmojiDrawer(Context context, AttributeSet attrs) {
|
2015-07-13 15:45:14 -07:00
|
|
|
super(context, attrs);
|
|
|
|
setOrientation(VERTICAL);
|
2015-05-29 10:54:59 -07:00
|
|
|
final View v = LayoutInflater.from(getContext()).inflate(R.layout.emoji_drawer, this, true);
|
2015-05-06 13:53:55 -07:00
|
|
|
initializeResources(v);
|
2015-05-21 17:29:23 -07:00
|
|
|
initializePageModels();
|
2015-05-06 13:53:55 -07:00
|
|
|
initializeEmojiGrid();
|
|
|
|
}
|
|
|
|
|
2015-07-02 16:47:03 -07:00
|
|
|
public void setEmojiEventListener(EmojiEventListener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
2015-05-06 13:53:55 -07:00
|
|
|
private void initializeResources(View v) {
|
|
|
|
Log.w("EmojiDrawer", "initializeResources()");
|
2015-07-02 16:47:03 -07:00
|
|
|
this.pager = (ViewPager) v.findViewById(R.id.emoji_pager);
|
|
|
|
this.strip = (PagerSlidingTabStrip) v.findViewById(R.id.tabs);
|
2015-05-06 13:53:55 -07:00
|
|
|
|
|
|
|
RepeatableImageKey backspace = (RepeatableImageKey)v.findViewById(R.id.backspace);
|
|
|
|
backspace.setOnKeyEventListener(new KeyEventListener() {
|
|
|
|
@Override public void onKeyEvent() {
|
2015-07-02 16:47:03 -07:00
|
|
|
if (listener != null) listener.onKeyEvent(DELETE_KEY_EVENT);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:45:14 -07:00
|
|
|
public boolean isShowing() {
|
|
|
|
return getVisibility() == VISIBLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void show(KeyboardAwareLinearLayout container) {
|
|
|
|
ViewGroup.LayoutParams params = getLayoutParams();
|
|
|
|
params.height = container.getKeyboardHeight();
|
2015-07-17 12:50:00 -07:00
|
|
|
Log.w("EmojiDrawer", "showing emoji drawer with height " + params.height);
|
2015-07-13 15:45:14 -07:00
|
|
|
setLayoutParams(params);
|
|
|
|
setVisibility(VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void dismiss() {
|
|
|
|
setVisibility(GONE);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeEmojiGrid() {
|
2015-05-29 10:54:59 -07:00
|
|
|
pager.setAdapter(new EmojiPagerAdapter(getContext(),
|
2015-05-20 17:12:31 -07:00
|
|
|
models,
|
2015-05-06 13:53:55 -07:00
|
|
|
new EmojiSelectionListener() {
|
2015-05-21 17:29:23 -07:00
|
|
|
@Override public void onEmojiSelected(String emoji) {
|
2015-07-07 14:25:41 -07:00
|
|
|
Log.w("EmojiDrawer", "onEmojiSelected()");
|
2015-05-21 17:29:23 -07:00
|
|
|
recentModel.onCodePointSelected(emoji);
|
2015-07-02 16:47:03 -07:00
|
|
|
if (listener != null) listener.onEmojiSelected(emoji);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
}));
|
2015-05-20 17:12:31 -07:00
|
|
|
|
2015-05-21 17:29:23 -07:00
|
|
|
if (recentModel.getEmoji().length == 0) {
|
2015-05-20 17:12:31 -07:00
|
|
|
pager.setCurrentItem(1);
|
|
|
|
}
|
2015-05-06 13:53:55 -07:00
|
|
|
strip.setViewPager(pager);
|
|
|
|
}
|
|
|
|
|
2015-05-21 17:29:23 -07:00
|
|
|
private void initializePageModels() {
|
2015-05-20 17:12:31 -07:00
|
|
|
this.models = new LinkedList<>();
|
2015-05-29 10:54:59 -07:00
|
|
|
this.recentModel = new RecentEmojiPageModel(getContext());
|
2015-05-20 17:12:31 -07:00
|
|
|
this.models.add(recentModel);
|
2015-05-21 17:29:23 -07:00
|
|
|
this.models.addAll(EmojiPages.PAGES);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-29 10:54:59 -07:00
|
|
|
public static class EmojiPagerAdapter extends PagerAdapter
|
2015-05-06 13:53:55 -07:00
|
|
|
implements PagerSlidingTabStrip.CustomTabProvider
|
|
|
|
{
|
|
|
|
private Context context;
|
|
|
|
private List<EmojiPageModel> pages;
|
|
|
|
private EmojiSelectionListener listener;
|
|
|
|
|
|
|
|
public EmojiPagerAdapter(@NonNull Context context,
|
|
|
|
@NonNull List<EmojiPageModel> pages,
|
|
|
|
@Nullable EmojiSelectionListener listener)
|
|
|
|
{
|
2015-05-29 10:54:59 -07:00
|
|
|
super();
|
2015-05-06 13:53:55 -07:00
|
|
|
this.context = context;
|
|
|
|
this.pages = pages;
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return pages.size();
|
|
|
|
}
|
|
|
|
|
2015-05-29 10:54:59 -07:00
|
|
|
@Override public Object instantiateItem(ViewGroup container, int position) {
|
|
|
|
EmojiPageView page = new EmojiPageView(context);
|
|
|
|
page.setModel(pages.get(position));
|
|
|
|
page.setEmojiSelectedListener(listener);
|
|
|
|
container.addView(page);
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public void destroyItem(ViewGroup container, int position, Object object) {
|
|
|
|
container.removeView((View)object);
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-20 16:51:09 -07:00
|
|
|
@Override public void setPrimaryItem(ViewGroup container, int position, Object object) {
|
2015-05-29 10:54:59 -07:00
|
|
|
EmojiPageView current = (EmojiPageView) object;
|
2015-05-20 16:51:09 -07:00
|
|
|
current.onSelected();
|
|
|
|
super.setPrimaryItem(container, position, object);
|
|
|
|
}
|
|
|
|
|
2015-05-29 10:54:59 -07:00
|
|
|
@Override public boolean isViewFromObject(View view, Object object) {
|
|
|
|
return view == object;
|
|
|
|
}
|
|
|
|
|
2015-05-06 13:53:55 -07:00
|
|
|
@Override public View getCustomTabView(ViewGroup viewGroup, int i) {
|
|
|
|
ImageView image = new ImageView(context);
|
2015-05-18 14:33:11 -07:00
|
|
|
image.setScaleType(ScaleType.CENTER_INSIDE);
|
2015-06-08 17:54:16 +02:00
|
|
|
image.setImageResource(ResUtil.getDrawableRes(context, pages.get(i).getIconAttr()));
|
2015-05-06 13:53:55 -07:00
|
|
|
return image;
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 16:47:03 -07:00
|
|
|
|
|
|
|
public interface EmojiEventListener extends EmojiSelectionListener {
|
|
|
|
void onKeyEvent(KeyEvent keyEvent);
|
|
|
|
}
|
2015-05-06 13:53:55 -07:00
|
|
|
}
|