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