DockObserver.java revision 008b1762a8d5c908281a832ff90817ade6c7f9f6
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 static android.provider.Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK; 20 21import android.content.ContentResolver; 22import android.content.Context; 23import android.content.Intent; 24import android.media.AudioManager; 25import android.media.Ringtone; 26import android.media.RingtoneManager; 27import android.net.Uri; 28import android.os.Handler; 29import android.os.Looper; 30import android.os.Message; 31import android.os.RemoteException; 32import android.os.ServiceManager; 33import android.os.PowerManager; 34import android.os.SystemClock; 35import android.os.UEventObserver; 36import android.provider.Settings; 37import android.service.dreams.IDreamManager; 38import android.util.Log; 39import android.util.Slog; 40 41import java.io.FileNotFoundException; 42import java.io.FileReader; 43 44/** 45 * <p>DockObserver monitors for a docking station. 46 */ 47final class DockObserver extends UEventObserver { 48 private static final String TAG = DockObserver.class.getSimpleName(); 49 private static final boolean LOG = false; 50 51 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; 52 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; 53 54 private static final int DEFAULT_DOCK = 1; 55 56 private static final int MSG_DOCK_STATE_CHANGED = 0; 57 58 private final Object mLock = new Object(); 59 60 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; 61 private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; 62 63 private boolean mSystemReady; 64 65 private final Context mContext; 66 67 public DockObserver(Context context) { 68 mContext = context; 69 init(); // set initial status 70 71 startObserving(DOCK_UEVENT_MATCH); 72 } 73 74 @Override 75 public void onUEvent(UEventObserver.UEvent event) { 76 if (Log.isLoggable(TAG, Log.VERBOSE)) { 77 Slog.v(TAG, "Dock UEVENT: " + event.toString()); 78 } 79 80 synchronized (mLock) { 81 try { 82 int newState = Integer.parseInt(event.get("SWITCH_STATE")); 83 if (newState != mDockState) { 84 mPreviousDockState = mDockState; 85 mDockState = newState; 86 if (mSystemReady) { 87 // Don't force screen on when undocking from the desk dock. 88 // The change in power state will do this anyway. 89 // FIXME - we should be configurable. 90 if ((mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK 91 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_LE_DESK 92 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_HE_DESK) || 93 mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { 94 PowerManager pm = 95 (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); 96 pm.wakeUp(SystemClock.uptimeMillis()); 97 } 98 updateLocked(); 99 } 100 } 101 } catch (NumberFormatException e) { 102 Slog.e(TAG, "Could not parse switch state from event " + event); 103 } 104 } 105 } 106 107 private void init() { 108 synchronized (mLock) { 109 try { 110 char[] buffer = new char[1024]; 111 FileReader file = new FileReader(DOCK_STATE_PATH); 112 try { 113 int len = file.read(buffer, 0, 1024); 114 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); 115 mPreviousDockState = mDockState; 116 } finally { 117 file.close(); 118 } 119 } catch (FileNotFoundException e) { 120 Slog.w(TAG, "This kernel does not have dock station support"); 121 } catch (Exception e) { 122 Slog.e(TAG, "" , e); 123 } 124 } 125 } 126 127 void systemReady() { 128 synchronized (mLock) { 129 // don't bother broadcasting undocked here 130 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { 131 updateLocked(); 132 } 133 mSystemReady = true; 134 } 135 } 136 137 private void updateLocked() { 138 mHandler.sendEmptyMessage(MSG_DOCK_STATE_CHANGED); 139 } 140 141 private void handleDockStateChange() { 142 synchronized (mLock) { 143 Slog.i(TAG, "Dock state changed: " + mDockState); 144 145 final ContentResolver cr = mContext.getContentResolver(); 146 147 if (Settings.Secure.getInt(cr, 148 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { 149 Slog.i(TAG, "Device not provisioned, skipping dock broadcast"); 150 return; 151 } 152 153 // Pack up the values and broadcast them to everyone 154 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); 155 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 156 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); 157 158 // Check if this is Bluetooth Dock 159 // TODO(BT): Get Dock address. 160 // String address = null; 161 // if (address != null) { 162 // intent.putExtra(BluetoothDevice.EXTRA_DEVICE, 163 // BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address)); 164 // } 165 166 // User feedback to confirm dock connection. Particularly 167 // useful for flaky contact pins... 168 if (Settings.System.getInt(cr, 169 Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1) { 170 String whichSound = null; 171 if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { 172 if ((mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) || 173 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || 174 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { 175 whichSound = Settings.System.DESK_UNDOCK_SOUND; 176 } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) { 177 whichSound = Settings.System.CAR_UNDOCK_SOUND; 178 } 179 } else { 180 if ((mDockState == Intent.EXTRA_DOCK_STATE_DESK) || 181 (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || 182 (mDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { 183 whichSound = Settings.System.DESK_DOCK_SOUND; 184 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) { 185 whichSound = Settings.System.CAR_DOCK_SOUND; 186 } 187 } 188 189 if (whichSound != null) { 190 final String soundPath = Settings.System.getString(cr, whichSound); 191 if (soundPath != null) { 192 final Uri soundUri = Uri.parse("file://" + soundPath); 193 if (soundUri != null) { 194 final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); 195 if (sfx != null) { 196 sfx.setStreamType(AudioManager.STREAM_SYSTEM); 197 sfx.play(); 198 } 199 } 200 } 201 } 202 } 203 204 IDreamManager mgr = IDreamManager.Stub.asInterface(ServiceManager.getService("dreams")); 205 if (mgr != null) { 206 // dreams feature enabled 207 boolean undocked = mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED; 208 if (undocked) { 209 try { 210 if (mgr.isDreaming()) { 211 mgr.awaken(); 212 } 213 } catch (RemoteException e) { 214 Slog.w(TAG, "Unable to awaken!", e); 215 } 216 } else { 217 if (isScreenSaverActivatedOnDock(mContext)) { 218 try { 219 mgr.dream(); 220 } catch (RemoteException e) { 221 Slog.w(TAG, "Unable to dream!", e); 222 } 223 } 224 } 225 } else { 226 // dreams feature not enabled, send legacy intent 227 mContext.sendStickyBroadcast(intent); 228 } 229 } 230 } 231 232 private static boolean isScreenSaverActivatedOnDock(Context context) { 233 return Settings.Secure.getInt(context.getContentResolver(), 234 SCREENSAVER_ACTIVATE_ON_DOCK, DEFAULT_DOCK) != 0; 235 } 236 237 private final Handler mHandler = new Handler(Looper.myLooper(), null, true) { 238 @Override 239 public void handleMessage(Message msg) { 240 switch (msg.what) { 241 case MSG_DOCK_STATE_CHANGED: 242 handleDockStateChange(); 243 break; 244 } 245 } 246 }; 247} 248