WiredHeadsetManager.java revision ca9988ba4e8aed7d04835eca57b62f397c1a08f4
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 17package com.android.server.telecom; 18 19import android.content.Context; 20import android.media.AudioDeviceCallback; 21import android.media.AudioDeviceInfo; 22import android.media.AudioManager; 23 24import com.android.internal.annotations.VisibleForTesting; 25import com.android.internal.util.IndentingPrintWriter; 26 27import java.util.Collections; 28import java.util.Set; 29import java.util.concurrent.ConcurrentHashMap; 30 31/** Listens for and caches headset state. */ 32@VisibleForTesting 33public class WiredHeadsetManager { 34 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 38 @VisibleForTesting 39 public interface Listener { 40 void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn); 41 } 42 43 /** Receiver for wired headset plugged and unplugged events. */ 44 private class WiredHeadsetCallback extends AudioDeviceCallback { 45 @Override 46 public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) { 47 Log.startSession("WHC.oADA"); 48 try { 49 updateHeadsetStatus(); 50 } finally { 51 Log.endSession(); 52 } 53 } 54 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 } 73 } 74 75 private final AudioManager mAudioManager; 76 private boolean mIsPluggedIn; 77 /** 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( 83 new ConcurrentHashMap<>(8, 0.9f, 1)); 84 85 public WiredHeadsetManager(Context context) { 86 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 87 mIsPluggedIn = mAudioManager.isWiredHeadsetOn(); 88 89 mAudioManager.registerAudioDeviceCallback(new WiredHeadsetCallback(), null); 90 } 91 92 @VisibleForTesting 93 public void addListener(Listener listener) { 94 mListeners.add(listener); 95 } 96 97 void removeListener(Listener listener) { 98 if (listener != null) { 99 mListeners.remove(listener); 100 } 101 } 102 103 @VisibleForTesting 104 public boolean isPluggedIn() { 105 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; 114 for (Listener listener : mListeners) { 115 listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn); 116 } 117 } 118 } 119 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 } 128} 129