diff --git a/core/api/system-current.txt b/core/api/system-current.txt index ecbdeaa5fdb8979599d6c69c125f835aae086437..51869b994cf86a9e40859198cbb81d7c52e8f31c 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -6640,6 +6640,7 @@ package android.hardware.usb { method public int getSupportedRoleCombinations(); method public int getUsbDataStatus(); method public boolean isConnected(); + method @FlaggedApi("android.hardware.usb.flags.enable_is_pd_compliant_api") public boolean isPdCompliant(); method public boolean isPowerTransferLimited(); method public boolean isRoleCombinationSupported(int, int); method public void writeToParcel(android.os.Parcel, int); diff --git a/core/java/android/hardware/usb/UsbPortStatus.java b/core/java/android/hardware/usb/UsbPortStatus.java index 4a5c4c8acd25a34ef5fa86ba8e649ce3c2293679..8616b6b15de69ebd77baaa274deadb959ea6f6c9 100644 --- a/core/java/android/hardware/usb/UsbPortStatus.java +++ b/core/java/android/hardware/usb/UsbPortStatus.java @@ -16,13 +16,11 @@ package android.hardware.usb; -import android.Manifest; import android.annotation.CheckResult; import android.annotation.FlaggedApi; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; -import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.hardware.usb.flags.Flags; import android.os.Parcel; @@ -30,7 +28,6 @@ import android.os.Parcelable; import com.android.internal.annotations.Immutable; -import java.lang.StringBuilder; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -579,6 +576,21 @@ public final class UsbPortStatus implements Parcelable { UsbPort.combineRolesAsBit(powerRole, dataRole)) != 0; } + /** + * This function checks if the port is USB Power Delivery (PD) compliant - + * https://www.usb.org/usb-charger-pd. All of the power and data roles must be supported for a + * port to be PD compliant. + * + * @return true if the port is PD compliant. + */ + @FlaggedApi(Flags.FLAG_ENABLE_IS_PD_COMPLIANT_API) + public boolean isPdCompliant() { + return isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_DEVICE) + && isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_HOST) + && isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_DEVICE) + && isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_HOST); + } + /** * Get the supported role combinations. */ diff --git a/core/java/android/hardware/usb/flags/usb_framework_flags.aconfig b/core/java/android/hardware/usb/flags/usb_framework_flags.aconfig new file mode 100644 index 0000000000000000000000000000000000000000..cc56a311e9a43cde1731985202197c1da4079e64 --- /dev/null +++ b/core/java/android/hardware/usb/flags/usb_framework_flags.aconfig @@ -0,0 +1,8 @@ +package: "android.hardware.usb.flags" + +flag { + name: "enable_is_pd_compliant_api" + namespace: "usb" + description: "Feature flag for the api to check if a port is PD compliant" + bug: "323470419" +} diff --git a/tests/UsbManagerTests/Android.bp b/tests/UsbManagerTests/Android.bp index 70c7dad2fd554e55e776fe6524cc9210218bec66..a16a7eafc8e85a850b61798ab1a1d48623df65c0 100644 --- a/tests/UsbManagerTests/Android.bp +++ b/tests/UsbManagerTests/Android.bp @@ -33,6 +33,8 @@ android_test { "platform-test-annotations", "truth", "UsbManagerTestLib", + "flag-junit", + "TestParameterInjector", ], jni_libs: [ // Required for ExtendedMockito diff --git a/tests/UsbManagerTests/src/android/hardware/usb/UsbPortStatusTest.java b/tests/UsbManagerTests/src/android/hardware/usb/UsbPortStatusTest.java new file mode 100644 index 0000000000000000000000000000000000000000..dabfcae8e0fde0cc55e49fc6b73fc4ceb131d651 --- /dev/null +++ b/tests/UsbManagerTests/src/android/hardware/usb/UsbPortStatusTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.usb; + +import static android.hardware.usb.UsbPortStatus.CONTAMINANT_DETECTION_NOT_SUPPORTED; +import static android.hardware.usb.UsbPortStatus.CONTAMINANT_PROTECTION_NONE; +import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE; +import static android.hardware.usb.UsbPortStatus.DATA_ROLE_HOST; +import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE; +import static android.hardware.usb.UsbPortStatus.MODE_NONE; +import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE; +import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK; +import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE; + +import static com.google.common.truth.Truth.assertThat; + +import android.hardware.usb.flags.Flags; +import android.platform.test.annotations.RequiresFlagsEnabled; +import android.platform.test.flag.junit.CheckFlagsRule; +import android.platform.test.flag.junit.DeviceFlagsValueProvider; + +import com.google.testing.junit.testparameterinjector.TestParameter; +import com.google.testing.junit.testparameterinjector.TestParameterInjector; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Tests for {@link android.hardware.usb.UsbPortStatus} */ +@RunWith(TestParameterInjector.class) +public class UsbPortStatusTest { + + @Rule + public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); + + @Test + @RequiresFlagsEnabled(Flags.FLAG_ENABLE_IS_PD_COMPLIANT_API) + public void testIsPdCompliant( + @TestParameter boolean isSinkDeviceRoleSupported, + @TestParameter boolean isSinkHostRoleSupported, + @TestParameter boolean isSourceDeviceRoleSupported, + @TestParameter boolean isSourceHostRoleSupported) { + int supportedRoleCombinations = getSupportedRoleCombinations( + isSinkDeviceRoleSupported, + isSinkHostRoleSupported, + isSourceDeviceRoleSupported, + isSinkHostRoleSupported); + UsbPortStatus usbPortStatus = new UsbPortStatus( + MODE_NONE, + POWER_ROLE_NONE, + DATA_ROLE_NONE, + supportedRoleCombinations, + CONTAMINANT_PROTECTION_NONE, + CONTAMINANT_DETECTION_NOT_SUPPORTED); + boolean expectedResult = isSinkDeviceRoleSupported + && isSinkHostRoleSupported + && isSourceDeviceRoleSupported + && isSourceHostRoleSupported; + + assertThat(usbPortStatus.isPdCompliant()).isEqualTo(expectedResult); + } + + private int getSupportedRoleCombinations( + boolean isSinkDeviceRoleSupported, + boolean isSinkHostRoleSupported, + boolean isSourceDeviceRoleSupported, + boolean isSourceHostRoleSupported) { + int result = UsbPort.combineRolesAsBit(POWER_ROLE_NONE, DATA_ROLE_NONE); + + if (isSinkDeviceRoleSupported) { + result |= UsbPort.combineRolesAsBit(POWER_ROLE_SINK, DATA_ROLE_DEVICE); + } + if (isSinkHostRoleSupported) { + result |= UsbPort.combineRolesAsBit(POWER_ROLE_SINK, DATA_ROLE_HOST); + } + if (isSourceDeviceRoleSupported) { + result |= UsbPort.combineRolesAsBit(POWER_ROLE_SOURCE, DATA_ROLE_DEVICE); + } + if (isSourceHostRoleSupported) { + result |= UsbPort.combineRolesAsBit(POWER_ROLE_SOURCE, DATA_ROLE_HOST); + } + + return result; + } +}