blob: 70af0fd81868fe69bc3abebd90cd9ea8185df60c [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;
23import android.media.AudioManager;
24import android.media.session.MediaSession;
25import android.media.session.MediaSessionManager;
26import android.view.KeyEvent;
27
28/**
29 * Static class to handle listening to the headset media buttons.
30 */
Santos Cordon81289982014-06-03 16:03:08 -070031final class HeadsetMediaButton extends CallsManagerListenerBase {
Santos Cordondeb8c892014-05-30 01:38:03 -070032
Santos Cordondeb8c892014-05-30 01:38:03 -070033 // Types of media button presses
34 static final int SHORT_PRESS = 1;
35 static final int LONG_PRESS = 2;
36
37 private final MediaSession.Callback mSessionCallback = new MediaSession.Callback() {
38 @Override
39 public void onMediaButtonEvent(Intent intent) {
40 KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
41 Log.v(this, "SessionCallback.onMediaButton()... event = %s.", event);
42 if ((event != null) && (event.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK)) {
43 Log.v(this, "SessionCallback: HEADSETHOOK");
44 boolean consumed = handleHeadsetHook(event);
45 Log.v(this, "==> handleHeadsetHook(): consumed = %b.", consumed);
46 }
47 }
48 };
49
Santos Cordondeb8c892014-05-30 01:38:03 -070050 private final CallsManager mCallsManager;
51
52 private final MediaSession mSession;
53
54 HeadsetMediaButton(Context context, CallsManager callsManager) {
55 mCallsManager = callsManager;
56
RoboErikb771c432014-07-15 12:44:33 -070057 // Create a MediaSession but don't enable it yet. This is a
Santos Cordondeb8c892014-05-30 01:38:03 -070058 // replacement for MediaButtonReceiver
RoboErikb771c432014-07-15 12:44:33 -070059 mSession = new MediaSession(context, HeadsetMediaButton.class.getSimpleName());
Santos Cordondeb8c892014-05-30 01:38:03 -070060 mSession.addCallback(mSessionCallback);
61 mSession.setFlags(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY
62 | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
RoboErik82612ad2014-06-02 10:48:51 -070063 mSession.setPlaybackToLocal(AudioManager.STREAM_VOICE_CALL);
Santos Cordondeb8c892014-05-30 01:38:03 -070064 }
65
66 /**
67 * Handles the wired headset button while in-call.
68 *
69 * @return true if we consumed the event.
70 */
71 private boolean handleHeadsetHook(KeyEvent event) {
72 Log.d(this, "handleHeadsetHook()...%s %s", event.getAction(), event.getRepeatCount());
73
74 if (event.isLongPress()) {
75 return mCallsManager.onMediaButton(LONG_PRESS);
76 } else if (event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) {
77 return mCallsManager.onMediaButton(SHORT_PRESS);
78 }
79
80 return true;
81 }
Santos Cordon81289982014-06-03 16:03:08 -070082
83 /** ${inheritDoc} */
84 @Override
85 public void onCallAdded(Call call) {
86 if (!mSession.isActive()) {
87 mSession.setActive(true);
88 }
89 }
90
91 /** ${inheritDoc} */
92 @Override
93 public void onCallRemoved(Call call) {
94 if (!mCallsManager.hasAnyCalls()) {
95 if (mSession.isActive()) {
96 mSession.setActive(false);
97 }
98 }
99 }
Santos Cordondeb8c892014-05-30 01:38:03 -0700100}