From 1b8e847a8711e4cef316962543faeff7fc4b9180 Mon Sep 17 00:00:00 2001 From: Badhri Jagan Sridharan Date: Mon, 13 Feb 2017 13:14:40 -0800 Subject: [PATCH 1/2] Fix handling usb dialog for wall chargers. UsbPort.POWER_ROLE_SINK is orthogonal to the type of the charger attached. POWER_ROLE_SINK would be the case for AC charging and USB charging. Therefore query BatteryManager for the charger type. Bug: 34972898 Test: Charging notification should not show for pixel-c chargers. Change-Id: I8dddcd7727b6af973bd173d2c6e325aa4be2ca3a --- .../android/server/usb/UsbDeviceManager.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java index 00c36f989df3b..40541f5a69940 100644 --- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java +++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java @@ -32,6 +32,7 @@ import android.hardware.usb.UsbAccessory; import android.hardware.usb.UsbManager; import android.hardware.usb.UsbPort; import android.hardware.usb.UsbPortStatus; +import android.os.BatteryManager; import android.os.FileUtils; import android.os.Handler; import android.os.Looper; @@ -111,6 +112,7 @@ public class UsbDeviceManager { private static final int MSG_USER_SWITCHED = 5; private static final int MSG_UPDATE_USER_RESTRICTIONS = 6; private static final int MSG_UPDATE_HOST_STATE = 7; + private static final int MSG_UPDATE_CHARGING_STATE = 9; private static final int AUDIO_MODE_SOURCE = 1; @@ -192,6 +194,15 @@ public class UsbDeviceManager { } }; + private final BroadcastReceiver mChargingReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); + boolean usbCharging = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; + mHandler.sendMessage(MSG_UPDATE_CHARGING_STATE, usbCharging); + } + }; + public UsbDeviceManager(Context context, UsbAlsaManager alsaManager) { mContext = context; mUsbAlsaManager = alsaManager; @@ -216,6 +227,8 @@ public class UsbDeviceManager { } mContext.registerReceiver(mHostReceiver, new IntentFilter(UsbManager.ACTION_USB_PORT_CHANGED)); + mContext.registerReceiver(mChargingReceiver, + new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); } private UsbSettingsManager getCurrentSettings() { @@ -330,6 +343,7 @@ public class UsbDeviceManager { private int mUsbNotificationId; private boolean mAdbNotificationShown; private int mCurrentUser = UserHandle.USER_NULL; + private boolean mUsbCharging; public UsbHandler(Looper looper) { super(looper); @@ -766,6 +780,10 @@ public class UsbDeviceManager { mPendingBootBroadcast = true; } break; + case MSG_UPDATE_CHARGING_STATE: + mUsbCharging = (msg.arg1 == 1); + updateUsbNotification(); + break; case MSG_ENABLE_ADB: setAdbEnabled(msg.arg1 == 1); break; @@ -850,7 +868,7 @@ public class UsbDeviceManager { } } else if (mSourcePower) { id = com.android.internal.R.string.usb_supplying_notification_title; - } else if (mHostConnected && mSinkPower) { + } else if (mHostConnected && mSinkPower && mUsbCharging) { id = com.android.internal.R.string.usb_charging_notification_title; } if (id != mUsbNotificationId) { @@ -954,6 +972,7 @@ public class UsbDeviceManager { pw.println(" mHostConnected: " + mHostConnected); pw.println(" mSourcePower: " + mSourcePower); pw.println(" mSinkPower: " + mSinkPower); + pw.println(" mUsbCharging: " + mUsbCharging); try { pw.println(" Kernel state: " + FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim()); From c584d77b93a4b9f1f0e216427efa32594a6eea28 Mon Sep 17 00:00:00 2001 From: Badhri Jagan Sridharan Date: Thu, 9 Feb 2017 19:45:21 -0800 Subject: [PATCH 2/2] Introduce debounce to filter rapidly toggling type-c ports Type-c ports can quickly toggle between connected/disconnected states. Introduce debounce to prevent sending spurious notifications. Bug: 34972898 Test: notification should not be queued for a pixel-c charger not connected to the power outlet. Change-Id: I4aa19f9f864fe5b77e65f6a07a3184d8aba1f5fc --- .../usb/java/com/android/server/usb/UsbDeviceManager.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java index 40541f5a69940..99eb3d26aba7b 100644 --- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java +++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java @@ -442,7 +442,10 @@ public class UsbDeviceManager { args.argi2 = sourcePower ? 1 :0; args.argi3 = sinkPower ? 1 :0; - obtainMessage(MSG_UPDATE_HOST_STATE, args).sendToTarget(); + removeMessages(MSG_UPDATE_HOST_STATE); + Message msg = obtainMessage(MSG_UPDATE_HOST_STATE, args); + // debounce rapid transitions of connect/disconnect on type-c ports + sendMessageDelayed(msg, UPDATE_DELAY); } private boolean waitForState(String state) {