Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
5 package org.mozilla.gecko;
7 import java.util.concurrent.SynchronousQueue;
8 import java.util.concurrent.TimeUnit;
10 import android.app.Activity;
12 public final class RobocopUtils {
13 private static final int MAX_WAIT_MS = 20000;
15 private RobocopUtils() {}
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 }