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