Loading packages/SettingsLib/src/com/android/settingslib/utils/ThreadUtils.java +21 −0 Original line number Diff line number Diff line Loading @@ -15,10 +15,13 @@ */ package com.android.settingslib.utils; import android.os.Handler; import android.os.Looper; public class ThreadUtils { private static volatile Thread sMainThread; private static volatile Handler sMainThreadHandler; /** * Returns true if the current thread is the UI thread. Loading @@ -30,6 +33,17 @@ public class ThreadUtils { return Thread.currentThread() == sMainThread; } /** * Returns a shared UI thread handler. */ public static Handler getUiThreadHandler() { if (sMainThreadHandler == null) { sMainThreadHandler = new Handler(Looper.getMainLooper()); } return sMainThreadHandler; } /** * Checks that the current thread is the UI thread. Otherwise throws an exception. */ Loading @@ -39,4 +53,11 @@ public class ThreadUtils { } } /** * Posts the runnable on the main thread. */ public static void postOnMainThread(Runnable runnable) { getUiThreadHandler().post(runnable); } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/ThreadUtilsTest.java +28 −3 Original line number Diff line number Diff line Loading @@ -16,14 +16,16 @@ package com.android.settingslib.utils; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import com.android.settingslib.TestConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import org.robolectric.shadows.ShadowLooper; @RunWith(RobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) Loading Loading @@ -56,4 +58,27 @@ public class ThreadUtilsTest { background.start(); background.join(); } @Test public void testPostOnMainThread_shouldRunOnMainTread() throws Exception { TestRunnable cr = new TestRunnable(); ShadowLooper.pauseMainLooper(); ThreadUtils.postOnMainThread(cr); assertThat(cr.called).isFalse(); ShadowLooper.runUiThreadTasks(); assertThat(cr.called).isTrue(); assertThat(cr.onUiThread).isTrue(); } private static class TestRunnable implements Runnable { volatile boolean called; volatile boolean onUiThread; public void run() { this.called = true; this.onUiThread = ThreadUtils.isMainThread(); } } } Loading
packages/SettingsLib/src/com/android/settingslib/utils/ThreadUtils.java +21 −0 Original line number Diff line number Diff line Loading @@ -15,10 +15,13 @@ */ package com.android.settingslib.utils; import android.os.Handler; import android.os.Looper; public class ThreadUtils { private static volatile Thread sMainThread; private static volatile Handler sMainThreadHandler; /** * Returns true if the current thread is the UI thread. Loading @@ -30,6 +33,17 @@ public class ThreadUtils { return Thread.currentThread() == sMainThread; } /** * Returns a shared UI thread handler. */ public static Handler getUiThreadHandler() { if (sMainThreadHandler == null) { sMainThreadHandler = new Handler(Looper.getMainLooper()); } return sMainThreadHandler; } /** * Checks that the current thread is the UI thread. Otherwise throws an exception. */ Loading @@ -39,4 +53,11 @@ public class ThreadUtils { } } /** * Posts the runnable on the main thread. */ public static void postOnMainThread(Runnable runnable) { getUiThreadHandler().post(runnable); } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/utils/ThreadUtilsTest.java +28 −3 Original line number Diff line number Diff line Loading @@ -16,14 +16,16 @@ package com.android.settingslib.utils; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import com.android.settingslib.TestConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import org.robolectric.shadows.ShadowLooper; @RunWith(RobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) Loading Loading @@ -56,4 +58,27 @@ public class ThreadUtilsTest { background.start(); background.join(); } @Test public void testPostOnMainThread_shouldRunOnMainTread() throws Exception { TestRunnable cr = new TestRunnable(); ShadowLooper.pauseMainLooper(); ThreadUtils.postOnMainThread(cr); assertThat(cr.called).isFalse(); ShadowLooper.runUiThreadTasks(); assertThat(cr.called).isTrue(); assertThat(cr.onUiThread).isTrue(); } private static class TestRunnable implements Runnable { volatile boolean called; volatile boolean onUiThread; public void run() { this.called = true; this.onUiThread = ThreadUtils.isMainThread(); } } }