Handle media keys in PlaybackActivities

Avoids the overhead of the broadcast when the activity has focus
This commit is contained in:
Christopher Eby 2011-08-23 22:04:35 -05:00
parent e5d3053a44
commit a8bf7536ac
2 changed files with 45 additions and 9 deletions

View File

@ -139,15 +139,10 @@ public class MediaButtonHandler implements Handler.Callback {
} }
/** /**
* Process a MediaButton broadcast. * Process a media button key press.
*
* @param intent The intent that was broadcast
* @return True if the intent was handled and the broadcast should be
* aborted.
*/ */
public boolean process(Intent intent) public boolean processKey(KeyEvent event)
{ {
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null || isInCall() || !useHeadsetControls()) if (event == null || isInCall() || !useHeadsetControls())
return false; return false;
@ -184,6 +179,19 @@ public class MediaButtonHandler implements Handler.Callback {
return true; return true;
} }
/**
* Process a MediaButton broadcast.
*
* @param intent The intent that was broadcast
* @return True if the intent was handled and the broadcast should be
* aborted.
*/
public boolean process(Intent intent)
{
KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
return processKey(event);
}
/** /**
* A delayed message that performs the single press action after the double * A delayed message that performs the single press action after the double
* click period has expired. * click period has expired.

View File

@ -93,6 +93,34 @@ public class PlaybackActivity extends Activity implements Handler.Callback, View
return false; return false;
} }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode) {
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_MEDIA_NEXT:
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
return MediaButtonHandler.getInstance().processKey(event);
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch (keyCode) {
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_MEDIA_NEXT:
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
return MediaButtonHandler.getInstance().processKey(event);
}
return super.onKeyUp(keyCode, event);
}
@Override @Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) public boolean onKeyLongPress(int keyCode, KeyEvent event)
{ {