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 | |
| 17 | package com.android.telecomm; |
| 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 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame^] | 25 | import java.util.HashSet; |
| 26 | |
| 27 | /** Listens for and caches headset state. */ |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 28 | class WiredHeadsetManager { |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame^] | 29 | interface Listener { |
| 30 | void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn); |
| 31 | } |
| 32 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 33 | /** 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 Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 46 | private final WiredHeadsetBroadcastReceiver mReceiver; |
| 47 | private boolean mIsPluggedIn; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame^] | 48 | private final HashSet<Listener> mListeners = new HashSet<>(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 49 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame^] | 50 | WiredHeadsetManager(Context context) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 51 | mReceiver = new WiredHeadsetBroadcastReceiver(); |
| 52 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 53 | 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 Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame^] | 61 | void addListener(Listener listener) { |
| 62 | mListeners.add(listener); |
| 63 | } |
| 64 | |
| 65 | void removeListener(Listener listener) { |
| 66 | mListeners.remove(listener); |
| 67 | } |
| 68 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 69 | 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 Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame^] | 79 | for (Listener listener : mListeners) { |
| 80 | listener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn); |
| 81 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |