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

Commit 0ee6ee0a authored by Philip P. Moltmann's avatar Philip P. Moltmann
Browse files

Expose UsbPort and UsbPortStatus as system API

This is needed by making the setup wizard use only system-api.

Test: Built, switched USB port state
Change-Id: I8e56859a5b36e7de91691522a34f7d6f62dcbb20
Fixes: 115301401
parent 81dc01cf
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -2515,7 +2515,37 @@ package android.hardware.usb {
  }

  public class UsbManager {
    method public java.util.List<android.hardware.usb.UsbPort> getPorts();
    method public void grantPermission(android.hardware.usb.UsbDevice, java.lang.String);
    field public static final java.lang.String ACTION_USB_PORT_CHANGED = "android.hardware.usb.action.USB_PORT_CHANGED";
  }

  public final class UsbPort {
    method public android.hardware.usb.UsbPortStatus getStatus();
    method public void setRoles(int, int);
  }

  public final class UsbPortStatus implements android.os.Parcelable {
    method public int describeContents();
    method public int getCurrentDataRole();
    method public int getCurrentMode();
    method public int getCurrentPowerRole();
    method public int getSupportedRoleCombinations();
    method public boolean isConnected();
    method public boolean isRoleCombinationSupported(int, int);
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator<android.hardware.usb.UsbPortStatus> CREATOR;
    field public static final int DATA_ROLE_DEVICE = 2; // 0x2
    field public static final int DATA_ROLE_HOST = 1; // 0x1
    field public static final int DATA_ROLE_NONE = 0; // 0x0
    field public static final int MODE_AUDIO_ACCESSORY = 4; // 0x4
    field public static final int MODE_DEBUG_ACCESSORY = 8; // 0x8
    field public static final int MODE_DFP = 2; // 0x2
    field public static final int MODE_NONE = 0; // 0x0
    field public static final int MODE_UFP = 1; // 0x1
    field public static final int POWER_ROLE_NONE = 0; // 0x0
    field public static final int POWER_ROLE_SINK = 2; // 0x2
    field public static final int POWER_ROLE_SOURCE = 1; // 0x1
  }

}
+2 −2
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ import android.app.PendingIntent;
import android.content.ComponentName;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbPort;
import android.hardware.usb.ParcelableUsbPort;
import android.hardware.usb.UsbPortStatus;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
@@ -112,7 +112,7 @@ interface IUsbManager
    ParcelFileDescriptor getControlFd(long function);

    /* Gets the list of USB ports. */
    UsbPort[] getPorts();
    List<ParcelableUsbPort> getPorts();

    /* Gets the status of the specified USB port. */
    UsbPortStatus getPortStatus(in String portId);
+2 −2
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015, The Android Open Source Project
 * Copyright (C) 2018, 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.
@@ -16,4 +16,4 @@

package android.hardware.usb;

parcelable UsbPort;
parcelable ParcelableUsbPort;
+87 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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 android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.internal.annotations.Immutable;

/**
 * A parcelable wrapper to send UsbPorts over binders.
 *
 * @hide
 */
@Immutable
public final class ParcelableUsbPort implements Parcelable {
    private final @NonNull String mId;
    private final int mSupportedModes;

    private ParcelableUsbPort(@NonNull String id, int supportedModes) {
        mId = id;
        mSupportedModes = supportedModes;
    }

    /**
     * Create the parcelable version of a {@link UsbPort}.
     *
     * @param port The port to create a parcealable version of
     *
     * @return The parcelable version of the port
     */
    public static @NonNull ParcelableUsbPort of(@NonNull UsbPort port) {
        return new ParcelableUsbPort(port.getId(), port.getSupportedModes());
    }

    /**
     * Create a {@link UsbPort} from this object.
     *
     * @param usbManager A link to the usbManager in the current context
     *
     * @return The UsbPort for this object
     */
    public @NonNull UsbPort getUsbPort(@NonNull UsbManager usbManager) {
        return new UsbPort(usbManager, mId, mSupportedModes);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mId);
        dest.writeInt(mSupportedModes);
    }

    public static final Creator<ParcelableUsbPort> CREATOR =
            new Creator<ParcelableUsbPort>() {
                @Override
                public ParcelableUsbPort createFromParcel(Parcel in) {
                    String id = in.readString();
                    int supportedModes = in.readInt();
                    return new ParcelableUsbPort(id, supportedModes);
                }

                @Override
                public ParcelableUsbPort[] newArray(int size) {
                    return new ParcelableUsbPort[size];
                }
            };
}
+31 −41
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
package android.hardware.usb;

import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresFeature;
import android.annotation.RequiresPermission;
@@ -39,9 +40,10 @@ import android.os.Process;
import android.os.RemoteException;
import android.util.Log;

import com.android.internal.util.Preconditions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

@@ -97,15 +99,11 @@ public class UsbManager {
     * Broadcast Action: A broadcast for USB port changes.
     *
     * This intent is sent when a USB port is added, removed, or changes state.
     * <ul>
     * <li> {@link #EXTRA_PORT} containing the {@link android.hardware.usb.UsbPort}
     * for the port.
     * <li> {@link #EXTRA_PORT_STATUS} containing the {@link android.hardware.usb.UsbPortStatus}
     * for the port, or null if the port has been removed
     * </ul>
     *
     * @hide
     */
    @SystemApi
    @RequiresPermission(Manifest.permission.MANAGE_USB)
    public static final String ACTION_USB_PORT_CHANGED =
            "android.hardware.usb.action.USB_PORT_CHANGED";

@@ -796,34 +794,44 @@ public class UsbManager {
     * device class (which supports all types of ports despite its name).
     * </p>
     *
     * @return The list of USB ports, or null if none.
     * @return The list of USB ports
     *
     * @hide
     */
    @UnsupportedAppUsage
    public UsbPort[] getPorts() {
    @SystemApi
    @RequiresPermission(Manifest.permission.MANAGE_USB)
    public @NonNull List<UsbPort> getPorts() {
        if (mService == null) {
            return null;
            return Collections.emptyList();
        }

        List<ParcelableUsbPort> parcelablePorts;
        try {
            return mService.getPorts();
            parcelablePorts = mService.getPorts();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }

        if (parcelablePorts == null) {
            return Collections.emptyList();
        } else {
            int numPorts = parcelablePorts.size();

            ArrayList<UsbPort> ports = new ArrayList<>(numPorts);
            for (int i = 0; i < numPorts; i++) {
                ports.add(parcelablePorts.get(i).getUsbPort(this));
            }

            return ports;
        }
    }

    /**
     * Gets the status of the specified USB port.
     *
     * @param port The port to query.
     * @return The status of the specified USB port, or null if unknown.
     * Should only be called by {@link UsbPort#getStatus}.
     *
     * @hide
     */
    @UnsupportedAppUsage
    public UsbPortStatus getPortStatus(UsbPort port) {
        Preconditions.checkNotNull(port, "port must not be null");

    UsbPortStatus getPortStatus(UsbPort port) {
        try {
            return mService.getPortStatus(port.getId());
        } catch (RemoteException e) {
@@ -832,29 +840,11 @@ public class UsbManager {
    }

    /**
     * Sets the desired role combination of the port.
     * <p>
     * The supported role combinations depend on what is connected to the port and may be
     * determined by consulting
     * {@link UsbPortStatus#isRoleCombinationSupported UsbPortStatus.isRoleCombinationSupported}.
     * </p><p>
     * Note: This function is asynchronous and may fail silently without applying
     * the requested changes.  If this function does cause a status change to occur then
     * a {@link #ACTION_USB_PORT_CHANGED} broadcast will be sent.
     * </p>
     *
     * @param powerRole The desired power role: {@link UsbPort#POWER_ROLE_SOURCE}
     * or {@link UsbPort#POWER_ROLE_SINK}, or 0 if no power role.
     * @param dataRole The desired data role: {@link UsbPort#DATA_ROLE_HOST}
     * or {@link UsbPort#DATA_ROLE_DEVICE}, or 0 if no data role.
     * Should only be called by {@link UsbPort#setRoles}.
     *
     * @hide
     */
    @UnsupportedAppUsage
    public void setPortRoles(UsbPort port, int powerRole, int dataRole) {
        Preconditions.checkNotNull(port, "port must not be null");
        UsbPort.checkRoles(powerRole, dataRole);

    void setPortRoles(UsbPort port, int powerRole, int dataRole) {
        Log.d(TAG, "setPortRoles Package:" + mContext.getPackageName());
        try {
            mService.setPortRoles(port.getId(), powerRole, dataRole);
Loading