Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.telecomm; |
| 18 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
| 24 | import android.media.AudioManager; |
| 25 | import android.media.session.MediaSession; |
| 26 | import android.media.session.MediaSessionManager; |
| 27 | import android.view.KeyEvent; |
| 28 | |
| 29 | /** |
| 30 | * Static class to handle listening to the headset media buttons. |
| 31 | */ |
| 32 | final class HeadsetMediaButton { |
| 33 | |
| 34 | /** |
| 35 | * Broadcast receiver for the ACTION_MEDIA_BUTTON broadcast intent. |
| 36 | * |
| 37 | * This functionality isn't lumped in with the other intents in TelecommBroadcastReceiver |
| 38 | * because we instantiate this as a totally separate BroadcastReceiver instance, since we need |
| 39 | * to manually adjust its IntentFilter's priority (to make sure we get these intents *before* |
| 40 | * the media player.) |
| 41 | */ |
| 42 | private final class MediaButtonBroadcastReceiver extends BroadcastReceiver { |
| 43 | @Override |
| 44 | public void onReceive(Context context, Intent intent) { |
| 45 | KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); |
| 46 | Log.v(this, "MediaButtonBroadcastReceiver.onReceive()... event = %s.", event); |
| 47 | if ((event != null) && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) { |
| 48 | boolean consumed = handleHeadsetHook(event); |
| 49 | Log.v(this, "==> handleHeadsetHook(): consumed = %b.", consumed); |
| 50 | if (consumed) { |
| 51 | abortBroadcast(); |
| 52 | } |
| 53 | } else { |
| 54 | if (CallsManager.getInstance().hasAnyCalls()) { |
| 55 | // If the phone is anything other than completely idle, then we consume and |
| 56 | // ignore any media key events, otherwise it is too easy to accidentally start |
| 57 | // playing music while a phone call is in progress. |
| 58 | Log.v(this, "MediaButtonBroadcastReceiver: consumed"); |
| 59 | abortBroadcast(); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Types of media button presses |
| 66 | static final int SHORT_PRESS = 1; |
| 67 | static final int LONG_PRESS = 2; |
| 68 | |
| 69 | private final MediaSession.Callback mSessionCallback = new MediaSession.Callback() { |
| 70 | @Override |
| 71 | public void onMediaButtonEvent(Intent intent) { |
| 72 | KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); |
| 73 | Log.v(this, "SessionCallback.onMediaButton()... event = %s.", event); |
| 74 | if ((event != null) && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) { |
| 75 | Log.v(this, "SessionCallback: HEADSETHOOK"); |
| 76 | boolean consumed = handleHeadsetHook(event); |
| 77 | Log.v(this, "==> handleHeadsetHook(): consumed = %b.", consumed); |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | private final MediaButtonBroadcastReceiver mMediaButtonReceiver = |
| 83 | new MediaButtonBroadcastReceiver(); |
| 84 | |
| 85 | private final CallsManager mCallsManager; |
| 86 | |
| 87 | private final MediaSession mSession; |
| 88 | |
| 89 | HeadsetMediaButton(Context context, CallsManager callsManager) { |
| 90 | mCallsManager = callsManager; |
| 91 | |
| 92 | // Use a separate receiver (from TelecommBroadcastReceiver) for ACTION_MEDIA_BUTTON |
| 93 | // broadcasts, since we need to manually adjust its priority (to make sure we get these |
| 94 | // intents *before* the media player.) |
| 95 | IntentFilter mediaButtonIntentFilter = |
| 96 | new IntentFilter(Intent.ACTION_MEDIA_BUTTON); |
| 97 | |
| 98 | // Make sure we're higher priority than the media player's MediaButtonIntentReceiver (which |
| 99 | // currently has the default priority of zero; see apps/Music/AndroidManifest.xml.) |
| 100 | mediaButtonIntentFilter.setPriority(1); |
| 101 | |
| 102 | context.registerReceiver(mMediaButtonReceiver, mediaButtonIntentFilter); |
| 103 | |
| 104 | // register the component so it gets priority for calls |
| 105 | AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 106 | am.registerMediaButtonEventReceiverForCalls(new ComponentName(context.getPackageName(), |
| 107 | MediaButtonBroadcastReceiver.class.getName())); |
| 108 | |
| 109 | // Register a MediaSession but don't enable it yet. This is a |
| 110 | // replacement for MediaButtonReceiver |
| 111 | MediaSessionManager msm = |
| 112 | (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); |
| 113 | mSession = msm.createSession(HeadsetMediaButton.class.getSimpleName()); |
| 114 | mSession.addCallback(mSessionCallback); |
| 115 | mSession.setFlags(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY |
| 116 | | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Handles the wired headset button while in-call. |
| 121 | * |
| 122 | * @return true if we consumed the event. |
| 123 | */ |
| 124 | private boolean handleHeadsetHook(KeyEvent event) { |
| 125 | Log.d(this, "handleHeadsetHook()...%s %s", event.getAction(), event.getRepeatCount()); |
| 126 | |
| 127 | if (event.isLongPress()) { |
| 128 | return mCallsManager.onMediaButton(LONG_PRESS); |
| 129 | } else if (event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) { |
| 130 | return mCallsManager.onMediaButton(SHORT_PRESS); |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | } |