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