| 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.text.TextUtils; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | import androidx.core.util.Pools; |
| 23 | |
| 24 | import org.telegram.messenger.BuildVars; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Collections; |
| 28 | import java.util.Date; |
| 29 | import java.util.List; |
| 30 | |
| 31 | /** |
| 32 | * Helper class that can enqueue and process adapter update operations. |
| 33 | * <p> |
| 34 | * To support animations, RecyclerView presents an older version the Adapter to best represent |
| 35 | * previous state of the layout. Sometimes, this is not trivial when items are removed that were |
| 36 | * not laid out, in which case, RecyclerView has no way of providing that item's view for |
| 37 | * animations. |
| 38 | * <p> |
| 39 | * AdapterHelper creates an UpdateOp for each adapter data change then pre-processes them. During |
| 40 | * pre processing, AdapterHelper finds out which UpdateOps can be deferred to second layout pass |
| 41 | * and which cannot. For the UpdateOps that cannot be deferred, AdapterHelper will change them |
| 42 | * according to previously deferred operation and dispatch them before the first layout pass. It |
| 43 | * also takes care of updating deferred UpdateOps since order of operations is changed by this |
| 44 | * process. |
| 45 | * <p> |
| 46 | * Although operations may be forwarded to LayoutManager in different orders, resulting data set |
| 47 | * is guaranteed to be the consistent. |
| 48 | */ |
| 49 | class AdapterHelper implements OpReorderer.Callback { |
| 50 | |
| 51 | static final int POSITION_TYPE_INVISIBLE = 0; |
| 52 | |
| 53 | static final int POSITION_TYPE_NEW_OR_LAID_OUT = 1; |
| 54 | |
| 55 | private static final boolean DEBUG = false; |
| 56 | |
| 57 | private static final String TAG = "AHT"; |
| 58 | |
| 59 | private Pools.Pool<UpdateOp> mUpdateOpPool = new Pools.SimplePool<UpdateOp>(UpdateOp.POOL_SIZE); |
| 60 | |
| 61 | final ArrayList<UpdateOp> mPendingUpdates = new ArrayList<UpdateOp>(); |
| 62 | |
| 63 | final ArrayList<UpdateOp> mPostponedList = new ArrayList<UpdateOp>(); |
| 64 | |
| 65 | final Callback mCallback; |
| 66 | |
| 67 | Runnable mOnItemProcessedCallback; |
| 68 | |
| 69 | final boolean mDisableRecycler; |
| 70 | |
| 71 | final OpReorderer mOpReorderer; |
| 72 | |
| 73 | private int mExistingUpdateTypes = 0; |
| 74 | |
| 75 | private final ArrayList<String> lastNotifies = BuildVars.DEBUG_VERSION ? new ArrayList<String>() : null; |
| 76 | private void logNotify(String name) { |
| 77 | if (lastNotifies == null) return; |
| 78 | while (lastNotifies.size() > 5) lastNotifies.remove(0); |
| 79 | final StringBuilder sb = new StringBuilder(); |
| 80 | sb.append(new Date().toString()).append(" ").append(name).append("\n"); |
| 81 | final StackTraceElement[] trace = new Exception().getStackTrace(); |
| 82 | for (int i = 0, j = 0; i < trace.length && j < 5; ++i) { |
| 83 | final String traceElement = trace[i].toString(); |
| 84 | if (traceElement.startsWith("androidx.recyclerview.widget.") && j == 0) { |
| 85 | continue; |
| 86 | } |
| 87 | sb.append("\n").append(traceElement).append("\n"); |
| 88 | j++; |
| 89 | } |
| 90 | lastNotifies.add(sb.toString()); |
| 91 | } |
| 92 | |
| 93 | public String getLastNotifies() { |
| 94 | if (lastNotifies == null) return null; |
| 95 | return TextUtils.join("\n\n", lastNotifies); |
| 96 | } |
| 97 | |
| 98 | AdapterHelper(Callback callback) { |
| 99 | this(callback, false); |
| 100 | } |
| 101 | |
| 102 | AdapterHelper(Callback callback, boolean disableRecycler) { |
| 103 | mCallback = callback; |
| 104 | mDisableRecycler = disableRecycler; |
| 105 | mOpReorderer = new OpReorderer(this); |
| 106 | } |
| 107 | |
| 108 | AdapterHelper addUpdateOp(UpdateOp... ops) { |
| 109 | Collections.addAll(mPendingUpdates, ops); |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | void reset() { |
| 114 | recycleUpdateOpsAndClearList(mPendingUpdates); |
| 115 | recycleUpdateOpsAndClearList(mPostponedList); |
| 116 | mExistingUpdateTypes = 0; |
| 117 | } |
| 118 | |
| 119 | void preProcess() { |
| 120 | mOpReorderer.reorderOps(mPendingUpdates); |
| 121 | final int count = mPendingUpdates.size(); |
| 122 | for (int i = 0; i < count; i++) { |
| 123 | UpdateOp op = mPendingUpdates.get(i); |
| 124 | switch (op.cmd) { |
| 125 | case UpdateOp.ADD: |
| 126 | applyAdd(op); |
| 127 | break; |
| 128 | case UpdateOp.REMOVE: |
| 129 | applyRemove(op); |
| 130 | break; |
| 131 | case UpdateOp.UPDATE: |
| 132 | applyUpdate(op); |
| 133 | break; |
| 134 | case UpdateOp.MOVE: |
| 135 | applyMove(op); |
| 136 | break; |
| 137 | } |
| 138 | if (mOnItemProcessedCallback != null) { |
| 139 | mOnItemProcessedCallback.run(); |
| 140 | } |
| 141 | } |
| 142 | mPendingUpdates.clear(); |
| 143 | } |
| 144 | |
| 145 | void consumePostponedUpdates() { |
| 146 | final int count = mPostponedList.size(); |
| 147 | for (int i = 0; i < count; i++) { |
| 148 | mCallback.onDispatchSecondPass(mPostponedList.get(i)); |
| 149 | } |
| 150 | recycleUpdateOpsAndClearList(mPostponedList); |
| 151 | mExistingUpdateTypes = 0; |
| 152 | } |
| 153 | |
| 154 | private void applyMove(UpdateOp op) { |
| 155 | // MOVE ops are pre-processed so at this point, we know that item is still in the adapter. |
| 156 | // otherwise, it would be converted into a REMOVE operation |
| 157 | postponeAndUpdateViewHolders(op); |
| 158 | } |
| 159 | |
| 160 | private void applyRemove(UpdateOp op) { |
| 161 | int tmpStart = op.positionStart; |
| 162 | int tmpCount = 0; |
| 163 | int tmpEnd = op.positionStart + op.itemCount; |
| 164 | int type = -1; |
| 165 | for (int position = op.positionStart; position < tmpEnd; position++) { |
| 166 | boolean typeChanged = false; |
| 167 | RecyclerView.ViewHolder vh = mCallback.findViewHolder(position); |
| 168 | if (vh != null || canFindInPreLayout(position)) { |
| 169 | // If a ViewHolder exists or this is a newly added item, we can defer this update |
| 170 | // to post layout stage. |
| 171 | // * For existing ViewHolders, we'll fake its existence in the pre-layout phase. |
| 172 | // * For items that are added and removed in the same process cycle, they won't |
| 173 | // have any effect in pre-layout since their add ops are already deferred to |
| 174 | // post-layout pass. |
| 175 | if (type == POSITION_TYPE_INVISIBLE) { |
| 176 | // Looks like we have other updates that we cannot merge with this one. |
| 177 | // Create an UpdateOp and dispatch it to LayoutManager. |
| 178 | UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); |
| 179 | dispatchAndUpdateViewHolders(newOp); |
| 180 | typeChanged = true; |
| 181 | } |
| 182 | type = POSITION_TYPE_NEW_OR_LAID_OUT; |
| 183 | } else { |
| 184 | // This update cannot be recovered because we don't have a ViewHolder representing |
| 185 | // this position. Instead, post it to LayoutManager immediately |
| 186 | if (type == POSITION_TYPE_NEW_OR_LAID_OUT) { |
| 187 | // Looks like we have other updates that we cannot merge with this one. |
| 188 | // Create UpdateOp op and dispatch it to LayoutManager. |
| 189 | UpdateOp newOp = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); |
| 190 | postponeAndUpdateViewHolders(newOp); |
| 191 | typeChanged = true; |
| 192 | } |
| 193 | type = POSITION_TYPE_INVISIBLE; |
| 194 | } |
| 195 | if (typeChanged) { |
| 196 | position -= tmpCount; // also equal to tmpStart |
| 197 | tmpEnd -= tmpCount; |
| 198 | tmpCount = 1; |
| 199 | } else { |
| 200 | tmpCount++; |
| 201 | } |
| 202 | } |
| 203 | if (tmpCount != op.itemCount) { // all 1 effect |
| 204 | recycleUpdateOp(op); |
| 205 | op = obtainUpdateOp(UpdateOp.REMOVE, tmpStart, tmpCount, null); |
| 206 | } |
| 207 | if (type == POSITION_TYPE_INVISIBLE) { |
| 208 | dispatchAndUpdateViewHolders(op); |
| 209 | } else { |
| 210 | postponeAndUpdateViewHolders(op); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private void applyUpdate(UpdateOp op) { |
| 215 | int tmpStart = op.positionStart; |
| 216 | int tmpCount = 0; |
| 217 | int tmpEnd = op.positionStart + op.itemCount; |
| 218 | int type = -1; |
| 219 | for (int position = op.positionStart; position < tmpEnd; position++) { |
| 220 | RecyclerView.ViewHolder vh = mCallback.findViewHolder(position); |
| 221 | if (vh != null || canFindInPreLayout(position)) { // deferred |
| 222 | if (type == POSITION_TYPE_INVISIBLE) { |
| 223 | UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, |
| 224 | op.payload); |
| 225 | dispatchAndUpdateViewHolders(newOp); |
| 226 | tmpCount = 0; |
| 227 | tmpStart = position; |
| 228 | } |
| 229 | type = POSITION_TYPE_NEW_OR_LAID_OUT; |
| 230 | } else { // applied |
| 231 | if (type == POSITION_TYPE_NEW_OR_LAID_OUT) { |
| 232 | UpdateOp newOp = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, |
| 233 | op.payload); |
| 234 | postponeAndUpdateViewHolders(newOp); |
| 235 | tmpCount = 0; |
| 236 | tmpStart = position; |
| 237 | } |
| 238 | type = POSITION_TYPE_INVISIBLE; |
| 239 | } |
| 240 | tmpCount++; |
| 241 | } |
| 242 | if (tmpCount != op.itemCount) { // all 1 effect |
| 243 | Object payload = op.payload; |
| 244 | recycleUpdateOp(op); |
| 245 | op = obtainUpdateOp(UpdateOp.UPDATE, tmpStart, tmpCount, payload); |
| 246 | } |
| 247 | if (type == POSITION_TYPE_INVISIBLE) { |
| 248 | dispatchAndUpdateViewHolders(op); |
| 249 | } else { |
| 250 | postponeAndUpdateViewHolders(op); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | private void dispatchAndUpdateViewHolders(UpdateOp op) { |
| 255 | // tricky part. |
| 256 | // traverse all postpones and revert their changes on this op if necessary, apply updated |
| 257 | // dispatch to them since now they are after this op. |
| 258 | if (op.cmd == UpdateOp.ADD || op.cmd == UpdateOp.MOVE) { |
| 259 | throw new IllegalArgumentException("should not dispatch add or move for pre layout"); |
| 260 | } |
| 261 | if (DEBUG) { |
| 262 | Log.d(TAG, "dispatch (pre)" + op); |
| 263 | Log.d(TAG, "postponed state before:"); |
| 264 | for (UpdateOp updateOp : mPostponedList) { |
| 265 | Log.d(TAG, updateOp.toString()); |
| 266 | } |
| 267 | Log.d(TAG, "----"); |
| 268 | } |
| 269 | |
| 270 | // handle each pos 1 by 1 to ensure continuity. If it breaks, dispatch partial |
| 271 | // TODO Since move ops are pushed to end, we should not need this anymore |
| 272 | int tmpStart = updatePositionWithPostponed(op.positionStart, op.cmd); |
| 273 | if (DEBUG) { |
| 274 | Log.d(TAG, "pos:" + op.positionStart + ",updatedPos:" + tmpStart); |
| 275 | } |
| 276 | int tmpCnt = 1; |
| 277 | int offsetPositionForPartial = op.positionStart; |
| 278 | final int positionMultiplier; |
| 279 | switch (op.cmd) { |
| 280 | case UpdateOp.UPDATE: |
| 281 | positionMultiplier = 1; |
| 282 | break; |
| 283 | case UpdateOp.REMOVE: |
| 284 | positionMultiplier = 0; |
| 285 | break; |
| 286 | default: |
| 287 | throw new IllegalArgumentException("op should be remove or update." + op); |
| 288 | } |
| 289 | for (int p = 1; p < op.itemCount; p++) { |
| 290 | final int pos = op.positionStart + (positionMultiplier * p); |
| 291 | int updatedPos = updatePositionWithPostponed(pos, op.cmd); |
| 292 | if (DEBUG) { |
| 293 | Log.d(TAG, "pos:" + pos + ",updatedPos:" + updatedPos); |
| 294 | } |
| 295 | boolean continuous = false; |
| 296 | switch (op.cmd) { |
| 297 | case UpdateOp.UPDATE: |
| 298 | continuous = updatedPos == tmpStart + 1; |
| 299 | break; |
| 300 | case UpdateOp.REMOVE: |
| 301 | continuous = updatedPos == tmpStart; |
| 302 | break; |
| 303 | } |
| 304 | if (continuous) { |
| 305 | tmpCnt++; |
| 306 | } else { |
| 307 | // need to dispatch this separately |
| 308 | UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt, op.payload); |
| 309 | if (DEBUG) { |
| 310 | Log.d(TAG, "need to dispatch separately " + tmp); |
| 311 | } |
| 312 | dispatchFirstPassAndUpdateViewHolders(tmp, offsetPositionForPartial); |
| 313 | recycleUpdateOp(tmp); |
| 314 | if (op.cmd == UpdateOp.UPDATE) { |
| 315 | offsetPositionForPartial += tmpCnt; |
| 316 | } |
| 317 | tmpStart = updatedPos; // need to remove previously dispatched |
| 318 | tmpCnt = 1; |
| 319 | } |
| 320 | } |
| 321 | Object payload = op.payload; |
| 322 | recycleUpdateOp(op); |
| 323 | if (tmpCnt > 0) { |
| 324 | UpdateOp tmp = obtainUpdateOp(op.cmd, tmpStart, tmpCnt, payload); |
| 325 | if (DEBUG) { |
| 326 | Log.d(TAG, "dispatching:" + tmp); |
| 327 | } |
| 328 | dispatchFirstPassAndUpdateViewHolders(tmp, offsetPositionForPartial); |
| 329 | recycleUpdateOp(tmp); |
| 330 | } |
| 331 | if (DEBUG) { |
| 332 | Log.d(TAG, "post dispatch"); |
| 333 | Log.d(TAG, "postponed state after:"); |
| 334 | for (UpdateOp updateOp : mPostponedList) { |
| 335 | Log.d(TAG, updateOp.toString()); |
| 336 | } |
| 337 | Log.d(TAG, "----"); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | void dispatchFirstPassAndUpdateViewHolders(UpdateOp op, int offsetStart) { |
| 342 | mCallback.onDispatchFirstPass(op); |
| 343 | switch (op.cmd) { |
| 344 | case UpdateOp.REMOVE: |
| 345 | mCallback.offsetPositionsForRemovingInvisible(offsetStart, op.itemCount); |
| 346 | break; |
| 347 | case UpdateOp.UPDATE: |
| 348 | mCallback.markViewHoldersUpdated(offsetStart, op.itemCount, op.payload); |
| 349 | break; |
| 350 | default: |
| 351 | throw new IllegalArgumentException("only remove and update ops can be dispatched" |
| 352 | + " in first pass"); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | private int updatePositionWithPostponed(int pos, int cmd) { |
| 357 | final int count = mPostponedList.size(); |
| 358 | for (int i = count - 1; i >= 0; i--) { |
| 359 | UpdateOp postponed = mPostponedList.get(i); |
| 360 | if (postponed.cmd == UpdateOp.MOVE) { |
| 361 | int start, end; |
| 362 | if (postponed.positionStart < postponed.itemCount) { |
| 363 | start = postponed.positionStart; |
| 364 | end = postponed.itemCount; |
| 365 | } else { |
| 366 | start = postponed.itemCount; |
| 367 | end = postponed.positionStart; |
| 368 | } |
| 369 | if (pos >= start && pos <= end) { |
| 370 | //i'm affected |
| 371 | if (start == postponed.positionStart) { |
| 372 | if (cmd == UpdateOp.ADD) { |
| 373 | postponed.itemCount++; |
| 374 | } else if (cmd == UpdateOp.REMOVE) { |
| 375 | postponed.itemCount--; |
| 376 | } |
| 377 | // op moved to left, move it right to revert |
| 378 | pos++; |
| 379 | } else { |
| 380 | if (cmd == UpdateOp.ADD) { |
| 381 | postponed.positionStart++; |
| 382 | } else if (cmd == UpdateOp.REMOVE) { |
| 383 | postponed.positionStart--; |
| 384 | } |
| 385 | // op was moved right, move left to revert |
| 386 | pos--; |
| 387 | } |
| 388 | } else if (pos < postponed.positionStart) { |
| 389 | // postponed MV is outside the dispatched OP. if it is before, offset |
| 390 | if (cmd == UpdateOp.ADD) { |
| 391 | postponed.positionStart++; |
| 392 | postponed.itemCount++; |
| 393 | } else if (cmd == UpdateOp.REMOVE) { |
| 394 | postponed.positionStart--; |
| 395 | postponed.itemCount--; |
| 396 | } |
| 397 | } |
| 398 | } else { |
| 399 | if (postponed.positionStart <= pos) { |
| 400 | if (postponed.cmd == UpdateOp.ADD) { |
| 401 | pos -= postponed.itemCount; |
| 402 | } else if (postponed.cmd == UpdateOp.REMOVE) { |
| 403 | pos += postponed.itemCount; |
| 404 | } |
| 405 | } else { |
| 406 | if (cmd == UpdateOp.ADD) { |
| 407 | postponed.positionStart++; |
| 408 | } else if (cmd == UpdateOp.REMOVE) { |
| 409 | postponed.positionStart--; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | if (DEBUG) { |
| 414 | Log.d(TAG, "dispath (step" + i + ")"); |
| 415 | Log.d(TAG, "postponed state:" + i + ", pos:" + pos); |
| 416 | for (UpdateOp updateOp : mPostponedList) { |
| 417 | Log.d(TAG, updateOp.toString()); |
| 418 | } |
| 419 | Log.d(TAG, "----"); |
| 420 | } |
| 421 | } |
| 422 | for (int i = mPostponedList.size() - 1; i >= 0; i--) { |
| 423 | UpdateOp op = mPostponedList.get(i); |
| 424 | if (op.cmd == UpdateOp.MOVE) { |
| 425 | if (op.itemCount == op.positionStart || op.itemCount < 0) { |
| 426 | mPostponedList.remove(i); |
| 427 | recycleUpdateOp(op); |
| 428 | } |
| 429 | } else if (op.itemCount <= 0) { |
| 430 | mPostponedList.remove(i); |
| 431 | recycleUpdateOp(op); |
| 432 | } |
| 433 | } |
| 434 | return pos; |
| 435 | } |
| 436 | |
| 437 | private boolean canFindInPreLayout(int position) { |
| 438 | final int count = mPostponedList.size(); |
| 439 | for (int i = 0; i < count; i++) { |
| 440 | UpdateOp op = mPostponedList.get(i); |
| 441 | if (op.cmd == UpdateOp.MOVE) { |
| 442 | if (findPositionOffset(op.itemCount, i + 1) == position) { |
| 443 | return true; |
| 444 | } |
| 445 | } else if (op.cmd == UpdateOp.ADD) { |
| 446 | // TODO optimize. |
| 447 | final int end = op.positionStart + op.itemCount; |
| 448 | for (int pos = op.positionStart; pos < end; pos++) { |
| 449 | if (findPositionOffset(pos, i + 1) == position) { |
| 450 | return true; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | private void applyAdd(UpdateOp op) { |
| 459 | postponeAndUpdateViewHolders(op); |
| 460 | } |
| 461 | |
| 462 | private void postponeAndUpdateViewHolders(UpdateOp op) { |
| 463 | if (DEBUG) { |
| 464 | Log.d(TAG, "postponing " + op); |
| 465 | } |
| 466 | mPostponedList.add(op); |
| 467 | switch (op.cmd) { |
| 468 | case UpdateOp.ADD: |
| 469 | mCallback.offsetPositionsForAdd(op.positionStart, op.itemCount); |
| 470 | break; |
| 471 | case UpdateOp.MOVE: |
| 472 | mCallback.offsetPositionsForMove(op.positionStart, op.itemCount); |
| 473 | break; |
| 474 | case UpdateOp.REMOVE: |
| 475 | mCallback.offsetPositionsForRemovingLaidOutOrNewView(op.positionStart, |
| 476 | op.itemCount); |
| 477 | break; |
| 478 | case UpdateOp.UPDATE: |
| 479 | mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload); |
| 480 | break; |
| 481 | default: |
| 482 | throw new IllegalArgumentException("Unknown update op type for " + op); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | boolean hasPendingUpdates() { |
| 487 | return mPendingUpdates.size() > 0; |
| 488 | } |
| 489 | |
| 490 | boolean hasAnyUpdateTypes(int updateTypes) { |
| 491 | return (mExistingUpdateTypes & updateTypes) != 0; |
| 492 | } |
| 493 | |
| 494 | int findPositionOffset(int position) { |
| 495 | return findPositionOffset(position, 0); |
| 496 | } |
| 497 | |
| 498 | int findPositionOffset(int position, int firstPostponedItem) { |
| 499 | int count = mPostponedList.size(); |
| 500 | for (int i = firstPostponedItem; i < count; ++i) { |
| 501 | UpdateOp op = mPostponedList.get(i); |
| 502 | if (op.cmd == UpdateOp.MOVE) { |
| 503 | if (op.positionStart == position) { |
| 504 | position = op.itemCount; |
| 505 | } else { |
| 506 | if (op.positionStart < position) { |
| 507 | position--; // like a remove |
| 508 | } |
| 509 | if (op.itemCount <= position) { |
| 510 | position++; // like an add |
| 511 | } |
| 512 | } |
| 513 | } else if (op.positionStart <= position) { |
| 514 | if (op.cmd == UpdateOp.REMOVE) { |
| 515 | if (position < op.positionStart + op.itemCount) { |
| 516 | return -1; |
| 517 | } |
| 518 | position -= op.itemCount; |
| 519 | } else if (op.cmd == UpdateOp.ADD) { |
| 520 | position += op.itemCount; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | return position; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * @return True if updates should be processed. |
| 529 | */ |
| 530 | boolean onItemRangeChanged(int positionStart, int itemCount, Object payload) { |
| 531 | if (itemCount < 1) { |
| 532 | return false; |
| 533 | } |
| 534 | if (BuildVars.DEBUG_VERSION) { |
| 535 | logNotify("onItemRangeChanged(" + positionStart + ", " + itemCount + ", " + payload + ")"); |
| 536 | } |
| 537 | mPendingUpdates.add(obtainUpdateOp(UpdateOp.UPDATE, positionStart, itemCount, payload)); |
| 538 | mExistingUpdateTypes |= UpdateOp.UPDATE; |
| 539 | return mPendingUpdates.size() == 1; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * @return True if updates should be processed. |
| 544 | */ |
| 545 | boolean onItemRangeInserted(int positionStart, int itemCount) { |
| 546 | if (itemCount < 1) { |
| 547 | return false; |
| 548 | } |
| 549 | if (BuildVars.DEBUG_VERSION) { |
| 550 | logNotify("onItemRangeInserted(" + positionStart + ", " + itemCount + ")"); |
| 551 | } |
| 552 | mPendingUpdates.add(obtainUpdateOp(UpdateOp.ADD, positionStart, itemCount, null)); |
| 553 | mExistingUpdateTypes |= UpdateOp.ADD; |
| 554 | return mPendingUpdates.size() == 1; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @return True if updates should be processed. |
| 559 | */ |
| 560 | boolean onItemRangeRemoved(int positionStart, int itemCount) { |
| 561 | if (itemCount < 1) { |
| 562 | return false; |
| 563 | } |
| 564 | if (BuildVars.DEBUG_VERSION) { |
| 565 | logNotify("onItemRangeRemoved(" + positionStart + ", " + itemCount + ")"); |
| 566 | } |
| 567 | mPendingUpdates.add(obtainUpdateOp(UpdateOp.REMOVE, positionStart, itemCount, null)); |
| 568 | mExistingUpdateTypes |= UpdateOp.REMOVE; |
| 569 | return mPendingUpdates.size() == 1; |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * @return True if updates should be processed. |
| 574 | */ |
| 575 | boolean onItemRangeMoved(int from, int to, int itemCount) { |
| 576 | if (from == to) { |
| 577 | return false; // no-op |
| 578 | } |
| 579 | if (itemCount != 1) { |
| 580 | throw new IllegalArgumentException("Moving more than 1 item is not supported yet"); |
| 581 | } |
| 582 | if (BuildVars.DEBUG_VERSION) { |
| 583 | logNotify("onItemRangeMoved(" + from + ", " + to + ", " + itemCount + ")"); |
| 584 | } |
| 585 | mPendingUpdates.add(obtainUpdateOp(UpdateOp.MOVE, from, to, null)); |
| 586 | mExistingUpdateTypes |= UpdateOp.MOVE; |
| 587 | return mPendingUpdates.size() == 1; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Skips pre-processing and applies all updates in one pass. |
| 592 | */ |
| 593 | void consumeUpdatesInOnePass() { |
| 594 | // we still consume postponed updates (if there is) in case there was a pre-process call |
| 595 | // w/o a matching consumePostponedUpdates. |
| 596 | consumePostponedUpdates(); |
| 597 | final int count = mPendingUpdates.size(); |
| 598 | for (int i = 0; i < count; i++) { |
| 599 | UpdateOp op = mPendingUpdates.get(i); |
| 600 | switch (op.cmd) { |
| 601 | case UpdateOp.ADD: |
| 602 | mCallback.onDispatchSecondPass(op); |
| 603 | mCallback.offsetPositionsForAdd(op.positionStart, op.itemCount); |
| 604 | break; |
| 605 | case UpdateOp.REMOVE: |
| 606 | mCallback.onDispatchSecondPass(op); |
| 607 | mCallback.offsetPositionsForRemovingInvisible(op.positionStart, op.itemCount); |
| 608 | break; |
| 609 | case UpdateOp.UPDATE: |
| 610 | mCallback.onDispatchSecondPass(op); |
| 611 | mCallback.markViewHoldersUpdated(op.positionStart, op.itemCount, op.payload); |
| 612 | break; |
| 613 | case UpdateOp.MOVE: |
| 614 | mCallback.onDispatchSecondPass(op); |
| 615 | mCallback.offsetPositionsForMove(op.positionStart, op.itemCount); |
| 616 | break; |
| 617 | } |
| 618 | if (mOnItemProcessedCallback != null) { |
| 619 | mOnItemProcessedCallback.run(); |
| 620 | } |
| 621 | } |
| 622 | recycleUpdateOpsAndClearList(mPendingUpdates); |
| 623 | mExistingUpdateTypes = 0; |
| 624 | } |
| 625 | |
| 626 | public int applyPendingUpdatesToPosition(int position) { |
| 627 | final int size = mPendingUpdates.size(); |
| 628 | for (int i = 0; i < size; i++) { |
| 629 | UpdateOp op = mPendingUpdates.get(i); |
| 630 | switch (op.cmd) { |
| 631 | case UpdateOp.ADD: |
| 632 | if (op.positionStart <= position) { |
| 633 | position += op.itemCount; |
| 634 | } |
| 635 | break; |
| 636 | case UpdateOp.REMOVE: |
| 637 | if (op.positionStart <= position) { |
| 638 | final int end = op.positionStart + op.itemCount; |
| 639 | if (end > position) { |
| 640 | return RecyclerView.NO_POSITION; |
| 641 | } |
| 642 | position -= op.itemCount; |
| 643 | } |
| 644 | break; |
| 645 | case UpdateOp.MOVE: |
| 646 | if (op.positionStart == position) { |
| 647 | position = op.itemCount; //position end |
| 648 | } else { |
| 649 | if (op.positionStart < position) { |
| 650 | position -= 1; |
| 651 | } |
| 652 | if (op.itemCount <= position) { |
| 653 | position += 1; |
| 654 | } |
| 655 | } |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | return position; |
| 660 | } |
| 661 | |
| 662 | boolean hasUpdates() { |
| 663 | return !mPostponedList.isEmpty() && !mPendingUpdates.isEmpty(); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Queued operation to happen when child views are updated. |
| 668 | */ |
| 669 | static class UpdateOp { |
| 670 | |
| 671 | static final int ADD = 1; |
| 672 | |
| 673 | static final int REMOVE = 1 << 1; |
| 674 | |
| 675 | static final int UPDATE = 1 << 2; |
| 676 | |
| 677 | static final int MOVE = 1 << 3; |
| 678 | |
| 679 | static final int POOL_SIZE = 30; |
| 680 | |
| 681 | int cmd; |
| 682 | |
| 683 | int positionStart; |
| 684 | |
| 685 | Object payload; |
| 686 | |
| 687 | // holds the target position if this is a MOVE |
| 688 | int itemCount; |
| 689 | |
| 690 | UpdateOp(int cmd, int positionStart, int itemCount, Object payload) { |
| 691 | this.cmd = cmd; |
| 692 | this.positionStart = positionStart; |
| 693 | this.itemCount = itemCount; |
| 694 | this.payload = payload; |
| 695 | } |
| 696 | |
| 697 | String cmdToString() { |
| 698 | switch (cmd) { |
| 699 | case ADD: |
| 700 | return "add"; |
| 701 | case REMOVE: |
| 702 | return "rm"; |
| 703 | case UPDATE: |
| 704 | return "up"; |
| 705 | case MOVE: |
| 706 | return "mv"; |
| 707 | } |
| 708 | return "??"; |
| 709 | } |
| 710 | |
| 711 | @Override |
| 712 | public String toString() { |
| 713 | return Integer.toHexString(System.identityHashCode(this)) |
| 714 | + "[" + cmdToString() + ",s:" + positionStart + "c:" + itemCount |
| 715 | + ",p:" + payload + "]"; |
| 716 | } |
| 717 | |
| 718 | @Override |
| 719 | public boolean equals(Object o) { |
| 720 | if (this == o) { |
| 721 | return true; |
| 722 | } |
| 723 | if (o == null || getClass() != o.getClass()) { |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | UpdateOp op = (UpdateOp) o; |
| 728 | |
| 729 | if (cmd != op.cmd) { |
| 730 | return false; |
| 731 | } |
| 732 | if (cmd == MOVE && Math.abs(itemCount - positionStart) == 1) { |
| 733 | // reverse of this is also true |
| 734 | if (itemCount == op.positionStart && positionStart == op.itemCount) { |
| 735 | return true; |
| 736 | } |
| 737 | } |
| 738 | if (itemCount != op.itemCount) { |
| 739 | return false; |
| 740 | } |
| 741 | if (positionStart != op.positionStart) { |
| 742 | return false; |
| 743 | } |
| 744 | if (payload != null) { |
| 745 | if (!payload.equals(op.payload)) { |
| 746 | return false; |
| 747 | } |
| 748 | } else if (op.payload != null) { |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | @Override |
| 756 | public int hashCode() { |
| 757 | int result = cmd; |
| 758 | result = 31 * result + positionStart; |
| 759 | result = 31 * result + itemCount; |
| 760 | return result; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public UpdateOp obtainUpdateOp(int cmd, int positionStart, int itemCount, Object payload) { |
| 766 | UpdateOp op = mUpdateOpPool.acquire(); |
| 767 | if (op == null) { |
| 768 | op = new UpdateOp(cmd, positionStart, itemCount, payload); |
| 769 | } else { |
| 770 | op.cmd = cmd; |
| 771 | op.positionStart = positionStart; |
| 772 | op.itemCount = itemCount; |
| 773 | op.payload = payload; |
| 774 | } |
| 775 | return op; |
| 776 | } |
| 777 | |
| 778 | @Override |
| 779 | public void recycleUpdateOp(UpdateOp op) { |
| 780 | if (!mDisableRecycler) { |
| 781 | op.payload = null; |
| 782 | mUpdateOpPool.release(op); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | void recycleUpdateOpsAndClearList(List<UpdateOp> ops) { |
| 787 | final int count = ops.size(); |
| 788 | for (int i = 0; i < count; i++) { |
| 789 | recycleUpdateOp(ops.get(i)); |
| 790 | } |
| 791 | ops.clear(); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Contract between AdapterHelper and RecyclerView. |
| 796 | */ |
| 797 | interface Callback { |
| 798 | |
| 799 | RecyclerView.ViewHolder findViewHolder(int position); |
| 800 | |
| 801 | void offsetPositionsForRemovingInvisible(int positionStart, int itemCount); |
| 802 | |
| 803 | void offsetPositionsForRemovingLaidOutOrNewView(int positionStart, int itemCount); |
| 804 | |
| 805 | void markViewHoldersUpdated(int positionStart, int itemCount, Object payloads); |
| 806 | |
| 807 | void onDispatchFirstPass(UpdateOp updateOp); |
| 808 | |
| 809 | void onDispatchSecondPass(UpdateOp updateOp); |
| 810 | |
| 811 | void offsetPositionsForAdd(int positionStart, int itemCount); |
| 812 | |
| 813 | void offsetPositionsForMove(int from, int to); |
| 814 | } |
| 815 | } |
| 816 | |