Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 18 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame^] | 20 | import android.media.AudioDeviceCallback; |
| 21 | import android.media.AudioDeviceInfo; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 22 | import android.media.AudioManager; |
| 23 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 24 | import com.android.internal.annotations.VisibleForTesting; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 25 | import com.android.internal.util.IndentingPrintWriter; |
| 26 | |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 27 | import java.util.Collections; |
| 28 | import java.util.Set; |
| 29 | import java.util.concurrent.ConcurrentHashMap; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 30 | |
| 31 | /** Listens for and caches headset state. */ |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 32 | @VisibleForTesting |
| 33 | public class WiredHeadsetManager { |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame^] | 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 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 38 | @VisibleForTesting |
| 39 | public interface Listener { |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 40 | void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn); |
| 41 | } |
| 42 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 43 | /** Receiver for wired headset plugged and unplugged events. */ |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame^] | 44 | private class WiredHeadsetCallback extends AudioDeviceCallback { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 45 | @Override |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame^] | 46 | public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) { |
| 47 | Log.startSession("WHC.oADA"); |
Brad Ebinger | 58f3658 | 2016-01-21 14:20:07 -0800 | [diff] [blame] | 48 | try { |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame^] | 49 | updateHeadsetStatus(); |
Brad Ebinger | 58f3658 | 2016-01-21 14:20:07 -0800 | [diff] [blame] | 50 | } finally { |
| 51 | Log.endSession(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame^] | 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 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Santos Cordon | c395e49 | 2015-09-25 14:06:54 -0700 | [diff] [blame] | 75 | private final AudioManager mAudioManager; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 76 | private boolean mIsPluggedIn; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 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( |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame^] | 83 | new ConcurrentHashMap<>(8, 0.9f, 1)); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 84 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 85 | public WiredHeadsetManager(Context context) { |
Santos Cordon | c395e49 | 2015-09-25 14:06:54 -0700 | [diff] [blame] | 86 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 87 | mIsPluggedIn = mAudioManager.isWiredHeadsetOn(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 88 | |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame^] | 89 | mAudioManager.registerAudioDeviceCallback(new WiredHeadsetCallback(), null); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 92 | @VisibleForTesting |
| 93 | public void addListener(Listener listener) { |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 94 | mListeners.add(listener); |
| 95 | } |
| 96 | |
| 97 | void removeListener(Listener listener) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 98 | if (listener != null) { |
| 99 | mListeners.remove(listener); |
| 100 | } |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 103 | @VisibleForTesting |
| 104 | public boolean isPluggedIn() { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 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; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 114 | for (Listener listener : mListeners) { |
| 115 | listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn); |
| 116 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 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 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 128 | } |