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 | |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 20 | import android.app.KeyguardManager; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 21 | import android.bluetooth.BluetoothAdapter; |
| 22 | import android.bluetooth.BluetoothDevice; |
Mike Lockwood | 9092ab4 | 2009-09-16 13:01:32 -0400 | [diff] [blame] | 23 | import android.content.ActivityNotFoundException; |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.os.Handler; |
| 28 | import android.os.Message; |
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 | 4949334 | 2009-10-02 10:44:41 -0700 | [diff] [blame] | 31 | import android.provider.Settings; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 32 | import android.server.BluetoothService; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | |
Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 35 | import com.android.internal.widget.LockPatternUtils; |
| 36 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 37 | import java.io.FileNotFoundException; |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 38 | import java.io.FileReader; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * <p>DockObserver monitors for a docking station. |
| 42 | */ |
| 43 | class 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 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 50 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 51 | private boolean mSystemReady; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 52 | |
| 53 | private final Context mContext; |
| 54 | |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 55 | private PowerManagerService mPowerManager; |
Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 56 | |
| 57 | private KeyguardManager.KeyguardLock mKeyguardLock; |
| 58 | private boolean mKeyguardDisabled; |
| 59 | private LockPatternUtils mLockPatternUtils; |
| 60 | |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 61 | // The broadcast receiver which receives the result of the ordered broadcast sent when |
| 62 | // the dock state changes. The original ordered broadcast is sent with an initial result |
| 63 | // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g., |
| 64 | // to RESULT_CANCELED, then the intent to start a dock app will not be sent. |
| 65 | private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() { |
| 66 | @Override |
| 67 | public void onReceive(Context context, Intent intent) { |
| 68 | if (getResultCode() != Activity.RESULT_OK) { |
| 69 | return; |
| 70 | } |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 71 | |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 72 | // Launch a dock activity |
| 73 | String category; |
| 74 | switch (mDockState) { |
| 75 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 76 | category = Intent.CATEGORY_CAR_DOCK; |
| 77 | break; |
| 78 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 79 | category = Intent.CATEGORY_DESK_DOCK; |
| 80 | break; |
| 81 | default: |
| 82 | category = null; |
| 83 | break; |
| 84 | } |
| 85 | if (category != null) { |
| 86 | intent = new Intent(Intent.ACTION_MAIN); |
| 87 | intent.addCategory(category); |
Dianne Hackborn | 9bfb707 | 2009-09-22 11:37:40 -0700 | [diff] [blame] | 88 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 89 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 90 | try { |
| 91 | mContext.startActivity(intent); |
| 92 | } catch (ActivityNotFoundException e) { |
| 93 | Log.w(TAG, e.getCause()); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | }; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 98 | |
| 99 | public DockObserver(Context context, PowerManagerService pm) { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 100 | mContext = context; |
Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 101 | mPowerManager = pm; |
Jim Miller | 31f90b6 | 2010-01-20 13:35:20 -0800 | [diff] [blame] | 102 | mLockPatternUtils = new LockPatternUtils(context); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 103 | init(); // set initial status |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 104 | startObserving(DOCK_UEVENT_MATCH); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onUEvent(UEventObserver.UEvent event) { |
| 109 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 110 | Log.v(TAG, "Dock UEVENT: " + event.toString()); |
| 111 | } |
| 112 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 113 | synchronized (this) { |
| 114 | try { |
| 115 | int newState = Integer.parseInt(event.get("SWITCH_STATE")); |
| 116 | if (newState != mDockState) { |
Mike Lockwood | 1d06992 | 2009-11-11 18:09:25 -0500 | [diff] [blame] | 117 | int oldState = mDockState; |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 118 | mDockState = newState; |
| 119 | if (mSystemReady) { |
Mike Lockwood | 1d06992 | 2009-11-11 18:09:25 -0500 | [diff] [blame] | 120 | // Don't force screen on when undocking from the desk dock. |
| 121 | // The change in power state will do this anyway. |
| 122 | // FIXME - we should be configurable. |
| 123 | if (oldState != Intent.EXTRA_DOCK_STATE_DESK || |
| 124 | newState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 125 | mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), |
| 126 | false, true); |
| 127 | } |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 128 | update(); |
| 129 | } |
| 130 | } |
| 131 | } catch (NumberFormatException e) { |
| 132 | Log.e(TAG, "Could not parse switch state from event " + event); |
| 133 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 137 | private final void init() { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 138 | char[] buffer = new char[1024]; |
| 139 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 140 | try { |
| 141 | FileReader file = new FileReader(DOCK_STATE_PATH); |
| 142 | int len = file.read(buffer, 0, 1024); |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 143 | mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 144 | |
| 145 | } catch (FileNotFoundException e) { |
| 146 | Log.w(TAG, "This kernel does not have dock station support"); |
| 147 | } catch (Exception e) { |
| 148 | Log.e(TAG, "" , e); |
| 149 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 150 | } |
| 151 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 152 | void systemReady() { |
| 153 | synchronized (this) { |
Mike Lockwood | 733fdf3 | 2009-09-28 19:08:53 -0400 | [diff] [blame] | 154 | KeyguardManager keyguardManager = |
| 155 | (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 156 | mKeyguardLock = keyguardManager.newKeyguardLock(TAG); |
| 157 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 158 | // don't bother broadcasting undocked here |
| 159 | if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 160 | update(); |
| 161 | } |
| 162 | mSystemReady = true; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 166 | private final void update() { |
| 167 | mHandler.sendEmptyMessage(0); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private final Handler mHandler = new Handler() { |
| 171 | @Override |
| 172 | public void handleMessage(Message msg) { |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 173 | synchronized (this) { |
Dianne Hackborn | 4949334 | 2009-10-02 10:44:41 -0700 | [diff] [blame] | 174 | Log.i(TAG, "Dock state changed: " + mDockState); |
| 175 | if (Settings.Secure.getInt(mContext.getContentResolver(), |
| 176 | Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { |
| 177 | Log.i(TAG, "Device not provisioned, skipping dock broadcast"); |
| 178 | return; |
| 179 | } |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 180 | // Pack up the values and broadcast them to everyone |
| 181 | Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 182 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 183 | intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); |
Jaikumar Ganesh | 3fbf7b6 | 2009-12-02 17:28:38 -0800 | [diff] [blame] | 184 | |
| 185 | // Check if this is Bluetooth Dock |
| 186 | String address = BluetoothService.readDockBluetoothAddress(); |
| 187 | if (address != null) |
| 188 | intent.putExtra(BluetoothDevice.EXTRA_DEVICE, |
| 189 | BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address)); |
| 190 | |
Mike LeBeau | 1f6c7e6 | 2009-09-19 18:06:52 -0700 | [diff] [blame] | 191 | // Send the ordered broadcast; the result receiver will receive after all |
| 192 | // broadcasts have been sent. If any broadcast receiver changes the result |
| 193 | // code from the initial value of RESULT_OK, then the result receiver will |
| 194 | // not launch the corresponding dock application. This gives apps a chance |
| 195 | // to override the behavior and stay in their app even when the device is |
| 196 | // placed into a dock. |
| 197 | mContext.sendStickyOrderedBroadcast( |
| 198 | intent, mResultReceiver, null, Activity.RESULT_OK, null, null); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | }; |
| 202 | } |