DockObserver.java revision 010e561a703381e16dd17caa7c942dea6978e6dc
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 17package com.android.server; 18 19import android.content.ContentResolver; 20import android.content.Context; 21import android.content.Intent; 22import android.content.pm.PackageManager; 23import android.media.AudioManager; 24import android.media.Ringtone; 25import android.media.RingtoneManager; 26import android.net.Uri; 27import android.os.Binder; 28import android.os.Handler; 29import android.os.Message; 30import android.os.PowerManager; 31import android.os.SystemClock; 32import android.os.UEventObserver; 33import android.os.UserHandle; 34import android.provider.Settings; 35import android.util.Log; 36import android.util.Slog; 37 38import java.io.FileDescriptor; 39import java.io.FileNotFoundException; 40import java.io.FileReader; 41import java.io.PrintWriter; 42 43/** 44 * DockObserver monitors for a docking station. 45 */ 46final class DockObserver extends SystemService { 47 private static final String TAG = "DockObserver"; 48 49 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; 50 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; 51 52 private static final int MSG_DOCK_STATE_CHANGED = 0; 53 54 private final PowerManager mPowerManager; 55 private final PowerManager.WakeLock mWakeLock; 56 57 private final Object mLock = new Object(); 58 59 private boolean mSystemReady; 60 61 private int mActualDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; 62 63 private int mReportedDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; 64 private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; 65 66 private boolean mUpdatesStopped; 67 68 public DockObserver(Context context) { 69 super(context); 70 71 mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); 72 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); 73 74 init(); // set initial status 75 76 mObserver.startObserving(DOCK_UEVENT_MATCH); 77 } 78 79 @Override 80 public void onStart() { 81 publishBinderService(TAG, new BinderService()); 82 } 83 84 @Override 85 public void onBootPhase(int phase) { 86 if (phase == PHASE_ACTIVITY_MANAGER_READY) { 87 synchronized (mLock) { 88 mSystemReady = true; 89 90 // don't bother broadcasting undocked here 91 if (mReportedDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { 92 updateLocked(); 93 } 94 } 95 } 96 } 97 98 private void init() { 99 synchronized (mLock) { 100 try { 101 char[] buffer = new char[1024]; 102 FileReader file = new FileReader(DOCK_STATE_PATH); 103 try { 104 int len = file.read(buffer, 0, 1024); 105 setActualDockStateLocked(Integer.valueOf((new String(buffer, 0, len)).trim())); 106 mPreviousDockState = mActualDockState; 107 } finally { 108 file.close(); 109 } 110 } catch (FileNotFoundException e) { 111 Slog.w(TAG, "This kernel does not have dock station support"); 112 } catch (Exception e) { 113 Slog.e(TAG, "" , e); 114 } 115 } 116 } 117 118 private void setActualDockStateLocked(int newState) { 119 mActualDockState = newState; 120 if (!mUpdatesStopped) { 121 setDockStateLocked(newState); 122 } 123 } 124 125 private void setDockStateLocked(int newState) { 126 if (newState != mReportedDockState) { 127 mReportedDockState = newState; 128 if (mSystemReady) { 129 // Wake up immediately when docked or undocked. 130 mPowerManager.wakeUp(SystemClock.uptimeMillis()); 131 updateLocked(); 132 } 133 } 134 } 135 136 private void updateLocked() { 137 mWakeLock.acquire(); 138 mHandler.sendEmptyMessage(MSG_DOCK_STATE_CHANGED); 139 } 140 141 private void handleDockStateChange() { 142 synchronized (mLock) { 143 Slog.i(TAG, "Dock state changed from " + mPreviousDockState + " to " 144 + mReportedDockState); 145 final int previousDockState = mPreviousDockState; 146 mPreviousDockState = mReportedDockState; 147 148 // Skip the dock intent if not yet provisioned. 149 final ContentResolver cr = getContext().getContentResolver(); 150 if (Settings.Global.getInt(cr, 151 Settings.Global.DEVICE_PROVISIONED, 0) == 0) { 152 Slog.i(TAG, "Device not provisioned, skipping dock broadcast"); 153 return; 154 } 155 156 // Pack up the values and broadcast them to everyone 157 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); 158 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 159 intent.putExtra(Intent.EXTRA_DOCK_STATE, mReportedDockState); 160 161 // Play a sound to provide feedback to confirm dock connection. 162 // Particularly useful for flaky contact pins... 163 if (Settings.Global.getInt(cr, 164 Settings.Global.DOCK_SOUNDS_ENABLED, 1) == 1) { 165 String whichSound = null; 166 if (mReportedDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { 167 if ((previousDockState == Intent.EXTRA_DOCK_STATE_DESK) || 168 (previousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || 169 (previousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { 170 whichSound = Settings.Global.DESK_UNDOCK_SOUND; 171 } else if (previousDockState == Intent.EXTRA_DOCK_STATE_CAR) { 172 whichSound = Settings.Global.CAR_UNDOCK_SOUND; 173 } 174 } else { 175 if ((mReportedDockState == Intent.EXTRA_DOCK_STATE_DESK) || 176 (mReportedDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || 177 (mReportedDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { 178 whichSound = Settings.Global.DESK_DOCK_SOUND; 179 } else if (mReportedDockState == Intent.EXTRA_DOCK_STATE_CAR) { 180 whichSound = Settings.Global.CAR_DOCK_SOUND; 181 } 182 } 183 184 if (whichSound != null) { 185 final String soundPath = Settings.Global.getString(cr, whichSound); 186 if (soundPath != null) { 187 final Uri soundUri = Uri.parse("file://" + soundPath); 188 if (soundUri != null) { 189 final Ringtone sfx = RingtoneManager.getRingtone( 190 getContext(), soundUri); 191 if (sfx != null) { 192 sfx.setStreamType(AudioManager.STREAM_SYSTEM); 193 sfx.play(); 194 } 195 } 196 } 197 } 198 } 199 200 // Send the dock event intent. 201 // There are many components in the system watching for this so as to 202 // adjust audio routing, screen orientation, etc. 203 getContext().sendStickyBroadcastAsUser(intent, UserHandle.ALL); 204 205 // Release the wake lock that was acquired when the message was posted. 206 mWakeLock.release(); 207 } 208 } 209 210 private final Handler mHandler = new Handler(true /*async*/) { 211 @Override 212 public void handleMessage(Message msg) { 213 switch (msg.what) { 214 case MSG_DOCK_STATE_CHANGED: 215 handleDockStateChange(); 216 break; 217 } 218 } 219 }; 220 221 private final UEventObserver mObserver = new UEventObserver() { 222 @Override 223 public void onUEvent(UEventObserver.UEvent event) { 224 if (Log.isLoggable(TAG, Log.VERBOSE)) { 225 Slog.v(TAG, "Dock UEVENT: " + event.toString()); 226 } 227 228 try { 229 synchronized (mLock) { 230 setActualDockStateLocked(Integer.parseInt(event.get("SWITCH_STATE"))); 231 } 232 } catch (NumberFormatException e) { 233 Slog.e(TAG, "Could not parse switch state from event " + event); 234 } 235 } 236 }; 237 238 private final class BinderService extends Binder { 239 @Override 240 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 241 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 242 != PackageManager.PERMISSION_GRANTED) { 243 pw.println("Permission Denial: can't dump dock observer service from from pid=" 244 + Binder.getCallingPid() 245 + ", uid=" + Binder.getCallingUid()); 246 return; 247 } 248 249 final long ident = Binder.clearCallingIdentity(); 250 try { 251 synchronized (mLock) { 252 if (args == null || args.length == 0 || "-a".equals(args[0])) { 253 pw.println("Current Dock Observer Service state:"); 254 if (mUpdatesStopped) { 255 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)"); 256 } 257 pw.println(" reported state: " + mReportedDockState); 258 pw.println(" previous state: " + mPreviousDockState); 259 pw.println(" actual state: " + mActualDockState); 260 } else if (args.length == 3 && "set".equals(args[0])) { 261 String key = args[1]; 262 String value = args[2]; 263 try { 264 if ("state".equals(key)) { 265 mUpdatesStopped = true; 266 setDockStateLocked(Integer.parseInt(value)); 267 } else { 268 pw.println("Unknown set option: " + key); 269 } 270 } catch (NumberFormatException ex) { 271 pw.println("Bad value: " + value); 272 } 273 } else if (args.length == 1 && "reset".equals(args[0])) { 274 mUpdatesStopped = false; 275 setDockStateLocked(mActualDockState); 276 } else { 277 pw.println("Dump current dock state, or:"); 278 pw.println(" set state <value>"); 279 pw.println(" reset"); 280 } 281 } 282 } finally { 283 Binder.restoreCallingIdentity(ident); 284 } 285 } 286 } 287} 288