michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import java.util.concurrent.SynchronousQueue; michael@0: import java.util.concurrent.TimeUnit; michael@0: michael@0: import android.app.Activity; michael@0: michael@0: public final class RobocopUtils { michael@0: private static final int MAX_WAIT_MS = 20000; michael@0: michael@0: private RobocopUtils() {} michael@0: michael@0: public static void runOnUiThreadSync(Activity activity, final Runnable runnable) { michael@0: final SynchronousQueue syncQueue = new SynchronousQueue(); michael@0: activity.runOnUiThread( michael@0: new Runnable() { michael@0: public void run() { michael@0: runnable.run(); michael@0: try { michael@0: syncQueue.put(new Object()); michael@0: } catch (InterruptedException e) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); michael@0: } michael@0: } michael@0: }); michael@0: try { michael@0: // Wait for the UiThread code to finish running michael@0: if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, michael@0: "time-out waiting for UI thread"); michael@0: FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR); michael@0: } michael@0: } catch (InterruptedException e) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e); michael@0: } michael@0: } michael@0: }