blob: 8bac52c760afd8f0ebba3ae031692be8d1373ecb [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
Jeff Brown4f8ecd82012-06-18 18:29:13 -070019import com.android.server.power.PowerManagerService;
20
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080021import android.bluetooth.BluetoothAdapter;
22import android.bluetooth.BluetoothDevice;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050023import android.content.ContentResolver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050024import android.content.Context;
25import android.content.Intent;
Daniel Sandlerec2c88d2010-02-20 01:04:57 -050026import android.media.AudioManager;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050027import android.media.Ringtone;
28import android.media.RingtoneManager;
29import android.net.Uri;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050030import android.os.Handler;
31import android.os.Message;
John Spurlockbc632a22012-07-31 08:28:12 -040032import android.os.RemoteException;
33import android.os.ServiceManager;
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 */
47class 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
Bernd Holzheybfca3a02010-02-10 17:39:51 +010054 private static final int MSG_DOCK_STATE = 0;
Bernd Holzheybfca3a02010-02-10 17:39:51 +010055
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070056 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050057 private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
58
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070059 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050060
61 private final Context mContext;
62
Ken Schultzf02c0742009-09-10 18:37:37 -050063 private PowerManagerService mPowerManager;
Bernd Holzheybfca3a02010-02-10 17:39:51 +010064
Ken Schultzf02c0742009-09-10 18:37:37 -050065 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050066 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -050067 mPowerManager = pm;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050068 init(); // set initial status
Tobias Haamel27b28b32010-02-09 23:09:17 +010069
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070070 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050071 }
72
73 @Override
74 public void onUEvent(UEventObserver.UEvent event) {
75 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080076 Slog.v(TAG, "Dock UEVENT: " + event.toString());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050077 }
78
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070079 synchronized (this) {
80 try {
81 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
82 if (newState != mDockState) {
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050083 mPreviousDockState = mDockState;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070084 mDockState = newState;
85 if (mSystemReady) {
Mike Lockwood1d069922009-11-11 18:09:25 -050086 // Don't force screen on when undocking from the desk dock.
87 // The change in power state will do this anyway.
88 // FIXME - we should be configurable.
Jeff Brown1a693182011-11-08 14:44:16 -080089 if ((mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK
90 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_LE_DESK
91 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_HE_DESK) ||
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050092 mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Mike Lockwood1d069922009-11-11 18:09:25 -050093 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(),
94 false, true);
95 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070096 update();
97 }
98 }
99 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800100 Slog.e(TAG, "Could not parse switch state from event " + event);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700101 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500102 }
103 }
104
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700105 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500106 char[] buffer = new char[1024];
107
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500108 try {
109 FileReader file = new FileReader(DOCK_STATE_PATH);
110 int len = file.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700111 file.close();
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500112 mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500113 } catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800114 Slog.w(TAG, "This kernel does not have dock station support");
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500115 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800116 Slog.e(TAG, "" , e);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500117 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500118 }
119
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700120 void systemReady() {
121 synchronized (this) {
122 // don't bother broadcasting undocked here
123 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
124 update();
125 }
126 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500127 }
128 }
129
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700130 private final void update() {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100131 mHandler.sendEmptyMessage(MSG_DOCK_STATE);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500132 }
133
134 private final Handler mHandler = new Handler() {
135 @Override
136 public void handleMessage(Message msg) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100137 switch (msg.what) {
138 case MSG_DOCK_STATE:
139 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800140 Slog.i(TAG, "Dock state changed: " + mDockState);
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500141
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100142 final ContentResolver cr = mContext.getContentResolver();
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500143
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100144 if (Settings.Secure.getInt(cr,
145 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800146 Slog.i(TAG, "Device not provisioned, skipping dock broadcast");
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100147 return;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500148 }
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100149 // Pack up the values and broadcast them to everyone
150 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
151 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800152 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100153
154 // Check if this is Bluetooth Dock
Jaikumar Ganesh9773ebb2012-01-17 16:51:41 -0800155 // TODO(BT): Get Dock address.
156 String address = null;
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100157 if (address != null)
158 intent.putExtra(BluetoothDevice.EXTRA_DEVICE,
159 BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address));
160
161 // User feedback to confirm dock connection. Particularly
162 // useful for flaky contact pins...
163 if (Settings.System.getInt(cr,
164 Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1)
165 {
166 String whichSound = null;
167 if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Praveen Bharathi21e941b2010-10-06 15:23:14 -0500168 if ((mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
169 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
170 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100171 whichSound = Settings.System.DESK_UNDOCK_SOUND;
172 } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) {
173 whichSound = Settings.System.CAR_UNDOCK_SOUND;
174 }
175 } else {
Praveen Bharathi21e941b2010-10-06 15:23:14 -0500176 if ((mDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
177 (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
178 (mDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100179 whichSound = Settings.System.DESK_DOCK_SOUND;
180 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) {
181 whichSound = Settings.System.CAR_DOCK_SOUND;
182 }
183 }
184
185 if (whichSound != null) {
186 final String soundPath = Settings.System.getString(cr, whichSound);
187 if (soundPath != null) {
188 final Uri soundUri = Uri.parse("file://" + soundPath);
189 if (soundUri != null) {
190 final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
Daniel Sandlerec2c88d2010-02-20 01:04:57 -0500191 if (sfx != null) {
192 sfx.setStreamType(AudioManager.STREAM_SYSTEM);
193 sfx.play();
194 }
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100195 }
196 }
197 }
198 }
199
John Spurlockbc632a22012-07-31 08:28:12 -0400200 IDreamManager mgr = IDreamManager.Stub.asInterface(ServiceManager.getService("dreams"));
201 if (mgr != null) {
202 // dreams feature enabled
203 boolean undocked = mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED;
204 if (undocked) {
205 try {
206 if (mgr.isDreaming()) {
207 mgr.awaken();
208 }
209 } catch (RemoteException e) {
210 Slog.w(TAG, "Unable to awaken!", e);
211 }
212 } else {
213 try {
214 mgr.dream();
215 } catch (RemoteException e) {
216 Slog.w(TAG, "Unable to dream!", e);
217 }
218 }
219 } else {
220 // dreams feature not enabled, send legacy intent
221 mContext.sendStickyBroadcast(intent);
222 }
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100223 }
224 break;
225 }
226 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100227 };
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500228}