blob: ef09b010320e91772ed5e9c418886e134deeaec9 [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
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050021import android.content.ContentResolver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050022import android.content.Context;
23import android.content.Intent;
Daniel Sandlerec2c88d2010-02-20 01:04:57 -050024import android.media.AudioManager;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050025import android.media.Ringtone;
26import android.media.RingtoneManager;
27import android.net.Uri;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050028import android.os.Handler;
Jeff Brown008b1762012-08-20 20:15:34 -070029import android.os.Looper;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050030import android.os.Message;
John Spurlockbc632a22012-07-31 08:28:12 -040031import android.os.RemoteException;
32import android.os.ServiceManager;
Jeff Brown96307042012-07-27 15:51:34 -070033import android.os.PowerManager;
Ken Schultzf02c0742009-09-10 18:37:37 -050034import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050035import android.os.UEventObserver;
Dianne Hackborn49493342009-10-02 10:44:41 -070036import android.provider.Settings;
John Spurlockbc632a22012-07-31 08:28:12 -040037import android.service.dreams.IDreamManager;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050038import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080039import android.util.Slog;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050040
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050041import java.io.FileNotFoundException;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080042import java.io.FileReader;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050043
44/**
45 * <p>DockObserver monitors for a docking station.
46 */
Jeff Brown008b1762012-08-20 20:15:34 -070047final class DockObserver extends UEventObserver {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050048 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
John Spurlock786546e2012-08-08 11:40:20 -040054 private static final int DEFAULT_DOCK = 1;
55
Jeff Brown008b1762012-08-20 20:15:34 -070056 private static final int MSG_DOCK_STATE_CHANGED = 0;
57
58 private final Object mLock = new Object();
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
Jeff Brown96307042012-07-27 15:51:34 -070067 public DockObserver(Context context) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050068 mContext = context;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050069 init(); // set initial status
Tobias Haamel27b28b32010-02-09 23:09:17 +010070
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070071 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050072 }
73
74 @Override
75 public void onUEvent(UEventObserver.UEvent event) {
76 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080077 Slog.v(TAG, "Dock UEVENT: " + event.toString());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050078 }
79
Jeff Brown008b1762012-08-20 20:15:34 -070080 synchronized (mLock) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070081 try {
82 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
83 if (newState != mDockState) {
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050084 mPreviousDockState = mDockState;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070085 mDockState = newState;
86 if (mSystemReady) {
Mike Lockwood1d069922009-11-11 18:09:25 -050087 // 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.
Jeff Brown1a693182011-11-08 14:44:16 -080090 if ((mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK
91 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_LE_DESK
92 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_HE_DESK) ||
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050093 mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Jeff Brown96307042012-07-27 15:51:34 -070094 PowerManager pm =
95 (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
96 pm.wakeUp(SystemClock.uptimeMillis());
Mike Lockwood1d069922009-11-11 18:09:25 -050097 }
Jeff Brown008b1762012-08-20 20:15:34 -070098 updateLocked();
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070099 }
100 }
101 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800102 Slog.e(TAG, "Could not parse switch state from event " + event);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700103 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500104 }
105 }
106
Jeff Brown008b1762012-08-20 20:15:34 -0700107 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 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500124 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500125 }
126
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700127 void systemReady() {
Jeff Brown008b1762012-08-20 20:15:34 -0700128 synchronized (mLock) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700129 // don't bother broadcasting undocked here
130 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Jeff Brown008b1762012-08-20 20:15:34 -0700131 updateLocked();
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700132 }
133 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500134 }
135 }
136
Jeff Brown008b1762012-08-20 20:15:34 -0700137 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 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500230 }
231
John Spurlock786546e2012-08-08 11:40:20 -0400232 private static boolean isScreenSaverActivatedOnDock(Context context) {
Jeff Brown008b1762012-08-20 20:15:34 -0700233 return Settings.Secure.getInt(context.getContentResolver(),
234 SCREENSAVER_ACTIVATE_ON_DOCK, DEFAULT_DOCK) != 0;
John Spurlock786546e2012-08-08 11:40:20 -0400235 }
236
Jeff Brown008b1762012-08-20 20:15:34 -0700237 private final Handler mHandler = new Handler(Looper.myLooper(), null, true) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500238 @Override
239 public void handleMessage(Message msg) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100240 switch (msg.what) {
Jeff Brown008b1762012-08-20 20:15:34 -0700241 case MSG_DOCK_STATE_CHANGED:
242 handleDockStateChange();
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100243 break;
244 }
245 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100246 };
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500247}