• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.Nullable;
25 import androidx.annotation.NonNull;
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.settings.R;
29 import com.android.settingslib.widget.TwoTargetPreference;
30 
31 /** A preference with tick icon. */
32 public class TickButtonPreference extends TwoTargetPreference {
33     private ImageView mCheckIcon;
34     private boolean mIsSelected = false;
35 
TickButtonPreference(Context context)36     public TickButtonPreference(Context context) {
37         super(context);
38     }
39 
TickButtonPreference(@onNull Context context, @Nullable AttributeSet attrs)40     public TickButtonPreference(@NonNull Context context, @Nullable AttributeSet attrs) {
41         super(context, attrs);
42     }
43 
44     @Override
onBindViewHolder(PreferenceViewHolder holder)45     public void onBindViewHolder(PreferenceViewHolder holder) {
46         super.onBindViewHolder(holder);
47         View divider =
48                 holder.findViewById(
49                         com.android.settingslib.widget.preference.twotarget.R.id
50                                 .two_target_divider);
51         if (divider != null) {
52             divider.setVisibility(View.GONE);
53         }
54         mCheckIcon = (ImageView) holder.findViewById(R.id.check_icon);
55         setSelected(mIsSelected);
56     }
57 
58     /** Set icon state.*/
setSelected(boolean isSelected)59     public void setSelected(boolean isSelected) {
60         if (mCheckIcon != null) {
61             mCheckIcon.setVisibility(isSelected ? View.VISIBLE : View.INVISIBLE);
62         }
63         mIsSelected = isSelected;
64     }
65 
66     /** Return state of presenting icon. */
isSelected()67     public boolean isSelected() {
68         return mIsSelected;
69     }
70 
71     @Override
getSecondTargetResId()72     protected int getSecondTargetResId() {
73         return R.layout.preference_check_icon;
74     }
75 
76 }
77