1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * Unless required by applicable law or agreed to in writing, software 8 * distributed under the License is distributed on an "AS IS" BASIS, 9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 * See the License for the specific language governing permissions and 11 * limitations under the License. 12 */ 13 14package android.support.v7.widget; 15 16import android.support.annotation.NonNull; 17 18import java.util.ArrayList; 19import java.util.List; 20import java.util.concurrent.CountDownLatch; 21import java.util.concurrent.TimeUnit; 22import android.support.v7.widget.BaseRecyclerViewAnimationsTest.AnimateDisappearance; 23import android.support.v7.widget.BaseRecyclerViewAnimationsTest.AnimateChange; 24import android.support.v7.widget.BaseRecyclerViewAnimationsTest.AnimatePersistence; 25import android.support.v7.widget.BaseRecyclerViewAnimationsTest.AnimateAppearance; 26 27public class LoggingItemAnimator extends DefaultItemAnimator { 28 29 final ArrayList<RecyclerView.ViewHolder> mAddVHs = new ArrayList<RecyclerView.ViewHolder>(); 30 31 final ArrayList<RecyclerView.ViewHolder> mRemoveVHs = new ArrayList<RecyclerView.ViewHolder>(); 32 33 final ArrayList<RecyclerView.ViewHolder> mMoveVHs = new ArrayList<RecyclerView.ViewHolder>(); 34 35 final ArrayList<RecyclerView.ViewHolder> mChangeOldVHs = new ArrayList<RecyclerView.ViewHolder>(); 36 37 final ArrayList<RecyclerView.ViewHolder> mChangeNewVHs = new ArrayList<RecyclerView.ViewHolder>(); 38 39 List<AnimateAppearance> mAnimateAppearanceList = new ArrayList<>(); 40 List<AnimateDisappearance> mAnimateDisappearanceList = new ArrayList<>(); 41 List<AnimatePersistence> mAnimatePersistenceList = new ArrayList<>(); 42 List<AnimateChange> mAnimateChangeList = new ArrayList<>(); 43 44 CountDownLatch mWaitForPendingAnimations; 45 46 public boolean contains(RecyclerView.ViewHolder viewHolder, 47 List<? extends BaseRecyclerViewAnimationsTest.AnimateLogBase> list) { 48 for (BaseRecyclerViewAnimationsTest.AnimateLogBase log : list) { 49 if (log.viewHolder == viewHolder) { 50 return true; 51 } 52 if (log instanceof AnimateChange) { 53 if (((AnimateChange) log).newHolder == viewHolder) { 54 return true; 55 } 56 } 57 } 58 return false; 59 } 60 61 @NonNull 62 @Override 63 public ItemHolderInfo recordPreLayoutInformation(@NonNull RecyclerView.State state, 64 @NonNull RecyclerView.ViewHolder viewHolder, 65 @AdapterChanges int changeFlags, @NonNull List<Object> payloads) { 66 BaseRecyclerViewAnimationsTest.LoggingInfo 67 loggingInfo = new BaseRecyclerViewAnimationsTest.LoggingInfo(viewHolder, changeFlags, payloads); 68 return loggingInfo; 69 } 70 71 @NonNull 72 @Override 73 public ItemHolderInfo recordPostLayoutInformation(@NonNull RecyclerView.State state, 74 @NonNull RecyclerView.ViewHolder viewHolder) { 75 BaseRecyclerViewAnimationsTest.LoggingInfo 76 loggingInfo = new BaseRecyclerViewAnimationsTest.LoggingInfo(viewHolder, 0, null); 77 return loggingInfo; 78 } 79 80 81 @Override 82 public boolean animateDisappearance(@NonNull RecyclerView.ViewHolder viewHolder, 83 @NonNull ItemHolderInfo preLayoutInfo, ItemHolderInfo postLayoutInfo) { 84 mAnimateDisappearanceList 85 .add(new AnimateDisappearance(viewHolder, 86 (BaseRecyclerViewAnimationsTest.LoggingInfo) preLayoutInfo, 87 (BaseRecyclerViewAnimationsTest.LoggingInfo) postLayoutInfo)); 88 return super.animateDisappearance(viewHolder, preLayoutInfo, postLayoutInfo); 89 } 90 91 @Override 92 public boolean animateAppearance(@NonNull RecyclerView.ViewHolder viewHolder, 93 ItemHolderInfo preLayoutInfo, 94 @NonNull ItemHolderInfo postLayoutInfo) { 95 mAnimateAppearanceList 96 .add(new AnimateAppearance(viewHolder, 97 (BaseRecyclerViewAnimationsTest.LoggingInfo) preLayoutInfo, 98 (BaseRecyclerViewAnimationsTest.LoggingInfo) postLayoutInfo)); 99 return super.animateAppearance(viewHolder, preLayoutInfo, postLayoutInfo); 100 } 101 102 @Override 103 public boolean animatePersistence(@NonNull RecyclerView.ViewHolder viewHolder, 104 @NonNull ItemHolderInfo preInfo, 105 @NonNull ItemHolderInfo postInfo) { 106 mAnimatePersistenceList 107 .add(new AnimatePersistence(viewHolder, 108 (BaseRecyclerViewAnimationsTest.LoggingInfo) preInfo, 109 (BaseRecyclerViewAnimationsTest.LoggingInfo) postInfo)); 110 return super.animatePersistence(viewHolder, preInfo, postInfo); 111 } 112 113 @Override 114 public boolean animateChange(@NonNull RecyclerView.ViewHolder oldHolder, 115 @NonNull RecyclerView.ViewHolder newHolder, @NonNull ItemHolderInfo preInfo, 116 @NonNull ItemHolderInfo postInfo) { 117 mAnimateChangeList 118 .add(new AnimateChange(oldHolder, newHolder, 119 (BaseRecyclerViewAnimationsTest.LoggingInfo) preInfo, 120 (BaseRecyclerViewAnimationsTest.LoggingInfo) postInfo)); 121 return super.animateChange(oldHolder, newHolder, preInfo, postInfo); 122 } 123 124 @Override 125 public void runPendingAnimations() { 126 if (mWaitForPendingAnimations != null) { 127 mWaitForPendingAnimations.countDown(); 128 } 129 super.runPendingAnimations(); 130 } 131 132 public void expectRunPendingAnimationsCall(int count) { 133 mWaitForPendingAnimations = new CountDownLatch(count); 134 } 135 136 public void waitForPendingAnimationsCall(int seconds) throws InterruptedException { 137 mWaitForPendingAnimations.await(seconds, TimeUnit.SECONDS); 138 } 139 140 @Override 141 public boolean animateAdd(RecyclerView.ViewHolder holder) { 142 mAddVHs.add(holder); 143 return super.animateAdd(holder); 144 } 145 146 @Override 147 public boolean animateRemove(RecyclerView.ViewHolder holder) { 148 mRemoveVHs.add(holder); 149 return super.animateRemove(holder); 150 } 151 152 @Override 153 public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY, 154 int toX, int toY) { 155 mMoveVHs.add(holder); 156 return super.animateMove(holder, fromX, fromY, toX, toY); 157 } 158 159 @Override 160 public boolean animateChange(RecyclerView.ViewHolder oldHolder, 161 RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY) { 162 if (oldHolder != null) { 163 mChangeOldVHs.add(oldHolder); 164 } 165 if (newHolder != null) { 166 mChangeNewVHs.add(newHolder); 167 } 168 return super.animateChange(oldHolder, newHolder, fromX, fromY, toX, toY); 169 } 170 171 public void reset() { 172 mAddVHs.clear(); 173 mRemoveVHs.clear(); 174 mMoveVHs.clear(); 175 mChangeOldVHs.clear(); 176 mChangeNewVHs.clear(); 177 mAnimateChangeList.clear(); 178 mAnimatePersistenceList.clear(); 179 mAnimateAppearanceList.clear(); 180 mAnimateDisappearanceList.clear(); 181 } 182}