11f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar/*
21f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * Copyright (C) 2015 The Android Open Source Project
31f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
41f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * you may not use this file except in compliance with the License.
51f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * You may obtain a copy of the License at
61f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
71f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * Unless required by applicable law or agreed to in writing, software
81f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
91f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
101f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * See the License for the specific language governing permissions and
111f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar * limitations under the License.
121f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar */
131f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
141f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyarpackage android.support.v7.widget;
151f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
16e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyarimport android.support.annotation.NonNull;
17e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar
181f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyarimport java.util.ArrayList;
199051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyarimport java.util.List;
201f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyarimport java.util.concurrent.CountDownLatch;
211f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyarimport java.util.concurrent.TimeUnit;
22e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyarimport android.support.v7.widget.BaseRecyclerViewAnimationsTest.AnimateDisappearance;
23e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyarimport android.support.v7.widget.BaseRecyclerViewAnimationsTest.AnimateChange;
24e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyarimport android.support.v7.widget.BaseRecyclerViewAnimationsTest.AnimatePersistence;
25e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyarimport android.support.v7.widget.BaseRecyclerViewAnimationsTest.AnimateAppearance;
261f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
271f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyarpublic class LoggingItemAnimator extends DefaultItemAnimator {
281f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
291f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    final ArrayList<RecyclerView.ViewHolder> mAddVHs = new ArrayList<RecyclerView.ViewHolder>();
301f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
311f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    final ArrayList<RecyclerView.ViewHolder> mRemoveVHs = new ArrayList<RecyclerView.ViewHolder>();
321f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
331f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    final ArrayList<RecyclerView.ViewHolder> mMoveVHs = new ArrayList<RecyclerView.ViewHolder>();
341f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
351f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    final ArrayList<RecyclerView.ViewHolder> mChangeOldVHs = new ArrayList<RecyclerView.ViewHolder>();
361f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
371f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    final ArrayList<RecyclerView.ViewHolder> mChangeNewVHs = new ArrayList<RecyclerView.ViewHolder>();
381f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
39e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar    List<AnimateAppearance> mAnimateAppearanceList = new ArrayList<>();
40e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar    List<AnimateDisappearance> mAnimateDisappearanceList = new ArrayList<>();
41e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar    List<AnimatePersistence> mAnimatePersistenceList = new ArrayList<>();
42e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar    List<AnimateChange> mAnimateChangeList = new ArrayList<>();
439051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar
441f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    CountDownLatch mWaitForPendingAnimations;
451f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
469051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    public boolean contains(RecyclerView.ViewHolder viewHolder,
479051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar            List<? extends BaseRecyclerViewAnimationsTest.AnimateLogBase> list) {
489051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        for (BaseRecyclerViewAnimationsTest.AnimateLogBase log : list) {
499051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar            if (log.viewHolder == viewHolder) {
509051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar                return true;
519051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar            }
5213a0acc0df34344c0b4fd4b494a64e7dcf195b56Yigit Boyar            if (log instanceof AnimateChange) {
5313a0acc0df34344c0b4fd4b494a64e7dcf195b56Yigit Boyar                if (((AnimateChange) log).newHolder == viewHolder) {
5413a0acc0df34344c0b4fd4b494a64e7dcf195b56Yigit Boyar                    return true;
5513a0acc0df34344c0b4fd4b494a64e7dcf195b56Yigit Boyar                }
5613a0acc0df34344c0b4fd4b494a64e7dcf195b56Yigit Boyar            }
579051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        }
589051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        return false;
599051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    }
609051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar
614143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @NonNull
624143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @Override
634143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public ItemHolderInfo recordPreLayoutInformation(@NonNull RecyclerView.State state,
644143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            @NonNull RecyclerView.ViewHolder viewHolder,
654143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            @AdapterChanges int changeFlags, @NonNull List<Object> payloads) {
664143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        BaseRecyclerViewAnimationsTest.LoggingInfo
674143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                loggingInfo = new BaseRecyclerViewAnimationsTest.LoggingInfo(viewHolder, changeFlags, payloads);
684143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        return loggingInfo;
694143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
704143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
714143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @NonNull
724143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @Override
734143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public ItemHolderInfo recordPostLayoutInformation(@NonNull RecyclerView.State state,
744143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            @NonNull RecyclerView.ViewHolder viewHolder) {
754143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        BaseRecyclerViewAnimationsTest.LoggingInfo
764143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                loggingInfo = new BaseRecyclerViewAnimationsTest.LoggingInfo(viewHolder, 0, null);
774143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        return loggingInfo;
784143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
794143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
804143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
819051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    @Override
82e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar    public boolean animateDisappearance(@NonNull RecyclerView.ViewHolder viewHolder,
83e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar            @NonNull ItemHolderInfo preLayoutInfo, ItemHolderInfo postLayoutInfo) {
849051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        mAnimateDisappearanceList
854143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                .add(new AnimateDisappearance(viewHolder,
864143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        (BaseRecyclerViewAnimationsTest.LoggingInfo) preLayoutInfo,
874143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        (BaseRecyclerViewAnimationsTest.LoggingInfo) postLayoutInfo));
88e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar        return super.animateDisappearance(viewHolder, preLayoutInfo, postLayoutInfo);
899051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    }
909051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar
919051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    @Override
92e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar    public boolean animateAppearance(@NonNull RecyclerView.ViewHolder viewHolder,
939051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar            ItemHolderInfo preLayoutInfo,
94e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar            @NonNull ItemHolderInfo postLayoutInfo) {
959051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        mAnimateAppearanceList
964143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                .add(new AnimateAppearance(viewHolder,
974143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        (BaseRecyclerViewAnimationsTest.LoggingInfo) preLayoutInfo,
984143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        (BaseRecyclerViewAnimationsTest.LoggingInfo) postLayoutInfo));
999051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        return super.animateAppearance(viewHolder, preLayoutInfo, postLayoutInfo);
1009051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    }
1019051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar
1029051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    @Override
103e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar    public boolean animatePersistence(@NonNull RecyclerView.ViewHolder viewHolder,
104e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar            @NonNull ItemHolderInfo preInfo,
105e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar            @NonNull ItemHolderInfo postInfo) {
1069051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        mAnimatePersistenceList
1074143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                .add(new AnimatePersistence(viewHolder,
1084143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        (BaseRecyclerViewAnimationsTest.LoggingInfo) preInfo,
1094143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        (BaseRecyclerViewAnimationsTest.LoggingInfo) postInfo));
1109051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        return super.animatePersistence(viewHolder, preInfo, postInfo);
1119051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    }
1129051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar
1139051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    @Override
114e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar    public boolean animateChange(@NonNull RecyclerView.ViewHolder oldHolder,
115e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar            @NonNull RecyclerView.ViewHolder newHolder, @NonNull ItemHolderInfo preInfo,
116e09e0b4ea04b6b6b0ef6c62979e8abdead0bf378Yigit Boyar            @NonNull ItemHolderInfo postInfo) {
1179051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        mAnimateChangeList
1184143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                .add(new AnimateChange(oldHolder, newHolder,
1194143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        (BaseRecyclerViewAnimationsTest.LoggingInfo) preInfo,
1204143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        (BaseRecyclerViewAnimationsTest.LoggingInfo) postInfo));
1219051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        return super.animateChange(oldHolder, newHolder, preInfo, postInfo);
1229051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar    }
1239051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar
1241f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    @Override
1251f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    public void runPendingAnimations() {
1261f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        if (mWaitForPendingAnimations != null) {
1271f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar            mWaitForPendingAnimations.countDown();
1281f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        }
1291f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        super.runPendingAnimations();
1301f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    }
1311f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
1321f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    public void expectRunPendingAnimationsCall(int count) {
1331f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mWaitForPendingAnimations = new CountDownLatch(count);
1341f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    }
1351f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
1361f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    public void waitForPendingAnimationsCall(int seconds) throws InterruptedException {
1371f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mWaitForPendingAnimations.await(seconds, TimeUnit.SECONDS);
1381f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    }
1391f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
1401f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    @Override
1411f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    public boolean animateAdd(RecyclerView.ViewHolder holder) {
1421f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mAddVHs.add(holder);
1431f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        return super.animateAdd(holder);
1441f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    }
1451f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
1461f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    @Override
1471f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    public boolean animateRemove(RecyclerView.ViewHolder holder) {
1481f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mRemoveVHs.add(holder);
1491f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        return super.animateRemove(holder);
1501f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    }
1511f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
1521f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    @Override
1531f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    public boolean animateMove(RecyclerView.ViewHolder holder, int fromX, int fromY,
1541f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar            int toX, int toY) {
1551f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mMoveVHs.add(holder);
1561f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        return super.animateMove(holder, fromX, fromY, toX, toY);
1571f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    }
1581f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
1591f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    @Override
1601f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    public boolean animateChange(RecyclerView.ViewHolder oldHolder,
1611f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar            RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY) {
1621f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        if (oldHolder != null) {
1631f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar            mChangeOldVHs.add(oldHolder);
1641f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        }
1651f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        if (newHolder != null) {
1661f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar            mChangeNewVHs.add(newHolder);
1671f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        }
1681f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        return super.animateChange(oldHolder, newHolder, fromX, fromY, toX, toY);
1691f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    }
1701f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar
1711f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    public void reset() {
1721f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mAddVHs.clear();
1731f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mRemoveVHs.clear();
1741f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mMoveVHs.clear();
1751f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mChangeOldVHs.clear();
1761f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar        mChangeNewVHs.clear();
1779051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        mAnimateChangeList.clear();
1789051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        mAnimatePersistenceList.clear();
1799051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        mAnimateAppearanceList.clear();
1809051a368c00c40f8679ae67147b9f5af6f93638dYigit Boyar        mAnimateDisappearanceList.clear();
1811f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar    }
1821f5c7b76bfc3da85513e6d2c6aa1058339f0c625Yigit Boyar}