Java Code Examples for android.view.KeyEvent#ACTION_UP

The following examples show how to use android.view.KeyEvent#ACTION_UP . 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: InteractionController.java    From za-Farmer with MIT License 6 votes vote down vote up
public boolean sendKey(int keyCode, int metaState) {
    if (DEBUG) {
        Log.d(LOG_TAG, "sendKey (" + keyCode + ", " + metaState + ")");
    }

    final long eventTime = SystemClock.uptimeMillis();
    KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,
            keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
            InputDevice.SOURCE_KEYBOARD);
    if (injectEventSync(downEvent)) {
        KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP,
                keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
                InputDevice.SOURCE_KEYBOARD);
        if(injectEventSync(upEvent)) {
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: NumberPicker.java    From DateTimePicker 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 3
Source File: XanderPanel.java    From XanderPanel with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP
            && !event.isCanceled() && mCancelable && !mDismissing) {
        dismiss();
        return true;
    }
    return false;
}
 
Example 4
Source File: ActionBarLayout.java    From KrGallery with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK
            && event.getAction() == KeyEvent.ACTION_UP) {
        return delegate != null && delegate.onPreIme() || super.dispatchKeyEventPreIme(event);
    }
    return super.dispatchKeyEventPreIme(event);
}
 
Example 5
Source File: TimePickerDialog.java    From narrate-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP) {
        return processKeyUp(keyCode);
    }
    return false;
}
 
Example 6
Source File: NumberPicker.java    From android_9.0.0_r45 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 7
Source File: PopupWindow.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
	if (BuildConfig.DEBUG) {
		Log.v("PopupWindow", "--onKey(" + String.valueOf(keyCode) + ')');
	}
	
	switch (keyCode) {
		case KeyEvent.KEYCODE_BACK: {
			switch (event.getAction()) {
				case KeyEvent.ACTION_UP: {
					if (!event.isCanceled()) {
						onBackPressed();
					}
					break;
				}
			}
			return true;
		}
	}
	
	// Delay auto-hiding if set to do so (and we didn't already handle this event).
	if (event.getAction() == KeyEvent.ACTION_DOWN) {
		onUserInteraction();
	}
	
	return false;
}
 
Example 8
Source File: FieldViewManager.java    From homescreenarcade with GNU General Public License v3.0 5 votes vote down vote up
public boolean handleKeyUp(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP) {
        synchronized(field) {
            if (!field.getGameState().isGameInProgress() || field.getGameState().isPaused()) {
                return false;
            }
            return updateFlippersForKeyCode(keyCode, false);
        }
    }
    return false;
}
 
Example 9
Source File: ActionBarSherlockCompat.java    From Libraries-for-Android-Developers with MIT License 5 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (ActionBarSherlock.DEBUG) Log.d(TAG, "[dispatchKeyEvent] event: " + event);

    final int keyCode = event.getKeyCode();

    // Not handled by the view hierarchy, does the action bar want it
    // to cancel out of something special?
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        final int action = event.getAction();
        // Back cancels action modes first.
        if (mActionMode != null) {
            if (action == KeyEvent.ACTION_UP) {
                mActionMode.finish();
            }
            if (ActionBarSherlock.DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning true");
            return true;
        }

        // Next collapse any expanded action views.
        if (wActionBar != null && wActionBar.hasExpandedActionView()) {
            if (action == KeyEvent.ACTION_UP) {
                wActionBar.collapseActionView();
            }
            if (ActionBarSherlock.DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning true");
            return true;
        }
    }

    if (ActionBarSherlock.DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning false");
    return false;
}
 
Example 10
Source File: ActionBarSherlockCompat.java    From zhangshangwuda with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (ActionBarSherlock.DEBUG) Log.d(TAG, "[dispatchKeyEvent] event: " + event);

    final int keyCode = event.getKeyCode();

    // Not handled by the view hierarchy, does the action bar want it
    // to cancel out of something special?
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        final int action = event.getAction();
        // Back cancels action modes first.
        if (mActionMode != null) {
            if (action == KeyEvent.ACTION_UP) {
                mActionMode.finish();
            }
            if (ActionBarSherlock.DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning true");
            return true;
        }

        // Next collapse any expanded action views.
        if (wActionBar != null && wActionBar.hasExpandedActionView()) {
            if (action == KeyEvent.ACTION_UP) {
                wActionBar.collapseActionView();
            }
            if (ActionBarSherlock.DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning true");
            return true;
        }
    }

    if (ActionBarSherlock.DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning false");
    return false;
}
 
Example 11
Source File: MenuPopupHelper.java    From android-apps with MIT License 5 votes vote down vote up
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_MENU) {
        dismiss();
        return true;
    }
    return false;
}
 
Example 12
Source File: UrlBar.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (KeyEvent.KEYCODE_BACK == keyCode && event.getAction() == KeyEvent.ACTION_UP) {
        mKeyboardHideHelper.monitorForKeyboardHidden();
    }
    return super.onKeyPreIme(keyCode, event);
}
 
Example 13
Source File: TimePickerDialog.java    From MaterialDateRangePicker with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP) {
        return processKeyUp(keyCode);
    }
    return false;
}
 
Example 14
Source File: FocusHelper.java    From TurboLauncher with Apache License 2.0 4 votes vote down vote up
/**
 * Handles key events in the workspace hotseat (bottom of the screen).
 */
static boolean handleHotseatButtonKeyEvent(View v, int keyCode, KeyEvent e, int orientation) {
    final ViewGroup parent = (ViewGroup) v.getParent();
    final ViewGroup launcher = (ViewGroup) parent.getParent();
    final Workspace workspace = (Workspace) launcher.findViewById(R.id.workspace);
    final int buttonIndex = parent.indexOfChild(v);
    final int buttonCount = parent.getChildCount();
    final int pageIndex = workspace.getCurrentPage();

    // NOTE: currently we don't special case for the phone UI in different
    // orientations, even though the hotseat is on the side in landscape mode.  This
    // is to ensure that accessibility consistency is maintained across rotations.

    final int action = e.getAction();
    final boolean handleKeyEvent = (action != KeyEvent.ACTION_UP);
    boolean wasHandled = false;
    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (handleKeyEvent) {
                // Select the previous button, otherwise snap to the previous page
                if (buttonIndex > 0) {
                    parent.getChildAt(buttonIndex - 1).requestFocus();
                } else {
                    workspace.snapToPage(pageIndex - 1);
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (handleKeyEvent) {
                // Select the next button, otherwise snap to the next page
                if (buttonIndex < (buttonCount - 1)) {
                    parent.getChildAt(buttonIndex + 1).requestFocus();
                } else {
                    workspace.snapToPage(pageIndex + 1);
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            if (handleKeyEvent) {
                // Select the first bubble text view in the current page of the workspace
                final CellLayout layout = (CellLayout) workspace.getChildAt(pageIndex);
                final ShortcutAndWidgetContainer children = layout.getShortcutsAndWidgets();
                final View newIcon = getIconInDirection(layout, children, -1, 1);
                if (newIcon != null) {
                    newIcon.requestFocus();
                } else {
                    workspace.requestFocus();
                }
            }
            wasHandled = true;
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            // Do nothing
            wasHandled = true;
            break;
        default: break;
    }
    return wasHandled;
}
 
Example 15
Source File: GamepadController.java    From crazyflie-android-client with GNU General Public License v2.0 4 votes vote down vote up
public void dealWithKeyEvent(KeyEvent event){
    if (event == null) {
        return;
    }
    switch (event.getAction()) {
    case KeyEvent.ACTION_DOWN:
        if (event.getKeyCode() == mEmergencyBtn){
            //quick solution
            mControls.resetAxisValues();
            mActivity.getPresenter().disconnect();
            Toast.makeText(mActivity, "Emergency Stop", Toast.LENGTH_SHORT).show();
        } else if (event.getKeyCode() == mRollTrimPlusBtn) {
            mControls.increaseTrim(PreferencesActivity.KEY_PREF_ROLLTRIM);
        } else if (event.getKeyCode() == mRollTrimMinusBtn) {
            mControls.decreaseTrim(PreferencesActivity.KEY_PREF_ROLLTRIM);
        } else if (event.getKeyCode() == mPitchTrimPlusBtn) {
            mControls.increaseTrim(PreferencesActivity.KEY_PREF_PITCHTRIM);
        } else if (event.getKeyCode() == mPitchTrimMinusBtn) {
            mControls.decreaseTrim(PreferencesActivity.KEY_PREF_PITCHTRIM);
        } else if (event.getKeyCode() == mAlt1Btn) {
            mActivity.getPresenter().runAltAction(mControls.getAlt1Action());
        } else if (event.getKeyCode() == mAlt2Btn) {
            mActivity.getPresenter().runAltAction(mControls.getAlt2Action());
        } else if (event.getKeyCode() == mHoverBtn) {
            // workaround until BleLink supports param subsystem
            if (mActivity.getPresenter() != null && mActivity.getPresenter().getCrazyflie() != null && mActivity.getPresenter().getCrazyflie().getDriver() instanceof RadioDriver) {
                mHover = true;
                mActivity.getPresenter().enableAltHoldMode(mHover);
            }
        }
        break;
    case KeyEvent.ACTION_UP:
        if(event.getKeyCode() == mHoverBtn) {
            // workaround until BleLink supports param subsystem
            if (mActivity.getPresenter() != null && mActivity.getPresenter().getCrazyflie() != null && mActivity.getPresenter().getCrazyflie().getDriver() instanceof RadioDriver) {
                mHover = false;
                mActivity.getPresenter().enableAltHoldMode(mHover);
            }
        }
        break;
    default:
        break;
    }
}
 
Example 16
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void dispatchVolumeKeyEventLocked(String packageName, int pid, int uid,
        boolean asSystemService, KeyEvent keyEvent, int stream, boolean musicOnly) {
    boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
    boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
    int direction = 0;
    boolean isMute = false;
    switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            direction = AudioManager.ADJUST_RAISE;
            break;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            direction = AudioManager.ADJUST_LOWER;
            break;
        case KeyEvent.KEYCODE_VOLUME_MUTE:
            isMute = true;
            break;
    }
    if (down || up) {
        int flags = AudioManager.FLAG_FROM_KEY;
        if (musicOnly) {
            // This flag is used when the screen is off to only affect active media.
            flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
        } else {
            // These flags are consistent with the home screen
            if (up) {
                flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
            } else {
                flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE;
            }
        }
        if (direction != 0) {
            // If this is action up we want to send a beep for non-music events
            if (up) {
                direction = 0;
            }
            dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
                    direction, flags);
        } else if (isMute) {
            if (down && keyEvent.getRepeatCount() == 0) {
                dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
                        AudioManager.ADJUST_TOGGLE_MUTE, flags);
            }
        }
    }
}
 
Example 17
Source File: DownloadServiceLifecycleSupport.java    From Audinaut with GNU General Public License v3.0 4 votes vote down vote up
private void handleKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() > 0) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                downloadService.fastForward();
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                downloadService.rewind();
                break;
        }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                if (lastPressTime < (System.currentTimeMillis() - 500)) {
                    lastPressTime = System.currentTimeMillis();
                    downloadService.togglePlayPause();
                } else {
                    downloadService.next(true);
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                if (lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
                    lastPressTime = System.currentTimeMillis();
                    downloadService.previous();
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                if (lastPressTime < (System.currentTimeMillis() - DEBOUNCE_TIME)) {
                    lastPressTime = System.currentTimeMillis();
                    downloadService.next();
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_REWIND:
                downloadService.rewind();
                break;
            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                downloadService.fastForward();
                break;
            case KeyEvent.KEYCODE_MEDIA_STOP:
                downloadService.stop();
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                if (downloadService.getPlayerState() != PlayerState.STARTED) {
                    downloadService.start();
                }
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                downloadService.pause();
            default:
                break;
        }
    }
}
 
Example 18
Source File: AbsDokitView.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
/**
 * 执行floatPage create
 *
 * @param context 上下文环境
 */
@SuppressLint("ClickableViewAccessibility")
void performCreate(Context context) {
    try {
        //调用onCreate方法
        onCreate(context);
        if (!isNormalMode()) {
            DokitViewManager.getInstance().addDokitViewAttachedListener(this);
        }
        if (isNormalMode()) {
            mRootView = new DokitFrameLayout(context);
        } else {
            //系统悬浮窗的返回按键监听
            mRootView = new DokitFrameLayout(context) {
                @Override
                public boolean dispatchKeyEvent(KeyEvent event) {
                    if (event.getAction() == KeyEvent.ACTION_UP && shouldDealBackKey()) {
                        //监听返回键
                        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_HOME) {
                            return onBackPressed();
                        }
                    }
                    return super.dispatchKeyEvent(event);
                }
            };
        }
        //添加根布局的layout回调
        addViewTreeObserverListener();


        //调用onCreateView抽象方法
        mChildView = onCreateView(context, mRootView);
        //将子View添加到rootview中
        mRootView.addView(mChildView);
        //设置根布局的手势拦截
        mRootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (getRootView() != null) {
                    return mTouchProxy.onTouchEvent(v, event);
                } else {
                    return false;
                }
            }
        });
        //调用onViewCreated回调
        onViewCreated(mRootView);

        mDokitViewLayoutParams = new DokitViewLayoutParams();
        //分别创建对应的LayoutParams
        if (isNormalMode()) {
            mFrameLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            mFrameLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
            mDokitViewLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        } else {
            mWindowLayoutParams = new WindowManager.LayoutParams();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //android 8.0
                mWindowLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                mWindowLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
            }
            if (!shouldDealBackKey()) {
                //参考:http://www.shirlman.com/tec/20160426/362
                //设置WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE会导致rootview监听不到返回按键的监听失效
                mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                mDokitViewLayoutParams.flags = DokitViewLayoutParams.FLAG_NOT_FOCUSABLE;
            }
            mWindowLayoutParams.format = PixelFormat.TRANSPARENT;
            mWindowLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
            mDokitViewLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
            //动态注册关闭系统弹窗的广播
            IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            context.registerReceiver(mInnerReceiver, intentFilter);
        }
        initDokitViewLayoutParams(mDokitViewLayoutParams);
        if (isNormalMode()) {
            onNormalLayoutParamsCreated(mFrameLayoutParams);
        } else {
            onSystemLayoutParamsCreated(mWindowLayoutParams);
        }
    } catch (Exception e) {
        LogHelper.e(TAG, "e===>" + e.getMessage());
        e.printStackTrace();
    }

}
 
Example 19
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 20
Source File: Instrumentation.java    From droidel 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;
}