Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 19 | import android.content.ContentResolver; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
Daniel Sandler | ec2c88d | 2010-02-20 01:04:57 -0500 | [diff] [blame] | 23 | import android.media.AudioManager; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 24 | import android.media.Ringtone; |
| 25 | import android.media.RingtoneManager; |
| 26 | import android.net.Uri; |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 27 | import android.os.Binder; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 28 | import android.os.Handler; |
| 29 | import android.os.Message; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 30 | import android.os.PowerManager; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 31 | import android.os.SystemClock; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 32 | import android.os.UEventObserver; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 33 | import android.os.UserHandle; |
Dianne Hackborn | 4949334 | 2009-10-02 10:44:41 -0700 | [diff] [blame] | 34 | import android.provider.Settings; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 35 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 36 | import android.util.Slog; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 37 | |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 38 | import java.io.FileDescriptor; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 39 | import java.io.FileNotFoundException; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 40 | import java.io.FileReader; |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 41 | import java.io.PrintWriter; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 42 | |
| 43 | /** |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 44 | * DockObserver monitors for a docking station. |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 45 | */ |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 46 | final class DockObserver extends SystemService { |
| 47 | private static final String TAG = "DockObserver"; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 48 | |
| 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 Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 52 | private static final int MSG_DOCK_STATE_CHANGED = 0; |
| 53 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 54 | private final PowerManager mPowerManager; |
| 55 | private final PowerManager.WakeLock mWakeLock; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 56 | |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 57 | private final Object mLock = new Object(); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 58 | |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 59 | 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 | |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 68 | private final boolean mAllowTheaterModeWakeFromDock; |
| 69 | |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 70 | public DockObserver(Context context) { |
| 71 | super(context); |
| 72 | |
| 73 | mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 74 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 75 | mAllowTheaterModeWakeFromDock = context.getResources().getBoolean( |
| 76 | com.android.internal.R.bool.config_allowTheaterModeWakeFromDock); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 77 | |
| 78 | init(); // set initial status |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 79 | |
| 80 | mObserver.startObserving(DOCK_UEVENT_MATCH); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 84 | public void onStart() { |
| 85 | publishBinderService(TAG, new BinderService()); |
| 86 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 87 | |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public void onBootPhase(int phase) { |
| 90 | if (phase == PHASE_ACTIVITY_MANAGER_READY) { |
| 91 | synchronized (mLock) { |
| 92 | mSystemReady = true; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 93 | |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 94 | // don't bother broadcasting undocked here |
| 95 | if (mReportedDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 96 | updateLocked(); |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 97 | } |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 98 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 102 | private void init() { |
| 103 | synchronized (mLock) { |
| 104 | try { |
| 105 | char[] buffer = new char[1024]; |
| 106 | FileReader file = new FileReader(DOCK_STATE_PATH); |
| 107 | try { |
| 108 | int len = file.read(buffer, 0, 1024); |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 109 | setActualDockStateLocked(Integer.valueOf((new String(buffer, 0, len)).trim())); |
| 110 | mPreviousDockState = mActualDockState; |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 111 | } finally { |
| 112 | file.close(); |
| 113 | } |
| 114 | } catch (FileNotFoundException e) { |
| 115 | Slog.w(TAG, "This kernel does not have dock station support"); |
| 116 | } catch (Exception e) { |
| 117 | Slog.e(TAG, "" , e); |
| 118 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 119 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 122 | private void setActualDockStateLocked(int newState) { |
| 123 | mActualDockState = newState; |
| 124 | if (!mUpdatesStopped) { |
| 125 | setDockStateLocked(newState); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private void setDockStateLocked(int newState) { |
| 130 | if (newState != mReportedDockState) { |
| 131 | mReportedDockState = newState; |
| 132 | if (mSystemReady) { |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 133 | // Wake up immediately when docked or undocked except in theater mode. |
| 134 | if (mAllowTheaterModeWakeFromDock |
| 135 | || Settings.Global.getInt(getContext().getContentResolver(), |
| 136 | Settings.Global.THEATER_MODE_ON, 0) == 0) { |
Dianne Hackborn | 280a64e | 2015-07-13 14:48:08 -0700 | [diff] [blame] | 137 | mPowerManager.wakeUp(SystemClock.uptimeMillis(), |
| 138 | "android.server:DOCK"); |
Bryce Lee | 584a445 | 2014-10-21 15:55:55 -0700 | [diff] [blame] | 139 | } |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 140 | updateLocked(); |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 141 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 145 | private void updateLocked() { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 146 | mWakeLock.acquire(); |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 147 | mHandler.sendEmptyMessage(MSG_DOCK_STATE_CHANGED); |
| 148 | } |
| 149 | |
| 150 | private void handleDockStateChange() { |
| 151 | synchronized (mLock) { |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 152 | Slog.i(TAG, "Dock state changed from " + mPreviousDockState + " to " |
| 153 | + mReportedDockState); |
| 154 | final int previousDockState = mPreviousDockState; |
| 155 | mPreviousDockState = mReportedDockState; |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 156 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 157 | // Skip the dock intent if not yet provisioned. |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 158 | final ContentResolver cr = getContext().getContentResolver(); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 159 | if (Settings.Global.getInt(cr, |
| 160 | Settings.Global.DEVICE_PROVISIONED, 0) == 0) { |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 161 | Slog.i(TAG, "Device not provisioned, skipping dock broadcast"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | // Pack up the values and broadcast them to everyone |
| 166 | Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); |
| 167 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 168 | intent.putExtra(Intent.EXTRA_DOCK_STATE, mReportedDockState); |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 169 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 170 | // Play a sound to provide feedback to confirm dock connection. |
| 171 | // Particularly useful for flaky contact pins... |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 172 | if (Settings.Global.getInt(cr, |
| 173 | Settings.Global.DOCK_SOUNDS_ENABLED, 1) == 1) { |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 174 | String whichSound = null; |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 175 | if (mReportedDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 176 | if ((previousDockState == Intent.EXTRA_DOCK_STATE_DESK) || |
| 177 | (previousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 178 | (previousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 179 | whichSound = Settings.Global.DESK_UNDOCK_SOUND; |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 180 | } else if (previousDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 181 | whichSound = Settings.Global.CAR_UNDOCK_SOUND; |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 182 | } |
| 183 | } else { |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 184 | if ((mReportedDockState == Intent.EXTRA_DOCK_STATE_DESK) || |
| 185 | (mReportedDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 186 | (mReportedDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 187 | whichSound = Settings.Global.DESK_DOCK_SOUND; |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 188 | } else if (mReportedDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 189 | whichSound = Settings.Global.CAR_DOCK_SOUND; |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | if (whichSound != null) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 194 | final String soundPath = Settings.Global.getString(cr, whichSound); |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 195 | if (soundPath != null) { |
| 196 | final Uri soundUri = Uri.parse("file://" + soundPath); |
| 197 | if (soundUri != null) { |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 198 | final Ringtone sfx = RingtoneManager.getRingtone( |
| 199 | getContext(), soundUri); |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 200 | if (sfx != null) { |
| 201 | sfx.setStreamType(AudioManager.STREAM_SYSTEM); |
| 202 | sfx.play(); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 209 | // Send the dock event intent. |
| 210 | // There are many components in the system watching for this so as to |
| 211 | // adjust audio routing, screen orientation, etc. |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 212 | getContext().sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 213 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 216 | private final Handler mHandler = new Handler(true /*async*/) { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 217 | @Override |
| 218 | public void handleMessage(Message msg) { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 219 | switch (msg.what) { |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 220 | case MSG_DOCK_STATE_CHANGED: |
| 221 | handleDockStateChange(); |
Prashant Malani | 7450614 | 2014-08-06 16:49:53 -0700 | [diff] [blame] | 222 | mWakeLock.release(); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 223 | break; |
| 224 | } |
| 225 | } |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 226 | }; |
Jeff Brown | 010e561 | 2014-05-29 17:48:33 -0700 | [diff] [blame] | 227 | |
| 228 | private final UEventObserver mObserver = new UEventObserver() { |
| 229 | @Override |
| 230 | public void onUEvent(UEventObserver.UEvent event) { |
| 231 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 232 | Slog.v(TAG, "Dock UEVENT: " + event.toString()); |
| 233 | } |
| 234 | |
| 235 | try { |
| 236 | synchronized (mLock) { |
| 237 | setActualDockStateLocked(Integer.parseInt(event.get("SWITCH_STATE"))); |
| 238 | } |
| 239 | } catch (NumberFormatException e) { |
| 240 | Slog.e(TAG, "Could not parse switch state from event " + event); |
| 241 | } |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | private final class BinderService extends Binder { |
| 246 | @Override |
| 247 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 248 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 249 | != PackageManager.PERMISSION_GRANTED) { |
| 250 | pw.println("Permission Denial: can't dump dock observer service from from pid=" |
| 251 | + Binder.getCallingPid() |
| 252 | + ", uid=" + Binder.getCallingUid()); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | final long ident = Binder.clearCallingIdentity(); |
| 257 | try { |
| 258 | synchronized (mLock) { |
| 259 | if (args == null || args.length == 0 || "-a".equals(args[0])) { |
| 260 | pw.println("Current Dock Observer Service state:"); |
| 261 | if (mUpdatesStopped) { |
| 262 | pw.println(" (UPDATES STOPPED -- use 'reset' to restart)"); |
| 263 | } |
| 264 | pw.println(" reported state: " + mReportedDockState); |
| 265 | pw.println(" previous state: " + mPreviousDockState); |
| 266 | pw.println(" actual state: " + mActualDockState); |
| 267 | } else if (args.length == 3 && "set".equals(args[0])) { |
| 268 | String key = args[1]; |
| 269 | String value = args[2]; |
| 270 | try { |
| 271 | if ("state".equals(key)) { |
| 272 | mUpdatesStopped = true; |
| 273 | setDockStateLocked(Integer.parseInt(value)); |
| 274 | } else { |
| 275 | pw.println("Unknown set option: " + key); |
| 276 | } |
| 277 | } catch (NumberFormatException ex) { |
| 278 | pw.println("Bad value: " + value); |
| 279 | } |
| 280 | } else if (args.length == 1 && "reset".equals(args[0])) { |
| 281 | mUpdatesStopped = false; |
| 282 | setDockStateLocked(mActualDockState); |
| 283 | } else { |
| 284 | pw.println("Dump current dock state, or:"); |
| 285 | pw.println(" set state <value>"); |
| 286 | pw.println(" reset"); |
| 287 | } |
| 288 | } |
| 289 | } finally { |
| 290 | Binder.restoreCallingIdentity(ident); |
| 291 | } |
| 292 | } |
| 293 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 294 | } |