blob: 8a4b45d9f3d4ec5775e2c23880f14eced2e45b54 [file] [log] [blame]
Dan Murphyc9f4eaf2009-08-12 15:15:43 -05001/*
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
17package com.android.server;
18
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070019import android.app.Activity;
Mike Lockwood9092ab42009-09-16 13:01:32 -040020import android.content.ActivityNotFoundException;
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070021import android.content.BroadcastReceiver;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050022import android.content.Context;
23import android.content.Intent;
24import android.os.Handler;
25import android.os.Message;
Ken Schultzf02c0742009-09-10 18:37:37 -050026import android.os.SystemClock;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050027import android.os.UEventObserver;
28import android.util.Log;
29
30import java.io.FileReader;
31import java.io.FileNotFoundException;
32
33/**
34 * <p>DockObserver monitors for a docking station.
35 */
36class 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 Lockwoodd0e82ce2009-08-27 16:19:07 -070043 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
44 private boolean mSystemReady;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050045
46 private final Context mContext;
47
Ken Schultzf02c0742009-09-10 18:37:37 -050048 private PowerManagerService mPowerManager;
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070049
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);
77 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
78 try {
79 mContext.startActivity(intent);
80 } catch (ActivityNotFoundException e) {
81 Log.w(TAG, e.getCause());
82 }
83 }
84 }
85 };
Ken Schultzf02c0742009-09-10 18:37:37 -050086
87 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050088 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -050089 mPowerManager = pm;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050090 init(); // set initial status
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070091 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050092 }
93
94 @Override
95 public void onUEvent(UEventObserver.UEvent event) {
96 if (Log.isLoggable(TAG, Log.VERBOSE)) {
97 Log.v(TAG, "Dock UEVENT: " + event.toString());
98 }
99
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700100 synchronized (this) {
101 try {
102 int newState = Integer.parseInt(event.get("SWITCH_STATE"));
103 if (newState != mDockState) {
104 mDockState = newState;
105 if (mSystemReady) {
106 update();
107 }
108 }
109 } catch (NumberFormatException e) {
110 Log.e(TAG, "Could not parse switch state from event " + event);
111 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500112 }
113 }
114
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700115 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500116 char[] buffer = new char[1024];
117
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500118 try {
119 FileReader file = new FileReader(DOCK_STATE_PATH);
120 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700121 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500122
123 } catch (FileNotFoundException e) {
124 Log.w(TAG, "This kernel does not have dock station support");
125 } catch (Exception e) {
126 Log.e(TAG, "" , e);
127 }
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500128 }
129
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700130 void systemReady() {
131 synchronized (this) {
132 // don't bother broadcasting undocked here
133 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
134 update();
135 }
136 mSystemReady = true;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500137 }
138 }
139
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700140 private final void update() {
141 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500142 }
143
144 private final Handler mHandler = new Handler() {
145 @Override
146 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700147 synchronized (this) {
148 Log.d(TAG, "Broadcasting dock state " + mDockState);
149 // Pack up the values and broadcast them to everyone
Ken Schultzf02c0742009-09-10 18:37:37 -0500150 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), false, true);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700151 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
152 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -0700153
154 // Send the ordered broadcast; the result receiver will receive after all
155 // broadcasts have been sent. If any broadcast receiver changes the result
156 // code from the initial value of RESULT_OK, then the result receiver will
157 // not launch the corresponding dock application. This gives apps a chance
158 // to override the behavior and stay in their app even when the device is
159 // placed into a dock.
160 mContext.sendStickyOrderedBroadcast(
161 intent, mResultReceiver, null, Activity.RESULT_OK, null, null);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500162 }
163 }
164 };
165}