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

Commit bacec345 authored by JW Wang's avatar JW Wang
Browse files

Add #connectWithTimeout (1/n)

Add #connectWithTimeout which allows us to specify the timeout before
giving up the connection. The method throws a TimeoutException so the
caller can catch it and retry connection again.

Note we don't change the exception spec. of #connect in order to be
source and binary compatible.

Bug: 147785023
Test: m
Change-Id: I5ac61ed0aef107f4e38166c0b95bc3a3fb419387
parent 75c5c5bd
Loading
Loading
Loading
Loading
+26 −5
Original line number Original line Diff line number Diff line
@@ -210,23 +210,44 @@ public final class UiAutomation {
    }
    }


    /**
    /**
     * Connects this UiAutomation to the accessibility introspection APIs with default flags.
     * Connects this UiAutomation to the accessibility introspection APIs with default flags
     * and default timeout.
     *
     *
     * @hide
     * @hide
     */
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    public void connect() {
    public void connect() {
        connect(0);
        try {
            connectWithTimeout(0, CONNECT_TIMEOUT_MILLIS);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Connects this UiAutomation to the accessibility introspection APIs with default timeout.
     *
     * @hide
     */
    public void connect(int flags) {
        try {
            connectWithTimeout(flags, CONNECT_TIMEOUT_MILLIS);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
    }


    /**
    /**
     * Connects this UiAutomation to the accessibility introspection APIs.
     * Connects this UiAutomation to the accessibility introspection APIs.
     *
     *
     * @param flags Any flags to apply to the automation as it gets connected
     * @param flags Any flags to apply to the automation as it gets connected
     * @param timeoutMillis The wait timeout in milliseconds
     *
     * @throws TimeoutException If not connected within the timeout
     *
     *
     * @hide
     * @hide
     */
     */
    public void connect(int flags) {
    public void connectWithTimeout(int flags, long timeoutMillis) throws TimeoutException {
        synchronized (mLock) {
        synchronized (mLock) {
            throwIfConnectedLocked();
            throwIfConnectedLocked();
            if (mIsConnecting) {
            if (mIsConnecting) {
@@ -254,9 +275,9 @@ public final class UiAutomation {
                        break;
                        break;
                    }
                    }
                    final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis;
                    final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis;
                    final long remainingTimeMillis = CONNECT_TIMEOUT_MILLIS - elapsedTimeMillis;
                    final long remainingTimeMillis = timeoutMillis - elapsedTimeMillis;
                    if (remainingTimeMillis <= 0) {
                    if (remainingTimeMillis <= 0) {
                        throw new RuntimeException("Error while connecting " + this);
                        throw new TimeoutException("Timeout while connecting " + this);
                    }
                    }
                    try {
                    try {
                        mLock.wait(remainingTimeMillis);
                        mLock.wait(remainingTimeMillis);