If you think the Android project PlayerHater listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*******************************************************************************
* Copyright 2013 Chris Rhoden, Rebecca Nesson, Public Radio Exchange
* //www.java2s.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/package org.prx.playerhater.broadcast;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
class HeadphoneButtonGestureHelper {
privatestaticfinalint BUTTON_PRESSED = 0;
privatestaticfinalint PLAY_PAUSE = 1;
privatestaticfinalint NEXT = 2;
privatestaticfinalint PREV = 3;
privatestaticfinalint MILISECONDS_DELAY = 250;
publicstaticfinal String TAG = "GESTURES";
privatelong mLastEventTime = 0;
privateint mCurrentAction = 1;
privatestatic Context lastContext;
privatefinal Handler mHandler = new ButtonHandler(this);
private RemoteControlButtonReceiver mMediaButtonReceiver;
publicvoid onHeadsetButtonPressed(long eventTime, Context context) {
if (eventTime - mLastEventTime <= MILISECONDS_DELAY + 100) {
mCurrentAction += 1;
if (mCurrentAction > 3) {
mCurrentAction = 1;
}
mHandler.removeMessages(BUTTON_PRESSED);
}
lastContext = context;
mLastEventTime = eventTime;
mHandler.sendEmptyMessageDelayed(BUTTON_PRESSED, MILISECONDS_DELAY);
}
publicvoid setReceiver(RemoteControlButtonReceiver receiver) {
mMediaButtonReceiver = receiver;
}
privatestaticclass ButtonHandler extends Handler {
privatefinal HeadphoneButtonGestureHelper mButtonGestureHelper;
private ButtonHandler(HeadphoneButtonGestureHelper ctx) {
mButtonGestureHelper = ctx;
}
@Override
publicvoid dispatchMessage(Message message) {
switch (mButtonGestureHelper.mCurrentAction) {
case PLAY_PAUSE:
mButtonGestureHelper.mMediaButtonReceiver
.onRemoteControlButtonPressed(
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, lastContext);
break;
case NEXT:
mButtonGestureHelper.mMediaButtonReceiver
.onRemoteControlButtonPressed(
KeyEvent.KEYCODE_MEDIA_NEXT, lastContext);
break;
case PREV:
mButtonGestureHelper.mMediaButtonReceiver
.onRemoteControlButtonPressed(
KeyEvent.KEYCODE_MEDIA_PREVIOUS, lastContext);
break;
}
mButtonGestureHelper.mLastEventTime = 0;
mButtonGestureHelper.mCurrentAction = 1;
}
}
}