1 /* 2 * Copyright (C) 2015 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 package com.android.dialer.voicemail; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.media.AudioManager; 23 import android.util.Log; 24 25 /** Listens for and caches headset state. */ 26 class WiredHeadsetManager { 27 private static final String TAG = WiredHeadsetManager.class.getSimpleName(); 28 29 interface Listener { onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn)30 void onWiredHeadsetPluggedInChanged(boolean oldIsPluggedIn, boolean newIsPluggedIn); 31 } 32 33 /** Receiver for wired headset plugged and unplugged events. */ 34 private class WiredHeadsetBroadcastReceiver extends BroadcastReceiver { 35 @Override onReceive(Context context, Intent intent)36 public void onReceive(Context context, Intent intent) { 37 if (AudioManager.ACTION_HEADSET_PLUG.equals(intent.getAction())) { 38 boolean isPluggedIn = intent.getIntExtra("state", 0) == 1; 39 Log.v(TAG, "ACTION_HEADSET_PLUG event, plugged in: " + isPluggedIn); 40 onHeadsetPluggedInChanged(isPluggedIn); 41 } 42 } 43 } 44 45 private final WiredHeadsetBroadcastReceiver mReceiver; 46 private boolean mIsPluggedIn; 47 private Listener mListener; 48 private Context mContext; 49 WiredHeadsetManager(Context context)50 WiredHeadsetManager(Context context) { 51 mContext = context; 52 mReceiver = new WiredHeadsetBroadcastReceiver(); 53 54 AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 55 mIsPluggedIn = audioManager.isWiredHeadsetOn(); 56 57 } 58 setListener(Listener listener)59 void setListener(Listener listener) { 60 mListener = listener; 61 } 62 isPluggedIn()63 boolean isPluggedIn() { 64 return mIsPluggedIn; 65 } 66 registerReceiver()67 void registerReceiver() { 68 // Register for misc other intent broadcasts. 69 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); 70 mContext.registerReceiver(mReceiver, intentFilter); 71 } 72 unregisterReceiver()73 void unregisterReceiver() { 74 mContext.unregisterReceiver(mReceiver); 75 } 76 onHeadsetPluggedInChanged(boolean isPluggedIn)77 private void onHeadsetPluggedInChanged(boolean isPluggedIn) { 78 if (mIsPluggedIn != isPluggedIn) { 79 Log.v(TAG, "onHeadsetPluggedInChanged, mIsPluggedIn: " + mIsPluggedIn + " -> " 80 + isPluggedIn); 81 boolean oldIsPluggedIn = mIsPluggedIn; 82 mIsPluggedIn = isPluggedIn; 83 if (mListener != null) { 84 mListener.onWiredHeadsetPluggedInChanged(oldIsPluggedIn, mIsPluggedIn); 85 } 86 } 87 } 88 }