Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 000db585 authored by Kiran S's avatar Kiran S Committed by Android (Google) Code Review
Browse files

Merge "Implement isPdCompliant API for UsbPortStatus" into main

parents c715b531 e3680491
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6644,6 +6644,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);
+15 −3
Original line number Diff line number Diff line
@@ -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.
     */
+8 −0
Original line number Diff line number Diff line
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"
}
+2 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ android_test {
        "platform-test-annotations",
        "truth",
        "UsbManagerTestLib",
        "flag-junit",
        "TestParameterInjector",
    ],
    jni_libs: [
        // Required for ExtendedMockito
+99 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}