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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 18 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
RoboErik | 07920f6 | 2014-07-21 15:19:41 -0700 | [diff] [blame] | 21 | import android.media.AudioAttributes; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 22 | import android.media.session.MediaSession; |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Looper; |
| 25 | import android.os.Message; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 26 | import android.telecom.Log; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 27 | import android.view.KeyEvent; |
| 28 | |
| 29 | /** |
| 30 | * Static class to handle listening to the headset media buttons. |
| 31 | */ |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 32 | public class HeadsetMediaButton extends CallsManagerListenerBase { |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 33 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 34 | // Types of media button presses |
| 35 | static final int SHORT_PRESS = 1; |
| 36 | static final int LONG_PRESS = 2; |
| 37 | |
RoboErik | 07920f6 | 2014-07-21 15:19:41 -0700 | [diff] [blame] | 38 | private static final AudioAttributes AUDIO_ATTRIBUTES = new AudioAttributes.Builder() |
| 39 | .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) |
| 40 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION).build(); |
| 41 | |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 42 | private static final int MSG_MEDIA_SESSION_INITIALIZE = 0; |
| 43 | private static final int MSG_MEDIA_SESSION_SET_ACTIVE = 1; |
| 44 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 45 | private final MediaSession.Callback mSessionCallback = new MediaSession.Callback() { |
| 46 | @Override |
RoboErik | a6e9d75 | 2014-08-07 16:10:08 -0700 | [diff] [blame] | 47 | public boolean onMediaButtonEvent(Intent intent) { |
Hall Liu | a148324 | 2017-11-09 14:27:49 -0800 | [diff] [blame^] | 48 | try { |
| 49 | Log.startSession("HMB.oMBE"); |
| 50 | KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); |
| 51 | Log.v(this, "SessionCallback.onMediaButton()... event = %s.", event); |
| 52 | if ((event != null) && ((event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK) || |
| 53 | (event.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE))) { |
| 54 | synchronized (mLock) { |
| 55 | Log.v(this, "SessionCallback: HEADSETHOOK/MEDIA_PLAY_PAUSE"); |
| 56 | boolean consumed = handleCallMediaButton(event); |
| 57 | Log.v(this, "==> handleCallMediaButton(): consumed = %b.", consumed); |
| 58 | return consumed; |
| 59 | } |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 60 | } |
Hall Liu | a148324 | 2017-11-09 14:27:49 -0800 | [diff] [blame^] | 61 | return true; |
| 62 | } finally { |
| 63 | Log.endSession(); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | }; |
| 67 | |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 68 | private final Handler mMediaSessionHandler = new Handler(Looper.getMainLooper()) { |
| 69 | @Override |
| 70 | public void handleMessage(Message msg) { |
| 71 | switch (msg.what) { |
| 72 | case MSG_MEDIA_SESSION_INITIALIZE: { |
| 73 | MediaSession session = new MediaSession( |
| 74 | mContext, |
| 75 | HeadsetMediaButton.class.getSimpleName()); |
| 76 | session.setCallback(mSessionCallback); |
| 77 | session.setFlags(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY |
| 78 | | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS); |
| 79 | session.setPlaybackToLocal(AUDIO_ATTRIBUTES); |
| 80 | mSession = session; |
| 81 | break; |
| 82 | } |
| 83 | case MSG_MEDIA_SESSION_SET_ACTIVE: { |
| 84 | if (mSession != null) { |
| 85 | boolean activate = msg.arg1 != 0; |
| 86 | if (activate != mSession.isActive()) { |
| 87 | mSession.setActive(activate); |
| 88 | } |
| 89 | } |
| 90 | break; |
| 91 | } |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | private final Context mContext; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 99 | private final CallsManager mCallsManager; |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 100 | private final TelecomSystem.SyncRoot mLock; |
| 101 | private MediaSession mSession; |
yifan.bai | c73ca8d | 2016-04-21 16:19:12 +0800 | [diff] [blame] | 102 | private KeyEvent mLastHookEvent; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 103 | |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 104 | public HeadsetMediaButton( |
| 105 | Context context, |
| 106 | CallsManager callsManager, |
| 107 | TelecomSystem.SyncRoot lock) { |
| 108 | mContext = context; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 109 | mCallsManager = callsManager; |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 110 | mLock = lock; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 111 | |
RoboErik | b771c43 | 2014-07-15 12:44:33 -0700 | [diff] [blame] | 112 | // Create a MediaSession but don't enable it yet. This is a |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 113 | // replacement for MediaButtonReceiver |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 114 | mMediaSessionHandler.obtainMessage(MSG_MEDIA_SESSION_INITIALIZE).sendToTarget(); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Handles the wired headset button while in-call. |
| 119 | * |
| 120 | * @return true if we consumed the event. |
| 121 | */ |
Andrew Chant | 0415220 | 2017-05-08 10:28:13 -0700 | [diff] [blame] | 122 | private boolean handleCallMediaButton(KeyEvent event) { |
| 123 | Log.d(this, "handleCallMediaButton()...%s %s", event.getAction(), event.getRepeatCount()); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 124 | |
yifan.bai | c73ca8d | 2016-04-21 16:19:12 +0800 | [diff] [blame] | 125 | // Save ACTION_DOWN Event temporarily. |
| 126 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 127 | mLastHookEvent = event; |
| 128 | } |
| 129 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 130 | if (event.isLongPress()) { |
| 131 | return mCallsManager.onMediaButton(LONG_PRESS); |
yifan.bai | c73ca8d | 2016-04-21 16:19:12 +0800 | [diff] [blame] | 132 | } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 133 | // We should not judge SHORT_PRESS by ACTION_UP event repeatCount, because it always |
| 134 | // return 0. |
| 135 | // Actually ACTION_DOWN event repeatCount only increases when LONG_PRESS performed. |
| 136 | if (mLastHookEvent != null && mLastHookEvent.getRepeatCount() == 0) { |
| 137 | return mCallsManager.onMediaButton(SHORT_PRESS); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (event.getAction() != KeyEvent.ACTION_DOWN) { |
| 142 | mLastHookEvent = null; |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 147 | |
| 148 | /** ${inheritDoc} */ |
| 149 | @Override |
| 150 | public void onCallAdded(Call call) { |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 151 | if (call.isExternalCall()) { |
| 152 | return; |
| 153 | } |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 154 | mMediaSessionHandler.obtainMessage(MSG_MEDIA_SESSION_SET_ACTIVE, 1, 0).sendToTarget(); |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** ${inheritDoc} */ |
| 158 | @Override |
| 159 | public void onCallRemoved(Call call) { |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 160 | if (call.isExternalCall()) { |
| 161 | return; |
| 162 | } |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 163 | if (!mCallsManager.hasAnyCalls()) { |
Ihab Awad | 731369c | 2015-05-19 09:23:21 -0700 | [diff] [blame] | 164 | mMediaSessionHandler.obtainMessage(MSG_MEDIA_SESSION_SET_ACTIVE, 0, 0).sendToTarget(); |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 167 | |
| 168 | /** ${inheritDoc} */ |
| 169 | @Override |
| 170 | public void onExternalCallChanged(Call call, boolean isExternalCall) { |
| 171 | if (isExternalCall) { |
| 172 | onCallRemoved(call); |
| 173 | } else { |
| 174 | onCallAdded(call); |
| 175 | } |
| 176 | } |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 177 | } |