|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko; |
|
6 |
|
7 import java.util.concurrent.SynchronousQueue; |
|
8 import java.util.concurrent.TimeUnit; |
|
9 |
|
10 import android.app.Activity; |
|
11 |
|
12 public final class RobocopUtils { |
|
13 private static final int MAX_WAIT_MS = 20000; |
|
14 |
|
15 private RobocopUtils() {} |
|
16 |
|
17 public static void runOnUiThreadSync(Activity activity, final Runnable runnable) { |
|
18 final SynchronousQueue syncQueue = new SynchronousQueue(); |
|
19 activity.runOnUiThread( |
|
20 new Runnable() { |
|
21 public void run() { |
|
22 runnable.run(); |
|
23 try { |
|
24 syncQueue.put(new Object()); |
|
25 } catch (InterruptedException e) { |
|
26 FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); |
|
27 } |
|
28 } |
|
29 }); |
|
30 try { |
|
31 // Wait for the UiThread code to finish running |
|
32 if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) { |
|
33 FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, |
|
34 "time-out waiting for UI thread"); |
|
35 FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR); |
|
36 } |
|
37 } catch (InterruptedException e) { |
|
38 FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); |
|
39 } |
|
40 } |
|
41 } |