Files
Lawnchair/tests/src/com/android/launcher3/util/Wait.java
T
Sunny Goyal 6f2bb1ada5 Bug fix in Alarm where it was not getting called correctly if the
new timeout was set that was smaller than the previously set timeout

> Using uptimeMillis in Alarm to avoid errors due to system time
changes
> Adding an extra check in Wait in case Thread.sleep eats up
all the timeout

Change-Id: Id1fac5e8fdb81a0c3c7a6a5e50586b2a2f180d06
(cherry picked from commit a2125e1d10)
2016-09-02 18:28:20 +00:00

40 lines
1.0 KiB
Java

package com.android.launcher3.util;
import android.os.SystemClock;
/**
* A utility class for waiting for a condition to be true.
*/
public class Wait {
private static final long DEFAULT_SLEEP_MS = 200;
public static boolean atMost(Condition condition, long timeout) {
return atMost(condition, timeout, DEFAULT_SLEEP_MS);
}
public static boolean atMost(Condition condition, long timeout, long sleepMillis) {
long endTime = SystemClock.uptimeMillis() + timeout;
while (SystemClock.uptimeMillis() < endTime) {
try {
if (condition.isTrue()) {
return true;
}
} catch (Throwable t) {
// Ignore
}
SystemClock.sleep(sleepMillis);
}
// Check once more before returning false.
try {
if (condition.isTrue()) {
return true;
}
} catch (Throwable t) {
// Ignore
}
return false;
}
}