summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Mike Lockwood <lockwood@android.com> 2011-07-21 13:55:16 -0700
committer Mike Lockwood <lockwood@android.com> 2011-07-21 14:30:36 -0700
commitd462ecf8f82076d21c85bdeeefbd16a1a91d2805 (patch)
tree88d12b0084c53b857ec58baae8c6cf3b1b6fe20d
parentedf0178265efee73da80754d1d76e3d69e1f593f (diff)
USB: Fix handling USB accessory attached when device is booting
Also defer anything that could start an activity from "system ready" to "boot completed" time. Bug: 5051683 Change-Id: I69db751cb991dd247bd0ac3c70a0d84c0d71f365 Signed-off-by: Mike Lockwood <lockwood@android.com>
-rw-r--r--services/java/com/android/server/usb/UsbDeviceManager.java38
-rw-r--r--services/jni/com_android_server_UsbDeviceManager.cpp15
2 files changed, 43 insertions, 10 deletions
diff --git a/services/java/com/android/server/usb/UsbDeviceManager.java b/services/java/com/android/server/usb/UsbDeviceManager.java
index c80cd0a87115..f94b2eb37d9b 100644
--- a/services/java/com/android/server/usb/UsbDeviceManager.java
+++ b/services/java/com/android/server/usb/UsbDeviceManager.java
@@ -19,6 +19,7 @@ package com.android.server.usb;
import android.app.PendingIntent;
import android.app.Notification;
import android.app.NotificationManager;
+import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
@@ -80,6 +81,7 @@ public class UsbDeviceManager {
private static final int MSG_ENABLE_ADB = 1;
private static final int MSG_SET_CURRENT_FUNCTION = 2;
private static final int MSG_SYSTEM_READY = 3;
+ private static final int MSG_BOOT_COMPLETED = 4;
// Delay for debouncing USB disconnects.
// We often get rapid connect/disconnect events when enabling USB functions,
@@ -87,7 +89,7 @@ public class UsbDeviceManager {
private static final int UPDATE_DELAY = 1000;
private UsbHandler mHandler;
- private boolean mSystemReady;
+ private boolean mBootCompleted;
private final Context mContext;
private final ContentResolver mContentResolver;
@@ -141,10 +143,15 @@ public class UsbDeviceManager {
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mHandler = new UsbHandler(thread.getLooper());
+
+ if (nativeIsStartRequested()) {
+ if (DEBUG) Slog.d(TAG, "accessory attached at boot");
+ setCurrentFunction(UsbManager.USB_FUNCTION_ACCESSORY, false);
+ }
}
public void systemReady() {
- mSystemReady = true;
+ if (DEBUG) Slog.d(TAG, "systemReady");
mNotificationManager = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
@@ -236,10 +243,16 @@ public class UsbDeviceManager {
private String mCurrentFunctions;
private String mDefaultFunctions;
private UsbAccessory mCurrentAccessory;
- private boolean mDeferAccessoryAttached;
private int mUsbNotificationId;
private boolean mAdbNotificationShown;
+ private final BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
+ public void onReceive(Context context, Intent intent) {
+ if (DEBUG) Slog.d(TAG, "boot completed");
+ mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
+ }
+ };
+
private static final int NOTIFICATION_NONE = 0;
private static final int NOTIFICATION_MTP = 1;
private static final int NOTIFICATION_PTP = 2;
@@ -285,6 +298,9 @@ public class UsbDeviceManager {
// Watch for USB configuration changes
mUEventObserver.startObserving(USB_STATE_MATCH);
mUEventObserver.startObserving(ACCESSORY_START_MATCH);
+
+ mContext.registerReceiver(mBootCompletedReceiver,
+ new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
} catch (Exception e) {
Slog.e(TAG, "Error initializing UsbHandler", e);
}
@@ -406,11 +422,9 @@ public class UsbDeviceManager {
mCurrentAccessory = new UsbAccessory(strings);
Slog.d(TAG, "entering USB accessory mode: " + mCurrentAccessory);
// defer accessoryAttached if system is not ready
- if (mSystemReady) {
+ if (mBootCompleted) {
mSettingsManager.accessoryAttached(mCurrentAccessory);
- } else {
- mDeferAccessoryAttached = true;
- }
+ } // else handle in mBootCompletedReceiver
} else {
Slog.e(TAG, "nativeGetAccessoryStrings failed");
}
@@ -421,7 +435,7 @@ public class UsbDeviceManager {
setEnabledFunctions(mDefaultFunctions);
if (mCurrentAccessory != null) {
- if (mSystemReady) {
+ if (mBootCompleted) {
mSettingsManager.accessoryDetached(mCurrentAccessory);
}
mCurrentAccessory = null;
@@ -463,7 +477,7 @@ public class UsbDeviceManager {
// restore defaults when USB is disconnected
doSetCurrentFunctions(mDefaultFunctions);
}
- if (mSystemReady) {
+ if (mBootCompleted) {
updateUsbState();
}
break;
@@ -497,7 +511,10 @@ public class UsbDeviceManager {
updateUsbNotification();
updateAdbNotification();
updateUsbState();
- if (mCurrentAccessory != null && mDeferAccessoryAttached) {
+ break;
+ case MSG_BOOT_COMPLETED:
+ mBootCompleted = true;
+ if (mCurrentAccessory != null) {
mSettingsManager.accessoryAttached(mCurrentAccessory);
}
break;
@@ -671,4 +688,5 @@ public class UsbDeviceManager {
private native String[] nativeGetAccessoryStrings();
private native ParcelFileDescriptor nativeOpenAccessory();
+ private native boolean nativeIsStartRequested();
}
diff --git a/services/jni/com_android_server_UsbDeviceManager.cpp b/services/jni/com_android_server_UsbDeviceManager.cpp
index 69541714260e..40f0dbd81430 100644
--- a/services/jni/com_android_server_UsbDeviceManager.cpp
+++ b/services/jni/com_android_server_UsbDeviceManager.cpp
@@ -99,11 +99,26 @@ static jobject android_server_UsbDeviceManager_openAccessory(JNIEnv *env, jobjec
gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
}
+static jboolean android_server_UsbDeviceManager_isStartRequested(JNIEnv *env, jobject thiz)
+{
+ int fd = open(DRIVER_NAME, O_RDWR);
+ if (fd < 0) {
+ LOGE("could not open %s", DRIVER_NAME);
+ return false;
+ }
+ int result = ioctl(fd, ACCESSORY_IS_START_REQUESTED);
+ close(fd);
+ return (result == 1);
+}
+
+
static JNINativeMethod method_table[] = {
{ "nativeGetAccessoryStrings", "()[Ljava/lang/String;",
(void*)android_server_UsbDeviceManager_getAccessoryStrings },
{ "nativeOpenAccessory", "()Landroid/os/ParcelFileDescriptor;",
(void*)android_server_UsbDeviceManager_openAccessory },
+ { "nativeIsStartRequested", "()Z",
+ (void*)android_server_UsbDeviceManager_isStartRequested },
};
int register_android_server_UsbDeviceManager(JNIEnv *env)