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