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 | |
Jeff Brown | 4f8ecd8 | 2012-06-18 18:29:13 -0700 | [diff] [blame] | 19 | import com.android.server.power.PowerManagerService; |
| 20 | |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 21 | import android.bluetooth.BluetoothAdapter; |
| 22 | import android.bluetooth.BluetoothDevice; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 23 | import android.content.ContentResolver; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Daniel Sandler | ec2c88d | 2010-02-20 01:04:57 -0500 | [diff] [blame] | 26 | import android.media.AudioManager; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 27 | import android.media.Ringtone; |
| 28 | import android.media.RingtoneManager; |
| 29 | import android.net.Uri; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.Message; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 32 | import android.os.SystemClock; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 33 | import android.os.UEventObserver; |
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 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 38 | import java.io.FileNotFoundException; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 39 | import java.io.FileReader; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * <p>DockObserver monitors for a docking station. |
| 43 | */ |
| 44 | class DockObserver extends UEventObserver { |
| 45 | private static final String TAG = DockObserver.class.getSimpleName(); |
| 46 | private static final boolean LOG = false; |
| 47 | |
| 48 | private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; |
| 49 | private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; |
| 50 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 51 | private static final int MSG_DOCK_STATE = 0; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 52 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 53 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 54 | private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 55 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 56 | private boolean mSystemReady; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 57 | |
| 58 | private final Context mContext; |
| 59 | |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 60 | private PowerManagerService mPowerManager; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 61 | |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 62 | public DockObserver(Context context, PowerManagerService pm) { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 63 | mContext = context; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 64 | mPowerManager = pm; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 65 | init(); // set initial status |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 66 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 67 | startObserving(DOCK_UEVENT_MATCH); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void onUEvent(UEventObserver.UEvent event) { |
| 72 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 73 | Slog.v(TAG, "Dock UEVENT: " + event.toString()); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 76 | synchronized (this) { |
| 77 | try { |
| 78 | int newState = Integer.parseInt(event.get("SWITCH_STATE")); |
| 79 | if (newState != mDockState) { |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 80 | mPreviousDockState = mDockState; |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 81 | mDockState = newState; |
| 82 | if (mSystemReady) { |
Mike Lockwood | 1d06992 | 2009-11-11 18:09:25 -0500 | [diff] [blame] | 83 | // Don't force screen on when undocking from the desk dock. |
| 84 | // The change in power state will do this anyway. |
| 85 | // FIXME - we should be configurable. |
Jeff Brown | 1a69318 | 2011-11-08 14:44:16 -0800 | [diff] [blame] | 86 | if ((mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK |
| 87 | && mPreviousDockState != Intent.EXTRA_DOCK_STATE_LE_DESK |
| 88 | && mPreviousDockState != Intent.EXTRA_DOCK_STATE_HE_DESK) || |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 89 | mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
Mike Lockwood | 1d06992 | 2009-11-11 18:09:25 -0500 | [diff] [blame] | 90 | mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), |
| 91 | false, true); |
| 92 | } |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 93 | update(); |
| 94 | } |
| 95 | } |
| 96 | } catch (NumberFormatException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 97 | Slog.e(TAG, "Could not parse switch state from event " + event); |
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 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 102 | private final void init() { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 103 | char[] buffer = new char[1024]; |
| 104 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 105 | try { |
| 106 | FileReader file = new FileReader(DOCK_STATE_PATH); |
| 107 | int len = file.read(buffer, 0, 1024); |
Brian Carlstrom | fd9ddd1 | 2010-11-04 11:24:58 -0700 | [diff] [blame] | 108 | file.close(); |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 109 | mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 110 | } catch (FileNotFoundException e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 111 | Slog.w(TAG, "This kernel does not have dock station support"); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 112 | } catch (Exception e) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 113 | Slog.e(TAG, "" , e); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 114 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 117 | void systemReady() { |
| 118 | synchronized (this) { |
| 119 | // don't bother broadcasting undocked here |
| 120 | if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 121 | update(); |
| 122 | } |
| 123 | mSystemReady = true; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 127 | private final void update() { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 128 | mHandler.sendEmptyMessage(MSG_DOCK_STATE); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | private final Handler mHandler = new Handler() { |
| 132 | @Override |
| 133 | public void handleMessage(Message msg) { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 134 | switch (msg.what) { |
| 135 | case MSG_DOCK_STATE: |
| 136 | synchronized (this) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 137 | Slog.i(TAG, "Dock state changed: " + mDockState); |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 138 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 139 | final ContentResolver cr = mContext.getContentResolver(); |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 140 | |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 141 | if (Settings.Secure.getInt(cr, |
| 142 | Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 143 | Slog.i(TAG, "Device not provisioned, skipping dock broadcast"); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 144 | return; |
Daniel Sandler | 0e9d2af | 2010-01-25 11:33:03 -0500 | [diff] [blame] | 145 | } |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 146 | // Pack up the values and broadcast them to everyone |
| 147 | Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); |
| 148 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 149 | intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 150 | |
| 151 | // Check if this is Bluetooth Dock |
Jaikumar Ganesh | 9773ebb | 2012-01-17 16:51:41 -0800 | [diff] [blame] | 152 | // TODO(BT): Get Dock address. |
| 153 | String address = null; |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 154 | if (address != null) |
| 155 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, |
| 156 | BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address)); |
| 157 | |
| 158 | // User feedback to confirm dock connection. Particularly |
| 159 | // useful for flaky contact pins... |
| 160 | if (Settings.System.getInt(cr, |
| 161 | Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1) |
| 162 | { |
| 163 | String whichSound = null; |
| 164 | if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 165 | if ((mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) || |
| 166 | (mPreviousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 167 | (mPreviousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 168 | whichSound = Settings.System.DESK_UNDOCK_SOUND; |
| 169 | } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
| 170 | whichSound = Settings.System.CAR_UNDOCK_SOUND; |
| 171 | } |
| 172 | } else { |
Praveen Bharathi | 21e941b | 2010-10-06 15:23:14 -0500 | [diff] [blame] | 173 | if ((mDockState == Intent.EXTRA_DOCK_STATE_DESK) || |
| 174 | (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || |
| 175 | (mDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 176 | whichSound = Settings.System.DESK_DOCK_SOUND; |
| 177 | } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) { |
| 178 | whichSound = Settings.System.CAR_DOCK_SOUND; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (whichSound != null) { |
| 183 | final String soundPath = Settings.System.getString(cr, whichSound); |
| 184 | if (soundPath != null) { |
| 185 | final Uri soundUri = Uri.parse("file://" + soundPath); |
| 186 | if (soundUri != null) { |
| 187 | final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); |
Daniel Sandler | ec2c88d | 2010-02-20 01:04:57 -0500 | [diff] [blame] | 188 | if (sfx != null) { |
| 189 | sfx.setStreamType(AudioManager.STREAM_SYSTEM); |
| 190 | sfx.play(); |
| 191 | } |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 197 | mContext.sendStickyBroadcast(intent); |
Bernd Holzhey | bfca3a0 | 2010-02-10 17:39:51 +0100 | [diff] [blame] | 198 | } |
| 199 | break; |
| 200 | } |
| 201 | } |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 202 | }; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 203 | } |