Implements the buttons layout for the extra defend

Bug: 235246949
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.fuelgauge.*
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.widget.CardPreferenceTest
Test: manual test
Change-Id: I1dc4ab31adf85c684a4c09bd6c9bcfb54b52dc3c
This commit is contained in:
Pajace Chen
2022-08-16 18:12:01 +08:00
committed by Wesley Wang
parent 662e5722b3
commit f3496399dd
13 changed files with 662 additions and 13 deletions

View File

@@ -18,18 +18,36 @@ package com.android.settings.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settings.R;
import com.google.android.material.card.MaterialCardView;
import java.util.Optional;
/**
* Preference that wrapped by {@link MaterialCardView}, only support to set icon, title and summary
*/
public class CardPreference extends Preference {
private View.OnClickListener mPrimaryBtnClickListener = null;
private View.OnClickListener mSecondaryBtnClickListener = null;
private String mPrimaryButtonText = null;
private String mSecondaryButtonText = null;
private Optional<Button> mPrimaryButton = Optional.empty();
private Optional<Button> mSecondaryButton = Optional.empty();
private Optional<View> mButtonsGroup = Optional.empty();
private boolean mPrimaryButtonVisible = false;
private boolean mSecondaryButtonVisible = false;
public CardPreference(Context context) {
this(context, null /* attrs */);
}
@@ -37,4 +55,94 @@ public class CardPreference extends Preference {
public CardPreference(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.cardPreferenceStyle);
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
initButtonsAndLayout(holder);
}
private void initButtonsAndLayout(PreferenceViewHolder holder) {
mPrimaryButton = Optional.ofNullable((Button) holder.findViewById(android.R.id.button1));
mSecondaryButton = Optional.ofNullable((Button) holder.findViewById(android.R.id.button2));
mButtonsGroup = Optional.ofNullable(holder.findViewById(R.id.card_preference_buttons));
setPrimaryButtonText(mPrimaryButtonText);
setPrimaryButtonClickListener(mPrimaryBtnClickListener);
setPrimaryButtonVisible(mPrimaryButtonVisible);
setSecondaryButtonText(mSecondaryButtonText);
setSecondaryButtonClickListener(mSecondaryBtnClickListener);
setSecondaryButtonVisible(mSecondaryButtonVisible);
}
/**
* Register a callback to be invoked when the primary button is clicked.
*
* @param l the callback that will run
*/
public void setPrimaryButtonClickListener(View.OnClickListener l) {
mPrimaryButton.ifPresent(button -> button.setOnClickListener(l));
mPrimaryBtnClickListener = l;
}
/**
* Register a callback to be invoked when the secondary button is clicked.
*
* @param l the callback that will run
*/
public void setSecondaryButtonClickListener(View.OnClickListener l) {
mSecondaryButton.ifPresent(button -> button.setOnClickListener(l));
mSecondaryBtnClickListener = l;
}
/**
* Sets the text to be displayed on primary button.
*
* @param text text to be displayed
*/
public void setPrimaryButtonText(String text) {
mPrimaryButton.ifPresent(button -> button.setText(text));
mPrimaryButtonText = text;
}
/**
* Sets the text to be displayed on secondary button.
*
* @param text text to be displayed
*/
public void setSecondaryButtonText(String text) {
mSecondaryButton.ifPresent(button -> button.setText(text));
mSecondaryButtonText = text;
}
/**
* Set the visible on the primary button.
*
* @param visible {@code true} for visible
*/
public void setPrimaryButtonVisible(boolean visible) {
mPrimaryButton.ifPresent(
button -> button.setVisibility(visible ? View.VISIBLE : View.GONE));
mPrimaryButtonVisible = visible;
updateButtonGroupsVisibility();
}
/**
* Set the visible on the secondary button.
*
* @param visible {@code true} for visible
*/
public void setSecondaryButtonVisible(boolean visible) {
mSecondaryButton.ifPresent(
button -> button.setVisibility(visible ? View.VISIBLE : View.GONE));
mSecondaryButtonVisible = visible;
updateButtonGroupsVisibility();
}
private void updateButtonGroupsVisibility() {
int visibility =
(mPrimaryButtonVisible || mSecondaryButtonVisible) ? View.VISIBLE : View.GONE;
mButtonsGroup.ifPresent(group -> group.setVisibility(visibility));
}
}