Java Code Examples for android.view.KeyEvent#KEYCODE_DPAD_CENTER

The following examples show how to use android.view.KeyEvent#KEYCODE_DPAD_CENTER . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkMovementMethod2.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean handleMovementKey(TextView widget, Spannable buffer, int keyCode,
        int movementMetaState, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            if (KeyEvent.metaStateHasNoModifiers(movementMetaState)) {
                if (event.getAction() == KeyEvent.ACTION_DOWN &&
                        event.getRepeatCount() == 0 && action(CLICK, widget, buffer)) {
                    return true;
                }
            }
            break;
    }
    return super.handleMovementKey(widget, buffer, keyCode, movementMetaState, event);
}
 
Example 2
Source File: Gallery.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Handles left, right, and clicking
 * @see android.view.View#onKeyDown
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        
    case KeyEvent.KEYCODE_DPAD_LEFT:
        if (moveDirection(-1)) {
            playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
            return true;
        }
        break;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (moveDirection(1)) {
            playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
            return true;
        }
        break;
    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_ENTER:
        mReceivedInvokeKeyDown = true;
        // fallthrough to default handling
    }
    
    return super.onKeyDown(keyCode, event);
}
 
Example 3
Source File: LinkMovementMethod2.java    From Nimingban with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean handleMovementKey(TextView widget, Spannable buffer, int keyCode,
        int movementMetaState, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            if (KeyEvent.metaStateHasNoModifiers(movementMetaState)) {
                if (event.getAction() == KeyEvent.ACTION_DOWN &&
                        event.getRepeatCount() == 0 && action(CLICK, widget, buffer)) {
                    return true;
                }
            }
            break;
    }
    return super.handleMovementKey(widget, buffer, keyCode, movementMetaState, event);
}
 
Example 4
Source File: VDVideoPlayButton.java    From NewsMe with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
	VDLog.e("VDVideoPlayButton", " onKeyUp ");
	if (keyCode == KeyEvent.KEYCODE_ENTER
			|| keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
		doClick();
		return true;
	}
	if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
		VDLog.e("VDVideoPlayButton",
				" onKeyUp KEYCODE_DPAD_RIGHT 111111111");
		int id = getNextFocusRightId();
		View v = ((ViewGroup) getParent()).findViewById(id);
		if (v != null) {
			VDLog.e("VDVideoPlayButton", " onKeyUp KEYCODE_DPAD_RIGHT");
			v.requestFocus();
		}
		return true;
	}
	return super.onKeyUp(keyCode, event);
}
 
Example 5
Source File: NumberPicker.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    boolean isKeyOfInterest = keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER;
    boolean isReleased = event.getAction() == KeyEvent.ACTION_UP;
    boolean isPressed = event.getAction() == KeyEvent.ACTION_DOWN
            && event.getAction() != KeyEvent.ACTION_MULTIPLE;

    if (isKeyOfInterest && isReleased) {
        stopUpdating();
    } else if (isKeyOfInterest && isPressed) {
        startUpdating(v == plusButton);
    }
    return false;
}
 
Example 6
Source File: RepeatingImageButton.java    From SMP with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_CENTER:
    case KeyEvent.KEYCODE_ENTER:
        // remove the repeater, but call the hook one more time
        removeCallbacks(mRepeater);
        if (mStartTime != 0) {
            doRepeat(true);
            mStartTime = 0;
        }
    }
    return super.onKeyUp(keyCode, event);
}
 
Example 7
Source File: RepeatingImageButton.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            // remove the repeater, but call the hook one more time
            removeCallbacks(mRepeater);
            if (mStartTime != 0) {
                doRepeat(true);
                mStartTime = 0;
            }
    }
    return super.onKeyUp(keyCode, event);
}
 
Example 8
Source File: ChooseTime.java    From zidoorecorder with Apache License 2.0 5 votes vote down vote up
@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					SoundTool.soundKey(keyCode);
					getCurr();
					System.out.println("bob  keyCode  = " + keyCode);
					if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ENTER
							|| keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
						if (mOnSetDateListener != null) {
							System.out.println("bob  isDate  = " + isDate);
							if (!isDate) {
								msTime =  mHour * 3600000 + mMin * 60000 + mSec * 1000;
								System.out.println("bob mHour = "+mHour);
								System.out.println("bob mMin = "+mMin);
								System.out.println("bob mSec = "+mSec);
								mOnSetDateListener.setDate(wheelMain.getCurrentTime(isDate), msTime);
							} else {
								state = false;
								msTime = currentDay * 24 * 3600000+currentHour * 3600000 + currentMinute * 60000 + currentSecond * 1000;
								msTimeAll=day* 24 * 3600000+houre*3600000+minute*60000+seconds*1000;
								System.out.println("bob..houre== "+houre+"minute== "+minute+"seconds==  "+seconds);
								System.out.println("bob..currentHour== "+currentHour+"currentMinute== "+currentMinute+"currentSecond=="+currentSecond);
//								if ((msTime-msTimeAll)>= 5000) {
//									
//								} else {
//									ZidooRecorderTool.Toast_MSG(mContext, mContext.getString(R.string.time_length));
//								}
								
								dataMsg = wheelMain.getCurrentTime(isDate);
								mOnSetDateListener.setDate(dataMsg, msTime);
							 	dialog.dismiss();
								 handler.removeCallbacks(updateThread);
							}
						}
						
					}
				}
				return false;
			}
 
Example 9
Source File: SkbContainer.java    From AndroidTVWidget with Apache License 2.0 5 votes vote down vote up
/**
 * 处理DOWN事件.
 */
@Override
public boolean onSoftKeyDown(int keyCode, KeyEvent event) {
	if (!isFocused()) {
		return false;
	}
	SoftKey tempSoftKey = new SoftKey();
	OPENLOG.D("onSoftKeyDown keyCode:" + keyCode);
	switch (keyCode) {
	case KeyEvent.KEYCODE_ENTER:
	case KeyEvent.KEYCODE_DPAD_CENTER:
		SoftKeyboard softKeyboard = mSoftKeyboardView.getSoftKeyboard();
		SoftKey softKey = softKeyboard.getSelectSoftKey();
		mSoftKeyboardView.setSoftKeyPress(true);
		OPENLOG.D("onSoftKeyDown softKey:" + softKey);
		if (!setKeyCodeEnter(softKey)) {
			return false;
		}
		break;
	case KeyEvent.KEYCODE_BACK:
		tempSoftKey.setKeyCode(KeyEvent.KEYCODE_BACK);
		onBack(tempSoftKey);
		break;
	case KeyEvent.KEYCODE_DEL:
		tempSoftKey.setKeyCode(KeyEvent.KEYCODE_DEL);
		onDelete(tempSoftKey);
		break;
	case KeyEvent.KEYCODE_DPAD_LEFT: // 左
	case KeyEvent.KEYCODE_DPAD_RIGHT: // 右
	case KeyEvent.KEYCODE_DPAD_UP: // 上
	case KeyEvent.KEYCODE_DPAD_DOWN: // 下
		mSoftKeyboardView.setSoftKeyPress(false);
		return actionForKeyEvent(keyCode); // 按键移动.
	default:
		OPENLOG.D("onSoftKeyDown false keyCode:" + keyCode);
		return false;
	}
	OPENLOG.D("onSoftKeyDown true keyCode:" + keyCode);
	return true;
}
 
Example 10
Source File: LiveActivityRel.java    From TvPlayer with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean isCtrl = leftMenuCtrl.getVisibility() == View.GONE;
    //确认键弹出频道列表界面
    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
        if (isCtrl) {
            leftMenuCtrl.setVisibility(View.VISIBLE);
            leftMenuCtrl.requestFocus();
        } else
            leftMenuCtrl.setVisibility(View.GONE);
    }
    //左右切换频道列表
    if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
        turnRight(null);
    }
    if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
        turnLeft(null);
    }
    int size = PlayListCache.channelInfoList.size() - 1;
    //上下换台
    if (keyCode == KeyEvent.KEYCODE_DPAD_UP && isCtrl) {
        PlayListCache.currChannel--;
        if (PlayListCache.currChannel < 0)
            PlayListCache.currChannel = size;
        LiveActivityRel.activityStart(LiveActivityRel.this, PlayListCache.channelInfoList.get(PlayListCache.currChannel));
        //结束当前播放
        LiveActivityRel.this.finish();
    }
    if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && isCtrl) {
        PlayListCache.currChannel++;
        if (PlayListCache.currChannel > size)
            PlayListCache.currChannel = 0;
        LiveActivityRel.activityStart(LiveActivityRel.this, PlayListCache.channelInfoList.get(PlayListCache.currChannel));
        //结束当前播放
        LiveActivityRel.this.finish();
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 11
Source File: RepeatingImageButton.java    From RoMote with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            // need to call super to make long press work, but return
            // true so that the application doesn't get the down event.
            super.onKeyDown(keyCode, event);
            return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 12
Source File: NumberPicker.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    final int keyCode = event.getKeyCode();
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            removeAllCallbacks();
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
        case KeyEvent.KEYCODE_DPAD_UP:
            if (!mHasSelectorWheel) {
                break;
            }
            switch (event.getAction()) {
                case KeyEvent.ACTION_DOWN:
                    if (mWrapSelectorWheel || ((keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
                            ? getValue() < getMaxValue() : getValue() > getMinValue())) {
                        requestFocus();
                        mLastHandledDownDpadKeyCode = keyCode;
                        removeAllCallbacks();
                        if (mFlingScroller.isFinished()) {
                            changeValueByOne(keyCode == KeyEvent.KEYCODE_DPAD_DOWN);
                        }
                        return true;
                    }
                    break;
                case KeyEvent.ACTION_UP:
                    if (mLastHandledDownDpadKeyCode == keyCode) {
                        mLastHandledDownDpadKeyCode = -1;
                        return true;
                    }
                    break;
            }
    }
    return super.dispatchKeyEvent(event);
}
 
Example 13
Source File: XulRenderContext.java    From starcor.xul with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean onKeyEvent(KeyEvent event) {
	XulLayout layout;
	if (_page == null || (layout = _page.getLayout()) == null) {
		lastKeyAction = -1;
		lastKeyCode = -1;
		lastKeyEventView = null;
		lastDownHandled = false;
		return false;
	}
	int keyAction = event.getAction();
	int keyCode = event.getKeyCode();
	XulView focusView = layout.getFocus();
	boolean ret = false;

	if (keyAction == KeyEvent.ACTION_DOWN) {
		if (layout.onKeyEvent(event)) {
			lastKeyAction = keyAction;
			lastKeyCode = keyCode;
			lastKeyEventView = getWeakReference(focusView);
			ret = true;
		} else switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			ret = layout.moveFocus(XulLayout.FocusDirection.MOVE_LEFT);
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			ret = layout.moveFocus(XulLayout.FocusDirection.MOVE_RIGHT);
			break;
		case KeyEvent.KEYCODE_DPAD_UP:
			ret = layout.moveFocus(XulLayout.FocusDirection.MOVE_UP);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			ret = layout.moveFocus(XulLayout.FocusDirection.MOVE_DOWN);
			break;
		case KeyEvent.KEYCODE_DPAD_CENTER:
		case KeyEvent.KEYCODE_ENTER:
			ret = layout.doClick(actionCallback);
			break;
		case KeyEvent.KEYCODE_BACK:
			ret = _page.popStates();
			if (ret) {
				XulPage.invokeActionNoPopup(_page, "statesRestored");
			}
			break;
		}
		lastDownHandled = ret;
	} else if (keyAction == KeyEvent.ACTION_UP) {
		XulView xulView = lastKeyEventView == null ? null : lastKeyEventView.get();
		if (xulView != focusView) {
			ret = lastDownHandled;
		} else if (layout.onKeyEvent(event)) {
			lastKeyAction = keyAction;
			lastKeyCode = keyCode;
			lastKeyEventView = getWeakReference(focusView);
			ret = true;
		} else switch (keyCode) {
		default:
			ret = lastDownHandled;
			break;
		}
		lastDownHandled = false;
	}

	lastKeyAction = keyAction;
	lastKeyCode = keyCode;
	lastKeyEventView = getWeakReference(focusView);

	if (ret) {
		suspendDrawableWorker();
	}
	return ret;
}
 
Example 14
Source File: VideoControlsLeanback.java    From ExoMedia with Apache License 2.0 4 votes vote down vote up
/**
 * NOTE: the view is not always the currently focused view, thus the
 * {@link #currentFocus} variable
 */
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_DOWN) {
        return false;
    }

    switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            if (isVisible && canViewHide && !isLoading) {
                hide();
                return true;
            } else if (controlsParent.getAnimation() != null) {
                //This occurs if we are animating the hide or show of the controls
                return true;
            }
            break;

        case KeyEvent.KEYCODE_DPAD_UP:
            showTemporary();
            return true;

        case KeyEvent.KEYCODE_DPAD_DOWN:
            hide();
            return true;

        case KeyEvent.KEYCODE_DPAD_LEFT:
            showTemporary();
            focusPrevious(currentFocus);
            return true;

        case KeyEvent.KEYCODE_DPAD_RIGHT:
            showTemporary();
            focusNext(currentFocus);
            return true;

        case KeyEvent.KEYCODE_DPAD_CENTER:
            showTemporary();
            currentFocus.callOnClick();
            return true;

        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            onPlayPauseClick();
            return true;

        case KeyEvent.KEYCODE_MEDIA_PLAY:
            if (videoView != null && !videoView.isPlaying()) {
                videoView.start();
                return true;
            }
            break;

        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            if (videoView != null && videoView.isPlaying()) {
                videoView.pause();
                return true;
            }
            break;

        case KeyEvent.KEYCODE_MEDIA_NEXT:
            onNextClick();
            return true;

        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            onPreviousClick();
            return true;

        case KeyEvent.KEYCODE_MEDIA_REWIND:
            onRewindClick();
            return true;

        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
            onFastForwardClick();
            return true;
    }

    return false;
}
 
Example 15
Source File: ListPopupWindow.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Filter key down events. By forwarding key down events to this function,
 * views using non-modal ListPopupWindow can have it handle key selection of items.
 *
 * @param keyCode keyCode param passed to the host view's onKeyDown
 * @param event event param passed to the host view's onKeyDown
 * @return true if the event was handled, false if it was ignored.
 *
 * @see #setModal(boolean)
 * @see #onKeyUp(int, KeyEvent)
 */
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
    // when the drop down is shown, we drive it directly
    if (isShowing()) {
        // the key events are forwarded to the list in the drop down view
        // note that ListView handles space but we don't want that to happen
        // also if selection is not currently in the drop down, then don't
        // let center or enter presses go there since that would cause it
        // to select one of its items
        if (keyCode != KeyEvent.KEYCODE_SPACE
                && (mDropDownList.getSelectedItemPosition() >= 0
                        || !KeyEvent.isConfirmKey(keyCode))) {
            int curIndex = mDropDownList.getSelectedItemPosition();
            boolean consumed;

            final boolean below = !mPopup.isAboveAnchor();

            final ListAdapter adapter = mAdapter;

            boolean allEnabled;
            int firstItem = Integer.MAX_VALUE;
            int lastItem = Integer.MIN_VALUE;

            if (adapter != null) {
                allEnabled = adapter.areAllItemsEnabled();
                firstItem = allEnabled ? 0 :
                        mDropDownList.lookForSelectablePosition(0, true);
                lastItem = allEnabled ? adapter.getCount() - 1 :
                        mDropDownList.lookForSelectablePosition(adapter.getCount() - 1, false);
            }

            if ((below && keyCode == KeyEvent.KEYCODE_DPAD_UP && curIndex <= firstItem) ||
                    (!below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN && curIndex >= lastItem)) {
                // When the selection is at the top, we block the key
                // event to prevent focus from moving.
                clearListSelection();
                mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                show();
                return true;
            } else {
                // WARNING: Please read the comment where mListSelectionHidden
                //          is declared
                mDropDownList.setListSelectionHidden(false);
            }

            consumed = mDropDownList.onKeyDown(keyCode, event);
            if (DEBUG) Log.v(TAG, "Key down: code=" + keyCode + " list consumed=" + consumed);

            if (consumed) {
                // If it handled the key event, then the user is
                // navigating in the list, so we should put it in front.
                mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
                // Here's a little trick we need to do to make sure that
                // the list view is actually showing its focus indicator,
                // by ensuring it has focus and getting its window out
                // of touch mode.
                mDropDownList.requestFocusFromTouch();
                show();

                switch (keyCode) {
                    // avoid passing the focus from the text view to the
                    // next component
                    case KeyEvent.KEYCODE_ENTER:
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                    case KeyEvent.KEYCODE_DPAD_UP:
                        return true;
                }
            } else {
                if (below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    // when the selection is at the bottom, we block the
                    // event to avoid going to the next focusable widget
                    if (curIndex == lastItem) {
                        return true;
                    }
                } else if (!below && keyCode == KeyEvent.KEYCODE_DPAD_UP &&
                        curIndex == firstItem) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 16
Source File: SimpleMonthView.java    From DateTimePicker with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // We need to handle focus change within the SimpleMonthView because we are simulating
    // multiple Views. The arrow keys will move between days until there is no space (no
    // day to the left, top, right, or bottom). Focus forward and back jumps out of the
    // SimpleMonthView, skipping over other SimpleMonthViews in the parent ViewPager
    // to the next focusable View in the hierarchy.
    boolean focusChanged = false;
    switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (event.hasNoModifiers()) {
                focusChanged = moveOneDay(isLayoutRtl());
            }
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (event.hasNoModifiers()) {
                focusChanged = moveOneDay(!isLayoutRtl());
            }
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            if (event.hasNoModifiers()) {
                ensureFocusedDay();
                if (mHighlightedDay > 7) {
                    mHighlightedDay -= 7;
                    focusChanged = true;
                }
            }
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (event.hasNoModifiers()) {
                ensureFocusedDay();
                if (mHighlightedDay <= mDaysInMonth - 7) {
                    mHighlightedDay += 7;
                    focusChanged = true;
                }
            }
            break;
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            if (mHighlightedDay != -1) {
                onDayClicked(mHighlightedDay);
                return true;
            }
            break;
        case KeyEvent.KEYCODE_TAB: {
            int focusChangeDirection = 0;
            if (event.hasNoModifiers()) {
                focusChangeDirection = View.FOCUS_FORWARD;
            } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
                focusChangeDirection = View.FOCUS_BACKWARD;
            }
            if (focusChangeDirection != 0) {
                final ViewParent parent = getParent();
                // move out of the ViewPager next/previous
                View nextFocus = this;
                do {
                    nextFocus = nextFocus.focusSearch(focusChangeDirection);
                } while (nextFocus != null && nextFocus != this &&
                        nextFocus.getParent() == parent);
                if (nextFocus != null) {
                    nextFocus.requestFocus();
                    return true;
                }
            }
            break;
        }
    }
    if (focusChanged) {
        invalidate();
        return true;
    } else {
        return super.onKeyDown(keyCode, event);
    }
}
 
Example 17
Source File: Apps.java    From emerald with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (event.getAction() != KeyEvent.ACTION_DOWN) {
		return super.onKeyDown(keyCode, event);
	}
	if (keyCode == KeyEvent.KEYCODE_MENU) {
		menu();
		return true;
	} else if (keyCode == KeyEvent.KEYCODE_BACK) {
		//Log.v(APP_TAG, "BACK pressed");
		if (searchIsOpened) {
			closeSearch();
		} else if (!isDefaultLauncher()) {
			finish();
			return true;
		} else {
			if (categories.getCurCategory().equals(CategoryManager.HIDDEN)) {
				findViewById(R.id.quit_hidden_apps).setVisibility(View.GONE);
				hideMainBarIfNeeded();
			}
			categories.prevCategory();
		}
		loadFilteredApps();
		return true;
	} else if (event.isAltPressed()) {
		if (keyCode >= KeyEvent.KEYCODE_1 && keyCode <= KeyEvent.KEYCODE_9) {
			Object app = dock.getApp(keyCode-KeyEvent.KEYCODE_1);
			if (app != null) {
				if (app instanceof AppData) {
					launch((AppData)app);
				} else if (app instanceof ShortcutData) {
					launch((ShortcutData)app);
				}
				return true;
			} else {
				return super.onKeyDown(keyCode, event);
			}
		} else if (keyCode == KeyEvent.KEYCODE_0 && !searchIsOpened) {
			openSearch();
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			categories.setCurCategory(categories.getCategory(CategoryManager.PREVIOUS));
			loadFilteredApps();
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			categories.setCurCategory(categories.getCategory(CategoryManager.NEXT));
			loadFilteredApps();
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			openCategoriesList();
		} else {
			return super.onKeyDown(keyCode, event);
		}
		return super.onKeyDown(keyCode, event);
	} else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
		View view = getCurrentFocus();
		if (view != null) {
			Object viewTag = view.getTag();
			if (viewTag instanceof AppData) {
				launch((AppData)viewTag);
				return true;
			} else if (viewTag instanceof ShortcutData) {
				launch((ShortcutData)viewTag);
				return true;
			}
			return super.onKeyDown(keyCode, event);
		}
		return super.onKeyDown(keyCode, event);
	} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
		if (options.getBoolean(Keys.VOLUME_BUTTONS, false)) {
			categories.setCurCategory(categories.getCategory(CategoryManager.PREVIOUS));
			loadFilteredApps();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
		if (options.getBoolean(Keys.VOLUME_BUTTONS, false)) {
			categories.setCurCategory(categories.getCategory(CategoryManager.NEXT));
			loadFilteredApps();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	} else {
		return super.onKeyDown(keyCode, event);
	}
}
 
Example 18
Source File: VideoPlayActivity.java    From dtube-mobile-unofficial with Apache License 2.0 4 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {

    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        Log.d("dtube","Key event "+event.getKeyCode()+"VS"+KeyEvent.KEYCODE_MEDIA_REWIND);
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_DPAD_CENTER:
                if (getCurrentFocus() != null && getCurrentFocus() instanceof  ViewGroup) {
                    //Open a dialog for options on a comment
                    if (getCurrentFocus().getId() == R.id.comments_lv) {

                        final View commentView = commentsListView.getSelectedView();
                        final AlertDialog.Builder builderSingle = new AlertDialog.Builder(VideoPlayActivity.this);

                        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(VideoPlayActivity.this, android.R.layout.select_dialog_item);
                        arrayAdapter.add(getResources().getString(R.string.like_comment));
                        arrayAdapter.add(getResources().getString(R.string.dislike_comment));

                        builderSingle.setNegativeButton("cancel", null);

                        builderSingle.setAdapter(arrayAdapter, (dialog, which) -> {
                            switch (which) {
                                case 0:
                                commentView.findViewById(R.id.comment_like).performClick();
                                break;

                                case 1:
                                    commentView.findViewById(R.id.comment_dislike).performClick();
                                    break;

                                case 2:
                                    commentView.findViewById(R.id.comment_reply).performClick();
                                    break;
                            }
                        });

                        //dialog display is delayed to prevent misfocus
                        commentView.postDelayed(builderSingle::show,10);

                        Log.d("dtube", "dispatch" + ((ViewGroup) getCurrentFocus()).getChildAt(0).getId() + "VS" + R.id.comment_item);
                    }else if (getCurrentFocus().getId() == R.id.suggestions_lv) {
                        //select the suggested video
                        onItemClick(suggestedVideosListView.getSelectedItemPosition());
                    }
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                wakeMediaControls();
                MediaPlayerSingleton.getInstance(this).togglePlayPause();
                break;

            case KeyEvent.KEYCODE_MEDIA_REWIND:
            case KeyEvent.KEYCODE_MEDIA_STEP_BACKWARD:
            case KeyEvent.KEYCODE_MEDIA_SKIP_BACKWARD:
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                wakeMediaControls();
                MediaPlayerSingleton.getInstance(this).rewind();
                break;

            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
            case KeyEvent.KEYCODE_MEDIA_SKIP_FORWARD:
            case KeyEvent.KEYCODE_MEDIA_STEP_FORWARD:
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                wakeMediaControls();
                MediaPlayerSingleton.getInstance(this).fastForward();
                break;

        }
    }
    return super.dispatchKeyEvent(event);
}
 
Example 19
Source File: TwoWayGridView.java    From recent-images with MIT License 4 votes vote down vote up
private boolean commonKey(int keyCode, int count, KeyEvent event) {
	if (mAdapter == null) {
		return false;
	}

	if (mDataChanged) {
		layoutChildren();
	}

	boolean handled = false;
	int action = event.getAction();

	if (action != KeyEvent.ACTION_UP) {
		if (mSelectedPosition < 0) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_UP:
			case KeyEvent.KEYCODE_DPAD_DOWN:
			case KeyEvent.KEYCODE_DPAD_LEFT:
			case KeyEvent.KEYCODE_DPAD_RIGHT:
			case KeyEvent.KEYCODE_DPAD_CENTER:
			case KeyEvent.KEYCODE_SPACE:
			case KeyEvent.KEYCODE_ENTER:
				resurrectSelection();
				return true;
			}
		}

		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			if (!event.isAltPressed()) {
				handled = mGridBuilder.arrowScroll(FOCUS_LEFT);
			} else {
				handled = fullScroll(FOCUS_UP);
			}
			break;

		case KeyEvent.KEYCODE_DPAD_RIGHT:
			if (!event.isAltPressed()) {
				handled = mGridBuilder.arrowScroll(FOCUS_RIGHT);
			} else {
				handled = fullScroll(FOCUS_DOWN);
			}
			break;

		case KeyEvent.KEYCODE_DPAD_UP:
			if (!event.isAltPressed()) {
				handled = mGridBuilder.arrowScroll(FOCUS_UP);

			} else {
				handled = fullScroll(FOCUS_UP);
			}
			break;

		case KeyEvent.KEYCODE_DPAD_DOWN:
			if (!event.isAltPressed()) {
				handled = mGridBuilder.arrowScroll(FOCUS_DOWN);
			} else {
				handled = fullScroll(FOCUS_DOWN);
			}
			break;

		case KeyEvent.KEYCODE_DPAD_CENTER:
		case KeyEvent.KEYCODE_ENTER: {
			if (getChildCount() > 0 && event.getRepeatCount() == 0) {
				keyPressed();
			}

			return true;
		}

		case KeyEvent.KEYCODE_SPACE:
			//if (mPopup == null || !mPopup.isShowing()) {
			if (!event.isShiftPressed()) {
				handled = pageScroll(FOCUS_DOWN);
			} else {
				handled = pageScroll(FOCUS_UP);
			}
			//}
			break;
		}
	}

	//if (!handled) {
	//    handled = sendToTextFilter(keyCode, count, event);
	//}

	if (handled) {
		return true;
	} else {
		switch (action) {
		case KeyEvent.ACTION_DOWN:
			return super.onKeyDown(keyCode, event);
		case KeyEvent.ACTION_UP:
			return super.onKeyUp(keyCode, event);
		case KeyEvent.ACTION_MULTIPLE:
			return super.onKeyMultiple(keyCode, count, event);
		default:
			return false;
		}
	}
}
 
Example 20
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
/**
 * Show the context menu for the currently focused view and executes a
 * particular context menu item.
 * 
 * @param targetActivity The activity in question.
 * @param id The identifier associated with the context menu item.
 * @param flag Additional flags, if any.
 * @return Whether the invocation was successful (for example, it could be
 *         false if item is disabled).
 */
public boolean invokeContextMenuAction(Activity targetActivity, int id, int flag) {
    validateNotAppThread();
    
    // Bring up context menu for current focus.
    // It'd be nice to do this through code, but currently ListView depends on
    //   long press to set metadata for its selected child
    
    final KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER); 
    sendKeySync(downEvent);

    // Need to wait for long press
    waitForIdleSync();
    try {
        Thread.sleep(ViewConfiguration.getLongPressTimeout());
    } catch (InterruptedException e) {
        Log.e(TAG, "Could not sleep for long press timeout", e);
        return false;
    }

    final KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER); 
    sendKeySync(upEvent);

    // Wait for context menu to appear
    waitForIdleSync();
    
    class ContextMenuRunnable implements Runnable {
        private final Activity activity;
        private final int identifier;
        private final int flags;
        boolean returnValue;
        
        public ContextMenuRunnable(Activity _activity, int _identifier,
                                int _flags) {
            activity = _activity;
            identifier = _identifier;
            flags = _flags;
        }
        
        public void run() {
            Window win = activity.getWindow();
            returnValue = win.performContextMenuIdentifierAction(
                        identifier, 
                        flags);                
        }
        
    }        
    
    ContextMenuRunnable cmr = new ContextMenuRunnable(targetActivity, id, flag);
    runOnMainSync(cmr);
    return cmr.returnValue;
}