support repeating keyevents for emoji backspace

Fixes #2945
Closes #2988

// FREEBIE
This commit is contained in:
Jake McGinty 2015-04-14 15:20:21 -07:00 committed by Moxie Marlinspike
parent ab82ff0b69
commit 72f3f79016
3 changed files with 125 additions and 27 deletions

View File

@ -16,13 +16,14 @@
android:layout_width="0dp"
android:layout_height="45dp" />
<ImageButton android:id="@+id/backspace"
android:src="@drawable/ic_emoji_backspace"
android:background="@color/emoji_tab_underline"
android:paddingLeft="@dimen/emoji_drawer_left_right_padding"
android:paddingRight="@dimen/emoji_drawer_left_right_padding"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<org.thoughtcrime.securesms.components.RepeatableImageKey
android:id="@+id/backspace"
android:src="@drawable/ic_emoji_backspace"
android:background="@color/emoji_tab_underline"
android:paddingLeft="@dimen/emoji_drawer_left_right_padding"
android:paddingRight="@dimen/emoji_drawer_left_right_padding"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>

View File

@ -9,7 +9,6 @@ import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Pair;
import android.view.HapticFeedbackConstants;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
@ -20,7 +19,6 @@ import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
@ -28,19 +26,21 @@ import android.widget.RelativeLayout;
import com.astuetz.PagerSlidingTabStrip;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.components.RepeatableImageKey.KeyEventListener;
import org.thoughtcrime.securesms.util.Emoji;
public class EmojiDrawer extends KeyboardAwareLinearLayout {
private static final KeyEvent DELETE_KEY_EVENT = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL);
private static final int RECENT_TYPE = 0;
private static final int ALL_TYPE = 1;
private FrameLayout[] gridLayouts = new FrameLayout[Emoji.PAGES.length+1];
private EditText composeText;
private Emoji emoji;
private ViewPager pager;
private PagerSlidingTabStrip strip;
private ImageButton backspace;
@SuppressWarnings("unused")
public EmojiDrawer(Context context) {
@ -70,7 +70,7 @@ public class EmojiDrawer extends KeyboardAwareLinearLayout {
}
private void initialize() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.emoji_drawer, this, true);
initializeResources();
@ -81,12 +81,18 @@ public class EmojiDrawer extends KeyboardAwareLinearLayout {
}
private void initializeResources() {
this.pager = (ViewPager ) findViewById(R.id.emoji_pager);
this.strip = (PagerSlidingTabStrip ) findViewById(R.id.tabs);
this.backspace = (ImageButton ) findViewById(R.id.backspace);
this.pager = (ViewPager) findViewById(R.id.emoji_pager);
this.strip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
this.emoji = Emoji.getInstance(getContext());
this.backspace.setOnClickListener(new BackspaceClickListener());
RepeatableImageKey backspace = (RepeatableImageKey)findViewById(R.id.backspace);
backspace.setOnKeyEventListener(new KeyEventListener() {
@Override public void onKeyEvent() {
if (composeText != null && composeText.getText().length() > 0) {
composeText.dispatchKeyEvent(DELETE_KEY_EVENT);
}
}
});
}
public void hide() {
@ -159,19 +165,7 @@ public class EmojiDrawer extends KeyboardAwareLinearLayout {
}
}
private class BackspaceClickListener implements OnClickListener {
private final KeyEvent deleteKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL);
@Override
public void onClick(View v) {
if (composeText.getText().length() > 0) {
composeText.dispatchKeyEvent(deleteKeyEvent);
v.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
}
}
}
private class EmojiGridAdapter extends BaseAdapter {

View File

@ -0,0 +1,103 @@
package org.thoughtcrime.securesms.components;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.AttributeSet;
import android.view.HapticFeedbackConstants;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.EditText;
import android.widget.ImageButton;
public class RepeatableImageKey extends ImageButton {
private KeyEventListener listener;
public RepeatableImageKey(Context context) {
super(context);
init();
}
public RepeatableImageKey(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RepeatableImageKey(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(VERSION_CODES.LOLLIPOP)
public RepeatableImageKey(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
setOnClickListener(new RepeaterClickListener());
setOnTouchListener(new RepeaterTouchListener());
}
public void setOnKeyEventListener(KeyEventListener listener) {
this.listener = listener;
}
private void notifyListener() {
if (this.listener != null) this.listener.onKeyEvent();
}
private class RepeaterClickListener implements OnClickListener {
@Override public void onClick(View v) {
notifyListener();
}
}
private class Repeater implements Runnable {
@TargetApi(VERSION_CODES.HONEYCOMB_MR1)
@Override
public void run() {
notifyListener();
postDelayed(this, VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1
? ViewConfiguration.getKeyRepeatDelay()
: 50);
}
}
private class RepeaterTouchListener implements OnTouchListener {
private Repeater repeater;
public RepeaterTouchListener() {
this.repeater = new Repeater();
}
@TargetApi(VERSION_CODES.HONEYCOMB_MR1)
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
view.postDelayed(repeater, VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1
? ViewConfiguration.getKeyRepeatTimeout()
: ViewConfiguration.getLongPressTimeout());
performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
return false;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
view.removeCallbacks(repeater);
return false;
default:
return false;
}
}
}
public interface KeyEventListener {
void onKeyEvent();
}
}