1 /*
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 
17 package com.android.server.telecom;
18 
19 import android.content.Context;
20 import android.media.AudioDeviceCallback;
21 import android.media.AudioDeviceInfo;
22 import android.media.AudioManager;
23 import android.telecom.Log;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.internal.util.IndentingPrintWriter;
27 
28 import java.util.Collections;
29 import java.util.Set;
30 import java.util.concurrent.ConcurrentHashMap;
31 
32 /** Listens for and caches headset state. */
33 @VisibleForTesting
34 public class WiredHeadsetManager {
35     @VisibleForTesting
36     public interface Listener {
onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn)37         void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn);
38     }
39 
40     /** Receiver for wired headset plugged and unplugged events. */
41     private class WiredHeadsetCallback extends AudioDeviceCallback {
42         @Override
onAudioDevicesAdded(AudioDeviceInfo[] addedDevices)43         public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
44             Log.startSession("WHC.oADA");
45             try {
46                 updateHeadsetStatus();
47             } finally {
48                 Log.endSession();
49             }
50         }
51 
52         @Override
onAudioDevicesRemoved(AudioDeviceInfo[] devices)53         public void onAudioDevicesRemoved(AudioDeviceInfo[] devices) {
54             Log.startSession("WHC.oADR");
55             try {
56                 updateHeadsetStatus();
57             } finally {
58                 Log.endSession();
59             }
60         }
61 
updateHeadsetStatus()62         private void updateHeadsetStatus() {
63             final boolean isPluggedIn = isWiredHeadsetPluggedIn();
64 
65             Log.i(WiredHeadsetManager.this, "ACTION_HEADSET_PLUG event, plugged in: %b, ",
66                     isPluggedIn);
67             onHeadsetPluggedInChanged(isPluggedIn);
68         }
69     }
70 
71     private final AudioManager mAudioManager;
72     private boolean mIsPluggedIn;
73     /**
74      * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
75      * load factor before resizing, 1 means we only expect a single thread to
76      * access the map so make only a single shard
77      */
78     private final Set<Listener> mListeners = Collections.newSetFromMap(
79             new ConcurrentHashMap<>(8, 0.9f, 1));
80 
WiredHeadsetManager(Context context)81     public WiredHeadsetManager(Context context) {
82         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
83         mIsPluggedIn = isWiredHeadsetPluggedIn();
84 
85         mAudioManager.registerAudioDeviceCallback(new WiredHeadsetCallback(), null);
86     }
87 
88     @VisibleForTesting
addListener(Listener listener)89     public void addListener(Listener listener) {
90         mListeners.add(listener);
91     }
92 
removeListener(Listener listener)93     void removeListener(Listener listener) {
94         if (listener != null) {
95             mListeners.remove(listener);
96         }
97     }
98 
99     @VisibleForTesting
isPluggedIn()100     public boolean isPluggedIn() {
101         return mIsPluggedIn;
102     }
103 
isWiredHeadsetPluggedIn()104     private boolean isWiredHeadsetPluggedIn() {
105         AudioDeviceInfo[] devices = mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL);
106         boolean isPluggedIn = false;
107         for (AudioDeviceInfo device : devices) {
108             switch (device.getType()) {
109                 case AudioDeviceInfo.TYPE_WIRED_HEADPHONES:
110                 case AudioDeviceInfo.TYPE_WIRED_HEADSET:
111                 case AudioDeviceInfo.TYPE_USB_HEADSET:
112                 case AudioDeviceInfo.TYPE_USB_DEVICE:
113                     isPluggedIn = true;
114             }
115             if (isPluggedIn) {
116                 break;
117             }
118         }
119         return isPluggedIn;
120     }
121 
onHeadsetPluggedInChanged(boolean isPluggedIn)122     private void onHeadsetPluggedInChanged(boolean isPluggedIn) {
123         if (mIsPluggedIn != isPluggedIn) {
124             Log.v(this, "onHeadsetPluggedInChanged, mIsPluggedIn: %b -> %b", mIsPluggedIn,
125                     isPluggedIn);
126             boolean oldIsPluggedIn = mIsPluggedIn;
127             mIsPluggedIn = isPluggedIn;
128             for (Listener listener : mListeners) {
129                 listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn);
130             }
131         }
132     }
133 
134     /**
135      * Dumps the state of the {@link WiredHeadsetManager}.
136      *
137      * @param pw The {@code IndentingPrintWriter} to write the state to.
138      */
dump(IndentingPrintWriter pw)139     public void dump(IndentingPrintWriter pw) {
140         pw.println("mIsPluggedIn: " + mIsPluggedIn);
141     }
142 }
143