1 /* 2 * Copyright (C) 2013 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.phone; 18 19 import com.google.android.collect.Lists; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.os.SystemProperties; 26 import android.util.Log; 27 28 import java.util.List; 29 30 /** 31 * Listens for and caches headset state. Used By the AudioRouter for maintaining 32 * overall audio state for use in the UI layer. Also provides method for connecting the bluetooth 33 * headset to the phone call. 34 */ 35 public class WiredHeadsetManager { 36 private static final String LOG_TAG = WiredHeadsetManager.class.getSimpleName(); 37 private static final boolean DBG = 38 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); 39 private static final boolean VDBG = (PhoneGlobals.DBG_LEVEL >= 2); 40 41 // True if a wired headset is currently plugged in, based on the state 42 // from the latest Intent.ACTION_HEADSET_PLUG broadcast we received in 43 // mReceiver.onReceive(). 44 private boolean mIsHeadsetPlugged = false; 45 private final WiredHeadsetBroadcastReceiver mReceiver; 46 private final List<WiredHeadsetListener> mListeners = Lists.newArrayList(); 47 48 public WiredHeadsetManager(Context context) { 49 mReceiver = new WiredHeadsetBroadcastReceiver(); 50 51 // Register for misc other intent broadcasts. 52 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); 53 context.registerReceiver(mReceiver, intentFilter); 54 } 55 56 /** 57 * Returns connection state of the wires headset. 58 */ 59 public boolean isHeadsetPlugged() { 60 return mIsHeadsetPlugged; 61 } 62 63 /** 64 * Add a listener for wired headset connection status. 65 */ 66 public void addWiredHeadsetListener(WiredHeadsetListener listener) { 67 if (!mListeners.contains(listener)) { 68 mListeners.add(listener); 69 } 70 } 71 72 /** 73 * Called when we get an event from the system for the headset connection state. 74 */ 75 private void onHeadsetConnection(boolean pluggedIn) { 76 if (DBG) { 77 Log.d(LOG_TAG, "Wired headset connected: " + pluggedIn); 78 } 79 mIsHeadsetPlugged = pluggedIn; 80 81 notifyListeners(); 82 } 83 84 private void notifyListeners() { 85 for (int i = 0; i < mListeners.size(); i++) { 86 mListeners.get(i).onWiredHeadsetConnection(mIsHeadsetPlugged); 87 } 88 } 89 90 /** 91 * Receiver for misc intent broadcasts the BluetoothManager cares about. 92 */ 93 private class WiredHeadsetBroadcastReceiver extends BroadcastReceiver { 94 @Override 95 public void onReceive(Context context, Intent intent) { 96 String action = intent.getAction(); 97 98 if (action.equals(Intent.ACTION_HEADSET_PLUG)) { 99 if (VDBG) Log.d(LOG_TAG, "mReceiver: ACTION_HEADSET_PLUG"); 100 if (VDBG) Log.d(LOG_TAG, " state: " + intent.getIntExtra("state", 0)); 101 if (VDBG) Log.d(LOG_TAG, " name: " + intent.getStringExtra("name")); 102 onHeadsetConnection(intent.getIntExtra("state", 0) == 1); 103 } 104 } 105 } 106 107 /** 108 * Listeners for those that want to know about the headset state. 109 */ 110 public interface WiredHeadsetListener { 111 void onWiredHeadsetConnection(boolean pluggedIn); 112 } 113 } 114