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 | |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 25 | import java.util.Collections; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.ConcurrentHashMap; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 28 | |
| 29 | /** Listens for and caches headset state. */ |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 30 | class WiredHeadsetManager { |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 31 | interface Listener { |
| 32 | void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn); |
| 33 | } |
| 34 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 35 | /** 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 Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 48 | private final WiredHeadsetBroadcastReceiver mReceiver; |
| 49 | private boolean mIsPluggedIn; |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 50 | /** |
| 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 Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 57 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 58 | WiredHeadsetManager(Context context) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 59 | mReceiver = new WiredHeadsetBroadcastReceiver(); |
| 60 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 61 | 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 Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 69 | void addListener(Listener listener) { |
| 70 | mListeners.add(listener); |
| 71 | } |
| 72 | |
| 73 | void removeListener(Listener listener) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 74 | if (listener != null) { |
| 75 | mListeners.remove(listener); |
| 76 | } |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 79 | 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 Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 89 | for (Listener listener : mListeners) { |
| 90 | listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn); |
| 91 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |