DockObserver.java revision 9773ebb7783ecd29957b43acbe5b26ba459a7458
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 17package com.android.server; 18 19import com.android.server.power.PowerManagerService; 20 21import android.bluetooth.BluetoothAdapter; 22import android.bluetooth.BluetoothDevice; 23import android.content.ContentResolver; 24import android.content.Context; 25import android.content.Intent; 26import android.media.AudioManager; 27import android.media.Ringtone; 28import android.media.RingtoneManager; 29import android.net.Uri; 30import android.os.Handler; 31import android.os.Message; 32import android.os.SystemClock; 33import android.os.UEventObserver; 34import android.provider.Settings; 35import android.util.Log; 36import android.util.Slog; 37 38import java.io.FileNotFoundException; 39import java.io.FileReader; 40 41/** 42 * <p>DockObserver monitors for a docking station. 43 */ 44class DockObserver extends UEventObserver { 45 private static final String TAG = DockObserver.class.getSimpleName(); 46 private static final boolean LOG = false; 47 48 private static final String DOCK_UEVENT_MATCH = "DEVPATH=/devices/virtual/switch/dock"; 49 private static final String DOCK_STATE_PATH = "/sys/class/switch/dock/state"; 50 51 private static final int MSG_DOCK_STATE = 0; 52 53 private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; 54 private int mPreviousDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; 55 56 private boolean mSystemReady; 57 58 private final Context mContext; 59 60 private PowerManagerService mPowerManager; 61 62 public DockObserver(Context context, PowerManagerService pm) { 63 mContext = context; 64 mPowerManager = pm; 65 init(); // set initial status 66 67 startObserving(DOCK_UEVENT_MATCH); 68 } 69 70 @Override 71 public void onUEvent(UEventObserver.UEvent event) { 72 if (Log.isLoggable(TAG, Log.VERBOSE)) { 73 Slog.v(TAG, "Dock UEVENT: " + event.toString()); 74 } 75 76 synchronized (this) { 77 try { 78 int newState = Integer.parseInt(event.get("SWITCH_STATE")); 79 if (newState != mDockState) { 80 mPreviousDockState = mDockState; 81 mDockState = newState; 82 if (mSystemReady) { 83 // Don't force screen on when undocking from the desk dock. 84 // The change in power state will do this anyway. 85 // FIXME - we should be configurable. 86 if ((mPreviousDockState != Intent.EXTRA_DOCK_STATE_DESK 87 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_LE_DESK 88 && mPreviousDockState != Intent.EXTRA_DOCK_STATE_HE_DESK) || 89 mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { 90 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), 91 false, true); 92 } 93 update(); 94 } 95 } 96 } catch (NumberFormatException e) { 97 Slog.e(TAG, "Could not parse switch state from event " + event); 98 } 99 } 100 } 101 102 private final void init() { 103 char[] buffer = new char[1024]; 104 105 try { 106 FileReader file = new FileReader(DOCK_STATE_PATH); 107 int len = file.read(buffer, 0, 1024); 108 file.close(); 109 mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); 110 } catch (FileNotFoundException e) { 111 Slog.w(TAG, "This kernel does not have dock station support"); 112 } catch (Exception e) { 113 Slog.e(TAG, "" , e); 114 } 115 } 116 117 void systemReady() { 118 synchronized (this) { 119 // don't bother broadcasting undocked here 120 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { 121 update(); 122 } 123 mSystemReady = true; 124 } 125 } 126 127 private final void update() { 128 mHandler.sendEmptyMessage(MSG_DOCK_STATE); 129 } 130 131 private final Handler mHandler = new Handler() { 132 @Override 133 public void handleMessage(Message msg) { 134 switch (msg.what) { 135 case MSG_DOCK_STATE: 136 synchronized (this) { 137 Slog.i(TAG, "Dock state changed: " + mDockState); 138 139 final ContentResolver cr = mContext.getContentResolver(); 140 141 if (Settings.Secure.getInt(cr, 142 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { 143 Slog.i(TAG, "Device not provisioned, skipping dock broadcast"); 144 return; 145 } 146 // Pack up the values and broadcast them to everyone 147 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); 148 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 149 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); 150 151 // Check if this is Bluetooth Dock 152 // TODO(BT): Get Dock address. 153 String address = null; 154 if (address != null) 155 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, 156 BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address)); 157 158 // User feedback to confirm dock connection. Particularly 159 // useful for flaky contact pins... 160 if (Settings.System.getInt(cr, 161 Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1) 162 { 163 String whichSound = null; 164 if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { 165 if ((mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) || 166 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || 167 (mPreviousDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { 168 whichSound = Settings.System.DESK_UNDOCK_SOUND; 169 } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) { 170 whichSound = Settings.System.CAR_UNDOCK_SOUND; 171 } 172 } else { 173 if ((mDockState == Intent.EXTRA_DOCK_STATE_DESK) || 174 (mDockState == Intent.EXTRA_DOCK_STATE_LE_DESK) || 175 (mDockState == Intent.EXTRA_DOCK_STATE_HE_DESK)) { 176 whichSound = Settings.System.DESK_DOCK_SOUND; 177 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) { 178 whichSound = Settings.System.CAR_DOCK_SOUND; 179 } 180 } 181 182 if (whichSound != null) { 183 final String soundPath = Settings.System.getString(cr, whichSound); 184 if (soundPath != null) { 185 final Uri soundUri = Uri.parse("file://" + soundPath); 186 if (soundUri != null) { 187 final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); 188 if (sfx != null) { 189 sfx.setStreamType(AudioManager.STREAM_SYSTEM); 190 sfx.play(); 191 } 192 } 193 } 194 } 195 } 196 197 mContext.sendStickyBroadcast(intent); 198 } 199 break; 200 } 201 } 202 }; 203} 204