1 /*
2  * Copyright (C) 2022 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.permissioncontroller.safetycenter.ui;
18 
19 import static android.os.Build.VERSION_CODES.TIRAMISU;
20 
21 import android.content.Context;
22 import android.safetycenter.SafetyCenterEntry;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.Nullable;
26 import androidx.annotation.RequiresApi;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceViewHolder;
29 
30 import com.android.permissioncontroller.R;
31 import com.android.permissioncontroller.safetycenter.ui.model.SafetyCenterViewModel;
32 import com.android.permissioncontroller.safetycenter.ui.view.SafetyEntryView;
33 
34 /** A preference that displays a visual representation of a {@link SafetyCenterEntry}. */
35 @RequiresApi(TIRAMISU)
36 public final class SafetyEntryPreference extends Preference implements ComparablePreference {
37 
38     private final PositionInCardList mPosition;
39     private final SafetyCenterEntry mEntry;
40     private final SafetyCenterViewModel mViewModel;
41     @Nullable private final Integer mLaunchTaskId;
42 
SafetyEntryPreference( Context context, @Nullable Integer launchTaskId, SafetyCenterEntry entry, PositionInCardList position, SafetyCenterViewModel viewModel)43     public SafetyEntryPreference(
44             Context context,
45             @Nullable Integer launchTaskId,
46             SafetyCenterEntry entry,
47             PositionInCardList position,
48             SafetyCenterViewModel viewModel) {
49         super(context);
50 
51         mEntry = entry;
52         mPosition = position;
53         mViewModel = viewModel;
54         mLaunchTaskId = launchTaskId;
55 
56         setLayoutResource(R.layout.preference_entry);
57     }
58 
59     @Override
onBindViewHolder(PreferenceViewHolder holder)60     public void onBindViewHolder(PreferenceViewHolder holder) {
61         super.onBindViewHolder(holder);
62 
63         ((SafetyEntryView) holder.itemView).showEntry(mEntry, mPosition, mLaunchTaskId, mViewModel);
64     }
65 
66     @Override
isSameItem(Preference other)67     public boolean isSameItem(Preference other) {
68         return other instanceof SafetyEntryPreference
69                 && TextUtils.equals(mEntry.getId(), ((SafetyEntryPreference) other).mEntry.getId());
70     }
71 
72     @Override
hasSameContents(Preference other)73     public boolean hasSameContents(Preference other) {
74         if (other instanceof SafetyEntryPreference) {
75             SafetyEntryPreference o = (SafetyEntryPreference) other;
76             return mEntry.equals(o.mEntry) && mPosition == o.mPosition;
77         }
78         return false;
79     }
80 }
81