• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.Button;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceViewHolder;
26 
27 import com.android.settings.R;
28 
29 import com.google.android.material.card.MaterialCardView;
30 
31 import java.util.Optional;
32 
33 /**
34  * Preference that wrapped by {@link MaterialCardView}, only support to set icon, title and summary
35  */
36 public class CardPreference extends Preference {
37 
38     private View.OnClickListener mPrimaryBtnClickListener = null;
39     private View.OnClickListener mSecondaryBtnClickListener = null;
40 
41     private String mPrimaryButtonText = null;
42     private String mSecondaryButtonText = null;
43 
44     private Optional<Button> mPrimaryButton = Optional.empty();
45     private Optional<Button> mSecondaryButton = Optional.empty();
46     private Optional<View> mButtonsGroup = Optional.empty();
47 
48     private boolean mPrimaryButtonVisible = false;
49     private boolean mSecondaryButtonVisible = false;
50 
CardPreference(Context context)51     public CardPreference(Context context) {
52         this(context, null /* attrs */);
53     }
54 
CardPreference(Context context, AttributeSet attrs)55     public CardPreference(Context context, AttributeSet attrs) {
56         super(context, attrs, R.attr.cardPreferenceStyle);
57     }
58 
59     @Override
onBindViewHolder(PreferenceViewHolder holder)60     public void onBindViewHolder(PreferenceViewHolder holder) {
61         super.onBindViewHolder(holder);
62 
63         initButtonsAndLayout(holder);
64     }
65 
initButtonsAndLayout(PreferenceViewHolder holder)66     private void initButtonsAndLayout(PreferenceViewHolder holder) {
67         mPrimaryButton = Optional.ofNullable((Button) holder.findViewById(android.R.id.button1));
68         mSecondaryButton = Optional.ofNullable((Button) holder.findViewById(android.R.id.button2));
69         mButtonsGroup = Optional.ofNullable(holder.findViewById(R.id.card_preference_buttons));
70 
71         setPrimaryButtonText(mPrimaryButtonText);
72         setPrimaryButtonClickListener(mPrimaryBtnClickListener);
73         setPrimaryButtonVisible(mPrimaryButtonVisible);
74         setSecondaryButtonText(mSecondaryButtonText);
75         setSecondaryButtonClickListener(mSecondaryBtnClickListener);
76         setSecondaryButtonVisible(mSecondaryButtonVisible);
77     }
78 
79     /**
80      * Register a callback to be invoked when the primary button is clicked.
81      *
82      * @param l the callback that will run
83      */
setPrimaryButtonClickListener(View.OnClickListener l)84     public void setPrimaryButtonClickListener(View.OnClickListener l) {
85         mPrimaryButton.ifPresent(button -> button.setOnClickListener(l));
86         mPrimaryBtnClickListener = l;
87     }
88 
89     /**
90      * Register a callback to be invoked when the secondary button is clicked.
91      *
92      * @param l the callback that will run
93      */
setSecondaryButtonClickListener(View.OnClickListener l)94     public void setSecondaryButtonClickListener(View.OnClickListener l) {
95         mSecondaryButton.ifPresent(button -> button.setOnClickListener(l));
96         mSecondaryBtnClickListener = l;
97     }
98 
99     /**
100      * Sets the text to be displayed on primary button.
101      *
102      * @param text text to be displayed
103      */
setPrimaryButtonText(String text)104     public void setPrimaryButtonText(String text) {
105         mPrimaryButton.ifPresent(button -> button.setText(text));
106         mPrimaryButtonText = text;
107     }
108 
109     /**
110      * Sets the text to be displayed on secondary button.
111      *
112      * @param text text to be displayed
113      */
setSecondaryButtonText(String text)114     public void setSecondaryButtonText(String text) {
115         mSecondaryButton.ifPresent(button -> button.setText(text));
116         mSecondaryButtonText = text;
117     }
118 
119     /**
120      * Set the visible on the primary button.
121      *
122      * @param visible {@code true} for visible
123      */
setPrimaryButtonVisible(boolean visible)124     public void setPrimaryButtonVisible(boolean visible) {
125         mPrimaryButton.ifPresent(
126                 button -> button.setVisibility(visible ? View.VISIBLE : View.GONE));
127         mPrimaryButtonVisible = visible;
128         updateButtonGroupsVisibility();
129     }
130 
131     /**
132      * Set the visible on the secondary button.
133      *
134      * @param visible {@code true} for visible
135      */
setSecondaryButtonVisible(boolean visible)136     public void setSecondaryButtonVisible(boolean visible) {
137         mSecondaryButton.ifPresent(
138                 button -> button.setVisibility(visible ? View.VISIBLE : View.GONE));
139         mSecondaryButtonVisible = visible;
140         updateButtonGroupsVisibility();
141     }
142 
143     /**
144      * Sets the text of content description on secondary button.
145      *
146      * @param text text for the content description
147      */
setSecondaryButtonContentDescription(String text)148     public void setSecondaryButtonContentDescription(String text) {
149         mSecondaryButton.ifPresent(button -> button.setContentDescription(text));
150     }
151 
updateButtonGroupsVisibility()152     private void updateButtonGroupsVisibility() {
153         int visibility =
154                 (mPrimaryButtonVisible || mSecondaryButtonVisible) ? View.VISIBLE : View.GONE;
155         mButtonsGroup.ifPresent(group -> group.setVisibility(visibility));
156     }
157 }
158