Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
[Android WebView] Propagate media keys to the AudioManager when WebVi…
Browse files Browse the repository at this point in the history
…ew is active.

Propagate media keys to the android AudioManager when a Webview
is active. This is to fix the problem of the start/stop/next etc.
media keys not working on a bluetooth headset when a WebView is
in focus, in particular when ads are shown.

[email protected]
BUG=479786
NOTRY=true
NOPRESUBMIT=true

Review URL: https://codereview.chromium.org/1099903005

Cr-Commit-Position: refs/heads/master@{#326591}
(cherry picked from commit 741d61b)

Review URL: https://codereview.chromium.org/1099343003

Cr-Commit-Position: refs/branch-heads/2357@{#222}
Cr-Branched-From: 59d4494-refs/heads/master@{#323860}
  • Loading branch information
timvolodine authored and Commit bot committed Apr 24, 2015
1 parent be0cbcb commit 6975112
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
Expand Down Expand Up @@ -78,9 +79,38 @@ public void handleKeyboardEvent(KeyEvent event) {
}
if (direction != 0 && tryToMoveFocus(direction)) return;
}
handleMediaKey(event);
mContentsClient.onUnhandledKeyEvent(event);
}

/**
* Redispatches unhandled media keys. This allows bluetooth headphones with play/pause or
* other buttons to function correctly.
*/
private void handleMediaKey(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.KEYCODE_MUTE:
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY:
case KeyEvent.KEYCODE_MEDIA_PAUSE:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_MEDIA_STOP:
case KeyEvent.KEYCODE_MEDIA_NEXT:
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
case KeyEvent.KEYCODE_MEDIA_REWIND:
case KeyEvent.KEYCODE_MEDIA_RECORD:
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
case KeyEvent.KEYCODE_MEDIA_CLOSE:
case KeyEvent.KEYCODE_MEDIA_EJECT:
case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
am.dispatchMediaKeyEvent(e);
break;
default:
break;
}
}

@SuppressLint("NewApi") // View#getLayoutDirection requires API level 17.
@Override
public boolean takeFocus(boolean reverse) {
Expand Down

0 comments on commit 6975112

Please sign in to comment.