blob: ef5f38cec2e88246908f74bfe24994abea07810d [file] [log] [blame]
Sailesh Nepal6aca10a2014-03-24 16:11:02 -07001/*
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 Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070018
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.media.AudioManager;
24
Tyler Gunn9787e0e2014-10-14 14:36:12 -070025import com.android.internal.util.IndentingPrintWriter;
26
Jay Shraunera82c8f72014-08-14 15:49:16 -070027import java.util.Collections;
28import java.util.Set;
29import java.util.concurrent.ConcurrentHashMap;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070030
31/** Listens for and caches headset state. */
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070032class WiredHeadsetManager {
Sailesh Nepalb88795a2014-07-15 14:53:27 -070033 interface Listener {
34 void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn);
35 }
36
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070037 /** 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 Cordonc395e492015-09-25 14:06:54 -070042 boolean isPluggedIn = mAudioManager.isWiredHeadsetOn();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070043 Log.v(WiredHeadsetManager.this, "ACTION_HEADSET_PLUG event, plugged in: %b",
44 isPluggedIn);
45 onHeadsetPluggedInChanged(isPluggedIn);
46 }
47 }
48 }
49
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070050 private final WiredHeadsetBroadcastReceiver mReceiver;
Santos Cordonc395e492015-09-25 14:06:54 -070051 private final AudioManager mAudioManager;
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070052 private boolean mIsPluggedIn;
Jay Shraunera82c8f72014-08-14 15:49:16 -070053 /**
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 Nepal6aca10a2014-03-24 16:11:02 -070060
Sailesh Nepalb88795a2014-07-15 14:53:27 -070061 WiredHeadsetManager(Context context) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070062 mReceiver = new WiredHeadsetBroadcastReceiver();
63
Santos Cordonc395e492015-09-25 14:06:54 -070064 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
65 mIsPluggedIn = mAudioManager.isWiredHeadsetOn();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070066
67 // Register for misc other intent broadcasts.
68 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
69 context.registerReceiver(mReceiver, intentFilter);
70 }
71
Sailesh Nepalb88795a2014-07-15 14:53:27 -070072 void addListener(Listener listener) {
73 mListeners.add(listener);
74 }
75
76 void removeListener(Listener listener) {
Jay Shraunera82c8f72014-08-14 15:49:16 -070077 if (listener != null) {
78 mListeners.remove(listener);
79 }
Sailesh Nepalb88795a2014-07-15 14:53:27 -070080 }
81
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070082 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 Nepalb88795a2014-07-15 14:53:27 -070092 for (Listener listener : mListeners) {
93 listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn);
94 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070095 }
96 }
Tyler Gunn9787e0e2014-10-14 14:36:12 -070097
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 Nepal6aca10a2014-03-24 16:11:02 -0700106}