| 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 Lockwood | 9092ab4 | 2009-09-16 13:01:32 -0400 | [diff] [blame] | 19 | import android.content.ActivityNotFoundException; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.os.Handler; |
| 23 | import android.os.Message; |
| Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 24 | import android.os.SystemClock; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 25 | import android.os.UEventObserver; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | import java.io.FileReader; |
| 29 | import java.io.FileNotFoundException; |
| 30 | |
| 31 | /** |
| 32 | * <p>DockObserver monitors for a docking station. |
| 33 | */ |
| 34 | class DockObserver extends UEventObserver { |
| 35 | private static final String TAG = DockObserver.class.getSimpleName(); |
| 36 | private static final boolean LOG = false; |
| 37 | |
| 38 | private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; |
| 39 | private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; |
| 40 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 41 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 42 | private boolean mSystemReady; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 43 | |
| 44 | private final Context mContext; |
| 45 | |
| Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 46 | private PowerManagerService mPowerManager; |
| 47 | |
| 48 | public DockObserver(Context context, PowerManagerService pm) { |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 49 | mContext = context; |
| Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 50 | mPowerManager = pm; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 51 | init(); // set initial status |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 52 | startObserving(DOCK_UEVENT_MATCH); |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void onUEvent(UEventObserver.UEvent event) { |
| 57 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 58 | Log.v(TAG, "Dock UEVENT: " + event.toString()); |
| 59 | } |
| 60 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 61 | synchronized (this) { |
| 62 | try { |
| 63 | int newState = Integer.parseInt(event.get("SWITCH_STATE")); |
| 64 | if (newState != mDockState) { |
| 65 | mDockState = newState; |
| 66 | if (mSystemReady) { |
| 67 | update(); |
| 68 | } |
| 69 | } |
| 70 | } catch (NumberFormatException e) { |
| 71 | Log.e(TAG, "Could not parse switch state from event " + event); |
| 72 | } |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 76 | private final void init() { |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 77 | char[] buffer = new char[1024]; |
| 78 | |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 79 | try { |
| 80 | FileReader file = new FileReader(DOCK_STATE_PATH); |
| 81 | int len = file.read(buffer, 0, 1024); |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 82 | mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 83 | |
| 84 | } catch (FileNotFoundException e) { |
| 85 | Log.w(TAG, "This kernel does not have dock station support"); |
| 86 | } catch (Exception e) { |
| 87 | Log.e(TAG, "" , e); |
| 88 | } |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 91 | void systemReady() { |
| 92 | synchronized (this) { |
| 93 | // don't bother broadcasting undocked here |
| 94 | if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 95 | update(); |
| 96 | } |
| 97 | mSystemReady = true; |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 101 | private final void update() { |
| 102 | mHandler.sendEmptyMessage(0); |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private final Handler mHandler = new Handler() { |
| 106 | @Override |
| 107 | public void handleMessage(Message msg) { |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 108 | synchronized (this) { |
| 109 | Log.d(TAG, "Broadcasting dock state " + mDockState); |
| 110 | // Pack up the values and broadcast them to everyone |
| Ken Schultz | f02c074 | 2009-09-10 18:37:37 -0500 | [diff] [blame] | 111 | mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), false, true); |
| Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame] | 112 | Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); |
| 113 | intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); |
| 114 | mContext.sendStickyBroadcast(intent); |
| Mike Lockwood | 9092ab4 | 2009-09-16 13:01:32 -0400 | [diff] [blame] | 115 | |
| 116 | // Launch a dock activity |
| 117 | String category; |
| 118 | switch (mDockState) { |
| 119 | case Intent.EXTRA_DOCK_STATE_CAR: |
| 120 | category = Intent.CATEGORY_CAR_DOCK; |
| 121 | break; |
| 122 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 123 | category = Intent.CATEGORY_DESK_DOCK; |
| 124 | break; |
| 125 | default: |
| 126 | category = null; |
| 127 | break; |
| 128 | } |
| 129 | if (category != null) { |
| 130 | intent = new Intent(Intent.ACTION_MAIN); |
| 131 | intent.addCategory(category); |
| 132 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 133 | try { |
| 134 | mContext.startActivity(intent); |
| 135 | } catch (ActivityNotFoundException e) { |
| 136 | Log.w(TAG, e.getCause()); |
| 137 | } |
| 138 | } |
| Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | }; |
| 142 | } |