summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/launcher3/util/Wait.java
blob: f9e53ba8b1876bee3ad4f44305ff79914a281894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
    }
}