1 /*
2  * Copyright (C) 2023 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 androidx.recyclerview.widget.DiffUtil.Callback;
20 
21 import com.android.launcher3.widget.model.WidgetsListBaseEntry;
22 
23 import java.util.List;
24 
25 /**
26  * DiffUtil callback to compare widgets
27  */
28 public class WidgetsDiffCallback extends Callback {
29 
30     private final List<WidgetsListBaseEntry> mOldEntries;
31     private final List<WidgetsListBaseEntry> mNewEntries;
32 
WidgetsDiffCallback( List<WidgetsListBaseEntry> oldEntries, List<WidgetsListBaseEntry> newEntries)33     public WidgetsDiffCallback(
34             List<WidgetsListBaseEntry> oldEntries,
35             List<WidgetsListBaseEntry> newEntries) {
36         mOldEntries = oldEntries;
37         mNewEntries = newEntries;
38     }
39 
40     @Override
getOldListSize()41     public int getOldListSize() {
42         return mOldEntries.size();
43     }
44 
45     @Override
getNewListSize()46     public int getNewListSize() {
47         return mNewEntries.size();
48     }
49 
50     @Override
areItemsTheSame(int oldItemPosition, int newItemPosition)51     public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
52         // Items are same if they point to the same package entry
53         WidgetsListBaseEntry oldItem = mOldEntries.get(oldItemPosition);
54         WidgetsListBaseEntry newItem = mNewEntries.get(newItemPosition);
55         return oldItem.getClass().equals(newItem.getClass())
56                 && oldItem.mPkgItem.equals(newItem.mPkgItem);
57     }
58 
59     @Override
areContentsTheSame(int oldItemPosition, int newItemPosition)60     public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
61         // Always update all entries since the icon may have changed
62         return false;
63     }
64 }
65