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