| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package android.os; |
| 16 |
|
| 17 |
import android.util.ArraySet; |
| 18 |
|
| 19 |
import java.util.concurrent.LinkedBlockingQueue; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class TestLooperManager { |
| 32 |
|
| 33 |
private static final ArraySet<Looper> sHeldLoopers = new ArraySet<>(); |
| 34 |
|
| 35 |
private final MessageQueue mQueue; |
| 36 |
private final Looper mLooper; |
| 37 |
private final LinkedBlockingQueue<MessageExecution> mExecuteQueue = new LinkedBlockingQueue<>(); |
| 38 |
|
| 39 |
private boolean mReleased; |
| 40 |
private boolean mLooperBlocked; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public TestLooperManager(Looper looper) { |
| 46 |
synchronized (sHeldLoopers) { |
| 47 |
if (sHeldLoopers.contains(looper)) { |
| 48 |
throw new RuntimeException("TestLooperManager already held for this looper"); |
| 49 |
} |
| 50 |
sHeldLoopers.add(looper); |
| 51 |
} |
| 52 |
mLooper = looper; |
| 53 |
mQueue = mLooper.getQueue(); |
| 54 |
|
| 55 |
new Handler(looper).post(new LooperHolder()); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public MessageQueue getMessageQueue() { |
| 62 |
checkReleased(); |
| 63 |
return mQueue; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
@Deprecated |
| 68 |
public MessageQueue getQueue() { |
| 69 |
return getMessageQueue(); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public Message next() { |
| 80 |
|
| 81 |
|
| 82 |
while (!mLooperBlocked) { |
| 83 |
synchronized (this) { |
| 84 |
try { |
| 85 |
wait(); |
| 86 |
} catch (InterruptedException e) { |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
checkReleased(); |
| 91 |
return mQueue.next(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public void release() { |
| 100 |
synchronized (sHeldLoopers) { |
| 101 |
sHeldLoopers.remove(mLooper); |
| 102 |
} |
| 103 |
checkReleased(); |
| 104 |
mReleased = true; |
| 105 |
mExecuteQueue.add(new MessageExecution()); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void execute(Message message) { |
| 117 |
checkReleased(); |
| 118 |
if (Looper.myLooper() == mLooper) { |
| 119 |
|
| 120 |
message.target.dispatchMessage(message); |
| 121 |
} else { |
| 122 |
MessageExecution execution = new MessageExecution(); |
| 123 |
execution.m = message; |
| 124 |
synchronized (execution) { |
| 125 |
mExecuteQueue.add(execution); |
| 126 |
|
| 127 |
try { |
| 128 |
execution.wait(); |
| 129 |
} catch (InterruptedException e) { |
| 130 |
} |
| 131 |
if (execution.response != null) { |
| 132 |
throw new RuntimeException(execution.response); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public void recycle(Message msg) { |
| 143 |
checkReleased(); |
| 144 |
msg.recycleUnchecked(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public boolean hasMessages(Handler h, Object object, int what) { |
| 155 |
checkReleased(); |
| 156 |
return mQueue.hasMessages(h, what, object); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public boolean hasMessages(Handler h, Object object, Runnable r) { |
| 167 |
checkReleased(); |
| 168 |
return mQueue.hasMessages(h, r, object); |
| 169 |
} |
| 170 |
|
| 171 |
private void checkReleased() { |
| 172 |
if (mReleased) { |
| 173 |
throw new RuntimeException("release() has already be called"); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private class LooperHolder implements Runnable { |
| 178 |
@Override |
| 179 |
public void run() { |
| 180 |
synchronized (TestLooperManager.this) { |
| 181 |
mLooperBlocked = true; |
| 182 |
TestLooperManager.this.notify(); |
| 183 |
} |
| 184 |
while (!mReleased) { |
| 185 |
try { |
| 186 |
final MessageExecution take = mExecuteQueue.take(); |
| 187 |
if (take.m != null) { |
| 188 |
processMessage(take); |
| 189 |
} |
| 190 |
} catch (InterruptedException e) { |
| 191 |
} |
| 192 |
} |
| 193 |
synchronized (TestLooperManager.this) { |
| 194 |
mLooperBlocked = false; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
private void processMessage(MessageExecution mex) { |
| 199 |
synchronized (mex) { |
| 200 |
try { |
| 201 |
mex.m.target.dispatchMessage(mex.m); |
| 202 |
mex.response = null; |
| 203 |
} catch (Throwable t) { |
| 204 |
mex.response = t; |
| 205 |
} |
| 206 |
mex.notifyAll(); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
private static class MessageExecution { |
| 212 |
private Message m; |
| 213 |
private Throwable response; |
| 214 |
} |
| 215 |
} |