Java Code Examples for android.view.KeyEvent#getEventTime()

The following examples show how to use android.view.KeyEvent#getEventTime() . 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: DoubleVolumeButtonThreeShortPressPatternMatcher.java    From talkback with Apache License 2.0 6 votes vote down vote up
private boolean isDownEventInterruptsActionSequence(KeyEvent keyEvent) {
  if (mDoubleButtonPresses != 0
      && keyEvent.getEventTime() - mLastDoubleButtonPressEndTimestamp > MULTIPLE_TAP_TIMEOUT) {
    // too long timeout between previous double volume buttons pushes
    return true;
  }

  VolumeButtonAction otherAction = getOtherAction(keyEvent);
  //noinspection RedundantIfStatement
  if (otherAction != null && !otherAction.pressed) {
    // other button was released before current action pressed
    return true;
  }

  return false;
}
 
Example 2
Source File: MainUI.java    From Busybox-Installer-No-Root with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){

    DrawerLayout drawer = findViewById(R.id.drawer_layout);

    if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
        if(drawer.isDrawerOpen(GravityCompat.START)){
            switch(event.getAction()){
                case KeyEvent.ACTION_DOWN:
                    if(event.getDownTime() - lastPressedTime < PERIOD){
                        finish();
                    }else{
                        Toast.makeText(context, R.string.press_again_to_exit, Toast.LENGTH_SHORT).show();
                        lastPressedTime = event.getEventTime();
                    }
                    return true;
            }
        }else if(!drawer.isDrawerOpen(GravityCompat.START)){
            drawer.openDrawer(GravityCompat.START);
        }
    }
    return false;
}
 
Example 3
Source File: HListView.java    From TV-HorizontalListView with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    boolean handled = false;

    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_NUMPAD_ENTER:
        case KeyEvent.KEYCODE_ENTER:
            mLastLongPress = event.getEventTime();
            performItemLongClick(getSelectedView(), getSelectedItemPosition(), getSelectedItemId());
            handled = true;
            break;
    }

    return handled || super.onKeyLongPress(keyCode, event);
}
 
Example 4
Source File: VolumeKeyListener.java    From Hentoid with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_DOWN) return false;

    Runnable listener;
    if (isVolumeKey(keyCode, KeyEvent.KEYCODE_VOLUME_DOWN)) {
        listener = onVolumeDownListener;
    } else if (isVolumeKey(keyCode, KeyEvent.KEYCODE_VOLUME_UP)) {
        listener = onVolumeUpListener;
    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
        listener = onBackListener;
    } else {
        return false;
    }

    if (event.getRepeatCount() == 0) {
        listener.run();
        nextNotifyTime = event.getEventTime() + cooldown;
    } else if (event.getEventTime() >= nextNotifyTime) {
        listener.run();
        nextNotifyTime = event.getEventTime() + (isTurboEnabled ? turboCooldown : cooldown);
    }
    return true;
}
 
Example 5
Source File: Performance.java    From talkback with Apache License 2.0 6 votes vote down vote up
/**
 * Method to start tracking processing latency for a key event.
 *
 * @param event A key event just received by TalkBack
 * @return An event id that can be used to track performance through later stages.
 */
public EventId onEventReceived(@NonNull KeyEvent event) {
  int keycode = event.getKeyCode();
  EventId eventId = new EventId(event.getEventTime(), EVENT_TYPE_KEY, keycode);
  if (!mEnabled) {
    return eventId;
  }

  // Segment key events based on key groups.
  String label = "KeyEvent-other";
  if (KeyEvent.KEYCODE_0 <= keycode && keycode <= KeyEvent.KEYCODE_9) {
    label = "KeyEvent-numeric";
  } else if (KeyEvent.KEYCODE_A <= keycode && keycode <= KeyEvent.KEYCODE_Z) {
    label = "KeyEvent-alpha";
  } else if (KeyEvent.KEYCODE_DPAD_UP <= keycode && keycode <= KeyEvent.KEYCODE_DPAD_CENTER) {
    label = "KeyEvent-dpad";
  } else if (KeyEvent.KEYCODE_VOLUME_UP <= keycode && keycode <= KeyEvent.KEYCODE_VOLUME_DOWN) {
    label = "KeyEvent-volume";
  }
  String[] labels = {label};

  onEventReceived(eventId, labels);
  return eventId;
}
 
Example 6
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 7
Source File: Instrumentation.java    From droidel with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 8
Source File: CordovaWebView.java    From phonegap-plugin-loading-spinner with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
    // If back key
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // A custom view is currently displayed  (e.g. playing a video)
        if(mCustomView != null) {
            this.hideCustomView();
        } else {
            // The webview is currently displayed
            // If back key is bound, then send event to JavaScript
            if (this.bound) {
                this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
                return true;
            } else {
                // If not bound
                // Go to previous page in webview if it is possible to go back
                if (this.backHistory()) {
                    return true;
                }
                // If not, then invoke default behaviour
                else {
                    //this.activityState = ACTIVITY_EXITING;
                	//return false;
                	// If they hit back button when app is initializing, app should exit instead of hang until initilazation (CB2-458)
                	this.cordova.getActivity().finish();
                }
            }
        }
    }
    // Legacy
    else if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (this.lastMenuEventTime < event.getEventTime()) {
            this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
        }
        this.lastMenuEventTime = event.getEventTime();
        return super.onKeyUp(keyCode, event);
    }
    // If search key
    else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
        return true;
    }
    else if(keyUpCodes.contains(keyCode))
    {
        //What the hell should this do?
        return super.onKeyUp(keyCode, event);
    }

    //Does webkit change this behavior?
    return super.onKeyUp(keyCode, event);
}
 
Example 9
Source File: MainUI.java    From AnLinux-App with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){

    DrawerLayout drawer = findViewById(R.id.drawer_layout);

    Fragment fragment = this.getFragmentManager().findFragmentById(R.id.fragmentHolder);
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

    if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
        if(fragment instanceof DashBoard){
            if(drawer.isDrawerOpen(GravityCompat.START)){
                switch(event.getAction()){
                    case KeyEvent.ACTION_DOWN:
                        if(event.getDownTime() - lastPressedTime < PERIOD){
                            finish();
                        }else{
                            Toast.makeText(context, R.string.press_again_to_exit, Toast.LENGTH_SHORT).show();
                            lastPressedTime = event.getEventTime();
                        }
                        return true;
                }
            }else if(!drawer.isDrawerOpen(GravityCompat.START)){
                drawer.openDrawer(GravityCompat.START);
            }
        }else if(fragment instanceof About){
            fragment = new DashBoard();
            fragmentTransaction.replace(R.id.fragmentHolder, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }else if(fragment instanceof DesktopEnvironment){
            fragment = new DashBoard();
            fragmentTransaction.replace(R.id.fragmentHolder, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }else if(fragment instanceof WindowManager){
            fragment = new DashBoard();
            fragmentTransaction.replace(R.id.fragmentHolder, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }else if(fragment instanceof SSH){
            fragment = new DashBoard();
            fragmentTransaction.replace(R.id.fragmentHolder, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }else if(fragment instanceof Uninstaller){
            fragment = new DashBoard();
            fragmentTransaction.replace(R.id.fragmentHolder, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }else if(fragment instanceof Patches){
            fragment = new DashBoard();
            fragmentTransaction.replace(R.id.fragmentHolder, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }else if(fragment instanceof SU){
            fragment = new DashBoard();
            fragmentTransaction.replace(R.id.fragmentHolder, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }else if(fragment instanceof RootfsDownload){
            fragment = new DashBoard();
            fragmentTransaction.replace(R.id.fragmentHolder, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    }
    return false;
}
 
Example 10
Source File: CordovaWebView.java    From wildfly-samples with MIT License 4 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
    // If back key
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // A custom view is currently displayed  (e.g. playing a video)
        if(mCustomView != null) {
            this.hideCustomView();
        } else {
            // The webview is currently displayed
            // If back key is bound, then send event to JavaScript
            if (this.bound) {
                this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
                return true;
            } else {
                // If not bound
                // Go to previous page in webview if it is possible to go back
                if (this.backHistory()) {
                    return true;
                }
                // If not, then invoke default behavior
                else {
                    //this.activityState = ACTIVITY_EXITING;
                	//return false;
                	// If they hit back button when app is initializing, app should exit instead of hang until initialization (CB2-458)
                	this.cordova.getActivity().finish();
                }
            }
        }
    }
    // Legacy
    else if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (this.lastMenuEventTime < event.getEventTime()) {
            this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
        }
        this.lastMenuEventTime = event.getEventTime();
        return super.onKeyUp(keyCode, event);
    }
    // If search key
    else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
        return true;
    }
    else if(keyUpCodes.contains(keyCode))
    {
        //What the hell should this do?
        return super.onKeyUp(keyCode, event);
    }

    //Does webkit change this behavior?
    return super.onKeyUp(keyCode, event);
}
 
Example 11
Source File: MediaButtonIntentReceiver.java    From MusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventTime = event.getEventTime() != 0 ?
                event.getEventTime() : System.currentTimeMillis();
        // Fallback to system time if event time was not available.

        String command = null;
        switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                command = MusicService.ACTION_STOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                command = MusicService.ACTION_TOGGLE_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                command = MusicService.ACTION_SKIP;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                command = MusicService.ACTION_REWIND;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                command = MusicService.ACTION_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                command = MusicService.ACTION_PLAY;
                break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK || keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                        if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(
                                MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventTime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 12
Source File: MediaButtonIntentReceiver.java    From AudioAnchor with GNU General Public License v3.0 4 votes vote down vote up
public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        // Fallback to system time if event time was not available.
        final long eventTime = event.getEventTime() != 0 ? event.getEventTime() : System.currentTimeMillis();

        String command = null;
        switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                command = MediaPlayerService.ACTION_STOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                command = MediaPlayerService.ACTION_TOGGLE_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                command = MediaPlayerService.ACTION_FORWARD;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                command = MediaPlayerService.ACTION_BACKWARD;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                command = MediaPlayerService.ACTION_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                command = MediaPlayerService.ACTION_PLAY;
                break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK || keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                        if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventTime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 13
Source File: DoublePlayPauseButtonShortPressPatternMatcher.java    From talkback with Apache License 2.0 4 votes vote down vote up
private boolean isDownEventInterruptsActionSequence(KeyEvent keyEvent) {
  // Too long timeout between previous buttons pushes.
  return buttonPresses != 0
      && keyEvent.getEventTime() - lastButtonPressEndTimestamp > MULTIPLE_TAP_TIMEOUT;
}
 
Example 14
Source File: MediaButtonIntentReceiver.java    From RetroMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventTime = event.getEventTime();

        String command = null;
        switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                command = ACTION_STOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                command = ACTION_TOGGLE_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                command = ACTION_SKIP;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                command = ACTION_REWIND;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                command = ACTION_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                command = ACTION_PLAY;
                break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(
                                MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventTime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 15
Source File: SingleVolumeButtonPressPatternMatcher.java    From talkback with Apache License 2.0 4 votes vote down vote up
private void handleActionUpEvent(KeyEvent event) {
  if (mAction != null) {
    mAction.pressed = false;
    mAction.endTimestamp = event.getEventTime();
  }
}
 
Example 16
Source File: CordovaWebView.java    From cordova-android-chromeview with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
    // If back key
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // A custom view is currently displayed  (e.g. playing a video)
        if(mCustomView != null) {
            this.hideCustomView();
        } else {
            // The webview is currently displayed
            // If back key is bound, then send event to JavaScript
            if (this.bound) {
                this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
                return true;
            } else {
                // If not bound
                // Go to previous page in webview if it is possible to go back
                if (this.backHistory()) {
                    return true;
                }
                // If not, then invoke default behaviour
                else {
                    //this.activityState = ACTIVITY_EXITING;
                	//return false;
                	// If they hit back button when app is initializing, app should exit instead of hang until initilazation (CB2-458)
                	this.cordova.getActivity().finish();
                }
            }
        }
    }
    // Legacy
    else if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (this.lastMenuEventTime < event.getEventTime()) {
            this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
        }
        this.lastMenuEventTime = event.getEventTime();
        return super.onKeyUp(keyCode, event);
    }
    // If search key
    else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
        return true;
    }
    else if(keyUpCodes.contains(keyCode))
    {
        //What the hell should this do?
        return super.onKeyUp(keyCode, event);
    }

    //Does webkit change this behavior?
    return super.onKeyUp(keyCode, event);
}
 
Example 17
Source File: MediaButtonIntentReceiver.java    From Muzesto with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
        if (PreferencesUtility.getInstance(context).pauseEnabledOnDetach())
            startService(context, MusicService.CMDPAUSE);
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventtime = event.getEventTime();

        String command = null;
        switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                command = MusicService.CMDSTOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                command = MusicService.CMDTOGGLEPAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                command = MusicService.CMDNEXT;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                command = MusicService.CMDPREVIOUS;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                command = MusicService.CMDPAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                command = MusicService.CMDPLAY;
                break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (mDown) {
                    if (MusicService.CMDTOGGLEPAUSE.equals(command)
                            || MusicService.CMDPLAY.equals(command)) {
                        if (mLastClickTime != 0
                                && eventtime - mLastClickTime > LONG_PRESS_DELAY) {
                            acquireWakeLockAndSendMessage(context,
                                    mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0);
                        }
                    }
                } else if (event.getRepeatCount() == 0) {

                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        if (eventtime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(
                                MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventtime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    mLaunched = false;
                    mDown = true;
                }
            } else {
                mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
                mDown = false;
            }
            if (isOrderedBroadcast()) {
                abortBroadcast();
            }
            releaseWakeLockIfHandlerIdle();
        }
    }
}
 
Example 18
Source File: CordovaWebView.java    From L.TileLayer.Cordova with MIT License 4 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
    // If back key
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // A custom view is currently displayed  (e.g. playing a video)
        if(mCustomView != null) {
            this.hideCustomView();
            return true;
        } else {
            // The webview is currently displayed
            // If back key is bound, then send event to JavaScript
            if (isButtonPlumbedToJs(KeyEvent.KEYCODE_BACK)) {
                sendJavascriptEvent("backbutton");
                return true;
            } else {
                // If not bound
                // Go to previous page in webview if it is possible to go back
                if (this.backHistory()) {
                    return true;
                }
                // If not, then invoke default behavior
            }
        }
    }
    // Legacy
    else if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (this.lastMenuEventTime < event.getEventTime()) {
            sendJavascriptEvent("menubutton");
        }
        this.lastMenuEventTime = event.getEventTime();
        return super.onKeyUp(keyCode, event);
    }
    // If search key
    else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        sendJavascriptEvent("searchbutton");
        return true;
    }

    //Does webkit change this behavior?
    return super.onKeyUp(keyCode, event);
}
 
Example 19
Source File: MediaButtonIntentReceiver.java    From Phonograph with GNU General Public License v3.0 4 votes vote down vote up
public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventTime = event.getEventTime() != 0 ?
                event.getEventTime() : System.currentTimeMillis();
        // Fallback to system time if event time was not available.

        String command = null;
        switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                command = MusicService.ACTION_STOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                command = MusicService.ACTION_TOGGLE_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                command = MusicService.ACTION_SKIP;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                command = MusicService.ACTION_REWIND;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                command = MusicService.ACTION_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                command = MusicService.ACTION_PLAY;
                break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK || keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                        if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(
                                MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventTime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 20
Source File: DoublePlayPauseButtonShortPressPatternMatcher.java    From talkback with Apache License 2.0 4 votes vote down vote up
private void handleActionUpEvent(KeyEvent event) {
  if (lastAction != null) {
    lastAction.pressed = false;
    lastAction.endTimestamp = event.getEventTime();
  }
}