| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.content.ContentResolver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.hardware.Usb; |
| 23 | import android.net.Uri; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
| 26 | import android.os.UEventObserver; |
| 27 | import android.provider.Settings; |
| 28 | import android.util.Log; |
| 29 | import android.util.Slog; |
| 30 | |
| 31 | import java.io.File; |
| 32 | import java.io.FileNotFoundException; |
| 33 | import java.io.FileReader; |
| 34 | import java.util.ArrayList; |
| 35 | |
| 36 | /** |
| 37 | * <p>UsbObserver monitors for changes to USB state. |
| 38 | */ |
| 39 | class UsbObserver extends UEventObserver { |
| 40 | private static final String TAG = UsbObserver.class.getSimpleName(); |
| 41 | private static final boolean LOG = false; |
| 42 | |
| 43 | private static final String USB_CONFIGURATION_MATCH = "DEVPATH=/devices/virtual/switch/usb_configuration"; |
| 44 | private static final String USB_FUNCTIONS_MATCH = "DEVPATH=/devices/virtual/usb_composite/"; |
| 45 | private static final String USB_CONFIGURATION_PATH = "/sys/class/switch/usb_configuration/state"; |
| 46 | private static final String USB_COMPOSITE_CLASS_PATH = "/sys/class/usb_composite"; |
| 47 | |
| 48 | private static final int MSG_UPDATE = 0; |
| 49 | |
| 50 | private int mUsbConfig = 0; |
| 51 | private int mPreviousUsbConfig = 0; |
| 52 | |
| 53 | // lists of enabled and disabled USB functions |
| 54 | private final ArrayList<String> mEnabledFunctions = new ArrayList<String>(); |
| 55 | private final ArrayList<String> mDisabledFunctions = new ArrayList<String>(); |
| 56 | |
| 57 | private boolean mSystemReady; |
| 58 | |
| 59 | private final Context mContext; |
| 60 | |
| 61 | private PowerManagerService mPowerManager; |
| 62 | |
| 63 | public UsbObserver(Context context) { |
| 64 | mContext = context; |
| 65 | init(); // set initial status |
| 66 | |
| 67 | startObserving(USB_CONFIGURATION_MATCH); |
| 68 | startObserving(USB_FUNCTIONS_MATCH); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void onUEvent(UEventObserver.UEvent event) { |
| 73 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 74 | Slog.v(TAG, "USB UEVENT: " + event.toString()); |
| 75 | } |
| 76 | |
| 77 | synchronized (this) { |
| 78 | String switchState = event.get("SWITCH_STATE"); |
| 79 | if (switchState != null) { |
| 80 | try { |
| 81 | int newConfig = Integer.parseInt(switchState); |
| 82 | if (newConfig != mUsbConfig) { |
| 83 | mPreviousUsbConfig = mUsbConfig; |
| 84 | mUsbConfig = newConfig; |
| 85 | // trigger an Intent broadcast |
| 86 | if (mSystemReady) { |
| 87 | update(); |
| 88 | } |
| 89 | } |
| 90 | } catch (NumberFormatException e) { |
| 91 | Slog.e(TAG, "Could not parse switch state from event " + event); |
| 92 | } |
| 93 | } else { |
| 94 | String function = event.get("FUNCTION"); |
| 95 | String enabledStr = event.get("ENABLED"); |
| 96 | if (function != null && enabledStr != null) { |
| 97 | // Note: we do not broadcast a change when a function is enabled or disabled. |
| 98 | // We just record the state change for the next broadcast. |
| 99 | boolean enabled = "1".equals(enabledStr); |
| 100 | if (enabled) { |
| 101 | if (!mEnabledFunctions.contains(function)) { |
| 102 | mEnabledFunctions.add(function); |
| 103 | } |
| 104 | mDisabledFunctions.remove(function); |
| 105 | } else { |
| 106 | if (!mDisabledFunctions.contains(function)) { |
| 107 | mDisabledFunctions.add(function); |
| 108 | } |
| 109 | mEnabledFunctions.remove(function); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | private final void init() { |
| 116 | char[] buffer = new char[1024]; |
| 117 | |
| 118 | try { |
| 119 | FileReader file = new FileReader(USB_CONFIGURATION_PATH); |
| 120 | int len = file.read(buffer, 0, 1024); |
| 121 | mPreviousUsbConfig = mUsbConfig = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 122 | |
| 123 | } catch (FileNotFoundException e) { |
| 124 | Slog.w(TAG, "This kernel does not have USB configuration switch support"); |
| 125 | } catch (Exception e) { |
| 126 | Slog.e(TAG, "" , e); |
| 127 | } |
| 128 | |
| 129 | try { |
| 130 | File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles(); |
| 131 | for (int i = 0; i < files.length; i++) { |
| 132 | File file = new File(files[i], "enable"); |
| 133 | FileReader reader = new FileReader(file); |
| 134 | int len = reader.read(buffer, 0, 1024); |
| 135 | int value = Integer.valueOf((new String(buffer, 0, len)).trim()); |
| 136 | String functionName = files[i].getName(); |
| 137 | if (value == 1) { |
| 138 | mEnabledFunctions.add(functionName); |
| 139 | } else { |
| 140 | mDisabledFunctions.add(functionName); |
| 141 | } |
| 142 | } |
| 143 | } catch (FileNotFoundException e) { |
| 144 | Slog.w(TAG, "This kernel does not have USB composite class support"); |
| 145 | } catch (Exception e) { |
| 146 | Slog.e(TAG, "" , e); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void systemReady() { |
| 151 | synchronized (this) { |
| 152 | update(); |
| 153 | mSystemReady = true; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private final void update() { |
| 158 | mHandler.sendEmptyMessage(MSG_UPDATE); |
| 159 | } |
| 160 | |
| 161 | private final Handler mHandler = new Handler() { |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 162 | private void addEnabledFunctions(Intent intent) { |
| 163 | // include state of all USB functions in our extras |
| 164 | for (int i = 0; i < mEnabledFunctions.size(); i++) { |
| 165 | intent.putExtra(mEnabledFunctions.get(i), Usb.USB_FUNCTION_ENABLED); |
| 166 | } |
| 167 | for (int i = 0; i < mDisabledFunctions.size(); i++) { |
| 168 | intent.putExtra(mDisabledFunctions.get(i), Usb.USB_FUNCTION_DISABLED); |
| 169 | } |
| 170 | } |
| 171 | |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 172 | @Override |
| 173 | public void handleMessage(Message msg) { |
| 174 | switch (msg.what) { |
| 175 | case MSG_UPDATE: |
| 176 | synchronized (this) { |
| 177 | final ContentResolver cr = mContext.getContentResolver(); |
| 178 | |
| 179 | if (Settings.Secure.getInt(cr, |
| 180 | Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { |
| 181 | Slog.i(TAG, "Device not provisioned, skipping USB broadcast"); |
| 182 | return; |
| 183 | } |
| 184 | // Send an Intent containing connected/disconnected state |
| 185 | // and the enabled/disabled state of all USB functions |
| 186 | Intent intent; |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 187 | boolean usbConnected = (mUsbConfig != 0); |
| 188 | if (usbConnected) { |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 189 | intent = new Intent(Usb.ACTION_USB_CONNECTED); |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 190 | addEnabledFunctions(intent); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 191 | } else { |
| 192 | intent = new Intent(Usb.ACTION_USB_DISCONNECTED); |
| 193 | } |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 194 | mContext.sendBroadcast(intent); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 195 | |
| Mike Lockwood | 709981e | 2010-06-28 09:58:58 -0400 | [diff] [blame] | 196 | // send a sticky broadcast for clients interested in both connect and disconnect |
| 197 | intent = new Intent(Usb.ACTION_USB_STATE); |
| 198 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 199 | intent.putExtra(Usb.USB_CONNECTED, usbConnected); |
| 200 | addEnabledFunctions(intent); |
| 201 | mContext.sendStickyBroadcast(intent); |
| Mike Lockwood | 2423607 | 2010-06-23 17:36:36 -0400 | [diff] [blame] | 202 | } |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | }; |
| 207 | } |