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; |
Daniel Sandler | ec2c88d | 2010-02-20 01:04:57 -0500 | [diff] [blame] | 22 | import android.media.AudioManager; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 23 | import android.media.Ringtone; |
| 24 | import android.media.RingtoneManager; |
| 25 | import android.net.Uri; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.Message; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 28 | import android.os.PowerManager; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 29 | import android.os.SystemClock; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 30 | import android.os.UEventObserver; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 31 | import android.os.UserHandle; |
Dianne Hackborn | 4949334 | 2009-10-02 10:44:41 -0700 | [diff] [blame] | 32 | import android.provider.Settings; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 33 | import android.util.Log; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 34 | import android.util.Slog; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 35 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 36 | import java.io.FileNotFoundException; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 37 | import java.io.FileReader; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * <p>DockObserver monitors for a docking station. |
| 41 | */ |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 42 | final class DockObserver extends UEventObserver { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 43 | private static final String TAG = DockObserver.class.getSimpleName(); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 44 | |
| 45 | private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; |
| 46 | private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; |
| 47 | |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 48 | private static final int MSG_DOCK_STATE_CHANGED = 0; |
| 49 | |
| 50 | private final Object mLock = new Object(); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 51 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 52 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 53 | private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 54 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 55 | private boolean mSystemReady; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 56 | |
| 57 | private final Context mContext; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 58 | private final PowerManager mPowerManager; |
| 59 | private final PowerManager.WakeLock mWakeLock; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 60 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 61 | public DockObserver(Context context) { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 62 | mContext = context; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 63 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 64 | mPowerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); |
| 65 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 66 | |
| 67 | init(); // set initial status |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 68 | startObserving(DOCK_UEVENT_MATCH); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void onUEvent(UEventObserver.UEvent event) { |
| 73 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 74 | Slog.v(TAG, "Dock UEVENT: " + event.toString()); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 77 | synchronized (mLock) { |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 78 | try { |
| 79 | int newState = Integer.parseInt(event.get("SWITCH_STATE")); |
| 80 | if (newState != mDockState) { |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 81 | mPreviousDockState = mDockState; |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 82 | mDockState = newState; |
| 83 | if (mSystemReady) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 84 | // Wake up immediately when docked or undocked. |
| 85 | mPowerManager.wakeUp(SystemClock.uptimeMillis()); |
| 86 | |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 87 | updateLocked(); |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 91 | Slog.e(TAG, "Could not parse switch state from event " + event); |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 92 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 96 | private void init() { |
| 97 | synchronized (mLock) { |
| 98 | try { |
| 99 | char[] buffer = new char[1024]; |
| 100 | FileReader file = new FileReader(DOCK_STATE_PATH); |
| 101 | try { |
| 102 | int len = file.read(buffer, 0, 1024); |
| 103 | mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 104 | mPreviousDockState = mDockState; |
| 105 | } finally { |
| 106 | file.close(); |
| 107 | } |
| 108 | } catch (FileNotFoundException e) { |
| 109 | Slog.w(TAG, "This kernel does not have dock station support"); |
| 110 | } catch (Exception e) { |
| 111 | Slog.e(TAG, "" , e); |
| 112 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 113 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 116 | void systemReady() { |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 117 | synchronized (mLock) { |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 118 | // don't bother broadcasting undocked here |
| 119 | if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 120 | updateLocked(); |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 121 | } |
| 122 | mSystemReady = true; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 126 | private void updateLocked() { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 127 | mWakeLock.acquire(); |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 128 | mHandler.sendEmptyMessage(MSG_DOCK_STATE_CHANGED); |
| 129 | } |
| 130 | |
| 131 | private void handleDockStateChange() { |
| 132 | synchronized (mLock) { |
| 133 | Slog.i(TAG, "Dock state changed: " + mDockState); |
| 134 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 135 | // Skip the dock intent if not yet provisioned. |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 136 | final ContentResolver cr = mContext.getContentResolver(); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 137 | if (Settings.Global.getInt(cr, |
| 138 | Settings.Global.DEVICE_PROVISIONED, 0) == 0) { |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 139 | Slog.i(TAG, "Device not provisioned, skipping dock broadcast"); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | // Pack up the values and broadcast them to everyone |
| 144 | Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); |
| 145 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 146 | intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); |
| 147 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 148 | // Play a sound to provide feedback to confirm dock connection. |
| 149 | // Particularly useful for flaky contact pins... |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 150 | if (Settings.Global.getInt(cr, |
| 151 | Settings.Global.DOCK_SOUNDS_ENABLED, 1) == 1) { |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 152 | String whichSound = null; |
| 153 | if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 154 | if ((mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) || |
| 155 | (mPreviousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 156 | (mPreviousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 157 | whichSound = Settings.Global.DESK_UNDOCK_SOUND; |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 158 | } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 159 | whichSound = Settings.Global.CAR_UNDOCK_SOUND; |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 160 | } |
| 161 | } else { |
| 162 | if ((mDockState == Intent.EXTRA_DOCK_STATE_DESK) || |
| 163 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 164 | (mDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 165 | whichSound = Settings.Global.DESK_DOCK_SOUND; |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 166 | } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 167 | whichSound = Settings.Global.CAR_DOCK_SOUND; |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | if (whichSound != null) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 172 | final String soundPath = Settings.Global.getString(cr, whichSound); |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 173 | if (soundPath != null) { |
| 174 | final Uri soundUri = Uri.parse("file://" + soundPath); |
| 175 | if (soundUri != null) { |
| 176 | final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); |
| 177 | if (sfx != null) { |
| 178 | sfx.setStreamType(AudioManager.STREAM_SYSTEM); |
| 179 | sfx.play(); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 186 | // Send the dock event intent. |
| 187 | // There are many components in the system watching for this so as to |
| 188 | // adjust audio routing, screen orientation, etc. |
| 189 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 190 | |
| 191 | // Release the wake lock that was acquired when the message was posted. |
| 192 | mWakeLock.release(); |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 193 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 196 | private final Handler mHandler = new Handler(true /*async*/) { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 197 | @Override |
| 198 | public void handleMessage(Message msg) { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 199 | switch (msg.what) { |
Jeff Brown | 008b176 | 2012-08-20 20:15:34 -0700 | [diff] [blame] | 200 | case MSG_DOCK_STATE_CHANGED: |
| 201 | handleDockStateChange(); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | } |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 205 | }; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 206 | } |