HeadsetMediaButton.java revision f15dc33f87f88e21ef745952a68af65c86e1bf1e
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
17package com.android.server.telecom;
18
19import android.content.Context;
20import android.content.Intent;
21import android.media.AudioAttributes;
22import android.media.session.MediaSession;
23import android.os.Handler;
24import android.os.Looper;
25import android.os.Message;
26import android.view.KeyEvent;
27
28/**
29 * Static class to handle listening to the headset media buttons.
30 */
31public class HeadsetMediaButton extends CallsManagerListenerBase {
32
33    // Types of media button presses
34    static final int SHORT_PRESS = 1;
35    static final int LONG_PRESS = 2;
36
37    private static final AudioAttributes AUDIO_ATTRIBUTES = new AudioAttributes.Builder()
38            .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
39            .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION).build();
40
41    private static final int MSG_MEDIA_SESSION_INITIALIZE = 0;
42    private static final int MSG_MEDIA_SESSION_SET_ACTIVE = 1;
43
44    private final MediaSession.Callback mSessionCallback = new MediaSession.Callback() {
45        @Override
46        public boolean onMediaButtonEvent(Intent intent) {
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)) {
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                }
56            }
57            return true;
58        }
59    };
60
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;
92    private final CallsManager mCallsManager;
93    private final TelecomSystem.SyncRoot mLock;
94    private MediaSession mSession;
95
96    public HeadsetMediaButton(
97            Context context,
98            CallsManager callsManager,
99            TelecomSystem.SyncRoot lock) {
100        mContext = context;
101        mCallsManager = callsManager;
102        mLock = lock;
103
104        // Create a MediaSession but don't enable it yet. This is a
105        // replacement for MediaButtonReceiver
106        mMediaSessionHandler.obtainMessage(MSG_MEDIA_SESSION_INITIALIZE).sendToTarget();
107    }
108
109    /**
110     * Handles the wired headset button while in-call.
111     *
112     * @return true if we consumed the event.
113     */
114    private boolean handleHeadsetHook(KeyEvent event) {
115        Log.d(this, "handleHeadsetHook()...%s %s", event.getAction(), event.getRepeatCount());
116
117        if (event.isLongPress()) {
118            return mCallsManager.onMediaButton(LONG_PRESS);
119        } else if (event.getAction() == KeyEvent.ACTION_UP && event.getRepeatCount() == 0) {
120            return mCallsManager.onMediaButton(SHORT_PRESS);
121        }
122
123        return true;
124    }
125
126    /** ${inheritDoc} */
127    @Override
128    public void onCallAdded(Call call) {
129        if (call.isExternalCall()) {
130            return;
131        }
132        mMediaSessionHandler.obtainMessage(MSG_MEDIA_SESSION_SET_ACTIVE, 1, 0).sendToTarget();
133    }
134
135    /** ${inheritDoc} */
136    @Override
137    public void onCallRemoved(Call call) {
138        if (call.isExternalCall()) {
139            return;
140        }
141        if (!mCallsManager.hasAnyCalls()) {
142            mMediaSessionHandler.obtainMessage(MSG_MEDIA_SESSION_SET_ACTIVE, 0, 0).sendToTarget();
143        }
144    }
145
146    /** ${inheritDoc} */
147    @Override
148    public void onExternalCallChanged(Call call, boolean isExternalCall) {
149        if (isExternalCall) {
150            onCallRemoved(call);
151        } else {
152            onCallAdded(call);
153        }
154    }
155}
156