blob: aa2bbd192c9ede39f52c58744b46a62b0e83419a [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
17package com.android.telecomm;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.media.AudioManager;
24
Jay Shraunera82c8f72014-08-14 15:49:16 -070025import java.util.Collections;
26import java.util.Set;
27import java.util.concurrent.ConcurrentHashMap;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070028
29/** Listens for and caches headset state. */
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070030class WiredHeadsetManager {
Sailesh Nepalb88795a2014-07-15 14:53:27 -070031 interface Listener {
32 void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn);
33 }
34
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070035 /** Receiver for wired headset plugged and unplugged events. */
36 private class WiredHeadsetBroadcastReceiver extends BroadcastReceiver {
37 @Override
38 public void onReceive(Context context, Intent intent) {
39 if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
40 boolean isPluggedIn = intent.getIntExtra("state", 0) == 1;
41 Log.v(WiredHeadsetManager.this, "ACTION_HEADSET_PLUG event, plugged in: %b",
42 isPluggedIn);
43 onHeadsetPluggedInChanged(isPluggedIn);
44 }
45 }
46 }
47
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070048 private final WiredHeadsetBroadcastReceiver mReceiver;
49 private boolean mIsPluggedIn;
Jay Shraunera82c8f72014-08-14 15:49:16 -070050 /**
51 * ConcurrentHashMap constructor params: 8 is initial table size, 0.9f is
52 * load factor before resizing, 1 means we only expect a single thread to
53 * access the map so make only a single shard
54 */
55 private final Set<Listener> mListeners = Collections.newSetFromMap(
56 new ConcurrentHashMap<Listener, Boolean>(8, 0.9f, 1));
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070057
Sailesh Nepalb88795a2014-07-15 14:53:27 -070058 WiredHeadsetManager(Context context) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070059 mReceiver = new WiredHeadsetBroadcastReceiver();
60
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070061 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
62 mIsPluggedIn = audioManager.isWiredHeadsetOn();
63
64 // Register for misc other intent broadcasts.
65 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
66 context.registerReceiver(mReceiver, intentFilter);
67 }
68
Sailesh Nepalb88795a2014-07-15 14:53:27 -070069 void addListener(Listener listener) {
70 mListeners.add(listener);
71 }
72
73 void removeListener(Listener listener) {
Jay Shraunera82c8f72014-08-14 15:49:16 -070074 if (listener != null) {
75 mListeners.remove(listener);
76 }
Sailesh Nepalb88795a2014-07-15 14:53:27 -070077 }
78
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070079 boolean isPluggedIn() {
80 return mIsPluggedIn;
81 }
82
83 private void onHeadsetPluggedInChanged(boolean isPluggedIn) {
84 if (mIsPluggedIn != isPluggedIn) {
85 Log.v(this, "onHeadsetPluggedInChanged, mIsPluggedIn: %b -> %b", mIsPluggedIn,
86 isPluggedIn);
87 boolean oldIsPluggedIn = mIsPluggedIn;
88 mIsPluggedIn = isPluggedIn;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070089 for (Listener listener : mListeners) {
90 listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn);
91 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070092 }
93 }
94}