| 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.util.Log; |
| 20 | import android.view.View; |
| 21 | import android.view.ViewGroup; |
| 22 | |
| 23 | import org.telegram.messenger.BuildVars; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Helper class to manage children. |
| 30 | * <p> |
| 31 | * It wraps a RecyclerView and adds ability to hide some children. There are two sets of methods |
| 32 | * provided by this class. <b>Regular</b> methods are the ones that replicate ViewGroup methods |
| 33 | * like getChildAt, getChildCount etc. These methods ignore hidden children. |
| 34 | * <p> |
| 35 | * When RecyclerView needs direct access to the view group children, it can call unfiltered |
| 36 | * methods like get getUnfilteredChildCount or getUnfilteredChildAt. |
| 37 | */ |
| 38 | public class ChildHelper { |
| 39 | |
| 40 | private static final boolean DEBUG = false;//BuildVars.DEBUG_VERSION; |
| 41 | |
| 42 | private static final String TAG = "ChildrenHelper"; |
| 43 | |
| 44 | final Callback mCallback; |
| 45 | |
| 46 | final Bucket mBucket; |
| 47 | |
| 48 | final List<View> mHiddenViews; |
| 49 | |
| 50 | ChildHelper(Callback callback) { |
| 51 | mCallback = callback; |
| 52 | mBucket = new Bucket(); |
| 53 | mHiddenViews = new ArrayList<View>(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Marks a child view as hidden |
| 58 | * |
| 59 | * @param child View to hide. |
| 60 | */ |
| 61 | public void hideViewInternal(View child) { |
| 62 | mHiddenViews.add(child); |
| 63 | mCallback.onEnteredHiddenState(child); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Unmarks a child view as hidden. |
| 68 | * |
| 69 | * @param child View to hide. |
| 70 | */ |
| 71 | public boolean unhideViewInternal(View child) { |
| 72 | if (mHiddenViews.remove(child)) { |
| 73 | mCallback.onLeftHiddenState(child); |
| 74 | return true; |
| 75 | } else { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public int getHiddenChildCount() { |
| 81 | return mHiddenViews.size(); |
| 82 | } |
| 83 | |
| 84 | public View getHiddenChildAt(int index) { |
| 85 | if (index < 0 || index >= mHiddenViews.size()) { |
| 86 | return null; |
| 87 | } |
| 88 | return mHiddenViews.get(index); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Adds a view to the ViewGroup |
| 93 | * |
| 94 | * @param child View to add. |
| 95 | * @param hidden If set to true, this item will be invisible from regular methods. |
| 96 | */ |
| 97 | void addView(View child, boolean hidden) { |
| 98 | addView(child, -1, hidden); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Add a view to the ViewGroup at an index |
| 103 | * |
| 104 | * @param child View to add. |
| 105 | * @param index Index of the child from the regular perspective (excluding hidden views). |
| 106 | * ChildHelper offsets this index to actual ViewGroup index. |
| 107 | * @param hidden If set to true, this item will be invisible from regular methods. |
| 108 | */ |
| 109 | void addView(View child, int index, boolean hidden) { |
| 110 | final int offset; |
| 111 | if (index < 0) { |
| 112 | offset = mCallback.getChildCount(); |
| 113 | } else { |
| 114 | offset = getOffset(index); |
| 115 | } |
| 116 | mBucket.insert(offset, hidden); |
| 117 | if (hidden) { |
| 118 | hideViewInternal(child); |
| 119 | } |
| 120 | mCallback.addView(child, offset); |
| 121 | if (DEBUG) { |
| 122 | Log.d(TAG, "addViewAt " + index + ",h:" + hidden + ", " + this); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private int getOffset(int index) { |
| 127 | if (index < 0) { |
| 128 | return -1; //anything below 0 won't work as diff will be undefined. |
| 129 | } |
| 130 | final int limit = mCallback.getChildCount(); |
| 131 | int offset = index; |
| 132 | while (offset < limit) { |
| 133 | final int removedBefore = mBucket.countOnesBefore(offset); |
| 134 | final int diff = index - (offset - removedBefore); |
| 135 | if (diff == 0) { |
| 136 | while (mBucket.get(offset)) { // ensure this offset is not hidden |
| 137 | offset++; |
| 138 | } |
| 139 | return offset; |
| 140 | } else { |
| 141 | offset += diff; |
| 142 | } |
| 143 | } |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Removes the provided View from underlying RecyclerView. |
| 149 | * |
| 150 | * @param view The view to remove. |
| 151 | */ |
| 152 | void removeView(View view) { |
| 153 | int index = mCallback.indexOfChild(view); |
| 154 | if (index < 0) { |
| 155 | return; |
| 156 | } |
| 157 | if (mBucket.remove(index)) { |
| 158 | unhideViewInternal(view); |
| 159 | } |
| 160 | mCallback.removeViewAt(index); |
| 161 | if (DEBUG) { |
| 162 | Log.d(TAG, "remove View off:" + index + "," + this); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Removes the view at the provided index from RecyclerView. |
| 168 | * |
| 169 | * @param index Index of the child from the regular perspective (excluding hidden views). |
| 170 | * ChildHelper offsets this index to actual ViewGroup index. |
| 171 | */ |
| 172 | void removeViewAt(int index) { |
| 173 | final int offset = getOffset(index); |
| 174 | final View view = mCallback.getChildAt(offset); |
| 175 | if (view == null) { |
| 176 | return; |
| 177 | } |
| 178 | if (mBucket.remove(offset)) { |
| 179 | unhideViewInternal(view); |
| 180 | } |
| 181 | mCallback.removeViewAt(offset); |
| 182 | if (DEBUG) { |
| 183 | Log.d(TAG, "removeViewAt " + index + ", off:" + offset + ", " + this); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns the child at provided index. |
| 189 | * |
| 190 | * @param index Index of the child to return in regular perspective. |
| 191 | */ |
| 192 | View getChildAt(int index) { |
| 193 | final int offset = getOffset(index); |
| 194 | return mCallback.getChildAt(offset); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Removes all views from the ViewGroup including the hidden ones. |
| 199 | */ |
| 200 | void removeAllViewsUnfiltered() { |
| 201 | mBucket.reset(); |
| 202 | for (int i = mHiddenViews.size() - 1; i >= 0; i--) { |
| 203 | mCallback.onLeftHiddenState(mHiddenViews.get(i)); |
| 204 | mHiddenViews.remove(i); |
| 205 | } |
| 206 | mCallback.removeAllViews(); |
| 207 | if (DEBUG) { |
| 208 | Log.d(TAG, "removeAllViewsUnfiltered"); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * This can be used to find a disappearing view by position. |
| 214 | * |
| 215 | * @param position The adapter position of the item. |
| 216 | * @return A hidden view with a valid ViewHolder that matches the position. |
| 217 | */ |
| 218 | View findHiddenNonRemovedView(int position) { |
| 219 | final int count = mHiddenViews.size(); |
| 220 | for (int i = 0; i < count; i++) { |
| 221 | final View view = mHiddenViews.get(i); |
| 222 | RecyclerView.ViewHolder holder = mCallback.getChildViewHolder(view); |
| 223 | if (holder.getLayoutPosition() == position |
| 224 | && !holder.isInvalid() |
| 225 | && !holder.isRemoved()) { |
| 226 | return view; |
| 227 | } |
| 228 | } |
| 229 | return null; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Attaches the provided view to the underlying ViewGroup. |
| 234 | * |
| 235 | * @param child Child to attach. |
| 236 | * @param index Index of the child to attach in regular perspective. |
| 237 | * @param layoutParams LayoutParams for the child. |
| 238 | * @param hidden If set to true, this item will be invisible to the regular methods. |
| 239 | */ |
| 240 | void attachViewToParent(View child, int index, ViewGroup.LayoutParams layoutParams, |
| 241 | boolean hidden) { |
| 242 | final int offset; |
| 243 | if (index < 0) { |
| 244 | offset = mCallback.getChildCount(); |
| 245 | } else { |
| 246 | offset = getOffset(index); |
| 247 | } |
| 248 | mBucket.insert(offset, hidden); |
| 249 | if (hidden) { |
| 250 | hideViewInternal(child); |
| 251 | } |
| 252 | mCallback.attachViewToParent(child, offset, layoutParams); |
| 253 | if (DEBUG) { |
| 254 | Log.d(TAG, "attach view to parent index:" + index + ",off:" + offset + "," |
| 255 | + "h:" + hidden + ", " + this); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Returns the number of children that are not hidden. |
| 261 | * |
| 262 | * @return Number of children that are not hidden. |
| 263 | * @see #getChildAt(int) |
| 264 | */ |
| 265 | public int getChildCount() { |
| 266 | return mCallback.getChildCount() - mHiddenViews.size(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Returns the total number of children. |
| 271 | * |
| 272 | * @return The total number of children including the hidden views. |
| 273 | * @see #getUnfilteredChildAt(int) |
| 274 | */ |
| 275 | int getUnfilteredChildCount() { |
| 276 | return mCallback.getChildCount(); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Returns a child by ViewGroup offset. ChildHelper won't offset this index. |
| 281 | * |
| 282 | * @param index ViewGroup index of the child to return. |
| 283 | * @return The view in the provided index. |
| 284 | */ |
| 285 | View getUnfilteredChildAt(int index) { |
| 286 | return mCallback.getChildAt(index); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Detaches the view at the provided index. |
| 291 | * |
| 292 | * @param index Index of the child to return in regular perspective. |
| 293 | */ |
| 294 | void detachViewFromParent(int index) { |
| 295 | final int offset = getOffset(index); |
| 296 | mBucket.remove(offset); |
| 297 | mCallback.detachViewFromParent(offset); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Returns the index of the child in regular perspective. |
| 302 | * |
| 303 | * @param child The child whose index will be returned. |
| 304 | * @return The regular perspective index of the child or -1 if it does not exists. |
| 305 | */ |
| 306 | int indexOfChild(View child) { |
| 307 | final int index = mCallback.indexOfChild(child); |
| 308 | if (index == -1) { |
| 309 | return -1; |
| 310 | } |
| 311 | if (mBucket.get(index)) { |
| 312 | if (DEBUG) { |
| 313 | throw new IllegalArgumentException("cannot get index of a hidden child"); |
| 314 | } else { |
| 315 | return -1; |
| 316 | } |
| 317 | } |
| 318 | // reverse the index |
| 319 | return index - mBucket.countOnesBefore(index); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Returns whether a View is visible to LayoutManager or not. |
| 324 | * |
| 325 | * @param view The child view to check. Should be a child of the Callback. |
| 326 | * @return True if the View is not visible to LayoutManager |
| 327 | */ |
| 328 | public boolean isHidden(View view) { |
| 329 | return mHiddenViews.contains(view); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Marks a child view as hidden. |
| 334 | * |
| 335 | * @param view The view to hide. |
| 336 | */ |
| 337 | void hide(View view) { |
| 338 | final int offset = mCallback.indexOfChild(view); |
| 339 | if (offset < 0) { |
| 340 | throw new IllegalArgumentException("view is not a child, cannot hide " + view); |
| 341 | } |
| 342 | if (DEBUG && mBucket.get(offset)) { |
| 343 | throw new RuntimeException("trying to hide same view twice, how come ? " + view); |
| 344 | } |
| 345 | mBucket.set(offset); |
| 346 | hideViewInternal(view); |
| 347 | if (DEBUG) { |
| 348 | Log.d(TAG, "hiding child " + view + " at offset " + offset + ", " + this); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Moves a child view from hidden list to regular list. |
| 354 | * Calling this method should probably be followed by a detach, otherwise, it will suddenly |
| 355 | * show up in LayoutManager's children list. |
| 356 | * |
| 357 | * @param view The hidden View to unhide |
| 358 | */ |
| 359 | void unhide(View view) { |
| 360 | final int offset = mCallback.indexOfChild(view); |
| 361 | if (offset < 0) { |
| 362 | throw new IllegalArgumentException("view is not a child, cannot hide " + view); |
| 363 | } |
| 364 | if (!mBucket.get(offset)) { |
| 365 | throw new RuntimeException("trying to unhide a view that was not hidden" + view); |
| 366 | } |
| 367 | mBucket.clear(offset); |
| 368 | unhideViewInternal(view); |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public String toString() { |
| 373 | return mBucket.toString() + ", hidden list:" + mHiddenViews.size(); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Removes a view from the ViewGroup if it is hidden. |
| 378 | * |
| 379 | * @param view The view to remove. |
| 380 | * @return True if the View is found and it is hidden. False otherwise. |
| 381 | */ |
| 382 | boolean removeViewIfHidden(View view) { |
| 383 | final int index = mCallback.indexOfChild(view); |
| 384 | if (index == -1) { |
| 385 | if (unhideViewInternal(view) && DEBUG) { |
| 386 | throw new IllegalStateException("view is in hidden list but not in view group"); |
| 387 | } |
| 388 | return true; |
| 389 | } |
| 390 | if (mBucket.get(index)) { |
| 391 | mBucket.remove(index); |
| 392 | if (!unhideViewInternal(view) && DEBUG) { |
| 393 | throw new IllegalStateException( |
| 394 | "removed a hidden view but it is not in hidden views list"); |
| 395 | } |
| 396 | mCallback.removeViewAt(index); |
| 397 | return true; |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Bitset implementation that provides methods to offset indices. |
| 404 | */ |
| 405 | static class Bucket { |
| 406 | |
| 407 | static final int BITS_PER_WORD = Long.SIZE; |
| 408 | |
| 409 | static final long LAST_BIT = 1L << (Long.SIZE - 1); |
| 410 | |
| 411 | long mData = 0; |
| 412 | |
| 413 | Bucket mNext; |
| 414 | |
| 415 | void set(int index) { |
| 416 | if (index >= BITS_PER_WORD) { |
| 417 | ensureNext(); |
| 418 | mNext.set(index - BITS_PER_WORD); |
| 419 | } else { |
| 420 | mData |= 1L << index; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | private void ensureNext() { |
| 425 | if (mNext == null) { |
| 426 | mNext = new Bucket(); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | void clear(int index) { |
| 431 | if (index >= BITS_PER_WORD) { |
| 432 | if (mNext != null) { |
| 433 | mNext.clear(index - BITS_PER_WORD); |
| 434 | } |
| 435 | } else { |
| 436 | mData &= ~(1L << index); |
| 437 | } |
| 438 | |
| 439 | } |
| 440 | |
| 441 | boolean get(int index) { |
| 442 | if (index >= BITS_PER_WORD) { |
| 443 | ensureNext(); |
| 444 | return mNext.get(index - BITS_PER_WORD); |
| 445 | } else { |
| 446 | return (mData & (1L << index)) != 0; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | void reset() { |
| 451 | mData = 0; |
| 452 | if (mNext != null) { |
| 453 | mNext.reset(); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | void insert(int index, boolean value) { |
| 458 | if (index >= BITS_PER_WORD) { |
| 459 | ensureNext(); |
| 460 | mNext.insert(index - BITS_PER_WORD, value); |
| 461 | } else { |
| 462 | final boolean lastBit = (mData & LAST_BIT) != 0; |
| 463 | long mask = (1L << index) - 1; |
| 464 | final long before = mData & mask; |
| 465 | final long after = ((mData & ~mask)) << 1; |
| 466 | mData = before | after; |
| 467 | if (value) { |
| 468 | set(index); |
| 469 | } else { |
| 470 | clear(index); |
| 471 | } |
| 472 | if (lastBit || mNext != null) { |
| 473 | ensureNext(); |
| 474 | mNext.insert(0, lastBit); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | boolean remove(int index) { |
| 480 | if (index >= BITS_PER_WORD) { |
| 481 | ensureNext(); |
| 482 | return mNext.remove(index - BITS_PER_WORD); |
| 483 | } else { |
| 484 | long mask = (1L << index); |
| 485 | final boolean value = (mData & mask) != 0; |
| 486 | mData &= ~mask; |
| 487 | mask = mask - 1; |
| 488 | final long before = mData & mask; |
| 489 | // cannot use >> because it adds one. |
| 490 | final long after = Long.rotateRight(mData & ~mask, 1); |
| 491 | mData = before | after; |
| 492 | if (mNext != null) { |
| 493 | if (mNext.get(0)) { |
| 494 | set(BITS_PER_WORD - 1); |
| 495 | } |
| 496 | mNext.remove(0); |
| 497 | } |
| 498 | return value; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | int countOnesBefore(int index) { |
| 503 | if (mNext == null) { |
| 504 | if (index >= BITS_PER_WORD) { |
| 505 | return Long.bitCount(mData); |
| 506 | } |
| 507 | return Long.bitCount(mData & ((1L << index) - 1)); |
| 508 | } |
| 509 | if (index < BITS_PER_WORD) { |
| 510 | return Long.bitCount(mData & ((1L << index) - 1)); |
| 511 | } else { |
| 512 | return mNext.countOnesBefore(index - BITS_PER_WORD) + Long.bitCount(mData); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public String toString() { |
| 518 | return mNext == null ? Long.toBinaryString(mData) |
| 519 | : mNext.toString() + "xx" + Long.toBinaryString(mData); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | interface Callback { |
| 524 | |
| 525 | int getChildCount(); |
| 526 | |
| 527 | void addView(View child, int index); |
| 528 | |
| 529 | int indexOfChild(View view); |
| 530 | |
| 531 | void removeViewAt(int index); |
| 532 | |
| 533 | View getChildAt(int offset); |
| 534 | |
| 535 | void removeAllViews(); |
| 536 | |
| 537 | RecyclerView.ViewHolder getChildViewHolder(View view); |
| 538 | |
| 539 | void attachViewToParent(View child, int index, ViewGroup.LayoutParams layoutParams); |
| 540 | |
| 541 | void detachViewFromParent(int offset); |
| 542 | |
| 543 | void onEnteredHiddenState(View child); |
| 544 | |
| 545 | void onLeftHiddenState(View child); |
| 546 | } |
| 547 | } |
| 548 | |