1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/build/mobile/robocop/RobocopUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,41 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko; 1.9 + 1.10 +import java.util.concurrent.SynchronousQueue; 1.11 +import java.util.concurrent.TimeUnit; 1.12 + 1.13 +import android.app.Activity; 1.14 + 1.15 +public final class RobocopUtils { 1.16 + private static final int MAX_WAIT_MS = 20000; 1.17 + 1.18 + private RobocopUtils() {} 1.19 + 1.20 + public static void runOnUiThreadSync(Activity activity, final Runnable runnable) { 1.21 + final SynchronousQueue syncQueue = new SynchronousQueue(); 1.22 + activity.runOnUiThread( 1.23 + new Runnable() { 1.24 + public void run() { 1.25 + runnable.run(); 1.26 + try { 1.27 + syncQueue.put(new Object()); 1.28 + } catch (InterruptedException e) { 1.29 + FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); 1.30 + } 1.31 + } 1.32 + }); 1.33 + try { 1.34 + // Wait for the UiThread code to finish running 1.35 + if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) { 1.36 + FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, 1.37 + "time-out waiting for UI thread"); 1.38 + FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR); 1.39 + } 1.40 + } catch (InterruptedException e) { 1.41 + FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); 1.42 + } 1.43 + } 1.44 +}