blob: b73c013022d4eb533bf5a94915570f79af89c7c3 [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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070018
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070019import android.content.Context;
Hall Liuca9988b2016-03-08 15:43:45 -080020import android.media.AudioDeviceCallback;
21import android.media.AudioDeviceInfo;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070022import android.media.AudioManager;
23
Hall Liuf62630a2015-10-27 14:53:39 -070024import com.android.internal.annotations.VisibleForTesting;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070025import com.android.internal.util.IndentingPrintWriter;
26
Jay Shraunera82c8f72014-08-14 15:49:16 -070027import java.util.Collections;
28import java.util.Set;
29import java.util.concurrent.ConcurrentHashMap;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070030
31/** Listens for and caches headset state. */
Hall Liuf62630a2015-10-27 14:53:39 -070032@VisibleForTesting
33public class WiredHeadsetManager {
Hall Liuca9988b2016-03-08 15:43:45 -080034 private static final int VALID_WIRED_DEVICES = AudioManager.DEVICE_OUT_WIRED_HEADSET |
35 AudioManager.DEVICE_OUT_WIRED_HEADPHONE |
36 AudioManager.DEVICE_OUT_USB_DEVICE;
37
Hall Liuf62630a2015-10-27 14:53:39 -070038 @VisibleForTesting
39 public interface Listener {
Sailesh Nepalb88795a2014-07-15 14:53:27 -070040 void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn);
41 }
42
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070043 /** Receiver for wired headset plugged and unplugged events. */
Hall Liuca9988b2016-03-08 15:43:45 -080044 private class WiredHeadsetCallback extends AudioDeviceCallback {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070045 @Override
Hall Liuca9988b2016-03-08 15:43:45 -080046 public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
47 Log.startSession("WHC.oADA");
Brad Ebinger58f36582016-01-21 14:20:07 -080048 try {
Hall Liuca9988b2016-03-08 15:43:45 -080049 updateHeadsetStatus();
Brad Ebinger58f36582016-01-21 14:20:07 -080050 } finally {
51 Log.endSession();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070052 }
53 }
Hall Liuca9988b2016-03-08 15:43:45 -080054
55 @Override
56 public void onAudioDevicesRemoved(AudioDeviceInfo[] devices) {
57 Log.startSession("WHC.oADR");
58 try {
59 updateHeadsetStatus();
60 } finally {
61 Log.endSession();
62 }
63 }
64
65 private void updateHeadsetStatus() {
66 int devices = mAudioManager.getDevicesForStream(AudioManager.STREAM_VOICE_CALL);
67
68 boolean isPluggedIn = (devices & VALID_WIRED_DEVICES) != 0;
69 Log.i(WiredHeadsetManager.this, "ACTION_HEADSET_PLUG event, plugged in: %b, " +
70 "devices: %s", isPluggedIn, Integer.toHexString(devices));
71 onHeadsetPluggedInChanged(isPluggedIn);
72 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070073 }
74
Santos Cordonc395e492015-09-25 14:06:54 -070075 private final AudioManager mAudioManager;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070076 private boolean mIsPluggedIn;
Jay Shraunera82c8f72014-08-14 15:49:16 -070077 /**
78 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
79 * load factor before resizing, 1 means we only expect a single thread to
80 * access the map so make only a single shard
81 */
82 private final Set<Listener> mListeners = Collections.newSetFromMap(
Hall Liuca9988b2016-03-08 15:43:45 -080083 new ConcurrentHashMap<>(8, 0.9f, 1));
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070084
Hall Liuf62630a2015-10-27 14:53:39 -070085 public WiredHeadsetManager(Context context) {
Santos Cordonc395e492015-09-25 14:06:54 -070086 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
87 mIsPluggedIn = mAudioManager.isWiredHeadsetOn();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070088
Hall Liuca9988b2016-03-08 15:43:45 -080089 mAudioManager.registerAudioDeviceCallback(new WiredHeadsetCallback(), null);
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070090 }
91
Hall Liuf62630a2015-10-27 14:53:39 -070092 @VisibleForTesting
93 public void addListener(Listener listener) {
Sailesh Nepalb88795a2014-07-15 14:53:27 -070094 mListeners.add(listener);
95 }
96
97 void removeListener(Listener listener) {
Jay Shraunera82c8f72014-08-14 15:49:16 -070098 if (listener != null) {
99 mListeners.remove(listener);
100 }
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700101 }
102
Hall Liuf62630a2015-10-27 14:53:39 -0700103 @VisibleForTesting
104 public boolean isPluggedIn() {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700105 return mIsPluggedIn;
106 }
107
108 private void onHeadsetPluggedInChanged(boolean isPluggedIn) {
109 if (mIsPluggedIn != isPluggedIn) {
110 Log.v(this, "onHeadsetPluggedInChanged, mIsPluggedIn: %b -> %b", mIsPluggedIn,
111 isPluggedIn);
112 boolean oldIsPluggedIn = mIsPluggedIn;
113 mIsPluggedIn = isPluggedIn;
Sailesh Nepalb88795a2014-07-15 14:53:27 -0700114 for (Listener listener : mListeners) {
115 listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn);
116 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700117 }
118 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700119
120 /**
121 * Dumps the state of the {@link WiredHeadsetManager}.
122 *
123 * @param pw The {@code IndentingPrintWriter} to write the state to.
124 */
125 public void dump(IndentingPrintWriter pw) {
126 pw.println("mIsPluggedIn: " + mIsPluggedIn);
127 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -0700128}