Reduce cost of FooterView#showHistory

FooterView#showHistory could take up to 3ms to execute due to resource
fetching. The strings are now cached and we abort earlier when possible.

Now the method is executed withing 800us

Bug: 210432290
Test: atest FooterViewTest
Test: https://ui.perfetto.dev/#!/?s=cf1a328f424429fd30f131cd31c7d17c8ff412eb5ae1cfeec272c5f2d5a1c5e
Change-Id: If09c47646e168e8dded17b59be8573d0a9386eb9
This commit is contained in:
Lucas Dupin
2022-02-03 16:49:47 -08:00
parent ee754300bf
commit d9b1835577

View File

@@ -35,6 +35,10 @@ public class FooterView extends StackScrollerDecorView {
private FooterViewButton mClearAllButton;
private FooterViewButton mManageButton;
private boolean mShowHistory;
// String cache, for performance reasons.
// Reading them from a Resources object can be quite slow sometimes.
private String mManageNotificationText;
private String mManageNotificationHistoryText;
public FooterView(Context context, AttributeSet attrs) {
super(context, attrs);
@@ -68,6 +72,8 @@ public class FooterView extends StackScrollerDecorView {
super.onFinishInflate();
mClearAllButton = (FooterViewButton) findSecondaryView();
mManageButton = findViewById(R.id.manage_text);
updateResources();
updateText();
}
public void setManageButtonClickListener(OnClickListener listener) {
@@ -86,15 +92,20 @@ public class FooterView extends StackScrollerDecorView {
}
public void showHistory(boolean showHistory) {
if (mShowHistory == showHistory) {
return;
}
mShowHistory = showHistory;
updateText();
}
private void updateText() {
if (mShowHistory) {
mManageButton.setText(R.string.manage_notifications_history_text);
mManageButton.setContentDescription(
mContext.getString(R.string.manage_notifications_history_text));
mManageButton.setText(mManageNotificationHistoryText);
mManageButton.setContentDescription(mManageNotificationHistoryText);
} else {
mManageButton.setText(R.string.manage_notifications_text);
mManageButton.setContentDescription(
mContext.getString(R.string.manage_notifications_text));
mManageButton.setText(mManageNotificationText);
mManageButton.setContentDescription(mManageNotificationText);
}
}
@@ -109,7 +120,8 @@ public class FooterView extends StackScrollerDecorView {
mClearAllButton.setText(R.string.clear_all_notifications_text);
mClearAllButton.setContentDescription(
mContext.getString(R.string.accessibility_clear_all));
showHistory(mShowHistory);
updateResources();
updateText();
}
/**
@@ -124,6 +136,12 @@ public class FooterView extends StackScrollerDecorView {
mManageButton.setTextColor(textColor);
}
private void updateResources() {
mManageNotificationText = getContext().getString(R.string.manage_notifications_text);
mManageNotificationHistoryText = getContext()
.getString(R.string.manage_notifications_history_text);
}
@Override
public ExpandableViewState createExpandableViewState() {
return new FooterViewState();