| 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package androidx.recyclerview.widget; |
| 18 | |
| 19 | import android.os.AsyncTask; |
| 20 | import android.os.Handler; |
| 21 | import android.os.Looper; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import java.util.concurrent.Executor; |
| 25 | import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | |
| 27 | class MessageThreadUtil<T> implements ThreadUtil<T> { |
| 28 | |
| 29 | @Override |
| 30 | public MainThreadCallback<T> getMainThreadProxy(final MainThreadCallback<T> callback) { |
| 31 | return new MainThreadCallback<T>() { |
| 32 | final MessageQueue mQueue = new MessageQueue(); |
| 33 | final private Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); |
| 34 | |
| 35 | static final int UPDATE_ITEM_COUNT = 1; |
| 36 | static final int ADD_TILE = 2; |
| 37 | static final int REMOVE_TILE = 3; |
| 38 | |
| 39 | @Override |
| 40 | public void updateItemCount(int generation, int itemCount) { |
| 41 | sendMessage(SyncQueueItem.obtainMessage(UPDATE_ITEM_COUNT, generation, itemCount)); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void addTile(int generation, TileList.Tile<T> tile) { |
| 46 | sendMessage(SyncQueueItem.obtainMessage(ADD_TILE, generation, tile)); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void removeTile(int generation, int position) { |
| 51 | sendMessage(SyncQueueItem.obtainMessage(REMOVE_TILE, generation, position)); |
| 52 | } |
| 53 | |
| 54 | private void sendMessage(SyncQueueItem msg) { |
| 55 | mQueue.sendMessage(msg); |
| 56 | mMainThreadHandler.post(mMainThreadRunnable); |
| 57 | } |
| 58 | |
| 59 | private Runnable mMainThreadRunnable = new Runnable() { |
| 60 | @Override |
| 61 | public void run() { |
| 62 | SyncQueueItem msg = mQueue.next(); |
| 63 | while (msg != null) { |
| 64 | switch (msg.what) { |
| 65 | case UPDATE_ITEM_COUNT: |
| 66 | callback.updateItemCount(msg.arg1, msg.arg2); |
| 67 | break; |
| 68 | case ADD_TILE: |
| 69 | @SuppressWarnings("unchecked") |
| 70 | TileList.Tile<T> tile = (TileList.Tile<T>) msg.data; |
| 71 | callback.addTile(msg.arg1, tile); |
| 72 | break; |
| 73 | case REMOVE_TILE: |
| 74 | callback.removeTile(msg.arg1, msg.arg2); |
| 75 | break; |
| 76 | default: |
| 77 | Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); |
| 78 | } |
| 79 | msg = mQueue.next(); |
| 80 | } |
| 81 | } |
| 82 | }; |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public BackgroundCallback<T> getBackgroundProxy(final BackgroundCallback<T> callback) { |
| 88 | return new BackgroundCallback<T>() { |
| 89 | final MessageQueue mQueue = new MessageQueue(); |
| 90 | private final Executor mExecutor = AsyncTask.THREAD_POOL_EXECUTOR; |
| 91 | AtomicBoolean mBackgroundRunning = new AtomicBoolean(false); |
| 92 | |
| 93 | static final int REFRESH = 1; |
| 94 | static final int UPDATE_RANGE = 2; |
| 95 | static final int LOAD_TILE = 3; |
| 96 | static final int RECYCLE_TILE = 4; |
| 97 | |
| 98 | @Override |
| 99 | public void refresh(int generation) { |
| 100 | sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(REFRESH, generation, null)); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void updateRange(int rangeStart, int rangeEnd, |
| 105 | int extRangeStart, int extRangeEnd, int scrollHint) { |
| 106 | sendMessageAtFrontOfQueue(SyncQueueItem.obtainMessage(UPDATE_RANGE, |
| 107 | rangeStart, rangeEnd, extRangeStart, extRangeEnd, scrollHint, null)); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void loadTile(int position, int scrollHint) { |
| 112 | sendMessage(SyncQueueItem.obtainMessage(LOAD_TILE, position, scrollHint)); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void recycleTile(TileList.Tile<T> tile) { |
| 117 | sendMessage(SyncQueueItem.obtainMessage(RECYCLE_TILE, 0, tile)); |
| 118 | } |
| 119 | |
| 120 | private void sendMessage(SyncQueueItem msg) { |
| 121 | mQueue.sendMessage(msg); |
| 122 | maybeExecuteBackgroundRunnable(); |
| 123 | } |
| 124 | |
| 125 | private void sendMessageAtFrontOfQueue(SyncQueueItem msg) { |
| 126 | mQueue.sendMessageAtFrontOfQueue(msg); |
| 127 | maybeExecuteBackgroundRunnable(); |
| 128 | } |
| 129 | |
| 130 | private void maybeExecuteBackgroundRunnable() { |
| 131 | if (mBackgroundRunning.compareAndSet(false, true)) { |
| 132 | mExecutor.execute(mBackgroundRunnable); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private Runnable mBackgroundRunnable = new Runnable() { |
| 137 | @Override |
| 138 | public void run() { |
| 139 | while (true) { |
| 140 | SyncQueueItem msg = mQueue.next(); |
| 141 | if (msg == null) { |
| 142 | break; |
| 143 | } |
| 144 | switch (msg.what) { |
| 145 | case REFRESH: |
| 146 | mQueue.removeMessages(REFRESH); |
| 147 | callback.refresh(msg.arg1); |
| 148 | break; |
| 149 | case UPDATE_RANGE: |
| 150 | mQueue.removeMessages(UPDATE_RANGE); |
| 151 | mQueue.removeMessages(LOAD_TILE); |
| 152 | callback.updateRange( |
| 153 | msg.arg1, msg.arg2, msg.arg3, msg.arg4, msg.arg5); |
| 154 | break; |
| 155 | case LOAD_TILE: |
| 156 | callback.loadTile(msg.arg1, msg.arg2); |
| 157 | break; |
| 158 | case RECYCLE_TILE: |
| 159 | @SuppressWarnings("unchecked") |
| 160 | TileList.Tile<T> tile = (TileList.Tile<T>) msg.data; |
| 161 | callback.recycleTile(tile); |
| 162 | break; |
| 163 | default: |
| 164 | Log.e("ThreadUtil", "Unsupported message, what=" + msg.what); |
| 165 | } |
| 166 | } |
| 167 | mBackgroundRunning.set(false); |
| 168 | } |
| 169 | }; |
| 170 | }; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Replica of android.os.Message. Unfortunately, cannot use it without a Handler and don't want |
| 175 | * to create a thread just for this component. |
| 176 | */ |
| 177 | static class SyncQueueItem { |
| 178 | |
| 179 | private static SyncQueueItem sPool; |
| 180 | private static final Object sPoolLock = new Object(); |
| 181 | SyncQueueItem next; |
| 182 | public int what; |
| 183 | public int arg1; |
| 184 | public int arg2; |
| 185 | public int arg3; |
| 186 | public int arg4; |
| 187 | public int arg5; |
| 188 | public Object data; |
| 189 | |
| 190 | void recycle() { |
| 191 | next = null; |
| 192 | what = arg1 = arg2 = arg3 = arg4 = arg5 = 0; |
| 193 | data = null; |
| 194 | synchronized (sPoolLock) { |
| 195 | if (sPool != null) { |
| 196 | next = sPool; |
| 197 | } |
| 198 | sPool = this; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static SyncQueueItem obtainMessage(int what, int arg1, int arg2, int arg3, int arg4, |
| 203 | int arg5, Object data) { |
| 204 | synchronized (sPoolLock) { |
| 205 | final SyncQueueItem item; |
| 206 | if (sPool == null) { |
| 207 | item = new SyncQueueItem(); |
| 208 | } else { |
| 209 | item = sPool; |
| 210 | sPool = sPool.next; |
| 211 | item.next = null; |
| 212 | } |
| 213 | item.what = what; |
| 214 | item.arg1 = arg1; |
| 215 | item.arg2 = arg2; |
| 216 | item.arg3 = arg3; |
| 217 | item.arg4 = arg4; |
| 218 | item.arg5 = arg5; |
| 219 | item.data = data; |
| 220 | return item; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static SyncQueueItem obtainMessage(int what, int arg1, int arg2) { |
| 225 | return obtainMessage(what, arg1, arg2, 0, 0, 0, null); |
| 226 | } |
| 227 | |
| 228 | static SyncQueueItem obtainMessage(int what, int arg1, Object data) { |
| 229 | return obtainMessage(what, arg1, 0, 0, 0, 0, data); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static class MessageQueue { |
| 234 | |
| 235 | private SyncQueueItem mRoot; |
| 236 | |
| 237 | synchronized SyncQueueItem next() { |
| 238 | if (mRoot == null) { |
| 239 | return null; |
| 240 | } |
| 241 | final SyncQueueItem next = mRoot; |
| 242 | mRoot = mRoot.next; |
| 243 | return next; |
| 244 | } |
| 245 | |
| 246 | synchronized void sendMessageAtFrontOfQueue(SyncQueueItem item) { |
| 247 | item.next = mRoot; |
| 248 | mRoot = item; |
| 249 | } |
| 250 | |
| 251 | synchronized void sendMessage(SyncQueueItem item) { |
| 252 | if (mRoot == null) { |
| 253 | mRoot = item; |
| 254 | return; |
| 255 | } |
| 256 | SyncQueueItem last = mRoot; |
| 257 | while (last.next != null) { |
| 258 | last = last.next; |
| 259 | } |
| 260 | last.next = item; |
| 261 | } |
| 262 | |
| 263 | synchronized void removeMessages(int what) { |
| 264 | while (mRoot != null && mRoot.what == what) { |
| 265 | SyncQueueItem item = mRoot; |
| 266 | mRoot = mRoot.next; |
| 267 | item.recycle(); |
| 268 | } |
| 269 | if (mRoot != null) { |
| 270 | SyncQueueItem prev = mRoot; |
| 271 | SyncQueueItem item = prev.next; |
| 272 | while (item != null) { |
| 273 | SyncQueueItem next = item.next; |
| 274 | if (item.what == what) { |
| 275 | prev.next = next; |
| 276 | item.recycle(); |
| 277 | } else { |
| 278 | prev = item; |
| 279 | } |
| 280 | item = next; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |