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 android.bluetooth.BluetoothAdapter; 20import android.bluetooth.BluetoothDevice; 21import android.content.ContentResolver; 22import android.content.Context; 23import android.content.Intent; 24import android.media.AudioManager; 25import android.media.Ringtone; 26import android.media.RingtoneManager; 27import android.net.Uri; 28import android.os.Handler; 29import android.os.Message; 30import android.os.SystemClock; 31import android.os.UEventObserver; 32import android.provider.Settings; 33import android.server.BluetoothService; 34import android.util.Log; 35import android.util.Slog; 36 37import java.io.FileNotFoundException; 38import java.io.FileReader; 39 40/** 41 * <p>DockObserver monitors for a docking station. 42 */ 43class 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 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 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 mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { 87 mPowerManager.userActivityWithForce(SystemClock.uptimeMillis(), 88 false, true); 89 } 90 update(); 91 } 92 } 93 } catch (NumberFormatException e) { 94 Slog.e(TAG, "Could not parse switch state from event " + event); 95 } 96 } 97 } 98 99 private final void init() { 100 char[] buffer = new char[1024]; 101 102 try { 103 FileReader file = new FileReader(DOCK_STATE_PATH); 104 int len = file.read(buffer, 0, 1024); 105 mPreviousDockState = mDockState = Integer.valueOf((new String(buffer, 0, len)).trim()); 106 107 } catch (FileNotFoundException e) { 108 Slog.w(TAG, "This kernel does not have dock station support"); 109 } catch (Exception e) { 110 Slog.e(TAG, "" , e); 111 } 112 } 113 114 void systemReady() { 115 synchronized (this) { 116 // don't bother broadcasting undocked here 117 if (mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) { 118 update(); 119 } 120 mSystemReady = true; 121 } 122 } 123 124 private final void update() { 125 mHandler.sendEmptyMessage(MSG_DOCK_STATE); 126 } 127 128 private final Handler mHandler = new Handler() { 129 @Override 130 public void handleMessage(Message msg) { 131 switch (msg.what) { 132 case MSG_DOCK_STATE: 133 synchronized (this) { 134 Slog.i(TAG, "Dock state changed: " + mDockState); 135 136 final ContentResolver cr = mContext.getContentResolver(); 137 138 if (Settings.Secure.getInt(cr, 139 Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { 140 Slog.i(TAG, "Device not provisioned, skipping dock broadcast"); 141 return; 142 } 143 // Pack up the values and broadcast them to everyone 144 Intent intent = new Intent(Intent.ACTION_DOCK_EVENT); 145 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 146 intent.putExtra(Intent.EXTRA_DOCK_STATE, mDockState); 147 148 // Check if this is Bluetooth Dock 149 String address = BluetoothService.readDockBluetoothAddress(); 150 if (address != null) 151 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, 152 BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address)); 153 154 // User feedback to confirm dock connection. Particularly 155 // useful for flaky contact pins... 156 if (Settings.System.getInt(cr, 157 Settings.System.DOCK_SOUNDS_ENABLED, 1) == 1) 158 { 159 String whichSound = null; 160 if (mDockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) { 161 if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_DESK) { 162 whichSound = Settings.System.DESK_UNDOCK_SOUND; 163 } else if (mPreviousDockState == Intent.EXTRA_DOCK_STATE_CAR) { 164 whichSound = Settings.System.CAR_UNDOCK_SOUND; 165 } 166 } else { 167 if (mDockState == Intent.EXTRA_DOCK_STATE_DESK) { 168 whichSound = Settings.System.DESK_DOCK_SOUND; 169 } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR) { 170 whichSound = Settings.System.CAR_DOCK_SOUND; 171 } 172 } 173 174 if (whichSound != null) { 175 final String soundPath = Settings.System.getString(cr, whichSound); 176 if (soundPath != null) { 177 final Uri soundUri = Uri.parse("file://" + soundPath); 178 if (soundUri != null) { 179 final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri); 180 if (sfx != null) { 181 sfx.setStreamType(AudioManager.STREAM_SYSTEM); 182 sfx.play(); 183 } 184 } 185 } 186 } 187 } 188 189 mContext.sendStickyBroadcast(intent); 190 } 191 break; 192 } 193 } 194 }; 195} 196