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