diff options
author | jknotten@chromium.org <jknotten@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-09 12:12:09 +0000 |
---|---|---|
committer | jknotten@chromium.org <jknotten@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-09 12:12:09 +0000 |
commit | 5aab223f965826e7752d0f22487ed85ca2bcaa05 (patch) | |
tree | 588677002e89d620bce8359f831b426d49722ec9 /base/android | |
parent | 61c9e8f83177bc9c4270c2d9f205e673f0f73b0f (diff) | |
download | chromium_src-5aab223f965826e7752d0f22487ed85ca2bcaa05.zip chromium_src-5aab223f965826e7752d0f22487ed85ca2bcaa05.tar.gz chromium_src-5aab223f965826e7752d0f22487ed85ca2bcaa05.tar.bz2 |
Upstream Geolocation for Android.
TEST=content_unittests: GeolocationProvider.* LocationArbitrator.*
There are also some integration tests downstream for the Java specific part.
Review URL: https://chromiumcodereview.appspot.com/10209003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@136019 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/android')
-rw-r--r-- | base/android/java/org/chromium/base/ThreadUtils.java | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/base/android/java/org/chromium/base/ThreadUtils.java b/base/android/java/org/chromium/base/ThreadUtils.java new file mode 100644 index 0000000..ca31c3d --- /dev/null +++ b/base/android/java/org/chromium/base/ThreadUtils.java @@ -0,0 +1,134 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.base; + +import android.os.Handler; +import android.os.Looper; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; + +/** + * Helper methods to deal with threading related tasks. + */ +public class ThreadUtils { + + /** + * Run the supplied Runnable on the main thread. The method will block until + * the Runnable completes. + * + * @param r The Runnable to run. + */ + public static void runOnUiThreadBlocking(final Runnable r) { + if (runningOnUiThread()) { + r.run(); + } else { + FutureTask<Void> task = new FutureTask<Void>(r, null); + postOnUiThread(task); + try { + task.get(); + } catch (Exception e) { + throw new RuntimeException("Exception occured while waiting for runnable", e); + } + } + } + + /** + * Run the supplied Callable on the main thread, wrapping any exceptions in + * a RuntimeException. The method will block until the Callable completes. + * + * @param c The Callable to run + * @return The result of the callable + */ + public static <T> T runOnUiThreadBlockingNoException(Callable<T> c) { + try { + return runOnUiThreadBlocking(c); + } catch (ExecutionException e) { + throw new RuntimeException("Error occured waiting for callable", e); + } + } + + /** + * Run the supplied Callable on the main thread, The method will block until + * the Callable completes. + * + * @param c The Callable to run + * @return The result of the callable + * @throws ExecutionException c's exception + */ + public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException { + FutureTask<T> task = new FutureTask<T>(c); + runOnUiThread(task); + try { + return task.get(); + } catch (InterruptedException e) { + throw new RuntimeException("Interrupted waiting for callable", e); + } + } + + /** + * Run the supplied FutureTask on the main thread. The method will block + * only if the current thread is the main thread. + * + * @param task The FutureTask to run + * @return The queried task (to aid inline construction) + */ + public static <T> FutureTask<T> runOnUiThread(FutureTask<T> task) { + if (runningOnUiThread()) { + task.run(); + } else { + postOnUiThread(task); + } + return task; + } + + /** + * Run the supplied Callable on the main thread. The method will block + * only if the current thread is the main thread. + * + * @param c The Callable to run + * @return A FutureTask wrapping the callable to retrieve results + */ + public static <T> FutureTask<T> runOnUiThread(Callable<T> c) { + return runOnUiThread(new FutureTask<T>(c)); + } + + /** + * Run the supplied Runnable on the main thread. The method will block + * only if the current thread is the main thread. + * + * @param r The Runnable to run + */ + public static void runOnUiThread(Runnable r) { + runOnUiThread(new FutureTask<Void>(r, null)); + } + + /** + * Post the supplied FutureTask to run on the main thread. The method will + * not block, even if called on the UI thread. + * + * @param task The FutureTask to run + * @return The queried task (to aid inline construction) + */ + public static <T> FutureTask<T> postOnUiThread(FutureTask<T> task) { + new Handler(Looper.getMainLooper()).post(task); + return task; + } + + /** + * Asserts that the current thread is running on the main thread. + */ + public static void assertOnUiThread() { + assert runningOnUiThread(); + } + + /** + * @return true iff the current thread is the main (UI) thread. + */ + public static boolean runningOnUiThread() { + return Looper.getMainLooper() == Looper.myLooper(); + } +} |