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; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 23 | import android.telecom.Log; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 24 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 25 | import com.android.internal.annotations.VisibleForTesting; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 26 | import com.android.internal.util.IndentingPrintWriter; |
| 27 | |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 28 | import java.util.Collections; |
| 29 | import java.util.Set; |
| 30 | import java.util.concurrent.ConcurrentHashMap; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 31 | |
| 32 | /** Listens for and caches headset state. */ |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 33 | @VisibleForTesting |
| 34 | public class WiredHeadsetManager { |
| 35 | @VisibleForTesting |
| 36 | public interface Listener { |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 37 | void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn); |
| 38 | } |
| 39 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 40 | /** Receiver for wired headset plugged and unplugged events. */ |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame] | 41 | private class WiredHeadsetCallback extends AudioDeviceCallback { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 42 | @Override |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame] | 43 | public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) { |
| 44 | Log.startSession("WHC.oADA"); |
Brad Ebinger | 58f3658 | 2016-01-21 14:20:07 -0800 | [diff] [blame] | 45 | try { |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame] | 46 | updateHeadsetStatus(); |
Brad Ebinger | 58f3658 | 2016-01-21 14:20:07 -0800 | [diff] [blame] | 47 | } finally { |
| 48 | Log.endSession(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | public void onAudioDevicesRemoved(AudioDeviceInfo[] devices) { |
| 54 | Log.startSession("WHC.oADR"); |
| 55 | try { |
| 56 | updateHeadsetStatus(); |
| 57 | } finally { |
| 58 | Log.endSession(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private void updateHeadsetStatus() { |
David Lin | 22c2ce9 | 2017-07-28 12:01:24 -0700 | [diff] [blame] | 63 | final boolean isPluggedIn = isWiredHeadsetPluggedIn(); |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame] | 64 | |
Hall Liu | 31ed9be | 2016-04-06 12:07:31 -0700 | [diff] [blame] | 65 | Log.i(WiredHeadsetManager.this, "ACTION_HEADSET_PLUG event, plugged in: %b, ", |
| 66 | isPluggedIn); |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame] | 67 | onHeadsetPluggedInChanged(isPluggedIn); |
| 68 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Santos Cordon | c395e49 | 2015-09-25 14:06:54 -0700 | [diff] [blame] | 71 | private final AudioManager mAudioManager; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 72 | private boolean mIsPluggedIn; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 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( |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame] | 79 | new ConcurrentHashMap<>(8, 0.9f, 1)); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 80 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 81 | public WiredHeadsetManager(Context context) { |
Santos Cordon | c395e49 | 2015-09-25 14:06:54 -0700 | [diff] [blame] | 82 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
David Lin | 22c2ce9 | 2017-07-28 12:01:24 -0700 | [diff] [blame] | 83 | mIsPluggedIn = isWiredHeadsetPluggedIn(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 84 | |
Hall Liu | ca9988b | 2016-03-08 15:43:45 -0800 | [diff] [blame] | 85 | mAudioManager.registerAudioDeviceCallback(new WiredHeadsetCallback(), null); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 88 | @VisibleForTesting |
| 89 | public void addListener(Listener listener) { |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 90 | mListeners.add(listener); |
| 91 | } |
| 92 | |
| 93 | void removeListener(Listener listener) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 94 | if (listener != null) { |
| 95 | mListeners.remove(listener); |
| 96 | } |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Hall Liu | f62630a | 2015-10-27 14:53:39 -0700 | [diff] [blame] | 99 | @VisibleForTesting |
| 100 | public boolean isPluggedIn() { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 101 | return mIsPluggedIn; |
| 102 | } |
| 103 | |
David Lin | 22c2ce9 | 2017-07-28 12:01:24 -0700 | [diff] [blame] | 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 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 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; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 128 | for (Listener listener : mListeners) { |
| 129 | listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn); |
| 130 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Dumps the state of the {@link WiredHeadsetManager}. |
| 136 | * |
| 137 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 138 | */ |
| 139 | public void dump(IndentingPrintWriter pw) { |
| 140 | pw.println("mIsPluggedIn: " + mIsPluggedIn); |
| 141 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 142 | } |