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