Example usage for android.view KeyEvent ACTION_UP

List of usage examples for android.view KeyEvent ACTION_UP

Introduction

In this page you can find the example usage for android.view KeyEvent ACTION_UP.

Prototype

int ACTION_UP

To view the source code for android.view KeyEvent ACTION_UP.

Click Source Link

Document

#getAction value: the key has been released.

Usage

From source file:com.b44t.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, boolean needGif, final Context context) {
    super(context);

    showStickers = needStickers;/*from w  w w  .ja v  a 2 s.  c  o  m*/
    showGifs = needGif;

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        views.add(gridView);

        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        gridView.setAdapter(emojiGridAdapter);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        adapters.add(emojiGridAdapter);
    }

    if (showStickers) {
        //StickersQuery.checkStickers();
        stickersGridView = new GridView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                boolean result = StickerPreviewViewer.getInstance().onInterceptTouchEvent(event,
                        stickersGridView, EmojiView.this.getMeasuredHeight());
                return super.onInterceptTouchEvent(event) || result;
            }

            @Override
            public void setVisibility(int visibility) {
                if (gifsGridView != null && gifsGridView.getVisibility() == VISIBLE) {
                    super.setVisibility(GONE);
                    return;
                }
                super.setVisibility(visibility);
            }
        };
        stickersGridView.setSelector(R.drawable.transparent);
        stickersGridView.setColumnWidth(AndroidUtilities.dp(72));
        stickersGridView.setNumColumns(-1);
        stickersGridView.setPadding(0, AndroidUtilities.dp(4), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersGridView);
        stickersGridAdapter = new StickersGridAdapter(context);
        stickersGridView.setAdapter(stickersGridAdapter);
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return StickerPreviewViewer.getInstance().onTouch(event, stickersGridView,
                        EmojiView.this.getMeasuredHeight(), stickersOnItemClickListener);
            }
        });
        stickersOnItemClickListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long i) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                StickerPreviewViewer.getInstance().reset();
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                TLRPC.Document document = cell.getSticker();
                addRecentSticker(document);
                if (listener != null) {
                    listener.onStickerSelected(document);
                }
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        AndroidUtilities.setListViewEdgeEffectColor(stickersGridView, 0xfff5f6f7);

        stickersWrap = new FrameLayout(context);
        stickersWrap.addView(stickersGridView);

        if (needGif) {
            gifsGridView = new RecyclerListView(context);
            gifsGridView.setTag(11);
            gifsGridView.setLayoutManager(flowLayoutManager = new ExtendedGridLayoutManager(context, 100) {

                private Size size = new Size();

                @Override
                protected Size getSizeForItem(int i) {
                    TLRPC.Document document = recentImages.get(i).document;
                    size.width = document.thumb != null && document.thumb.w != 0 ? document.thumb.w : 100;
                    size.height = document.thumb != null && document.thumb.h != 0 ? document.thumb.h : 100;
                    for (int b = 0; b < document.attributes.size(); b++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(b);
                        if (attribute instanceof TLRPC.TL_documentAttributeImageSize
                                || attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            size.width = attribute.w;
                            size.height = attribute.h;
                            break;
                        }
                    }
                    return size;
                }
            });
            flowLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return flowLayoutManager.getSpanSizeForItem(position);
                }
            });
            gifsGridView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(android.graphics.Rect outRect, View view, RecyclerView parent,
                        RecyclerView.State state) {
                    outRect.left = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                    int position = parent.getChildAdapterPosition(view);
                    if (!flowLayoutManager.isFirstRow(position)) {
                        outRect.top = AndroidUtilities.dp(2);
                    }
                    outRect.right = flowLayoutManager.isLastInRow(position) ? 0 : AndroidUtilities.dp(2);
                }
            });
            gifsGridView.setOverScrollMode(RecyclerListView.OVER_SCROLL_NEVER);
            gifsGridView.setAdapter(gifsAdapter = new GifsAdapter(context));
            gifsGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size() || listener == null) {
                        return;
                    }
                    TLRPC.Document document = recentImages.get(position).document;
                    listener.onGifSelected(document);
                }
            });
            gifsGridView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
                @Override
                public boolean onItemClick(View view, int position) {
                    if (position < 0 || position >= recentImages.size()) {
                        return false;
                    }
                    final MediaController.SearchImage searchImage = recentImages.get(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setMessage(LocaleController.getString("DeleteGif", R.string.DeleteGif));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK).toUpperCase(),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    recentImages.remove(searchImage);
                                    /*TLRPC.TL_messages_saveGif req = new TLRPC.TL_messages_saveGif();
                                    req.id = new TLRPC.TL_inputDocument();
                                    req.id.id = searchImage.document.id;
                                    req.id.access_hash = searchImage.document.access_hash;
                                    req.unsave = true;
                                    ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() {
                                    @Override
                                    public void run(TLObject response, TLRPC.TL_error error) {
                                            
                                    }
                                    });*/
                                    //MessagesStorage.getInstance().removeWebRecent(searchImage);
                                    if (gifsAdapter != null) {
                                        gifsAdapter.notifyDataSetChanged();
                                    }
                                    if (recentImages.isEmpty()) {
                                        updateStickerTabs();
                                    }
                                }
                            });
                    builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                    builder.show().setCanceledOnTouchOutside(true);
                    return true;
                }
            });
            gifsGridView.setVisibility(GONE);
            stickersWrap.addView(gifsGridView);
        }

        stickersEmptyView = new TextView(context);
        stickersEmptyView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        stickersEmptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        stickersEmptyView.setTextColor(0xff888888);
        stickersWrap.addView(stickersEmptyView,
                LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER));
        stickersGridView.setEmptyView(stickersEmptyView);

        scrollSlidingTabStrip = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (first) {
                    first = false;
                    lastX = ev.getX();
                }
                float newTranslationX = scrollSlidingTabStrip.getTranslationX();
                if (scrollSlidingTabStrip.getScrollX() == 0 && newTranslationX == 0) {
                    if (!startedScroll && lastX - ev.getX() < 0) {
                        if (pager.beginFakeDrag()) {
                            startedScroll = true;
                            lastTranslateX = scrollSlidingTabStrip.getTranslationX();
                        }
                    } else if (startedScroll && lastX - ev.getX() > 0) {
                        if (pager.isFakeDragging()) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                }
                if (startedScroll) {
                    int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                    try {
                        pager.fakeDragBy(dx);
                        lastTranslateX = newTranslationX;
                    } catch (Exception e) {
                        try {
                            pager.endFakeDrag();
                        } catch (Exception e2) {
                            //don't promt
                        }
                        startedScroll = false;
                        FileLog.e("messenger", e);
                    }
                }
                lastX = ev.getX();
                if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
                    first = true;
                    if (startedScroll) {
                        pager.endFakeDrag();
                        startedScroll = false;
                    }
                }
                return startedScroll || super.onTouchEvent(ev);
            }
        };
        scrollSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
        scrollSlidingTabStrip.setIndicatorColor(0xffe2e5e7);
        scrollSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
        scrollSlidingTabStrip.setVisibility(INVISIBLE);
        addView(scrollSlidingTabStrip,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        scrollSlidingTabStrip.setTranslationX(AndroidUtilities.displaySize.x);
        updateStickerTabs();
        scrollSlidingTabStrip.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (gifsGridView != null) {
                    if (page == gifTabBum + 1) {
                        if (gifsGridView.getVisibility() != VISIBLE) {
                            listener.onGifTab(true);
                            showGifTab();
                        }
                    } else {
                        if (gifsGridView.getVisibility() == VISIBLE) {
                            listener.onGifTab(false);
                            gifsGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getCount() != 0 ? GONE : VISIBLE);
                        }
                    }
                }
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else {
                    if (page == gifTabBum + 1) {
                        return;
                    } else {
                        if (page == recentTabBum + 1) {
                            views.get(6).setSelection(0);
                            return;
                        }
                    }
                }
                int index = page - 1 - stickersTabOffset;
                if (index == stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                views.get(6).setSelection(stickersGridAdapter.getPositionForPack(stickerSets.get(index)));
            }
        });

        stickersGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                checkStickersScroll(firstVisibleItem);
            }
        });
    }

    setBackgroundColor(0xfff5f6f7);

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    pagerSlidingTabStripContainer = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pagerSlidingTabStripContainer.setOrientation(LinearLayout.HORIZONTAL);
    pagerSlidingTabStripContainer.setBackgroundColor(0xfff5f6f7);
    addView(pagerSlidingTabStripContainer, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    PagerSlidingTabStrip pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    pagerSlidingTabStripContainer.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth(), positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    pagerSlidingTabStripContainer.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    recentsWrap = new FrameLayout(context);
    recentsWrap.addView(views.get(0));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    recentsWrap.addView(textView);
    views.get(0).setEmptyView(textView);

    addView(pager, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP, 0, 48, 0, 0));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });

    loadRecents();
}

From source file:com.android.talkback.SpeechController.java

/**
 * Stops the TTS engine when the Ctrl key is tapped without any other keys.
 *///from  ww  w  .  ja va2s  .  c  o  m
@Override
public boolean onKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        mInterruptKeyDown = (keyCode == KeyEvent.KEYCODE_CTRL_LEFT || keyCode == KeyEvent.KEYCODE_CTRL_RIGHT);
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        if (mInterruptKeyDown
                && (keyCode == KeyEvent.KEYCODE_CTRL_LEFT || keyCode == KeyEvent.KEYCODE_CTRL_RIGHT)) {
            mInterruptKeyDown = false;
            mService.interruptAllFeedback();
        }
    }

    return false;
}

From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java

private final int getMappedKeyCode(final int button, final KeyEvent event) {
    switch (button) {
    case MAP_LEFTCLICK:
        DosBoxControl.nativeMouse(0, 0, 0, 0, (event.getAction() == KeyEvent.ACTION_DOWN) ? 0 : 1, BTN_A);
        return MAP_EVENT_CONSUMED;
    case MAP_RIGHTCLICK:
        DosBoxControl.nativeMouse(0, 0, 0, 0, (event.getAction() == KeyEvent.ACTION_DOWN) ? 0 : 1, BTN_B);
        return MAP_EVENT_CONSUMED;
    case MAP_JOYBTN_A:
        DosBoxControl.nativeJoystick(0, 0, (event.getAction() == KeyEvent.ACTION_DOWN) ? 0 : 1, BTN_A);
        return MAP_EVENT_CONSUMED;
    case MAP_JOYBTN_B:
        DosBoxControl.nativeJoystick(0, 0, (event.getAction() == KeyEvent.ACTION_DOWN) ? 0 : 1, BTN_B);
        return MAP_EVENT_CONSUMED;
    case MAP_CYCLEUP:
        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (mParent.mTurboOn) {
                mParent.mTurboOn = false;
                DBMain.nativeSetOption(DBMenuSystem.DOSBOX_OPTION_ID_TURBO_ON, mParent.mTurboOn ? 1 : 0, null,
                        true);//w  w  w .j a v  a  2s.c om
            }
            DBMain.nativeSetOption(DBMenuSystem.DOSBOX_OPTION_ID_CYCLE_ADJUST, 1, null, true);
            if (DosBoxControl.nativeGetAutoAdjust()) {
                Toast.makeText(mParent, "Auto Cycles [" + DosBoxControl.nativeGetCycleCount() + "%]",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mParent, "DosBox Cycles: " + DosBoxControl.nativeGetCycleCount(),
                        Toast.LENGTH_SHORT).show();
            }
        }
        return MAP_EVENT_CONSUMED;
    case MAP_CYCLEDOWN:
        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (mParent.mTurboOn) {
                mParent.mTurboOn = false;
                DBMain.nativeSetOption(DBMenuSystem.DOSBOX_OPTION_ID_TURBO_ON, mParent.mTurboOn ? 1 : 0, null,
                        true);
            }
            DBMain.nativeSetOption(DBMenuSystem.DOSBOX_OPTION_ID_CYCLE_ADJUST, 0, null, true);
            if (DosBoxControl.nativeGetAutoAdjust()) {
                Toast.makeText(mParent, "Auto Cycles [" + DosBoxControl.nativeGetCycleCount() + "%]",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mParent, "DosBox Cycles: " + DosBoxControl.nativeGetCycleCount(),
                        Toast.LENGTH_SHORT).show();
            }
        }
        return MAP_EVENT_CONSUMED;
    case MAP_SHOWKEYBOARD:
        if (event.getAction() == KeyEvent.ACTION_UP) {
            DBMenuSystem.doShowKeyboard(mParent);
        }
        return MAP_EVENT_CONSUMED;
    case MAP_SPECIALKEYS:
        if (event.getAction() == KeyEvent.ACTION_UP) {
            mContextMenu = DBMenuSystem.CONTEXT_MENU_SPECIAL_KEYS;
            mParent.openContextMenu(this);
        }
        return MAP_EVENT_CONSUMED;
    case MAP_ADJUSTCYCLES:
        if (event.getAction() == KeyEvent.ACTION_UP) {
            mContextMenu = DBMenuSystem.CONTEXT_MENU_CYCLES;
            mParent.openContextMenu(this);
        }
        return MAP_EVENT_CONSUMED;
    case MAP_ADJUSTFRAMES:
        if (event.getAction() == KeyEvent.ACTION_UP) {
            mContextMenu = DBMenuSystem.CONTEXT_MENU_FRAMESKIP;
            mParent.openContextMenu(this);
        }
        return MAP_EVENT_CONSUMED;
    case MAP_UNLOCK_SPEED:
        if (mParent.mTurboOn) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                DBMain.nativeSetOption(DBMenuSystem.DOSBOX_OPTION_ID_TURBO_ON, 0, null, true); // turn off
                mParent.mTurboOn = false;
            }
        } else {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                DBMain.nativeSetOption(DBMenuSystem.DOSBOX_OPTION_ID_TURBO_ON, 1, null, true); // turn on
                mParent.mTurboOn = true;
            }
        }
        return MAP_EVENT_CONSUMED;
    default:
        return button;
    }
}

From source file:com.yangtsaosoftware.pebblemessenger.services.PebbleCenter.java

private void answerCall(boolean isSpeakon) {

    Context context = getApplicationContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    // ??//w ww  . ja v a 2 s  . c  o m
    if (!(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn())) {
        // 4.1??? 4.1??Permission Denial: not allowed to
        // send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
        // uid=10017
        // ??????android.permission.CALL_PRIVLEGED?????4.1??????????NULL??

        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));
        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

        /*TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method mthAnswerCall;
        try {
        mthAnswerCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
        mthAnswerCall.setAccessible(true);
        ITelephony iTel = (ITelephony) mthAnswerCall.invoke(telMag, (Object[]) null);
        iTel.answerRingingCall();
                
        } catch (Exception e) {
        e.printStackTrace();
        }*/
        if (isSpeakon) {
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                Constants.log("speakerset", "Problem while sleeping");
            }
            Constants.log("speakerset", "AudioManager answer mode:" + audioManager.getMode() + " speaker mod:"
                    + String.valueOf(audioManager.isSpeakerphoneOn()));
            while (audioManager.getMode() != AudioManager.MODE_IN_CALL) {
                try {
                    Thread.sleep(300);

                } catch (InterruptedException e) {
                    Constants.log("speakerset", "Problem while sleeping");
                }
            }
            // audioManager.setMicrophoneMute(true);
            audioManager.setSpeakerphoneOn(true);
            // audioManager.setMode(AudioManager.MODE_IN_CALL);
            Constants.log("speakerset", String.format("AudioManager set mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        }
    } else {
        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

    }
}

From source file:org.appcelerator.titanium.view.TiUIView.java

/**
 * Registers a callback to be invoked when a hardware key is pressed in this view.
 *
 * @param v The view to have the key listener to attach to.
 *//*ww w.ja  va  2s  .co  m*/
protected void registerForKeyPressEvents(final View v) {
    if (v == null) {
        return;
    }

    v.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                KrollDict data = new KrollDict();
                data.put(TiC.EVENT_PROPERTY_KEYCODE, keyCode);
                proxy.fireEvent(TiC.EVENT_KEY_PRESSED, data);

                switch (keyCode) {
                case KeyEvent.KEYCODE_ENTER:
                case KeyEvent.KEYCODE_DPAD_CENTER:
                    if (proxy.hasListeners(TiC.EVENT_CLICK)) {
                        proxy.fireEvent(TiC.EVENT_CLICK, null);
                        return true;
                    }
                }
            }
            return false;
        }
    });
}

From source file:com.iiordanov.runsoft.bVNC.RemoteCanvasActivity.java

@Override
public boolean onKey(View v, int keyCode, KeyEvent evt) {

    boolean consumed = false;
    if (keyCode == KeyEvent.KEYCODE_BACK && chat_layout.getVisibility() == View.VISIBLE) {

        chat_layout.setVisibility(View.GONE);
        return false;
    }// w  w  w .j a  va2s .  c  o  m
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (evt.getAction() == KeyEvent.ACTION_DOWN)
            return super.onKeyDown(keyCode, evt);
        else
            return super.onKeyUp(keyCode, evt);
    }

    try {
        if (evt.getAction() == KeyEvent.ACTION_DOWN || evt.getAction() == KeyEvent.ACTION_MULTIPLE) {
            consumed = inputHandler.onKeyDown(keyCode, evt);
        } else if (evt.getAction() == KeyEvent.ACTION_UP) {
            consumed = inputHandler.onKeyUp(keyCode, evt);
        }

        resetOnScreenKeys(keyCode);
    } catch (NullPointerException e) {
    }

    return consumed;
}

From source file:net.bluehack.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, boolean needGif, final Context context) {
    super(context);

    showGifs = needGif;//  ww  w  .  jav  a  2 s .  com

    dotDrawable = context.getResources().getDrawable(R.drawable.bluecircle);

    if (Build.VERSION.SDK_INT >= 21) {
        outlineProvider = new ViewOutlineProvider() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(view.getPaddingLeft(), view.getPaddingTop(),
                        view.getMeasuredWidth() - view.getPaddingRight(),
                        view.getMeasuredHeight() - view.getPaddingBottom(), AndroidUtilities.dp(6));
            }
        };
    }

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        gridView.setAdapter(emojiGridAdapter);
        adapters.add(emojiGridAdapter);
        emojiGrids.add(gridView);
        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.addView(gridView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT,
                LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT, 0, 48, 0, 0));

        views.add(frameLayout);
    }

    if (needStickers) {
        stickersWrap = new FrameLayout(context);

        StickersQuery.checkStickers(StickersQuery.TYPE_IMAGE);
        StickersQuery.checkFeaturedStickers();
        stickersGridView = new RecyclerListView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                boolean result = StickerPreviewViewer.getInstance().onInterceptTouchEvent(event,
                        stickersGridView, EmojiView.this.getMeasuredHeight());
                return super.onInterceptTouchEvent(event) || result;
            }

            @Override
            public void setVisibility(int visibility) {
                if (gifsGridView != null && gifsGridView.getVisibility() == VISIBLE
                        || trendingGridView != null && trendingGridView.getVisibility() == VISIBLE) {
                    super.setVisibility(GONE);
                    return;
                }
                super.setVisibility(visibility);
            }
        };

        stickersGridView.setLayoutManager(stickersLayoutManager = new GridLayoutManager(context, 5));
        stickersLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == stickersGridAdapter.totalItems) {
                    return stickersGridAdapter.stickersPerRow;
                }
                return 1;
            }
        });
        stickersGridView.setPadding(0, AndroidUtilities.dp(4 + 48), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersWrap);
        stickersGridView.setAdapter(stickersGridAdapter = new StickersGridAdapter(context));
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return StickerPreviewViewer.getInstance().onTouch(event, stickersGridView,
                        EmojiView.this.getMeasuredHeight(), stickersOnItemClickListener);
            }
        });
        stickersOnItemClickListener = new RecyclerListView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                StickerPreviewViewer.getInstance().reset();
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                listener.onStickerSelected(cell.getSticker());
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        stickersGridView.setGlowColor(0xfff5f6f7);
        stickersWrap.addView(stickersGridView);

        trendingGridView = new RecyclerListView(context);
        trendingGridView.setItemAnimator(null);
        trendingGridView.setLayoutAnimation(null);
        trendingGridView.setLayoutManager(trendingLayoutManager = new GridLayoutManager(context, 5) {
            @Override
            public boolean supportsPredictiveItemAnimations() {
                return false;
            }
        });
        trendingLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (trendingGridAdapter.cache.get(position) instanceof Integer
                        || position == trendingGridAdapter.totalItems) {
                    return trendingGridAdapter.stickersPerRow;
                }
                return 1;
            }
        });
        trendingGridView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                checkStickersTabY(recyclerView, dy);
            }
        });
        trendingGridView.setClipToPadding(false);
        trendingGridView.setPadding(0, AndroidUtilities.dp(48), 0, 0);
        trendingGridView.setAdapter(trendingGridAdapter = new TrendingGridAdapter(context));
        trendingGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                TLRPC.StickerSetCovered pack = trendingGridAdapter.positionsToSets.get(position);
                if (pack != null) {
                    listener.onShowStickerSet(pack);
                }
            }
        });
        trendingGridAdapter.notifyDataSetChanged();
        trendingGridView.setGlowColor(0xfff5f6f7);
        trendingGridView.setVisibility(GONE);
        stickersWrap.addView(trendingGridView);

        if (needGif) {
            gifsGridView = new RecyclerListView(context);
            gifsGridView.setClipToPadding(false);
            gifsGridView.setPadding(0, AndroidUtilities.dp(48), 0, 0);
            gifsGridView.setLayoutManager(flowLayoutManager = new ExtendedGridLayoutManager(context, 100) {

                private Size size = new Size();

                @Override
                protected Size getSizeForItem(int i) {
                    TLRPC.Document document = recentGifs.get(i);
                    size.width = document.thumb != null && document.thumb.w != 0 ? document.thumb.w : 100;
                    size.height = document.thumb != null && document.thumb.h != 0 ? document.thumb.h : 100;
                    for (int b = 0; b < document.attributes.size(); b++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(b);
                        if (attribute instanceof TLRPC.TL_documentAttributeImageSize
                                || attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            size.width = attribute.w;
                            size.height = attribute.h;
                            break;
                        }
                    }
                    return size;
                }
            });
            flowLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return flowLayoutManager.getSpanSizeForItem(position);
                }
            });
            gifsGridView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(android.graphics.Rect outRect, View view, RecyclerView parent,
                        RecyclerView.State state) {
                    outRect.left = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                    int position = parent.getChildAdapterPosition(view);
                    if (!flowLayoutManager.isFirstRow(position)) {
                        outRect.top = AndroidUtilities.dp(2);
                    }
                    outRect.right = flowLayoutManager.isLastInRow(position) ? 0 : AndroidUtilities.dp(2);
                }
            });
            gifsGridView.setOverScrollMode(RecyclerListView.OVER_SCROLL_NEVER);
            gifsGridView.setAdapter(gifsAdapter = new GifsAdapter(context));
            gifsGridView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    checkStickersTabY(recyclerView, dy);
                }
            });
            gifsGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (position < 0 || position >= recentGifs.size() || listener == null) {
                        return;
                    }
                    listener.onGifSelected(recentGifs.get(position));
                }
            });
            gifsGridView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
                @Override
                public boolean onItemClick(View view, int position) {
                    if (position < 0 || position >= recentGifs.size()) {
                        return false;
                    }
                    final TLRPC.Document searchImage = recentGifs.get(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
                    builder.setMessage(LocaleController.getString("DeleteGif", R.string.DeleteGif));
                    builder.setPositiveButton(LocaleController.getString("OK", R.string.OK).toUpperCase(),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    StickersQuery.removeRecentGif(searchImage);
                                    recentGifs = StickersQuery.getRecentGifs();
                                    if (gifsAdapter != null) {
                                        gifsAdapter.notifyDataSetChanged();
                                    }
                                    if (recentGifs.isEmpty()) {
                                        updateStickerTabs();
                                    }
                                }
                            });
                    builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
                    builder.show().setCanceledOnTouchOutside(true);
                    return true;
                }
            });
            gifsGridView.setVisibility(GONE);
            stickersWrap.addView(gifsGridView);
        }

        stickersEmptyView = new TextView(context);
        stickersEmptyView.setText(LocaleController.getString("NoStickers", R.string.NoStickers));
        stickersEmptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        stickersEmptyView.setTextColor(0xff888888);
        stickersWrap.addView(stickersEmptyView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT,
                LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, 48, 0, 0));
        stickersGridView.setEmptyView(stickersEmptyView);

        stickersTab = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (first) {
                    first = false;
                    lastX = ev.getX();
                }
                float newTranslationX = stickersTab.getTranslationX();
                if (stickersTab.getScrollX() == 0 && newTranslationX == 0) {
                    if (!startedScroll && lastX - ev.getX() < 0) {
                        if (pager.beginFakeDrag()) {
                            startedScroll = true;
                            lastTranslateX = stickersTab.getTranslationX();
                        }
                    } else if (startedScroll && lastX - ev.getX() > 0) {
                        if (pager.isFakeDragging()) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                }
                if (startedScroll) {
                    int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                    try {
                        pager.fakeDragBy(dx);
                        lastTranslateX = newTranslationX;
                    } catch (Exception e) {
                        try {
                            pager.endFakeDrag();
                        } catch (Exception e2) {
                            //don't promt
                        }
                        startedScroll = false;
                        FileLog.e("tmessages", e);
                    }
                }
                lastX = ev.getX();
                if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
                    first = true;
                    if (startedScroll) {
                        pager.endFakeDrag();
                        startedScroll = false;
                    }
                }
                return startedScroll || super.onTouchEvent(ev);
            }
        };
        stickersTab.setUnderlineHeight(AndroidUtilities.dp(1));
        stickersTab.setIndicatorColor(0xffe2e5e7);
        stickersTab.setUnderlineColor(0xffe2e5e7);
        stickersTab.setBackgroundColor(0xfff5f6f7);
        stickersTab.setVisibility(INVISIBLE);
        addView(stickersTab,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        stickersTab.setTranslationX(AndroidUtilities.displaySize.x);
        updateStickerTabs();
        stickersTab.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (gifsGridView != null) {
                    if (page == gifTabNum + 1) {
                        if (gifsGridView.getVisibility() != VISIBLE) {
                            listener.onGifTab(true);
                            showGifTab();
                        }
                    } else if (page == trendingTabNum + 1) {
                        if (trendingGridView.getVisibility() != VISIBLE) {
                            showTrendingTab();
                        }
                    } else {
                        if (gifsGridView.getVisibility() == VISIBLE) {
                            listener.onGifTab(false);
                            gifsGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getItemCount() != 0 ? GONE : VISIBLE);
                            saveNewPage();
                        } else if (trendingGridView.getVisibility() == VISIBLE) {
                            trendingGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getItemCount() != 0 ? GONE : VISIBLE);
                            saveNewPage();
                        }
                    }
                }
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else {
                    if (page == gifTabNum + 1 || page == trendingTabNum + 1) {
                        return;
                    } else if (page == recentTabBum + 1) {
                        stickersLayoutManager.scrollToPositionWithOffset(0, 0);
                        checkStickersTabY(null, 0);
                        stickersTab.onPageScrolled(recentTabBum + 1,
                                (recentTabBum > 0 ? recentTabBum : stickersTabOffset) + 1);
                        return;
                    }
                }
                int index = page - 1 - stickersTabOffset;
                if (index >= stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                stickersLayoutManager.scrollToPositionWithOffset(
                        stickersGridAdapter.getPositionForPack(stickerSets.get(index)), 0);
                checkStickersTabY(null, 0);
                checkScroll();
            }
        });

        stickersGridView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                checkScroll();
                checkStickersTabY(recyclerView, dy);
            }
        });
    }

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    emojiTab = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    emojiTab.setOrientation(LinearLayout.HORIZONTAL);
    addView(emojiTab, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(2));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    emojiTab.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
                    positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {
            saveNewPage();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    emojiTab.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    textView.setClickable(false);
    textView.setFocusable(false);
    ((FrameLayout) views.get(0)).addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT,
            LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, 48, 0, 0));
    emojiGrids.get(0).setEmptyView(textView);

    addView(pager, 0, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });
    currentPage = getContext().getSharedPreferences("emoji", Activity.MODE_PRIVATE).getInt("selected_page", 0);
    loadRecents();
}

From source file:kr.wdream.ui.Components.EmojiView.java

public EmojiView(boolean needStickers, boolean needGif, final Context context) {
    super(context);

    showGifs = needGif;//  w  ww.  j ava2s  .  c  o m

    dotDrawable = context.getResources().getDrawable(kr.wdream.storyshop.R.drawable.bluecircle);

    if (Build.VERSION.SDK_INT >= 21) {
        outlineProvider = new ViewOutlineProvider() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(view.getPaddingLeft(), view.getPaddingTop(),
                        view.getMeasuredWidth() - view.getPaddingRight(),
                        view.getMeasuredHeight() - view.getPaddingBottom(), AndroidUtilities.dp(6));
            }
        };
    }

    for (int i = 0; i < EmojiData.dataColored.length + 1; i++) {
        GridView gridView = new GridView(context);
        if (AndroidUtilities.isTablet()) {
            gridView.setColumnWidth(AndroidUtilities.dp(60));
        } else {
            gridView.setColumnWidth(AndroidUtilities.dp(45));
        }
        gridView.setNumColumns(-1);
        EmojiGridAdapter emojiGridAdapter = new EmojiGridAdapter(i - 1);
        AndroidUtilities.setListViewEdgeEffectColor(gridView, 0xfff5f6f7);
        gridView.setAdapter(emojiGridAdapter);
        adapters.add(emojiGridAdapter);
        emojiGrids.add(gridView);
        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.addView(gridView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT,
                LayoutHelper.MATCH_PARENT, Gravity.TOP | Gravity.LEFT, 0, 48, 0, 0));

        views.add(frameLayout);
    }

    if (needStickers) {
        stickersWrap = new FrameLayout(context);

        StickersQuery.checkStickers(StickersQuery.TYPE_IMAGE);
        StickersQuery.checkFeaturedStickers();
        stickersGridView = new RecyclerListView(context) {
            @Override
            public boolean onInterceptTouchEvent(MotionEvent event) {
                boolean result = StickerPreviewViewer.getInstance().onInterceptTouchEvent(event,
                        stickersGridView, EmojiView.this.getMeasuredHeight());
                return super.onInterceptTouchEvent(event) || result;
            }

            @Override
            public void setVisibility(int visibility) {
                if (gifsGridView != null && gifsGridView.getVisibility() == VISIBLE
                        || trendingGridView != null && trendingGridView.getVisibility() == VISIBLE) {
                    super.setVisibility(GONE);
                    return;
                }
                super.setVisibility(visibility);
            }
        };

        stickersGridView.setLayoutManager(stickersLayoutManager = new GridLayoutManager(context, 5));
        stickersLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == stickersGridAdapter.totalItems) {
                    return stickersGridAdapter.stickersPerRow;
                }
                return 1;
            }
        });
        stickersGridView.setPadding(0, AndroidUtilities.dp(4 + 48), 0, 0);
        stickersGridView.setClipToPadding(false);
        views.add(stickersWrap);
        stickersGridView.setAdapter(stickersGridAdapter = new StickersGridAdapter(context));
        stickersGridView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return StickerPreviewViewer.getInstance().onTouch(event, stickersGridView,
                        EmojiView.this.getMeasuredHeight(), stickersOnItemClickListener);
            }
        });
        stickersOnItemClickListener = new RecyclerListView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (!(view instanceof StickerEmojiCell)) {
                    return;
                }
                StickerPreviewViewer.getInstance().reset();
                StickerEmojiCell cell = (StickerEmojiCell) view;
                if (cell.isDisabled()) {
                    return;
                }
                cell.disable();
                listener.onStickerSelected(cell.getSticker());
            }
        };
        stickersGridView.setOnItemClickListener(stickersOnItemClickListener);
        stickersGridView.setGlowColor(0xfff5f6f7);
        stickersWrap.addView(stickersGridView);

        trendingGridView = new RecyclerListView(context);
        trendingGridView.setItemAnimator(null);
        trendingGridView.setLayoutAnimation(null);
        trendingGridView.setLayoutManager(trendingLayoutManager = new GridLayoutManager(context, 5) {
            @Override
            public boolean supportsPredictiveItemAnimations() {
                return false;
            }
        });
        trendingLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (trendingGridAdapter.cache.get(position) instanceof Integer
                        || position == trendingGridAdapter.totalItems) {
                    return trendingGridAdapter.stickersPerRow;
                }
                return 1;
            }
        });
        trendingGridView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                checkStickersTabY(recyclerView, dy);
            }
        });
        trendingGridView.setClipToPadding(false);
        trendingGridView.setPadding(0, AndroidUtilities.dp(48), 0, 0);
        trendingGridView.setAdapter(trendingGridAdapter = new TrendingGridAdapter(context));
        trendingGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                TLRPC.StickerSetCovered pack = trendingGridAdapter.positionsToSets.get(position);
                if (pack != null) {
                    listener.onShowStickerSet(pack);
                }
            }
        });
        trendingGridAdapter.notifyDataSetChanged();
        trendingGridView.setGlowColor(0xfff5f6f7);
        trendingGridView.setVisibility(GONE);
        stickersWrap.addView(trendingGridView);

        if (needGif) {
            gifsGridView = new RecyclerListView(context);
            gifsGridView.setClipToPadding(false);
            gifsGridView.setPadding(0, AndroidUtilities.dp(48), 0, 0);
            gifsGridView.setLayoutManager(flowLayoutManager = new ExtendedGridLayoutManager(context, 100) {

                private Size size = new Size();

                @Override
                protected Size getSizeForItem(int i) {
                    TLRPC.Document document = recentGifs.get(i);
                    size.width = document.thumb != null && document.thumb.w != 0 ? document.thumb.w : 100;
                    size.height = document.thumb != null && document.thumb.h != 0 ? document.thumb.h : 100;
                    for (int b = 0; b < document.attributes.size(); b++) {
                        TLRPC.DocumentAttribute attribute = document.attributes.get(b);
                        if (attribute instanceof TLRPC.TL_documentAttributeImageSize
                                || attribute instanceof TLRPC.TL_documentAttributeVideo) {
                            size.width = attribute.w;
                            size.height = attribute.h;
                            break;
                        }
                    }
                    return size;
                }
            });
            flowLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return flowLayoutManager.getSpanSizeForItem(position);
                }
            });
            gifsGridView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(android.graphics.Rect outRect, View view, RecyclerView parent,
                        RecyclerView.State state) {
                    outRect.left = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                    int position = parent.getChildAdapterPosition(view);
                    if (!flowLayoutManager.isFirstRow(position)) {
                        outRect.top = AndroidUtilities.dp(2);
                    }
                    outRect.right = flowLayoutManager.isLastInRow(position) ? 0 : AndroidUtilities.dp(2);
                }
            });
            gifsGridView.setOverScrollMode(RecyclerListView.OVER_SCROLL_NEVER);
            gifsGridView.setAdapter(gifsAdapter = new GifsAdapter(context));
            gifsGridView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    checkStickersTabY(recyclerView, dy);
                }
            });
            gifsGridView.setOnItemClickListener(new RecyclerListView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    if (position < 0 || position >= recentGifs.size() || listener == null) {
                        return;
                    }
                    listener.onGifSelected(recentGifs.get(position));
                }
            });
            gifsGridView.setOnItemLongClickListener(new RecyclerListView.OnItemLongClickListener() {
                @Override
                public boolean onItemClick(View view, int position) {
                    if (position < 0 || position >= recentGifs.size()) {
                        return false;
                    }
                    final TLRPC.Document searchImage = recentGifs.get(position);
                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                    builder.setTitle(
                            LocaleController.getString("AppName", kr.wdream.storyshop.R.string.AppName));
                    builder.setMessage(
                            LocaleController.getString("DeleteGif", kr.wdream.storyshop.R.string.DeleteGif));
                    builder.setPositiveButton(
                            LocaleController.getString("OK", kr.wdream.storyshop.R.string.OK).toUpperCase(),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    StickersQuery.removeRecentGif(searchImage);
                                    recentGifs = StickersQuery.getRecentGifs();
                                    if (gifsAdapter != null) {
                                        gifsAdapter.notifyDataSetChanged();
                                    }
                                    if (recentGifs.isEmpty()) {
                                        updateStickerTabs();
                                    }
                                }
                            });
                    builder.setNegativeButton(
                            LocaleController.getString("Cancel", kr.wdream.storyshop.R.string.Cancel), null);
                    builder.show().setCanceledOnTouchOutside(true);
                    return true;
                }
            });
            gifsGridView.setVisibility(GONE);
            stickersWrap.addView(gifsGridView);
        }

        stickersEmptyView = new TextView(context);
        stickersEmptyView
                .setText(LocaleController.getString("NoStickers", kr.wdream.storyshop.R.string.NoStickers));
        stickersEmptyView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        stickersEmptyView.setTextColor(0xff888888);
        stickersWrap.addView(stickersEmptyView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT,
                LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, 48, 0, 0));
        stickersGridView.setEmptyView(stickersEmptyView);

        stickersTab = new ScrollSlidingTabStrip(context) {

            boolean startedScroll;
            float lastX;
            float lastTranslateX;
            boolean first = true;

            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                if (getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                return super.onInterceptTouchEvent(ev);
            }

            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (first) {
                    first = false;
                    lastX = ev.getX();
                }
                float newTranslationX = stickersTab.getTranslationX();
                if (stickersTab.getScrollX() == 0 && newTranslationX == 0) {
                    if (!startedScroll && lastX - ev.getX() < 0) {
                        if (pager.beginFakeDrag()) {
                            startedScroll = true;
                            lastTranslateX = stickersTab.getTranslationX();
                        }
                    } else if (startedScroll && lastX - ev.getX() > 0) {
                        if (pager.isFakeDragging()) {
                            pager.endFakeDrag();
                            startedScroll = false;
                        }
                    }
                }
                if (startedScroll) {
                    int dx = (int) (ev.getX() - lastX + newTranslationX - lastTranslateX);
                    try {
                        pager.fakeDragBy(dx);
                        lastTranslateX = newTranslationX;
                    } catch (Exception e) {
                        try {
                            pager.endFakeDrag();
                        } catch (Exception e2) {
                            //don't promt
                        }
                        startedScroll = false;
                        FileLog.e("tmessages", e);
                    }
                }
                lastX = ev.getX();
                if (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP) {
                    first = true;
                    if (startedScroll) {
                        pager.endFakeDrag();
                        startedScroll = false;
                    }
                }
                return startedScroll || super.onTouchEvent(ev);
            }
        };
        stickersTab.setUnderlineHeight(AndroidUtilities.dp(1));
        stickersTab.setIndicatorColor(0xffe2e5e7);
        stickersTab.setUnderlineColor(0xffe2e5e7);
        stickersTab.setBackgroundColor(0xfff5f6f7);
        stickersTab.setVisibility(INVISIBLE);
        addView(stickersTab,
                LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, 48, Gravity.LEFT | Gravity.TOP));
        stickersTab.setTranslationX(AndroidUtilities.displaySize.x);
        updateStickerTabs();
        stickersTab.setDelegate(new ScrollSlidingTabStrip.ScrollSlidingTabStripDelegate() {
            @Override
            public void onPageSelected(int page) {
                if (gifsGridView != null) {
                    if (page == gifTabNum + 1) {
                        if (gifsGridView.getVisibility() != VISIBLE) {
                            listener.onGifTab(true);
                            showGifTab();
                        }
                    } else if (page == trendingTabNum + 1) {
                        if (trendingGridView.getVisibility() != VISIBLE) {
                            showTrendingTab();
                        }
                    } else {
                        if (gifsGridView.getVisibility() == VISIBLE) {
                            listener.onGifTab(false);
                            gifsGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getItemCount() != 0 ? GONE : VISIBLE);
                            saveNewPage();
                        } else if (trendingGridView.getVisibility() == VISIBLE) {
                            trendingGridView.setVisibility(GONE);
                            stickersGridView.setVisibility(VISIBLE);
                            stickersEmptyView
                                    .setVisibility(stickersGridAdapter.getItemCount() != 0 ? GONE : VISIBLE);
                            saveNewPage();
                        }
                    }
                }
                if (page == 0) {
                    pager.setCurrentItem(0);
                    return;
                } else {
                    if (page == gifTabNum + 1 || page == trendingTabNum + 1) {
                        return;
                    } else if (page == recentTabBum + 1) {
                        stickersLayoutManager.scrollToPositionWithOffset(0, 0);
                        checkStickersTabY(null, 0);
                        stickersTab.onPageScrolled(recentTabBum + 1,
                                (recentTabBum > 0 ? recentTabBum : stickersTabOffset) + 1);
                        return;
                    }
                }
                int index = page - 1 - stickersTabOffset;
                if (index >= stickerSets.size()) {
                    if (listener != null) {
                        listener.onStickersSettingsClick();
                    }
                    return;
                }
                if (index >= stickerSets.size()) {
                    index = stickerSets.size() - 1;
                }
                stickersLayoutManager.scrollToPositionWithOffset(
                        stickersGridAdapter.getPositionForPack(stickerSets.get(index)), 0);
                checkStickersTabY(null, 0);
                checkScroll();
            }
        });

        stickersGridView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                checkScroll();
                checkStickersTabY(recyclerView, dy);
            }
        });
    }

    pager = new ViewPager(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    pager.setAdapter(new EmojiPagesAdapter());

    emojiTab = new LinearLayout(context) {
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }
    };
    emojiTab.setOrientation(LinearLayout.HORIZONTAL);
    addView(emojiTab, LayoutHelper.createFrame(LayoutParams.MATCH_PARENT, 48));

    pagerSlidingTabStrip = new PagerSlidingTabStrip(context);
    pagerSlidingTabStrip.setViewPager(pager);
    pagerSlidingTabStrip.setShouldExpand(true);
    pagerSlidingTabStrip.setIndicatorHeight(AndroidUtilities.dp(0));
    pagerSlidingTabStrip.setUnderlineHeight(AndroidUtilities.dp(1));
    //?   ?
    pagerSlidingTabStrip.setIndicatorColor(0xff2b96e2);
    pagerSlidingTabStrip.setUnderlineColor(0xffe2e5e7);
    emojiTab.addView(pagerSlidingTabStrip, LayoutHelper.createLinear(0, 48, 1.0f));
    pagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            EmojiView.this.onPageScrolled(position, getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
                    positionOffsetPixels);
        }

        @Override
        public void onPageSelected(int position) {
            saveNewPage();
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    FrameLayout frameLayout = new FrameLayout(context);
    emojiTab.addView(frameLayout, LayoutHelper.createLinear(52, 48));

    backspaceButton = new ImageView(context) {
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                backspacePressed = true;
                backspaceOnce = false;
                postBackspaceRunnable(350);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL
                    || event.getAction() == MotionEvent.ACTION_UP) {
                backspacePressed = false;
                if (!backspaceOnce) {
                    if (listener != null && listener.onBackspace()) {
                        backspaceButton.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
                    }
                }
            }
            super.onTouchEvent(event);
            return true;
        }
    };
    backspaceButton.setImageResource(kr.wdream.storyshop.R.drawable.ic_smiles_backspace);
    backspaceButton.setBackgroundResource(kr.wdream.storyshop.R.drawable.ic_emoji_backspace);
    backspaceButton.setScaleType(ImageView.ScaleType.CENTER);
    frameLayout.addView(backspaceButton, LayoutHelper.createFrame(52, 48));

    View view = new View(context);
    view.setBackgroundColor(0xffe2e5e7);
    frameLayout.addView(view, LayoutHelper.createFrame(52, 1, Gravity.LEFT | Gravity.BOTTOM));

    TextView textView = new TextView(context);
    textView.setText(LocaleController.getString("NoRecent", kr.wdream.storyshop.R.string.NoRecent));
    textView.setTextSize(18);
    textView.setTextColor(0xff888888);
    textView.setGravity(Gravity.CENTER);
    textView.setClickable(false);
    textView.setFocusable(false);
    ((FrameLayout) views.get(0)).addView(textView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT,
            LayoutHelper.WRAP_CONTENT, Gravity.CENTER, 0, 48, 0, 0));
    emojiGrids.get(0).setEmptyView(textView);

    addView(pager, 0, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT,
            Gravity.LEFT | Gravity.TOP));

    emojiSize = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 40 : 32);
    pickerView = new EmojiColorPickerView(context);
    pickerViewPopup = new EmojiPopupWindow(pickerView,
            popupWidth = AndroidUtilities.dp((AndroidUtilities.isTablet() ? 40 : 32) * 6 + 10 + 4 * 5),
            popupHeight = AndroidUtilities.dp(AndroidUtilities.isTablet() ? 64 : 56));
    pickerViewPopup.setOutsideTouchable(true);
    pickerViewPopup.setClippingEnabled(true);
    pickerViewPopup.setInputMethodMode(EmojiPopupWindow.INPUT_METHOD_NOT_NEEDED);
    pickerViewPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED);
    pickerViewPopup.getContentView().setFocusableInTouchMode(true);
    pickerViewPopup.getContentView().setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_UP && pickerViewPopup != null
                    && pickerViewPopup.isShowing()) {
                pickerViewPopup.dismiss();
                return true;
            }
            return false;
        }
    });
    currentPage = getContext().getSharedPreferences("emoji", Activity.MODE_PRIVATE).getInt("selected_page", 0);
    loadRecents();
}

From source file:com.android.mail.ui.ConversationViewFragment.java

@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        mOriginalKeyedView = view;//from w w  w  . ja v a  2s .  c  o m
    }

    if (mOriginalKeyedView != null) {
        final int id = mOriginalKeyedView.getId();
        final boolean isRtl = ViewUtils.isViewRtl(mOriginalKeyedView);
        final boolean isActionUp = keyEvent.getAction() == KeyEvent.ACTION_UP;
        final boolean isStart = KeyboardUtils.isKeycodeDirectionStart(keyCode, isRtl);
        final boolean isEnd = KeyboardUtils.isKeycodeDirectionEnd(keyCode, isRtl);
        final boolean isUp = keyCode == KeyEvent.KEYCODE_DPAD_UP;
        final boolean isDown = keyCode == KeyEvent.KEYCODE_DPAD_DOWN;

        // First we run the event by the controller
        // We manually check if the view+direction combination should shift focus away from the
        // conversation view to the thread list in two-pane landscape mode.
        final boolean isTwoPaneLand = mNavigationController.isTwoPaneLandscape();
        final boolean navigateAway = shouldNavigateAway(id, isStart, isTwoPaneLand);
        if (mNavigationController.onInterceptKeyFromCV(keyCode, keyEvent, navigateAway)) {
            return true;
        }

        // If controller didn't handle the event, check directional interception.
        if ((isStart || isEnd) && shouldInterceptLeftRightEvents(id, isStart, isEnd, isTwoPaneLand)) {
            return true;
        } else if (isUp || isDown) {
            // We don't do anything on up/down for overlay
            if (id == R.id.conversation_topmost_overlay) {
                return true;
            }

            // We manually handle up/down navigation through the overlay items because the
            // system's default isn't optimal for two-pane landscape since it's not a real list.
            final View next = mConversationContainer.getNextOverlayView(mOriginalKeyedView, isDown);
            if (next != null) {
                focusAndScrollToView(next);
            } else if (!isActionUp) {
                // Scroll in the direction of the arrow if next view isn't found.
                final int currentY = mWebView.getScrollY();
                if (isUp && currentY > 0) {
                    mWebView.scrollBy(0, -Math.min(currentY, DEFAULT_VERTICAL_SCROLL_DISTANCE_PX));
                } else if (isDown) {
                    final int webviewEnd = (int) (mWebView.getContentHeight() * mWebView.getScale());
                    final int currentEnd = currentY + mWebView.getHeight();
                    if (currentEnd < webviewEnd) {
                        mWebView.scrollBy(0,
                                Math.min(webviewEnd - currentEnd, DEFAULT_VERTICAL_SCROLL_DISTANCE_PX));
                    }
                }
            }
            return true;
        }

        // Finally we handle the special keys
        if (keyCode == KeyEvent.KEYCODE_BACK && id != R.id.conversation_topmost_overlay) {
            if (isActionUp) {
                mTopmostOverlay.requestFocus();
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_ENTER && id == R.id.conversation_topmost_overlay) {
            if (isActionUp) {
                mWebView.scrollTo(0, 0);
                mConversationContainer.focusFirstMessageHeader();
            }
            return true;
        }
    }
    return false;
}