diff options
author | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-23 13:38:09 +0000 |
---|---|---|
committer | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-23 13:38:09 +0000 |
commit | 3549fc1ea5a0a4e959573ffa173c584c853c6582 (patch) | |
tree | 671981e2a8f003addbd6d3f3bfaf1805cb197670 /base/android/java | |
parent | 3fb0169853cbeca7abcd266fbf56310a02d85508 (diff) | |
download | chromium_src-3549fc1ea5a0a4e959573ffa173c584c853c6582.zip chromium_src-3549fc1ea5a0a4e959573ffa173c584c853c6582.tar.gz chromium_src-3549fc1ea5a0a4e959573ffa173c584c853c6582.tar.bz2 |
Android code should not assume main thread == UI thread.
BUG=305352
Internal Bug b/10932261
Originally from http://crrev.com/25092005/ PS9
Original Description:
[Android] Android code should not assume main thread == UI thread.
Although typically the case, the main thread of an android app is not
necessarily the thread in which the View is hosted (the Android "UI"
thread). Update base.ThreadUtils to be able to specify a non-main thread
UI thread, and update places in the code that make this assumption to
use the refactored ThreadUtils class.
Change-Id: Ie2967fedafcece90f4e65b52a84bdd21f6b745cd
Review URL: https://codereview.chromium.org/26424003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@230397 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/android/java')
-rw-r--r-- | base/android/java/src/org/chromium/base/ThreadUtils.java | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/base/android/java/src/org/chromium/base/ThreadUtils.java b/base/android/java/src/org/chromium/base/ThreadUtils.java index a880ede..af50de6 100644 --- a/base/android/java/src/org/chromium/base/ThreadUtils.java +++ b/base/android/java/src/org/chromium/base/ThreadUtils.java @@ -17,6 +17,42 @@ import java.util.concurrent.FutureTask; */ public class ThreadUtils { + private static final Object sLock = new Object(); + + private static boolean sWillOverride = false; + + private static Handler sUiThreadHandler = null; + + public static void setWillOverrideUiThread() { + synchronized (sLock) { + sWillOverride = true; + } + } + + public static void setUiThread(Looper looper) { + synchronized (sLock) { + if (sUiThreadHandler != null && sUiThreadHandler.getLooper() != looper) { + throw new RuntimeException("UI thread looper is already set to " + + sUiThreadHandler.getLooper() + " (Main thread looper is " + + Looper.getMainLooper() + "), cannot set to new looper " + looper); + } else { + sUiThreadHandler = new Handler(looper); + } + } + } + + private static Handler getUiThreadHandler() { + synchronized (sLock) { + if (sUiThreadHandler == null) { + if (sWillOverride) { + throw new RuntimeException("Did not yet override the UI thread"); + } + sUiThreadHandler = new Handler(Looper.getMainLooper()); + } + return sUiThreadHandler; + } + } + /** * Run the supplied Runnable on the main thread. The method will block until the Runnable * completes. @@ -107,7 +143,7 @@ public class ThreadUtils { if (runningOnUiThread()) { r.run(); } else { - LazyHolder.sUiThreadHandler.post(r); + getUiThreadHandler().post(r); } } @@ -119,7 +155,7 @@ public class ThreadUtils { * @return The queried task (to aid inline construction) */ public static <T> FutureTask<T> postOnUiThread(FutureTask<T> task) { - LazyHolder.sUiThreadHandler.post(task); + getUiThreadHandler().post(task); return task; } @@ -130,7 +166,7 @@ public class ThreadUtils { * @param task The Runnable to run */ public static void postOnUiThread(Runnable r) { - LazyHolder.sUiThreadHandler.post(r); + getUiThreadHandler().post(r); } /** @@ -141,7 +177,7 @@ public class ThreadUtils { * @param delayMillis The delay in milliseconds until the Runnable will be run */ public static void postOnUiThreadDelayed(Runnable r, long delayMillis) { - LazyHolder.sUiThreadHandler.postDelayed(r, delayMillis); + getUiThreadHandler().postDelayed(r, delayMillis); } /** @@ -155,7 +191,11 @@ public class ThreadUtils { * @return true iff the current thread is the main (UI) thread. */ public static boolean runningOnUiThread() { - return Looper.getMainLooper() == Looper.myLooper(); + return getUiThreadHandler().getLooper() == Looper.myLooper(); + } + + public static Looper getUiThreadLooper() { + return getUiThreadHandler().getLooper(); } /** @@ -165,8 +205,4 @@ public class ThreadUtils { public static void setThreadPriorityAudio(int tid) { Process.setThreadPriority(tid, Process.THREAD_PRIORITY_AUDIO); } - - private static class LazyHolder { - private static Handler sUiThreadHandler = new Handler(Looper.getMainLooper()); - } } |