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.systemui.accessibility.floatingmenu;
18 
19 import android.text.TextUtils;
20 
21 import androidx.recyclerview.widget.DiffUtil;
22 
23 import com.android.internal.accessibility.dialog.AccessibilityTarget;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * A {@link DiffUtil.Callback} to calculate the difference between old and new menu target List.
30  */
31 class MenuTargetsCallback extends DiffUtil.Callback {
32     private final List<AccessibilityTarget> mOldTargets = new ArrayList<>();
33     private final List<AccessibilityTarget> mNewTargets = new ArrayList<>();
34 
MenuTargetsCallback(List<AccessibilityTarget> oldTargets, List<AccessibilityTarget> newTargets)35     MenuTargetsCallback(List<AccessibilityTarget> oldTargets,
36             List<AccessibilityTarget> newTargets) {
37         mOldTargets.addAll(oldTargets);
38         mNewTargets.addAll(newTargets);
39     }
40 
41     @Override
getOldListSize()42     public int getOldListSize() {
43         return mOldTargets.size();
44     }
45 
46     @Override
getNewListSize()47     public int getNewListSize() {
48         return mNewTargets.size();
49     }
50 
51     @Override
areItemsTheSame(int oldIndex, int newIndex)52     public boolean areItemsTheSame(int oldIndex, int newIndex) {
53         return mOldTargets.get(oldIndex).getId().equals(mNewTargets.get(newIndex).getId());
54     }
55 
56     @Override
areContentsTheSame(int oldIndex, int newIndex)57     public boolean areContentsTheSame(int oldIndex, int newIndex) {
58         if (!TextUtils.equals(mOldTargets.get(oldIndex).getLabel(),
59                 mNewTargets.get(newIndex).getLabel())) {
60             return false;
61         }
62 
63         if (!TextUtils.equals(mOldTargets.get(oldIndex).getStateDescription(),
64                 mNewTargets.get(newIndex).getStateDescription())) {
65             return false;
66         }
67 
68         return true;
69     }
70 }
71