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

Commit ca687ac3 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Change requestWait API according to request" into oc-dev

parents 087daa0d 9b6dd2b2
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -15950,7 +15950,7 @@ package android.hardware.usb {
    method public java.lang.String getSerial();
    method public java.lang.String getSerial();
    method public boolean releaseInterface(android.hardware.usb.UsbInterface);
    method public boolean releaseInterface(android.hardware.usb.UsbInterface);
    method public android.hardware.usb.UsbRequest requestWait();
    method public android.hardware.usb.UsbRequest requestWait();
    method public android.hardware.usb.UsbRequest requestWait(int);
    method public android.hardware.usb.UsbRequest requestWait(long) throws java.util.concurrent.TimeoutException;
    method public boolean setConfiguration(android.hardware.usb.UsbConfiguration);
    method public boolean setConfiguration(android.hardware.usb.UsbConfiguration);
    method public boolean setInterface(android.hardware.usb.UsbInterface);
    method public boolean setInterface(android.hardware.usb.UsbInterface);
  }
  }
+1 −1
Original line number Original line Diff line number Diff line
@@ -17440,7 +17440,7 @@ package android.hardware.usb {
    method public java.lang.String getSerial();
    method public java.lang.String getSerial();
    method public boolean releaseInterface(android.hardware.usb.UsbInterface);
    method public boolean releaseInterface(android.hardware.usb.UsbInterface);
    method public android.hardware.usb.UsbRequest requestWait();
    method public android.hardware.usb.UsbRequest requestWait();
    method public android.hardware.usb.UsbRequest requestWait(int);
    method public android.hardware.usb.UsbRequest requestWait(long) throws java.util.concurrent.TimeoutException;
    method public boolean resetDevice();
    method public boolean resetDevice();
    method public boolean setConfiguration(android.hardware.usb.UsbConfiguration);
    method public boolean setConfiguration(android.hardware.usb.UsbConfiguration);
    method public boolean setInterface(android.hardware.usb.UsbInterface);
    method public boolean setInterface(android.hardware.usb.UsbInterface);
+1 −1
Original line number Original line Diff line number Diff line
@@ -16005,7 +16005,7 @@ package android.hardware.usb {
    method public java.lang.String getSerial();
    method public java.lang.String getSerial();
    method public boolean releaseInterface(android.hardware.usb.UsbInterface);
    method public boolean releaseInterface(android.hardware.usb.UsbInterface);
    method public android.hardware.usb.UsbRequest requestWait();
    method public android.hardware.usb.UsbRequest requestWait();
    method public android.hardware.usb.UsbRequest requestWait(int);
    method public android.hardware.usb.UsbRequest requestWait(long) throws java.util.concurrent.TimeoutException;
    method public boolean setConfiguration(android.hardware.usb.UsbConfiguration);
    method public boolean setConfiguration(android.hardware.usb.UsbConfiguration);
    method public boolean setInterface(android.hardware.usb.UsbInterface);
    method public boolean setInterface(android.hardware.usb.UsbInterface);
  }
  }
+32 −15
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.SystemApi;
import android.content.Context;
import android.content.Context;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;


import com.android.internal.util.Preconditions;
import com.android.internal.util.Preconditions;
@@ -27,7 +28,9 @@ import com.android.internal.util.Preconditions;
import dalvik.system.CloseGuard;
import dalvik.system.CloseGuard;


import java.io.FileDescriptor;
import java.io.FileDescriptor;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeoutException;


/**
/**
 * This class is used for sending and receiving data and control messages to a USB device.
 * This class is used for sending and receiving data and control messages to a USB device.
@@ -268,16 +271,29 @@ public class UsbDeviceConnection {
     *
     *
     * @return a completed USB request, or null if an error occurred
     * @return a completed USB request, or null if an error occurred
     *
     *
     * @throws IllegalArgumentException if the number of bytes read or written is more than the
     * @throws IllegalArgumentException Before API {@value Build.VERSION_CODES#O}: if the number of
     *                                  limit of the request's buffer. The number of bytes is
     *                                  bytes read or written is more than the limit of the
     *                                  determined by the {@code length} parameter of
     *                                  request's buffer. The number of bytes is determined by the
     *                                  {@code length} parameter of
     *                                  {@link UsbRequest#queue(ByteBuffer, int)}
     * @throws BufferOverflowException In API {@value Build.VERSION_CODES#O} and after: if the
     *                                 number of bytes read or written is more than the limit of the
     *                                 request's buffer. The number of bytes is determined by the
     *                                 {@code length} parameter of
     *                                 {@link UsbRequest#queue(ByteBuffer, int)}
     *                                 {@link UsbRequest#queue(ByteBuffer, int)}
     */
     */
    public UsbRequest requestWait() {
    public UsbRequest requestWait() {
        UsbRequest request = null;
        try {
            // -1 is special value indicating infinite wait
            // -1 is special value indicating infinite wait
        UsbRequest request = native_request_wait(-1);
            request = native_request_wait(-1);
        } catch (TimeoutException e) {
            // Does not happen, infinite timeout
        }

        if (request != null) {
        if (request != null) {
            request.dequeue();
            request.dequeue(
                    mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O);
        }
        }
        return request;
        return request;
    }
    }
@@ -290,24 +306,25 @@ public class UsbDeviceConnection {
     * android.hardware.usb.UsbRequest#getClientData} can be useful in determining how to process
     * android.hardware.usb.UsbRequest#getClientData} can be useful in determining how to process
     * the result of this function.</p>
     * the result of this function.</p>
     * <p>Android processes {@link UsbRequest UsbRequests} asynchronously. Hence it is not
     * <p>Android processes {@link UsbRequest UsbRequests} asynchronously. Hence it is not
     * guaranteed that {@link #requestWait(int) requestWait(0)} returns a request that has been
     * guaranteed that {@link #requestWait(long) requestWait(0)} returns a request that has been
     * queued right before even if the request could have been processed immediately.</p>
     * queued right before even if the request could have been processed immediately.</p>
     *
     *
     * @param timeout timeout in milliseconds. If 0 this method does not wait.
     * @param timeout timeout in milliseconds. If 0 this method does not wait.
     *
     *
     * @return a completed USB request, or {@code null} if an error or time out occurred
     * @return a completed USB request, or {@code null} if an error occurred
     *
     *
     * @throws IllegalArgumentException if the number of bytes read or written is more than the
     * @throws BufferOverflowException if the number of bytes read or written is more than the
     *                                 limit of the request's buffer. The number of bytes is
     *                                 limit of the request's buffer. The number of bytes is
     *                                 determined by the {@code length} parameter of
     *                                 determined by the {@code length} parameter of
     *                                 {@link UsbRequest#queue(ByteBuffer, int)}
     *                                 {@link UsbRequest#queue(ByteBuffer, int)}
     * @throws TimeoutException if no request was received in {@code timeout} milliseconds.
     */
     */
    public UsbRequest requestWait(int timeout) {
    public UsbRequest requestWait(long timeout) throws TimeoutException {
        timeout = Preconditions.checkArgumentNonnegative(timeout, "timeout");
        timeout = Preconditions.checkArgumentNonnegative(timeout, "timeout");


        UsbRequest request = native_request_wait(timeout);
        UsbRequest request = native_request_wait(timeout);
        if (request != null) {
        if (request != null) {
            request.dequeue();
            request.dequeue(true);
        }
        }
        return request;
        return request;
    }
    }
@@ -350,7 +367,7 @@ public class UsbDeviceConnection {
            int index, byte[] buffer, int offset, int length, int timeout);
            int index, byte[] buffer, int offset, int length, int timeout);
    private native int native_bulk_request(int endpoint, byte[] buffer,
    private native int native_bulk_request(int endpoint, byte[] buffer,
            int offset, int length, int timeout);
            int offset, int length, int timeout);
    private native UsbRequest native_request_wait(int timeout);
    private native UsbRequest native_request_wait(long timeout) throws TimeoutException;
    private native String native_get_serial();
    private native String native_get_serial();
    private native boolean native_reset_device();
    private native boolean native_reset_device();
}
}
+15 −2
Original line number Original line Diff line number Diff line
@@ -17,11 +17,13 @@
package android.hardware.usb;
package android.hardware.usb;


import android.annotation.Nullable;
import android.annotation.Nullable;
import android.util.Log;


import com.android.internal.util.Preconditions;
import com.android.internal.util.Preconditions;


import dalvik.system.CloseGuard;
import dalvik.system.CloseGuard;


import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;


/**
/**
@@ -276,7 +278,7 @@ public class UsbRequest {
        return wasQueued;
        return wasQueued;
    }
    }


    /* package */ void dequeue() {
    /* package */ void dequeue(boolean useBufferOverflowInsteadOfIllegalArg) {
        boolean isSend = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT);
        boolean isSend = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT);
        int bytesTransferred;
        int bytesTransferred;


@@ -313,7 +315,18 @@ public class UsbRequest {
                    bytesTransferred = native_dequeue_array(mBuffer.array(), mLength, isSend);
                    bytesTransferred = native_dequeue_array(mBuffer.array(), mLength, isSend);
                }
                }
                if (bytesTransferred >= 0) {
                if (bytesTransferred >= 0) {
                    mBuffer.position(Math.min(bytesTransferred, mLength));
                    int bytesToStore = Math.min(bytesTransferred, mLength);
                    try {
                        mBuffer.position(bytesToStore);
                    } catch (IllegalArgumentException e) {
                        if (useBufferOverflowInsteadOfIllegalArg) {
                            Log.e(TAG, "Buffer " + mBuffer + " does not have enough space to read "
                                    + bytesToStore + " bytes", e);
                            throw new BufferOverflowException();
                        } else {
                            throw e;
                        }
                    }
                }
                }
            }
            }


Loading