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.settings.widget;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.widget.ImageView;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceViewHolder;
26 
27 import com.android.settings.R;
28 
29 /** A preference with tick icon. */
30 public class TickButtonPreference extends Preference {
31     private ImageView mCheckIcon;
32     private boolean mIsSelected = false;
33 
TickButtonPreference(Context context)34     public TickButtonPreference(Context context) {
35         super(context);
36         init(context, null);
37     }
38 
TickButtonPreference(Context context, AttributeSet attrs)39     public TickButtonPreference(Context context, AttributeSet attrs) {
40         super(context, attrs);
41         init(context, attrs);
42     }
43 
init(Context context, AttributeSet attrs)44     private void init(Context context, AttributeSet attrs) {
45         setWidgetLayoutResource(R.layout.preference_check_icon);
46     }
47 
48     @Override
onBindViewHolder(PreferenceViewHolder holder)49     public void onBindViewHolder(PreferenceViewHolder holder) {
50         super.onBindViewHolder(holder);
51         mCheckIcon = (ImageView) holder.findViewById(R.id.check_icon);
52         setSelected(mIsSelected);
53     }
54 
55     /** Set icon state.*/
setSelected(boolean isSelected)56     public void setSelected(boolean isSelected) {
57         if (mCheckIcon != null) {
58             mCheckIcon.setVisibility(isSelected ? View.VISIBLE : View.INVISIBLE);
59         }
60         mIsSelected = isSelected;
61     }
62 
63     /** Return state of presenting icon. */
isSelected()64     public boolean isSelected() {
65         return mIsSelected;
66     }
67 }
68