Add #connectWithTimeout (1/n) am: 67c91c405c

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1470700

Change-Id: Idab13ed7ae1e20e8a9b064727485db40c3aadd9d
This commit is contained in:
JW Wang
2020-10-27 18:22:24 +00:00
committed by Automerger Merge Worker

View File

@@ -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);