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 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.os.Handler; |
| 22 | import android.os.Message; |
| 23 | import android.os.UEventObserver; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import java.io.FileReader; |
| 27 | import java.io.FileNotFoundException; |
| 28 | |
| 29 | /** |
| 30 | * <p>DockObserver monitors for a docking station. |
| 31 | */ |
| 32 | class DockObserver extends UEventObserver { |
| 33 | private static final String TAG = DockObserver.class.getSimpleName(); |
| 34 | private static final boolean LOG = false; |
| 35 | |
| 36 | private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; |
| 37 | private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; |
| 38 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame^] | 39 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 40 | private boolean mSystemReady; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 41 | |
| 42 | private final Context mContext; |
| 43 | |
| 44 | public DockObserver(Context context) { |
| 45 | mContext = context; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 46 | init(); // set initial status |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame^] | 47 | startObserving(DOCK_UEVENT_MATCH); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public void onUEvent(UEventObserver.UEvent event) { |
| 52 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 53 | Log.v(TAG, "Dock UEVENT: " + event.toString()); |
| 54 | } |
| 55 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame^] | 56 | synchronized (this) { |
| 57 | try { |
| 58 | int newState = Integer.parseInt(event.get("SWITCH_STATE")); |
| 59 | if (newState != mDockState) { |
| 60 | mDockState = newState; |
| 61 | if (mSystemReady) { |
| 62 | update(); |
| 63 | } |
| 64 | } |
| 65 | } catch (NumberFormatException e) { |
| 66 | Log.e(TAG, "Could not parse switch state from event " + event); |
| 67 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame^] | 71 | private final void init() { |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 72 | char[] buffer = new char[1024]; |
| 73 | |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 74 | try { |
| 75 | FileReader file = new FileReader(DOCK_STATE_PATH); |
| 76 | int len = file.read(buffer, 0, 1024); |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame^] | 77 | mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 78 | |
| 79 | } catch (FileNotFoundException e) { |
| 80 | Log.w(TAG, "This kernel does not have dock station support"); |
| 81 | } catch (Exception e) { |
| 82 | Log.e(TAG, "" , e); |
| 83 | } |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame^] | 86 | void systemReady() { |
| 87 | synchronized (this) { |
| 88 | // don't bother broadcasting undocked here |
| 89 | if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { |
| 90 | update(); |
| 91 | } |
| 92 | mSystemReady = true; |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame^] | 96 | private final void update() { |
| 97 | mHandler.sendEmptyMessage(0); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private final Handler mHandler = new Handler() { |
| 101 | @Override |
| 102 | public void handleMessage(Message msg) { |
Mike Lockwood | d0e82ce | 2009-08-27 16:19:07 -0700 | [diff] [blame^] | 103 | synchronized (this) { |
| 104 | Log.d(TAG, "Broadcasting dock state " + mDockState); |
| 105 | // Pack up the values and broadcast them to everyone |
| 106 | Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); |
| 107 | intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); |
| 108 | mContext.sendStickyBroadcast(intent); |
Dan Murphy | c9f4eaf | 2009-08-12 15:15:43 -0500 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | }; |
| 112 | } |