blob: 6f050d370b8333b520b85ab1638a9a38f60d4e0a [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;
20
Jeff Brown4f8ecd82012-06-18 18:29:13 -070021import com.android.server.power.PowerManagerService;
22
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080023import android.bluetooth.BluetoothAdapter;
24import android.bluetooth.BluetoothDevice;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050025import android.content.ContentResolver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050026import android.content.Context;
27import android.content.Intent;
Daniel Sandlerec2c88d2010-02-20 01:04:57 -050028import android.media.AudioManager;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050029import android.media.Ringtone;
30import android.media.RingtoneManager;
31import android.net.Uri;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050032import android.os.Handler;
33import android.os.Message;
John Spurlockbc632a22012-07-31 08:28:12 -040034import android.os.RemoteException;
35import android.os.ServiceManager;
Ken Schultzf02c0742009-09-10 18:37:37 -050036import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050037import android.os.UEventObserver;
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 */
49class 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
John Spurlock786546e2012-08-08 11:40:20 -040056 private static final int DEFAULT_DOCK = 1;
57
Bernd Holzheybfca3a02010-02-10 17:39:51 +010058 private static final int MSG_DOCK_STATE = 0;
Bernd Holzheybfca3a02010-02-10 17:39:51 +010059
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070060 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050061 private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
62
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070063 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050064
65 private final Context mContext;
66
Ken Schultzf02c0742009-09-10 18:37:37 -050067 private PowerManagerService mPowerManager;
Bernd Holzheybfca3a02010-02-10 17:39:51 +010068
Ken Schultzf02c0742009-09-10 18:37:37 -050069 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050070 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -050071 mPowerManager = pm;
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
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070083 synchronized (this) {
84 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) {
Mike Lockwood1d069922009-11-11 18:09:25 -050097 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(),
98 false, true);
99 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700100 update();
101 }
102 }
103 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800104 Slog.e(TAG, "Could not parse switch state from event " + event);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700105 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500106 }
107 }
108
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700109 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500110 char[] buffer = new char[1024];
111
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500112 try {
113 FileReader file = new FileReader(DOCK_STATE_PATH);
114 int len = file.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700115 file.close();
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500116 mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500117 } catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800118 Slog.w(TAG, "This kernel does not have dock station support");
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500119 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800120 Slog.e(TAG, "" , e);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500121 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500122 }
123
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700124 void systemReady() {
125 synchronized (this) {
126 // don't bother broadcasting undocked here
127 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
128 update();
129 }
130 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500131 }
132 }
133
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700134 private final void update() {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100135 mHandler.sendEmptyMessage(MSG_DOCK_STATE);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500136 }
137
John Spurlock786546e2012-08-08 11:40:20 -0400138 private static boolean isScreenSaverActivatedOnDock(Context context) {
139 return 0 != Settings.Secure.getInt(
140 context.getContentResolver(), SCREENSAVER_ACTIVATE_ON_DOCK, DEFAULT_DOCK);
141 }
142
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500143 private final Handler mHandler = new Handler() {
144 @Override
145 public void handleMessage(Message msg) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100146 switch (msg.what) {
147 case MSG_DOCK_STATE:
148 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800149 Slog.i(TAG, "Dock state changed: " + mDockState);
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500150
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100151 final ContentResolver cr = mContext.getContentResolver();
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500152
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100153 if (Settings.Secure.getInt(cr,
154 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800155 Slog.i(TAG, "Device not provisioned, skipping dock broadcast");
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100156 return;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500157 }
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100158 // Pack up the values and broadcast them to everyone
159 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
160 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800161 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100162
163 // Check if this is Bluetooth Dock
Jaikumar Ganesh9773ebb2012-01-17 16:51:41 -0800164 // TODO(BT): Get Dock address.
165 String address = null;
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100166 if (address != null)
167 intent.putExtra(BluetoothDevice.EXTRA_DEVICE,
168 BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address));
169
170 // User feedback to confirm dock connection. Particularly
171 // useful for flaky contact pins...
172 if (Settings.System.getInt(cr,
173 Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1)
174 {
175 String whichSound = null;
176 if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Praveen Bharathi21e941b2010-10-06 15:23:14 -0500177 if ((mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
178 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
179 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100180 whichSound = Settings.System.DESK_UNDOCK_SOUND;
181 } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) {
182 whichSound = Settings.System.CAR_UNDOCK_SOUND;
183 }
184 } else {
Praveen Bharathi21e941b2010-10-06 15:23:14 -0500185 if ((mDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
186 (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
187 (mDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100188 whichSound = Settings.System.DESK_DOCK_SOUND;
189 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) {
190 whichSound = Settings.System.CAR_DOCK_SOUND;
191 }
192 }
193
194 if (whichSound != null) {
195 final String soundPath = Settings.System.getString(cr, whichSound);
196 if (soundPath != null) {
197 final Uri soundUri = Uri.parse("file://" + soundPath);
198 if (soundUri != null) {
199 final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
Daniel Sandlerec2c88d2010-02-20 01:04:57 -0500200 if (sfx != null) {
201 sfx.setStreamType(AudioManager.STREAM_SYSTEM);
202 sfx.play();
203 }
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100204 }
205 }
206 }
207 }
208
John Spurlockbc632a22012-07-31 08:28:12 -0400209 IDreamManager mgr = IDreamManager.Stub.asInterface(ServiceManager.getService("dreams"));
210 if (mgr != null) {
211 // dreams feature enabled
212 boolean undocked = mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED;
213 if (undocked) {
214 try {
215 if (mgr.isDreaming()) {
216 mgr.awaken();
217 }
218 } catch (RemoteException e) {
219 Slog.w(TAG, "Unable to awaken!", e);
220 }
221 } else {
John Spurlock786546e2012-08-08 11:40:20 -0400222 if (isScreenSaverActivatedOnDock(mContext)) {
223 try {
224 mgr.dream();
225 } catch (RemoteException e) {
226 Slog.w(TAG, "Unable to dream!", e);
227 }
John Spurlockbc632a22012-07-31 08:28:12 -0400228 }
229 }
230 } else {
231 // dreams feature not enabled, send legacy intent
232 mContext.sendStickyBroadcast(intent);
233 }
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100234 }
235 break;
236 }
237 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100238 };
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500239}