blob: 65c39f2ad788f304de601518b65a6e924aa0c3b1 [file] [log] [blame]
Dan Murphyc9f4eaf2009-08-12 15:15:43 -05001/*
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
John Spurlock786546e2012-08-08 11:40:20 -040019import static android.provider.Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK;
John Spurlock1a868b72012-08-22 09:56:51 -040020import static android.provider.Settings.Secure.SCREENSAVER_ENABLED;
John Spurlock786546e2012-08-08 11:40:20 -040021
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050022import android.content.ContentResolver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050023import android.content.Context;
24import android.content.Intent;
Daniel Sandlerec2c88d2010-02-20 01:04:57 -050025import android.media.AudioManager;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050026import android.media.Ringtone;
27import android.media.RingtoneManager;
28import android.net.Uri;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050029import android.os.Handler;
Jeff Brown008b1762012-08-20 20:15:34 -070030import android.os.Looper;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050031import android.os.Message;
John Spurlockbc632a22012-07-31 08:28:12 -040032import android.os.RemoteException;
33import android.os.ServiceManager;
Jeff Brown96307042012-07-27 15:51:34 -070034import android.os.PowerManager;
Ken Schultzf02c0742009-09-10 18:37:37 -050035import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050036import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070037import android.os.UserHandle;
Dianne Hackborn49493342009-10-02 10:44:41 -070038import android.provider.Settings;
John Spurlockbc632a22012-07-31 08:28:12 -040039import android.service.dreams.IDreamManager;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050040import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080041import android.util.Slog;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050042
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050043import java.io.FileNotFoundException;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080044import java.io.FileReader;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050045
46/**
47 * <p>DockObserver monitors for a docking station.
48 */
Jeff Brown008b1762012-08-20 20:15:34 -070049final class DockObserver extends UEventObserver {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050050 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
John Spurlock1a868b72012-08-22 09:56:51 -040056 private static final int DEFAULT_SCREENSAVER_ENABLED = 1;
57 private static final int DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK = 1;
John Spurlock786546e2012-08-08 11:40:20 -040058
Jeff Brown008b1762012-08-20 20:15:34 -070059 private static final int MSG_DOCK_STATE_CHANGED = 0;
60
61 private final Object mLock = new Object();
Bernd Holzheybfca3a02010-02-10 17:39:51 +010062
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070063 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050064 private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
65
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070066 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050067
68 private final Context mContext;
69
Jeff Brown96307042012-07-27 15:51:34 -070070 public DockObserver(Context context) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050071 mContext = context;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050072 init(); // set initial status
Tobias Haamel27b28b32010-02-09 23:09:17 +010073
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070074 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050075 }
76
77 @Override
78 public void onUEvent(UEventObserver.UEvent event) {
79 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080080 Slog.v(TAG, "Dock UEVENT: " + event.toString());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050081 }
82
Jeff Brown008b1762012-08-20 20:15:34 -070083 synchronized (mLock) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070084 try {
85 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
86 if (newState != mDockState) {
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050087 mPreviousDockState = mDockState;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070088 mDockState = newState;
89 if (mSystemReady) {
Mike Lockwood1d069922009-11-11 18:09:25 -050090 // 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.
Jeff Brown1a693182011-11-08 14:44:16 -080093 if ((mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK
94 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_LE_DESK
95 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_HE_DESK) ||
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050096 mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Jeff Brown96307042012-07-27 15:51:34 -070097 PowerManager pm =
98 (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
99 pm.wakeUp(SystemClock.uptimeMillis());
Mike Lockwood1d069922009-11-11 18:09:25 -0500100 }
Jeff Brown008b1762012-08-20 20:15:34 -0700101 updateLocked();
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700102 }
103 }
104 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800105 Slog.e(TAG, "Could not parse switch state from event " + event);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700106 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500107 }
108 }
109
Jeff Brown008b1762012-08-20 20:15:34 -0700110 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 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500127 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500128 }
129
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700130 void systemReady() {
Jeff Brown008b1762012-08-20 20:15:34 -0700131 synchronized (mLock) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700132 // don't bother broadcasting undocked here
133 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Jeff Brown008b1762012-08-20 20:15:34 -0700134 updateLocked();
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700135 }
136 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500137 }
138 }
139
Jeff Brown008b1762012-08-20 20:15:34 -0700140 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.Secure.getInt(cr,
151 Settings.Secure.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.System.getInt(cr,
172 Settings.System.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.System.DESK_UNDOCK_SOUND;
179 } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) {
180 whichSound = Settings.System.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.System.DESK_DOCK_SOUND;
187 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) {
188 whichSound = Settings.System.CAR_DOCK_SOUND;
189 }
190 }
191
192 if (whichSound != null) {
193 final String soundPath = Settings.System.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 {
John Spurlock1a868b72012-08-22 09:56:51 -0400220 if (isScreenSaverEnabled(mContext) && isScreenSaverActivatedOnDock(mContext)) {
Jeff Brown008b1762012-08-20 20:15:34 -0700221 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
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700230 mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
Jeff Brown008b1762012-08-20 20:15:34 -0700231 }
232 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500233 }
234
John Spurlock1a868b72012-08-22 09:56:51 -0400235 private static boolean isScreenSaverEnabled(Context context) {
236 return Settings.Secure.getInt(context.getContentResolver(),
237 SCREENSAVER_ENABLED, DEFAULT_SCREENSAVER_ENABLED) != 0;
238 }
239
John Spurlock786546e2012-08-08 11:40:20 -0400240 private static boolean isScreenSaverActivatedOnDock(Context context) {
Jeff Brown008b1762012-08-20 20:15:34 -0700241 return Settings.Secure.getInt(context.getContentResolver(),
John Spurlock1a868b72012-08-22 09:56:51 -0400242 SCREENSAVER_ACTIVATE_ON_DOCK, DEFAULT_SCREENSAVER_ACTIVATED_ON_DOCK) != 0;
John Spurlock786546e2012-08-08 11:40:20 -0400243 }
244
Jeff Browna2910d02012-08-25 12:29:46 -0700245 private final Handler mHandler = new Handler(true /*async*/) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500246 @Override
247 public void handleMessage(Message msg) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100248 switch (msg.what) {
Jeff Brown008b1762012-08-20 20:15:34 -0700249 case MSG_DOCK_STATE_CHANGED:
250 handleDockStateChange();
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100251 break;
252 }
253 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100254 };
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500255}