The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.server; |
| 18 | |
| 19 | import android.app.ActivityManagerNative; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
Nick Pelly | 9ac9321 | 2009-04-08 15:09:15 -0700 | [diff] [blame] | 24 | import android.os.PowerManager; |
| 25 | import android.os.PowerManager.WakeLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.UEventObserver; |
| 27 | import android.util.Log; |
| 28 | import android.media.AudioManager; |
| 29 | |
| 30 | import java.io.FileReader; |
| 31 | import java.io.FileNotFoundException; |
| 32 | |
| 33 | /** |
| 34 | * <p>HeadsetObserver monitors for a wired headset. |
| 35 | */ |
| 36 | class HeadsetObserver extends UEventObserver { |
| 37 | private static final String TAG = HeadsetObserver.class.getSimpleName(); |
Eric Olsen | e7096eb | 2009-11-23 13:06:07 -0800 | [diff] [blame] | 38 | private static final boolean LOG = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | private static final String HEADSET_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/h2w"; |
| 41 | private static final String HEADSET_STATE_PATH = "/sys/class/switch/h2w/state"; |
| 42 | private static final String HEADSET_NAME_PATH = "/sys/class/switch/h2w/name"; |
| 43 | |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 44 | private static final int BIT_HEADSET = (1 << 0); |
| 45 | private static final int BIT_HEADSET_NO_MIC = (1 << 1); |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 46 | private static final int SUPPORTED_HEADSETS = (BIT_HEADSET|BIT_HEADSET_NO_MIC); |
| 47 | private static final int HEADSETS_WITH_MIC = BIT_HEADSET; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 48 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | private int mHeadsetState; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 50 | private int mPrevHeadsetState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | private String mHeadsetName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
Nick Pelly | 9ac9321 | 2009-04-08 15:09:15 -0700 | [diff] [blame] | 53 | private final Context mContext; |
| 54 | private final WakeLock mWakeLock; // held while there is a pending route change |
| 55 | |
Eric Laurent | aead64d | 2010-02-02 09:42:33 -0800 | [diff] [blame] | 56 | private boolean mHandleTTY; |
| 57 | private int mTTYState; |
| 58 | private AudioManager mAudioManager = null; |
| 59 | |
| 60 | // special use of bits in headset state received from kernel made by some |
| 61 | // platforms to indicate changes in TTY mode. |
| 62 | private static final int BIT_TTY_OFF = 0; |
| 63 | private static final int BIT_TTY_FULL = (1 << 2); |
| 64 | private static final int BIT_TTY_VCO = (1 << 5); |
| 65 | private static final int BIT_TTY_HCO = (1 << 6); |
| 66 | private static final int TTY_BITS_MASK = (BIT_TTY_FULL | BIT_TTY_VCO | BIT_TTY_HCO); |
| 67 | |
| 68 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | public HeadsetObserver(Context context) { |
| 70 | mContext = context; |
Nick Pelly | 9ac9321 | 2009-04-08 15:09:15 -0700 | [diff] [blame] | 71 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 72 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "HeadsetObserver"); |
| 73 | mWakeLock.setReferenceCounted(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
| 75 | startObserving(HEADSET_UEVENT_MATCH); |
| 76 | |
Eric Laurent | aead64d | 2010-02-02 09:42:33 -0800 | [diff] [blame] | 77 | // read settings for TTY mode indication method |
| 78 | mHandleTTY = context.getResources().getBoolean( |
| 79 | com.android.internal.R.bool.tty_mode_uses_headset_events); |
| 80 | mTTYState = BIT_TTY_OFF; |
| 81 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | init(); // set initial status |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onUEvent(UEventObserver.UEvent event) { |
Joe Onorato | 9a5e3e1 | 2009-07-01 21:04:03 -0400 | [diff] [blame] | 87 | if (LOG) Log.v(TAG, "Headset UEVENT: " + event.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | |
| 89 | try { |
| 90 | update(event.get("SWITCH_NAME"), Integer.parseInt(event.get("SWITCH_STATE"))); |
| 91 | } catch (NumberFormatException e) { |
| 92 | Log.e(TAG, "Could not parse switch state from event " + event); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private synchronized final void init() { |
| 97 | char[] buffer = new char[1024]; |
| 98 | |
| 99 | String newName = mHeadsetName; |
| 100 | int newState = mHeadsetState; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 101 | mPrevHeadsetState = mHeadsetState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | try { |
| 103 | FileReader file = new FileReader(HEADSET_STATE_PATH); |
| 104 | int len = file.read(buffer, 0, 1024); |
| 105 | newState = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 106 | |
| 107 | file = new FileReader(HEADSET_NAME_PATH); |
| 108 | len = file.read(buffer, 0, 1024); |
| 109 | newName = new String(buffer, 0, len).trim(); |
| 110 | |
| 111 | } catch (FileNotFoundException e) { |
| 112 | Log.w(TAG, "This kernel does not have wired headset support"); |
| 113 | } catch (Exception e) { |
| 114 | Log.e(TAG, "" , e); |
| 115 | } |
| 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | update(newName, newState); |
| 118 | } |
| 119 | |
| 120 | private synchronized final void update(String newName, int newState) { |
Eric Laurent | aead64d | 2010-02-02 09:42:33 -0800 | [diff] [blame] | 121 | // handle TTY state change first |
| 122 | if (mHandleTTY) { |
| 123 | int ttyState = newState & TTY_BITS_MASK; |
| 124 | if (ttyState != mTTYState) { |
| 125 | String ttyMode; |
| 126 | |
| 127 | switch (ttyState) { |
| 128 | case BIT_TTY_FULL: |
| 129 | ttyMode = "tty_full"; |
| 130 | break; |
| 131 | case BIT_TTY_VCO: |
| 132 | ttyMode = "tty_vco"; |
| 133 | break; |
| 134 | case BIT_TTY_HCO: |
| 135 | ttyMode = "tty_hco"; |
| 136 | break; |
| 137 | case BIT_TTY_OFF: |
| 138 | ttyMode = "tty_off"; |
| 139 | break; |
| 140 | default: |
| 141 | ttyMode = "tty_invalid"; |
| 142 | break; |
| 143 | |
| 144 | } |
| 145 | if (ttyMode != "tty_invalid") { |
| 146 | mTTYState = ttyState; |
| 147 | if (mAudioManager == null) { |
| 148 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| 149 | } |
| 150 | mAudioManager.setParameters("tty_mode="+ttyMode); |
| 151 | } |
| 152 | } |
| 153 | } |
Eric Laurent | 923d7d7 | 2009-11-12 12:09:06 -0800 | [diff] [blame] | 154 | // Retain only relevant bits |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 155 | int headsetState = newState & SUPPORTED_HEADSETS; |
| 156 | int newOrOld = headsetState | mHeadsetState; |
Eric Laurent | 700aab67 | 2010-01-22 07:50:58 -0800 | [diff] [blame] | 157 | int delay = 0; |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 158 | // reject all suspect transitions: only accept state changes from: |
| 159 | // - a: 0 heaset to 1 headset |
| 160 | // - b: 1 headset to 0 headset |
| 161 | if (mHeadsetState == headsetState || ((newOrOld & (newOrOld - 1)) != 0)) { |
| 162 | return; |
| 163 | } |
Eric Laurent | 923d7d7 | 2009-11-12 12:09:06 -0800 | [diff] [blame] | 164 | |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 165 | mHeadsetName = newName; |
| 166 | mPrevHeadsetState = mHeadsetState; |
| 167 | mHeadsetState = headsetState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 169 | if (headsetState == 0) { |
| 170 | Intent intent = new Intent(AudioManager.ACTION_AUDIO_BECOMING_NOISY); |
| 171 | mContext.sendBroadcast(intent); |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 172 | // It can take hundreds of ms flush the audio pipeline after |
| 173 | // apps pause audio playback, but audio route changes are |
| 174 | // immediate, so delay the route change by 1000ms. |
| 175 | // This could be improved once the audio sub-system provides an |
| 176 | // interface to clear the audio pipeline. |
Eric Laurent | 700aab67 | 2010-01-22 07:50:58 -0800 | [diff] [blame] | 177 | delay = 1000; |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 178 | } else { |
Eric Laurent | 700aab67 | 2010-01-22 07:50:58 -0800 | [diff] [blame] | 179 | // Insert the same delay for headset connection so that the connection event is not |
| 180 | // broadcast before the disconnection event in case of fast removal/insertion |
| 181 | if (mHandler.hasMessages(0)) { |
| 182 | delay = 1000; |
| 183 | } |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 184 | } |
Eric Laurent | 700aab67 | 2010-01-22 07:50:58 -0800 | [diff] [blame] | 185 | mWakeLock.acquire(); |
| 186 | mHandler.sendMessageDelayed(mHandler.obtainMessage(0, |
| 187 | mHeadsetState, |
| 188 | mPrevHeadsetState, |
| 189 | mHeadsetName), |
| 190 | delay); |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Eric Laurent | da4cc34 | 2009-12-14 03:45:41 -0800 | [diff] [blame] | 193 | private synchronized final void sendIntents(int headsetState, int prevHeadsetState, String headsetName) { |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 194 | int allHeadsets = SUPPORTED_HEADSETS; |
| 195 | for (int curHeadset = 1; allHeadsets != 0; curHeadset <<= 1) { |
| 196 | if ((curHeadset & allHeadsets) != 0) { |
Eric Laurent | da4cc34 | 2009-12-14 03:45:41 -0800 | [diff] [blame] | 197 | sendIntent(curHeadset, headsetState, prevHeadsetState, headsetName); |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 198 | allHeadsets &= ~curHeadset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Eric Laurent | da4cc34 | 2009-12-14 03:45:41 -0800 | [diff] [blame] | 203 | private final void sendIntent(int headset, int headsetState, int prevHeadsetState, String headsetName) { |
| 204 | if ((headsetState & headset) != (prevHeadsetState & headset)) { |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 205 | // Pack up the values and broadcast them to everyone |
| 206 | Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG); |
| 207 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 208 | int state = 0; |
| 209 | int microphone = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 211 | if ((headset & HEADSETS_WITH_MIC) != 0) { |
| 212 | microphone = 1; |
| 213 | } |
Eric Laurent | da4cc34 | 2009-12-14 03:45:41 -0800 | [diff] [blame] | 214 | if ((headsetState & headset) != 0) { |
Eric Laurent | 923d7d7 | 2009-11-12 12:09:06 -0800 | [diff] [blame] | 215 | state = 1; |
| 216 | } |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 217 | intent.putExtra("state", state); |
Eric Laurent | da4cc34 | 2009-12-14 03:45:41 -0800 | [diff] [blame] | 218 | intent.putExtra("name", headsetName); |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 219 | intent.putExtra("microphone", microphone); |
| 220 | |
Eric Laurent | da4cc34 | 2009-12-14 03:45:41 -0800 | [diff] [blame] | 221 | if (LOG) Log.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+headsetName+" mic: "+microphone); |
Eric Laurent | 2083b29 | 2009-11-20 07:26:56 -0800 | [diff] [blame] | 222 | // TODO: Should we require a permission? |
| 223 | ActivityManagerNative.broadcastStickyIntent(intent, null); |
Eric Laurent | 923d7d7 | 2009-11-12 12:09:06 -0800 | [diff] [blame] | 224 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | private final Handler mHandler = new Handler() { |
| 228 | @Override |
| 229 | public void handleMessage(Message msg) { |
Eric Laurent | da4cc34 | 2009-12-14 03:45:41 -0800 | [diff] [blame] | 230 | sendIntents(msg.arg1, msg.arg2, (String)msg.obj); |
Nick Pelly | 9ac9321 | 2009-04-08 15:09:15 -0700 | [diff] [blame] | 231 | mWakeLock.release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
Nick Pelly | 9ac9321 | 2009-04-08 15:09:15 -0700 | [diff] [blame] | 233 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | } |