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 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
| 23 | import android.media.AudioManager; |
| 24 | |
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. */ |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 32 | class WiredHeadsetManager { |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 33 | interface Listener { |
| 34 | void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn); |
| 35 | } |
| 36 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 37 | /** Receiver for wired headset plugged and unplugged events. */ |
| 38 | private class WiredHeadsetBroadcastReceiver extends BroadcastReceiver { |
| 39 | @Override |
| 40 | public void onReceive(Context context, Intent intent) { |
| 41 | if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) { |
Santos Cordon | c395e49 | 2015-09-25 14:06:54 -0700 | [diff] [blame^] | 42 | boolean isPluggedIn = mAudioManager.isWiredHeadsetOn(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 43 | Log.v(WiredHeadsetManager.this, "ACTION_HEADSET_PLUG event, plugged in: %b", |
| 44 | isPluggedIn); |
| 45 | onHeadsetPluggedInChanged(isPluggedIn); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 50 | private final WiredHeadsetBroadcastReceiver mReceiver; |
Santos Cordon | c395e49 | 2015-09-25 14:06:54 -0700 | [diff] [blame^] | 51 | private final AudioManager mAudioManager; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 52 | private boolean mIsPluggedIn; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 53 | /** |
| 54 | * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is |
| 55 | * load factor before resizing, 1 means we only expect a single thread to |
| 56 | * access the map so make only a single shard |
| 57 | */ |
| 58 | private final Set<Listener> mListeners = Collections.newSetFromMap( |
| 59 | new ConcurrentHashMap<Listener, Boolean>(8, 0.9f, 1)); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 60 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 61 | WiredHeadsetManager(Context context) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 62 | mReceiver = new WiredHeadsetBroadcastReceiver(); |
| 63 | |
Santos Cordon | c395e49 | 2015-09-25 14:06:54 -0700 | [diff] [blame^] | 64 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 65 | mIsPluggedIn = mAudioManager.isWiredHeadsetOn(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 66 | |
| 67 | // Register for misc other intent broadcasts. |
| 68 | IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); |
| 69 | context.registerReceiver(mReceiver, intentFilter); |
| 70 | } |
| 71 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 72 | void addListener(Listener listener) { |
| 73 | mListeners.add(listener); |
| 74 | } |
| 75 | |
| 76 | void removeListener(Listener listener) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 77 | if (listener != null) { |
| 78 | mListeners.remove(listener); |
| 79 | } |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 82 | boolean isPluggedIn() { |
| 83 | return mIsPluggedIn; |
| 84 | } |
| 85 | |
| 86 | private void onHeadsetPluggedInChanged(boolean isPluggedIn) { |
| 87 | if (mIsPluggedIn != isPluggedIn) { |
| 88 | Log.v(this, "onHeadsetPluggedInChanged, mIsPluggedIn: %b -> %b", mIsPluggedIn, |
| 89 | isPluggedIn); |
| 90 | boolean oldIsPluggedIn = mIsPluggedIn; |
| 91 | mIsPluggedIn = isPluggedIn; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 92 | for (Listener listener : mListeners) { |
| 93 | listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn); |
| 94 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Dumps the state of the {@link WiredHeadsetManager}. |
| 100 | * |
| 101 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 102 | */ |
| 103 | public void dump(IndentingPrintWriter pw) { |
| 104 | pw.println("mIsPluggedIn: " + mIsPluggedIn); |
| 105 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 106 | } |