blob: d05c280093fbf47c820e9140d3a8894ed53c5670 [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
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050019import android.content.ContentResolver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050020import android.content.Context;
21import android.content.Intent;
Jeff Brown010e5612014-05-29 17:48:33 -070022import android.content.pm.PackageManager;
Daniel Sandlerec2c88d2010-02-20 01:04:57 -050023import android.media.AudioManager;
Daniel Sandler0e9d2af2010-01-25 11:33:03 -050024import android.media.Ringtone;
25import android.media.RingtoneManager;
26import android.net.Uri;
Jeff Brown010e5612014-05-29 17:48:33 -070027import android.os.Binder;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050028import android.os.Handler;
29import android.os.Message;
Jeff Brown96307042012-07-27 15:51:34 -070030import android.os.PowerManager;
Ken Schultzf02c0742009-09-10 18:37:37 -050031import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050032import android.os.UEventObserver;
Dianne Hackborn5ac72a22012-08-29 18:32:08 -070033import android.os.UserHandle;
Dianne Hackborn49493342009-10-02 10:44:41 -070034import android.provider.Settings;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050035import android.util.Log;
Joe Onorato8a9b2202010-02-26 18:56:32 -080036import android.util.Slog;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050037
Jeff Brown010e5612014-05-29 17:48:33 -070038import java.io.FileDescriptor;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050039import java.io.FileNotFoundException;
Jaikumar Ganesh3fbf7b62009-12-02 17:28:38 -080040import java.io.FileReader;
Jeff Brown010e5612014-05-29 17:48:33 -070041import java.io.PrintWriter;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050042
43/**
Jeff Brown010e5612014-05-29 17:48:33 -070044 * DockObserver monitors for a docking station.
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050045 */
Jeff Brown010e5612014-05-29 17:48:33 -070046final class DockObserver extends SystemService {
47 private static final String TAG = "DockObserver";
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050048
49 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock";
50 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state";
51
Jeff Brown008b1762012-08-20 20:15:34 -070052 private static final int MSG_DOCK_STATE_CHANGED = 0;
53
Jeff Brown62c82e42012-09-26 01:30:41 -070054 private final PowerManager mPowerManager;
55 private final PowerManager.WakeLock mWakeLock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050056
Jeff Brown010e5612014-05-29 17:48:33 -070057 private final Object mLock = new Object();
Tobias Haamel27b28b32010-02-09 23:09:17 +010058
Jeff Brown010e5612014-05-29 17:48:33 -070059 private boolean mSystemReady;
60
61 private int mActualDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
62
63 private int mReportedDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
64 private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
65
66 private boolean mUpdatesStopped;
67
68 public DockObserver(Context context) {
69 super(context);
70
71 mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
Jeff Brown62c82e42012-09-26 01:30:41 -070072 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
73
74 init(); // set initial status
Jeff Brown010e5612014-05-29 17:48:33 -070075
76 mObserver.startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050077 }
78
79 @Override
Jeff Brown010e5612014-05-29 17:48:33 -070080 public void onStart() {
81 publishBinderService(TAG, new BinderService());
82 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050083
Jeff Brown010e5612014-05-29 17:48:33 -070084 @Override
85 public void onBootPhase(int phase) {
86 if (phase == PHASE_ACTIVITY_MANAGER_READY) {
87 synchronized (mLock) {
88 mSystemReady = true;
Jeff Brown62c82e42012-09-26 01:30:41 -070089
Jeff Brown010e5612014-05-29 17:48:33 -070090 // don't bother broadcasting undocked here
91 if (mReportedDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
92 updateLocked();
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070093 }
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070094 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050095 }
96 }
97
Jeff Brown008b1762012-08-20 20:15:34 -070098 private void init() {
99 synchronized (mLock) {
100 try {
101 char[] buffer = new char[1024];
102 FileReader file = new FileReader(DOCK_STATE_PATH);
103 try {
104 int len = file.read(buffer, 0, 1024);
Jeff Brown010e5612014-05-29 17:48:33 -0700105 setActualDockStateLocked(Integer.valueOf((new String(buffer, 0, len)).trim()));
106 mPreviousDockState = mActualDockState;
Jeff Brown008b1762012-08-20 20:15:34 -0700107 } finally {
108 file.close();
109 }
110 } catch (FileNotFoundException e) {
111 Slog.w(TAG, "This kernel does not have dock station support");
112 } catch (Exception e) {
113 Slog.e(TAG, "" , e);
114 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500115 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500116 }
117
Jeff Brown010e5612014-05-29 17:48:33 -0700118 private void setActualDockStateLocked(int newState) {
119 mActualDockState = newState;
120 if (!mUpdatesStopped) {
121 setDockStateLocked(newState);
122 }
123 }
124
125 private void setDockStateLocked(int newState) {
126 if (newState != mReportedDockState) {
127 mReportedDockState = newState;
128 if (mSystemReady) {
129 // Wake up immediately when docked or undocked.
130 mPowerManager.wakeUp(SystemClock.uptimeMillis());
Jeff Brown008b1762012-08-20 20:15:34 -0700131 updateLocked();
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700132 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500133 }
134 }
135
Jeff Brown008b1762012-08-20 20:15:34 -0700136 private void updateLocked() {
Jeff Brown62c82e42012-09-26 01:30:41 -0700137 mWakeLock.acquire();
Jeff Brown008b1762012-08-20 20:15:34 -0700138 mHandler.sendEmptyMessage(MSG_DOCK_STATE_CHANGED);
139 }
140
141 private void handleDockStateChange() {
142 synchronized (mLock) {
Jeff Brown010e5612014-05-29 17:48:33 -0700143 Slog.i(TAG, "Dock state changed from " + mPreviousDockState + " to "
144 + mReportedDockState);
145 final int previousDockState = mPreviousDockState;
146 mPreviousDockState = mReportedDockState;
Jeff Brown008b1762012-08-20 20:15:34 -0700147
Jeff Brown62c82e42012-09-26 01:30:41 -0700148 // Skip the dock intent if not yet provisioned.
Jeff Brown010e5612014-05-29 17:48:33 -0700149 final ContentResolver cr = getContext().getContentResolver();
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700150 if (Settings.Global.getInt(cr,
151 Settings.Global.DEVICE_PROVISIONED, 0) == 0) {
Jeff Brown008b1762012-08-20 20:15:34 -0700152 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);
Jeff Brown010e5612014-05-29 17:48:33 -0700159 intent.putExtra(Intent.EXTRA_DOCK_STATE, mReportedDockState);
Jeff Brown008b1762012-08-20 20:15:34 -0700160
Jeff Brown62c82e42012-09-26 01:30:41 -0700161 // Play a sound to provide feedback to confirm dock connection.
162 // Particularly useful for flaky contact pins...
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700163 if (Settings.Global.getInt(cr,
164 Settings.Global.DOCK_SOUNDS_ENABLED, 1) == 1) {
Jeff Brown008b1762012-08-20 20:15:34 -0700165 String whichSound = null;
Jeff Brown010e5612014-05-29 17:48:33 -0700166 if (mReportedDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
167 if ((previousDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
168 (previousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
169 (previousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700170 whichSound = Settings.Global.DESK_UNDOCK_SOUND;
Jeff Brown010e5612014-05-29 17:48:33 -0700171 } else if (previousDockState == Intent.EXTRA_DOCK_STATE_CAR) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700172 whichSound = Settings.Global.CAR_UNDOCK_SOUND;
Jeff Brown008b1762012-08-20 20:15:34 -0700173 }
174 } else {
Jeff Brown010e5612014-05-29 17:48:33 -0700175 if ((mReportedDockState == Intent.EXTRA_DOCK_STATE_DESK) ||
176 (mReportedDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) ||
177 (mReportedDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700178 whichSound = Settings.Global.DESK_DOCK_SOUND;
Jeff Brown010e5612014-05-29 17:48:33 -0700179 } else if (mReportedDockState == Intent.EXTRA_DOCK_STATE_CAR) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700180 whichSound = Settings.Global.CAR_DOCK_SOUND;
Jeff Brown008b1762012-08-20 20:15:34 -0700181 }
182 }
183
184 if (whichSound != null) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700185 final String soundPath = Settings.Global.getString(cr, whichSound);
Jeff Brown008b1762012-08-20 20:15:34 -0700186 if (soundPath != null) {
187 final Uri soundUri = Uri.parse("file://" + soundPath);
188 if (soundUri != null) {
Jeff Brown010e5612014-05-29 17:48:33 -0700189 final Ringtone sfx = RingtoneManager.getRingtone(
190 getContext(), soundUri);
Jeff Brown008b1762012-08-20 20:15:34 -0700191 if (sfx != null) {
192 sfx.setStreamType(AudioManager.STREAM_SYSTEM);
193 sfx.play();
194 }
195 }
196 }
197 }
198 }
199
Jeff Brown62c82e42012-09-26 01:30:41 -0700200 // Send the dock event intent.
201 // There are many components in the system watching for this so as to
202 // adjust audio routing, screen orientation, etc.
Jeff Brown010e5612014-05-29 17:48:33 -0700203 getContext().sendStickyBroadcastAsUser(intent, UserHandle.ALL);
Jeff Brown008b1762012-08-20 20:15:34 -0700204 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500205 }
206
Jeff Browna2910d02012-08-25 12:29:46 -0700207 private final Handler mHandler = new Handler(true /*async*/) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500208 @Override
209 public void handleMessage(Message msg) {
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100210 switch (msg.what) {
Jeff Brown008b1762012-08-20 20:15:34 -0700211 case MSG_DOCK_STATE_CHANGED:
212 handleDockStateChange();
Prashant Malani74506142014-08-06 16:49:53 -0700213 mWakeLock.release();
Bernd Holzheybfca3a02010-02-10 17:39:51 +0100214 break;
215 }
216 }
Tobias Haamel27b28b32010-02-09 23:09:17 +0100217 };
Jeff Brown010e5612014-05-29 17:48:33 -0700218
219 private final UEventObserver mObserver = new UEventObserver() {
220 @Override
221 public void onUEvent(UEventObserver.UEvent event) {
222 if (Log.isLoggable(TAG, Log.VERBOSE)) {
223 Slog.v(TAG, "Dock UEVENT: " + event.toString());
224 }
225
226 try {
227 synchronized (mLock) {
228 setActualDockStateLocked(Integer.parseInt(event.get("SWITCH_STATE")));
229 }
230 } catch (NumberFormatException e) {
231 Slog.e(TAG, "Could not parse switch state from event " + event);
232 }
233 }
234 };
235
236 private final class BinderService extends Binder {
237 @Override
238 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
239 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
240 != PackageManager.PERMISSION_GRANTED) {
241 pw.println("Permission Denial: can't dump dock observer service from from pid="
242 + Binder.getCallingPid()
243 + ", uid=" + Binder.getCallingUid());
244 return;
245 }
246
247 final long ident = Binder.clearCallingIdentity();
248 try {
249 synchronized (mLock) {
250 if (args == null || args.length == 0 || "-a".equals(args[0])) {
251 pw.println("Current Dock Observer Service state:");
252 if (mUpdatesStopped) {
253 pw.println(" (UPDATES STOPPED -- use 'reset' to restart)");
254 }
255 pw.println(" reported state: " + mReportedDockState);
256 pw.println(" previous state: " + mPreviousDockState);
257 pw.println(" actual state: " + mActualDockState);
258 } else if (args.length == 3 && "set".equals(args[0])) {
259 String key = args[1];
260 String value = args[2];
261 try {
262 if ("state".equals(key)) {
263 mUpdatesStopped = true;
264 setDockStateLocked(Integer.parseInt(value));
265 } else {
266 pw.println("Unknown set option: " + key);
267 }
268 } catch (NumberFormatException ex) {
269 pw.println("Bad value: " + value);
270 }
271 } else if (args.length == 1 && "reset".equals(args[0])) {
272 mUpdatesStopped = false;
273 setDockStateLocked(mActualDockState);
274 } else {
275 pw.println("Dump current dock state, or:");
276 pw.println(" set state <value>");
277 pw.println(" reset");
278 }
279 }
280 } finally {
281 Binder.restoreCallingIdentity(ident);
282 }
283 }
284 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500285}