blob: 64789d33692d2b15fc3080a70026ca4cc07a4833 [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
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080019import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothDevice;
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;
29import android.os.Message;
Ken Schultzf02c0742009-09-10 18:37:37 -050030import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050031import android.os.UEventObserver;
Dianne Hackborn49493342009-10-02 10:44:41 -070032import android.provider.Settings;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080033import android.server.BluetoothService;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050034import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080035import android.util.Slog;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050036
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050037import java.io.FileNotFoundException;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080038import java.io.FileReader;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050039
40/**
41 * <p>DockObserver monitors for a docking station.
42 */
43class DockObserver extends UEventObserver {
44 private static final String TAG = DockObserver.class.getSimpleName();
45 private static final boolean LOG = false;
46
47 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock";
48 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state";
49
Bernd Holzheybfca3a02010-02-10 17:39:51 +010050 private static final int MSG_DOCK_STATE = 0;
Bernd Holzheybfca3a02010-02-10 17:39:51 +010051
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070052 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050053 private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
54
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070055 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050056
57 private final Context mContext;
58
Ken Schultzf02c0742009-09-10 18:37:37 -050059 private PowerManagerService mPowerManager;
Bernd Holzheybfca3a02010-02-10 17:39:51 +010060
Ken Schultzf02c0742009-09-10 18:37:37 -050061 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050062 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -050063 mPowerManager = pm;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050064 init(); // set initial status
Tobias Haamel27b28b32010-02-09 23:09:17 +010065
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070066 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050067 }
68
69 @Override
70 public void onUEvent(UEventObserver.UEvent event) {
71 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080072 Slog.v(TAG, "Dock UEVENT: " + event.toString());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050073 }
74
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070075 synchronized (this) {
76 try {
77 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
78 if (newState != mDockState) {
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050079 mPreviousDockState = mDockState;
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070080 mDockState = newState;
81 if (mSystemReady) {
Mike Lockwood1d069922009-11-11 18:09:25 -050082 // Don't force screen on when undocking from the desk dock.
83 // The change in power state will do this anyway.
84 // FIXME - we should be configurable.
Jeff Brown1a693182011-11-08 14:44:16 -080085 if ((mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK
86 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_LE_DESK
87 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_HE_DESK) ||
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050088 mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Mike Lockwood1d069922009-11-11 18:09:25 -050089 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(),
90 false, true);
91 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070092 update();
93 }
94 }
95 } catch (NumberFormatException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080096 Slog.e(TAG, "Could not parse switch state from event " + event);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070097 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050098 }
99 }
100
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700101 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500102 char[] buffer = new char[1024];
103
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500104 try {
105 FileReader file = new FileReader(DOCK_STATE_PATH);
106 int len = file.read(buffer, 0, 1024);
Brian Carlstromfd9ddd12010-11-04 11:24:58 -0700107 file.close();
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500108 mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500109 } catch (FileNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800110 Slog.w(TAG, "This kernel does not have dock station support");
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500111 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800112 Slog.e(TAG, "" , e);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500113 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500114 }
115
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700116 void systemReady() {
117 synchronized (this) {
118 // don't bother broadcasting undocked here
119 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
120 update();
121 }
122 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500123 }
124 }
125
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700126 private final void update() {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100127 mHandler.sendEmptyMessage(MSG_DOCK_STATE);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500128 }
129
130 private final Handler mHandler = new Handler() {
131 @Override
132 public void handleMessage(Message msg) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100133 switch (msg.what) {
134 case MSG_DOCK_STATE:
135 synchronized (this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800136 Slog.i(TAG, "Dock state changed: " + mDockState);
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500137
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100138 final ContentResolver cr = mContext.getContentResolver();
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500139
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100140 if (Settings.Secure.getInt(cr,
141 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800142 Slog.i(TAG, "Device not provisioned, skipping dock broadcast");
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100143 return;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -0500144 }
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100145 // Pack up the values and broadcast them to everyone
146 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
147 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
Dianne Hackborn7299c412010-03-04 18:41:49 -0800148 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100149
150 // Check if this is Bluetooth Dock
151 String address = BluetoothService.readDockBluetoothAddress();
152 if (address != null)
153 intent.putExtra(BluetoothDevice.EXTRA_DEVICE,
154 BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address));
155
156 // User feedback to confirm dock connection. Particularly
157 // useful for flaky contact pins...
158 if (Settings.System.getInt(cr,
159 Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1)
160 {
161 String whichSound = null;
162 if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
Praveen Bharathi21e941b2010-10-06 15:23:14 -0500163 if ((mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
164 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
165 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100166 whichSound = Settings.System.DESK_UNDOCK_SOUND;
167 } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) {
168 whichSound = Settings.System.CAR_UNDOCK_SOUND;
169 }
170 } else {
Praveen Bharathi21e941b2010-10-06 15:23:14 -0500171 if ((mDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
172 (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
173 (mDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100174 whichSound = Settings.System.DESK_DOCK_SOUND;
175 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) {
176 whichSound = Settings.System.CAR_DOCK_SOUND;
177 }
178 }
179
180 if (whichSound != null) {
181 final String soundPath = Settings.System.getString(cr, whichSound);
182 if (soundPath != null) {
183 final Uri soundUri = Uri.parse("file://" + soundPath);
184 if (soundUri != null) {
185 final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
Daniel Sandlerec2c88d2010-02-20 01:04:57 -0500186 if (sfx != null) {
187 sfx.setStreamType(AudioManager.STREAM_SYSTEM);
188 sfx.play();
189 }
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100190 }
191 }
192 }
193 }
194
Dianne Hackborn7299c412010-03-04 18:41:49 -0800195 mContext.sendStickyBroadcast(intent);
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100196 }
197 break;
198 }
199 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100200 };
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500201}