blob: a70134d3f5fb6fdf14aaf8e9b751217b9eb74a1c [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);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -070077 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
78 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -070079 try {
80 mContext.startActivity(intent);
81 } catch (ActivityNotFoundException e) {
82 Log.w(TAG, e.getCause());
83 }
84 }
85 }
86 };
Ken Schultzf02c0742009-09-10 18:37:37 -050087
88 public DockObserver(Context context, PowerManagerService pm) {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050089 mContext = context;
Ken Schultzf02c0742009-09-10 18:37:37 -050090 mPowerManager = pm;
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050091 init(); // set initial status
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -070092 startObserving(DOCK_UEVENT_MATCH);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -050093 }
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 Lockwoodd0e82ce2009-08-27 16:19:07 -0700101 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 Murphyc9f4eaf2009-08-12 15:15:43 -0500113 }
114 }
115
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700116 private final void init() {
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500117 char[] buffer = new char[1024];
118
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500119 try {
120 FileReader file = new FileReader(DOCK_STATE_PATH);
121 int len = file.read(buffer, 0, 1024);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700122 mDockState = Integer.valueOf((new String(buffer, 0, len)).trim());
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500123
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 Murphyc9f4eaf2009-08-12 15:15:43 -0500129 }
130
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700131 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 Murphyc9f4eaf2009-08-12 15:15:43 -0500138 }
139 }
140
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700141 private final void update() {
142 mHandler.sendEmptyMessage(0);
Dan Murphyc9f4eaf2009-08-12 15:15:43 -0500143 }
144
145 private final Handler mHandler = new Handler() {
146 @Override
147 public void handleMessage(Message msg) {
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700148 synchronized (this) {
149 Log.d(TAG, "Broadcasting dock state " + mDockState);
150 // Pack up the values and broadcast them to everyone
Ken Schultzf02c0742009-09-10 18:37:37 -0500151 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), false, true);
Mike Lockwoodd0e82ce2009-08-27 16:19:07 -0700152 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT);
153 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState);
Mike LeBeau1f6c7e62009-09-19 18:06:52 -0700154
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 Murphyc9f4eaf2009-08-12 15:15:43 -0500163 }
164 }
165 };
166}