1 /*
2  * Copyright (C) 2024 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 com.android.launcher3.widget.picker;
18 
19 import static com.android.launcher3.widget.picker.WidgetsListAdapter.VIEW_TYPE_WIDGETS_LIST;
20 
21 import androidx.recyclerview.widget.DefaultItemAnimator;
22 import androidx.recyclerview.widget.RecyclerView;
23 
24 public class WidgetsListItemAnimator extends DefaultItemAnimator {
25     public static final int CHANGE_DURATION_MS = 90;
26     public static final int MOVE_DURATION_MS = 90;
27     public static final int ADD_DURATION_MS = 120;
28 
WidgetsListItemAnimator()29     public WidgetsListItemAnimator() {
30         super();
31 
32         // Disable change animations because it disrupts the item focus upon adapter item
33         // change.
34         setSupportsChangeAnimations(false);
35         // Make the moves a bit faster, so that the amount of time for which user sees the
36         // bottom-sheet background before "add" animation starts is less making it smoother.
37         setChangeDuration(CHANGE_DURATION_MS);
38         setMoveDuration(MOVE_DURATION_MS);
39         setAddDuration(ADD_DURATION_MS);
40     }
41     @Override
animateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop)42     public boolean animateChange(RecyclerView.ViewHolder oldHolder,
43             RecyclerView.ViewHolder newHolder, int fromLeft, int fromTop, int toLeft,
44             int toTop) {
45         // As we expand an item, the content / widgets list that appears (with add
46         // event) also gets change events as its previews load asynchronously. The
47         // super implementation of animateChange cancels the animations on it - breaking
48         // the "add animation". Instead, here, we skip "change" animation for content
49         // list - because we want it to either appear or disappear. And, the previews
50         // themselves have their own animation when loaded, so, we don't need change
51         // animations for them anyway. Below, we do-nothing.
52         if (oldHolder.getItemViewType() == VIEW_TYPE_WIDGETS_LIST) {
53             dispatchChangeStarting(oldHolder, true);
54             dispatchChangeFinished(oldHolder, true);
55             return true;
56         }
57         return super.animateChange(oldHolder, newHolder, fromLeft, fromTop, toLeft,
58                 toTop);
59     }
60 }
61