blob: cca89b9b178ace6eca6488a76058981411ca563b [file] [log] [blame]
Santos Cordondeb8c892014-05-30 01:38:03 -07001/*
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
17package com.android.telecomm;
18
Santos Cordondeb8c892014-05-30 01:38:03 -070019import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
RoboErik07920f62014-07-21 15:19:41 -070023import android.media.AudioAttributes;
Santos Cordondeb8c892014-05-30 01:38:03 -070024import android.media.AudioManager;
25import android.media.session.MediaSession;
26import android.media.session.MediaSessionManager;
27import android.view.KeyEvent;
28
29/**
30 * Static class to handle listening to the headset media buttons.
31 */
Santos Cordon81289982014-06-03 16:03:08 -070032final class HeadsetMediaButton extends CallsManagerListenerBase {
Santos Cordondeb8c892014-05-30 01:38:03 -070033
Santos Cordondeb8c892014-05-30 01:38:03 -070034 // Types of media button presses
35 static final int SHORT_PRESS = 1;
36 static final int LONG_PRESS = 2;
37
RoboErik07920f62014-07-21 15:19:41 -070038 private static final AudioAttributes AUDIO_ATTRIBUTES = new AudioAttributes.Builder()
39 .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
40 .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION).build();
41
Santos Cordondeb8c892014-05-30 01:38:03 -070042 private final MediaSession.Callback mSessionCallback = new MediaSession.Callback() {
43 @Override
44 public void onMediaButtonEvent(Intent intent) {
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);
51 }
52 }
53 };
54
Santos Cordondeb8c892014-05-30 01:38:03 -070055 private final CallsManager mCallsManager;
56
57 private final MediaSession mSession;
58
59 HeadsetMediaButton(Context context, CallsManager callsManager) {
60 mCallsManager = callsManager;
61
RoboErikb771c432014-07-15 12:44:33 -070062 // Create a MediaSession but don't enable it yet. This is a
Santos Cordondeb8c892014-05-30 01:38:03 -070063 // replacement for MediaButtonReceiver
RoboErikb771c432014-07-15 12:44:33 -070064 mSession = new MediaSession(context, HeadsetMediaButton.class.getSimpleName());
Santos Cordondeb8c892014-05-30 01:38:03 -070065 mSession.addCallback(mSessionCallback);
66 mSession.setFlags(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY
67 | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
RoboErik07920f62014-07-21 15:19:41 -070068 mSession.setPlaybackToLocal(AUDIO_ATTRIBUTES);
Santos Cordondeb8c892014-05-30 01:38:03 -070069 }
70
71 /**
72 * Handles the wired headset button while in-call.
73 *
74 * @return true if we consumed the event.
75 */
76 private boolean handleHeadsetHook(KeyEvent event) {
77 Log.d(this, "handleHeadsetHook()...%s %s", event.getAction(), event.getRepeatCount());
78
79 if (event.isLongPress()) {
80 return mCallsManager.onMediaButton(LONG_PRESS);
81 } else if (event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) {
82 return mCallsManager.onMediaButton(SHORT_PRESS);
83 }
84
85 return true;
86 }
Santos Cordon81289982014-06-03 16:03:08 -070087
88 /** ${inheritDoc} */
89 @Override
90 public void onCallAdded(Call call) {
91 if (!mSession.isActive()) {
92 mSession.setActive(true);
93 }
94 }
95
96 /** ${inheritDoc} */
97 @Override
98 public void onCallRemoved(Call call) {
99 if (!mCallsManager.hasAnyCalls()) {
100 if (mSession.isActive()) {
101 mSession.setActive(false);
102 }
103 }
104 }
Santos Cordondeb8c892014-05-30 01:38:03 -0700105}