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