blob: 329df7166983242e082b3a0afe80af8d1f63185b [file] [log] [blame]
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001/*
2 * Copyright (C) 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
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.media.AudioManager;
24
25/**
26 * Listens for and caches headset state. Used By the CallAudioManger for maintaining
27 * overall audio state for use in the UI layer. Also provides method for connecting the bluetooth
28 * headset to the phone call.
29 */
30class WiredHeadsetManager {
31 /** Receiver for wired headset plugged and unplugged events. */
32 private class WiredHeadsetBroadcastReceiver extends BroadcastReceiver {
33 @Override
34 public void onReceive(Context context, Intent intent) {
35 if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
36 boolean isPluggedIn = intent.getIntExtra("state", 0) == 1;
37 Log.v(WiredHeadsetManager.this, "ACTION_HEADSET_PLUG event, plugged in: %b",
38 isPluggedIn);
39 onHeadsetPluggedInChanged(isPluggedIn);
40 }
41 }
42 }
43
44 private final CallAudioManager mCallAudioManager;
45 private final WiredHeadsetBroadcastReceiver mReceiver;
46 private boolean mIsPluggedIn;
47
48 WiredHeadsetManager(CallAudioManager callAudioManager) {
49 mCallAudioManager = callAudioManager;
50 mReceiver = new WiredHeadsetBroadcastReceiver();
51
52 Context context = TelecommApp.getInstance();
53 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
54 mIsPluggedIn = audioManager.isWiredHeadsetOn();
55
56 // Register for misc other intent broadcasts.
57 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
58 context.registerReceiver(mReceiver, intentFilter);
59 }
60
61 boolean isPluggedIn() {
62 return mIsPluggedIn;
63 }
64
65 private void onHeadsetPluggedInChanged(boolean isPluggedIn) {
66 if (mIsPluggedIn != isPluggedIn) {
67 Log.v(this, "onHeadsetPluggedInChanged, mIsPluggedIn: %b -> %b", mIsPluggedIn,
68 isPluggedIn);
69 boolean oldIsPluggedIn = mIsPluggedIn;
70 mIsPluggedIn = isPluggedIn;
71 mCallAudioManager.onHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn);
72 }
73 }
74}