Loading core/java/android/os/TestLooperManager.java +12 −16 Original line number Original line Diff line number Diff line Loading @@ -18,6 +18,7 @@ import android.annotation.FlaggedApi; import android.annotation.Nullable; import android.annotation.Nullable; import android.util.ArraySet; import android.util.ArraySet; import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue; /** /** Loading @@ -38,10 +39,13 @@ public class TestLooperManager { private final MessageQueue mQueue; private final MessageQueue mQueue; private final Looper mLooper; private final Looper mLooper; private final LinkedBlockingQueue<MessageExecution> mExecuteQueue = new LinkedBlockingQueue<>(); private final LinkedBlockingQueue<MessageExecution> mExecuteQueue = new LinkedBlockingQueue<>(); private final boolean mLooperIsMyLooper; // When this latch is zero, it's guaranteed that the LooperHolder Message // is not in the underlying queue. private final CountDownLatch mLooperHolderLatch = new CountDownLatch(1); private boolean mReleased; private boolean mReleased; private boolean mLooperBlocked; private final boolean mLooperIsMyLooper; /** /** * @hide * @hide Loading @@ -59,6 +63,8 @@ public class TestLooperManager { if (!mLooperIsMyLooper) { if (!mLooperIsMyLooper) { // Post a message that will keep the looper blocked as long as we are dispatching. // Post a message that will keep the looper blocked as long as we are dispatching. new Handler(looper).post(new LooperHolder()); new Handler(looper).post(new LooperHolder()); } else { mLooperHolderLatch.countDown(); } } } } Loading Loading @@ -222,23 +228,16 @@ public class TestLooperManager { * is not in the underlying queue. * is not in the underlying queue. */ */ private void waitForLooperHolder() { private void waitForLooperHolder() { while (!mLooperIsMyLooper && !mLooperBlocked) { synchronized (this) { try { try { wait(); mLooperHolderLatch.await(); } catch (InterruptedException e) { } catch (InterruptedException e) { } } } } } } private class LooperHolder implements Runnable { private class LooperHolder implements Runnable { @Override @Override public void run() { public void run() { synchronized (TestLooperManager.this) { mLooperHolderLatch.countDown(); mLooperBlocked = true; TestLooperManager.this.notify(); } while (!mReleased) { while (!mReleased) { try { try { final MessageExecution take = mExecuteQueue.take(); final MessageExecution take = mExecuteQueue.take(); Loading @@ -248,9 +247,6 @@ public class TestLooperManager { } catch (InterruptedException e) { } catch (InterruptedException e) { } } } } synchronized (TestLooperManager.this) { mLooperBlocked = false; } } } private void processMessage(MessageExecution mex) { private void processMessage(MessageExecution mex) { Loading Loading
core/java/android/os/TestLooperManager.java +12 −16 Original line number Original line Diff line number Diff line Loading @@ -18,6 +18,7 @@ import android.annotation.FlaggedApi; import android.annotation.Nullable; import android.annotation.Nullable; import android.util.ArraySet; import android.util.ArraySet; import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue; /** /** Loading @@ -38,10 +39,13 @@ public class TestLooperManager { private final MessageQueue mQueue; private final MessageQueue mQueue; private final Looper mLooper; private final Looper mLooper; private final LinkedBlockingQueue<MessageExecution> mExecuteQueue = new LinkedBlockingQueue<>(); private final LinkedBlockingQueue<MessageExecution> mExecuteQueue = new LinkedBlockingQueue<>(); private final boolean mLooperIsMyLooper; // When this latch is zero, it's guaranteed that the LooperHolder Message // is not in the underlying queue. private final CountDownLatch mLooperHolderLatch = new CountDownLatch(1); private boolean mReleased; private boolean mReleased; private boolean mLooperBlocked; private final boolean mLooperIsMyLooper; /** /** * @hide * @hide Loading @@ -59,6 +63,8 @@ public class TestLooperManager { if (!mLooperIsMyLooper) { if (!mLooperIsMyLooper) { // Post a message that will keep the looper blocked as long as we are dispatching. // Post a message that will keep the looper blocked as long as we are dispatching. new Handler(looper).post(new LooperHolder()); new Handler(looper).post(new LooperHolder()); } else { mLooperHolderLatch.countDown(); } } } } Loading Loading @@ -222,23 +228,16 @@ public class TestLooperManager { * is not in the underlying queue. * is not in the underlying queue. */ */ private void waitForLooperHolder() { private void waitForLooperHolder() { while (!mLooperIsMyLooper && !mLooperBlocked) { synchronized (this) { try { try { wait(); mLooperHolderLatch.await(); } catch (InterruptedException e) { } catch (InterruptedException e) { } } } } } } private class LooperHolder implements Runnable { private class LooperHolder implements Runnable { @Override @Override public void run() { public void run() { synchronized (TestLooperManager.this) { mLooperHolderLatch.countDown(); mLooperBlocked = true; TestLooperManager.this.notify(); } while (!mReleased) { while (!mReleased) { try { try { final MessageExecution take = mExecuteQueue.take(); final MessageExecution take = mExecuteQueue.take(); Loading @@ -248,9 +247,6 @@ public class TestLooperManager { } catch (InterruptedException e) { } catch (InterruptedException e) { } } } } synchronized (TestLooperManager.this) { mLooperBlocked = false; } } } private void processMessage(MessageExecution mex) { private void processMessage(MessageExecution mex) { Loading