• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.launcher3.util;
2 
3 import android.os.SystemClock;
4 
5 /**
6  * A utility class for waiting for a condition to be true.
7  */
8 public class Wait {
9 
10     private static final long DEFAULT_SLEEP_MS = 200;
11 
atMost(Condition condition, long timeout)12     public static boolean atMost(Condition condition, long timeout) {
13         return atMost(condition, timeout, DEFAULT_SLEEP_MS);
14     }
15 
atMost(Condition condition, long timeout, long sleepMillis)16     public static boolean atMost(Condition condition, long timeout, long sleepMillis) {
17         long endTime = SystemClock.uptimeMillis() + timeout;
18         while (SystemClock.uptimeMillis() < endTime) {
19             try {
20                 if (condition.isTrue()) {
21                     return true;
22                 }
23             } catch (Throwable t) {
24                 // Ignore
25             }
26             SystemClock.sleep(sleepMillis);
27         }
28 
29         // Check once more before returning false.
30         try {
31             if (condition.isTrue()) {
32                 return true;
33             }
34         } catch (Throwable t) {
35             // Ignore
36         }
37         return false;
38     }
39 }
40