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 | */ |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 32 | final 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 | |
| 38 | private final MediaSession.Callback mSessionCallback = new MediaSession.Callback() { |
| 39 | @Override |
| 40 | public void onMediaButtonEvent(Intent intent) { |
| 41 | KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); |
| 42 | Log.v(this, "SessionCallback.onMediaButton()... event = %s.", event); |
| 43 | if ((event != null) && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) { |
| 44 | Log.v(this, "SessionCallback: HEADSETHOOK"); |
| 45 | boolean consumed = handleHeadsetHook(event); |
| 46 | Log.v(this, "==> handleHeadsetHook(): consumed = %b.", consumed); |
| 47 | } |
| 48 | } |
| 49 | }; |
| 50 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 51 | private final CallsManager mCallsManager; |
| 52 | |
| 53 | private final MediaSession mSession; |
| 54 | |
| 55 | HeadsetMediaButton(Context context, CallsManager callsManager) { |
| 56 | mCallsManager = callsManager; |
| 57 | |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 58 | // Register a MediaSession but don't enable it yet. This is a |
| 59 | // replacement for MediaButtonReceiver |
| 60 | MediaSessionManager msm = |
| 61 | (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); |
| 62 | mSession = msm.createSession(HeadsetMediaButton.class.getSimpleName()); |
| 63 | mSession.addCallback(mSessionCallback); |
| 64 | mSession.setFlags(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY |
| 65 | | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS); |
RoboErik | 82612ad | 2014-06-02 10:48:51 -0700 | [diff] [blame] | 66 | mSession.setPlaybackToLocal(AudioManager.STREAM_VOICE_CALL); |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Handles the wired headset button while in-call. |
| 71 | * |
| 72 | * @return true if we consumed the event. |
| 73 | */ |
| 74 | private boolean handleHeadsetHook(KeyEvent event) { |
| 75 | Log.d(this, "handleHeadsetHook()...%s %s", event.getAction(), event.getRepeatCount()); |
| 76 | |
| 77 | if (event.isLongPress()) { |
| 78 | return mCallsManager.onMediaButton(LONG_PRESS); |
| 79 | } else if (event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) { |
| 80 | return mCallsManager.onMediaButton(SHORT_PRESS); |
| 81 | } |
| 82 | |
| 83 | return true; |
| 84 | } |
Santos Cordon | 8128998 | 2014-06-03 16:03:08 -0700 | [diff] [blame] | 85 | |
| 86 | /** ${inheritDoc} */ |
| 87 | @Override |
| 88 | public void onCallAdded(Call call) { |
| 89 | if (!mSession.isActive()) { |
| 90 | mSession.setActive(true); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** ${inheritDoc} */ |
| 95 | @Override |
| 96 | public void onCallRemoved(Call call) { |
| 97 | if (!mCallsManager.hasAnyCalls()) { |
| 98 | if (mSession.isActive()) { |
| 99 | mSession.setActive(false); |
| 100 | } |
| 101 | } |
| 102 | } |
Santos Cordon | deb8c89 | 2014-05-30 01:38:03 -0700 | [diff] [blame] | 103 | } |