blob: e59f1a5ea311e0aa81f7a8ed748b04e0a7e149ce [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
Sailesh Nepalb88795a2014-07-15 14:53:27 -070025import java.util.HashSet;
26
27/** Listens for and caches headset state. */
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070028class WiredHeadsetManager {
Sailesh Nepalb88795a2014-07-15 14:53:27 -070029 interface Listener {
30 void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn);
31 }
32
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070033 /** Receiver for wired headset plugged and unplugged events. */
34 private class WiredHeadsetBroadcastReceiver extends BroadcastReceiver {
35 @Override
36 public void onReceive(Context context, Intent intent) {
37 if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
38 boolean isPluggedIn = intent.getIntExtra("state", 0) == 1;
39 Log.v(WiredHeadsetManager.this, "ACTION_HEADSET_PLUG event, plugged in: %b",
40 isPluggedIn);
41 onHeadsetPluggedInChanged(isPluggedIn);
42 }
43 }
44 }
45
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070046 private final WiredHeadsetBroadcastReceiver mReceiver;
47 private boolean mIsPluggedIn;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070048 private final HashSet<Listener> mListeners = new HashSet<>();
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070049
Sailesh Nepalb88795a2014-07-15 14:53:27 -070050 WiredHeadsetManager(Context context) {
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070051 mReceiver = new WiredHeadsetBroadcastReceiver();
52
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070053 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
54 mIsPluggedIn = audioManager.isWiredHeadsetOn();
55
56 // Register for misc other intent broadcasts.
57 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
58 context.registerReceiver(mReceiver, intentFilter);
59 }
60
Sailesh Nepalb88795a2014-07-15 14:53:27 -070061 void addListener(Listener listener) {
62 mListeners.add(listener);
63 }
64
65 void removeListener(Listener listener) {
66 mListeners.remove(listener);
67 }
68
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070069 boolean isPluggedIn() {
70 return mIsPluggedIn;
71 }
72
73 private void onHeadsetPluggedInChanged(boolean isPluggedIn) {
74 if (mIsPluggedIn != isPluggedIn) {
75 Log.v(this, "onHeadsetPluggedInChanged, mIsPluggedIn: %b -> %b", mIsPluggedIn,
76 isPluggedIn);
77 boolean oldIsPluggedIn = mIsPluggedIn;
78 mIsPluggedIn = isPluggedIn;
Sailesh Nepalb88795a2014-07-15 14:53:27 -070079 for (Listener listener : mListeners) {
80 listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn);
81 }
Sailesh Nepal6aca10a2014-03-24 16:11:02 -070082 }
83 }
84}