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