Bar chart shows blank view during loading state
We'd expect that bar chart can show blank view
during loading state.
If user doesn't change its loading state, preference
draws view directly.
But if user changes its loading state to true explicitly,
if means bar chart needs to take some time to load
data. So, we won't initialize view now.
Once the state is updated to false, we will start to
initialize view again.
Test: visual, robotest
Bug: 123539793
Change-Id: I5fc9e5a48e49c43ade6fd5a4dc7ba3458cf7c07d
diff --git a/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarChartPreference.java b/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarChartPreference.java
index d332bac..3b87fca 100644
--- a/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarChartPreference.java
+++ b/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarChartPreference.java
@@ -87,6 +87,7 @@
};
private int mMaxBarHeight;
+ private boolean mIsLoading;
private BarChartInfo mBarChartInfo;
public BarChartPreference(Context context) {
@@ -134,12 +135,33 @@
notifyChanged();
}
+ /**
+ * Set loading state for {@link BarChartPreference}.
+ *
+ * By default, {@link BarChartPreference} doesn't care about it.
+ *
+ * But if user sets loading state to true explicitly, it means {@link BarChartPreference}
+ * needs to take some time to load data. So we won't initialize any view now.
+ *
+ * Once the state is updated to false, we will start to initialize view again.
+ *
+ * @param isLoading whether or not {@link BarChartPreference} is in loading state.
+ */
+ public void updateLoadingState(boolean isLoading) {
+ mIsLoading = isLoading;
+ notifyChanged();
+ }
+
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
holder.setDividerAllowedAbove(true);
holder.setDividerAllowedBelow(true);
+ // If the state is loading, we just show a blank view.
+ if (mIsLoading) {
+ return;
+ }
// We must show title of bar chart.
bindChartTitleView(holder);
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/BarChartPreferenceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/BarChartPreferenceTest.java
index c3ea336..1080cf4 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/BarChartPreferenceTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/BarChartPreferenceTest.java
@@ -20,6 +20,7 @@
import android.content.Context;
import android.graphics.drawable.Drawable;
+import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
@@ -46,6 +47,7 @@
private BarView mBarView2;
private BarView mBarView3;
private BarView mBarView4;
+ private TextView mTitleView;
private TextView mDetailsView;
private PreferenceViewHolder mHolder;
private BarChartPreference mPreference;
@@ -63,6 +65,7 @@
mBarView2 = mBarChartView.findViewById(R.id.bar_view2);
mBarView3 = mBarChartView.findViewById(R.id.bar_view3);
mBarView4 = mBarChartView.findViewById(R.id.bar_view4);
+ mTitleView = mBarChartView.findViewById(R.id.bar_chart_title);
mDetailsView = mBarChartView.findViewById(R.id.bar_chart_details);
mBarChartInfo = new BarChartInfo.Builder()
@@ -76,7 +79,6 @@
@Test
public void initializeBarChart_titleSet_shouldSetTitleInChartView() {
- final TextView titleView = mBarChartView.findViewById(R.id.bar_chart_title);
final BarChartInfo barChartInfo = new BarChartInfo.Builder()
.setTitle(R.string.debug_app)
.build();
@@ -84,8 +86,8 @@
mPreference.initializeBarChart(barChartInfo);
mPreference.onBindViewHolder(mHolder);
- assertThat(titleView.getVisibility()).isEqualTo(View.VISIBLE);
- assertThat(titleView.getText()).isEqualTo(mContext.getText(R.string.debug_app));
+ assertThat(mTitleView.getVisibility()).isEqualTo(View.VISIBLE);
+ assertThat(mTitleView.getText()).isEqualTo(mContext.getText(R.string.debug_app));
}
@Test
@@ -99,8 +101,7 @@
// We don't add any bar view yet.
mPreference.onBindViewHolder(mHolder);
- assertThat(mBarChartView.findViewById(R.id.bar_chart_title).getVisibility())
- .isEqualTo(View.VISIBLE);
+ assertThat(mTitleView.getVisibility()).isEqualTo(View.VISIBLE);
assertThat(mBarChartView.findViewById(R.id.empty_view).getVisibility())
.isEqualTo(View.VISIBLE);
assertThat(mBarChartView.findViewById(R.id.bar_views_container).getVisibility())
@@ -302,4 +303,38 @@
assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE);
assertThat(mBarView1.hasOnClickListeners()).isTrue();
}
+
+ @Test
+ public void onBindViewHolder_loadingStateIsTrue_shouldNotInitAnyView() {
+ final BarViewInfo viewInfo = new BarViewInfo(mIcon, 30 /* barNumber */, R.string.debug_app);
+ viewInfo.setClickListener(v -> {
+ });
+ final BarViewInfo[] barViewsInfo = new BarViewInfo[]{viewInfo};
+
+ mPreference.initializeBarChart(mBarChartInfo);
+ mPreference.setBarViewInfos(barViewsInfo);
+ mPreference.updateLoadingState(true /* isLoading */);
+
+ mPreference.onBindViewHolder(mHolder);
+
+ assertThat(TextUtils.isEmpty(mTitleView.getText())).isTrue();
+ assertThat(TextUtils.isEmpty(mDetailsView.getText())).isTrue();
+ }
+
+ @Test
+ public void onBindViewHolder_loadingStateIsFalse_shouldInitAnyView() {
+ final BarViewInfo viewInfo = new BarViewInfo(mIcon, 30 /* barNumber */, R.string.debug_app);
+ viewInfo.setClickListener(v -> {
+ });
+ final BarViewInfo[] barViewsInfo = new BarViewInfo[]{viewInfo};
+
+ mPreference.initializeBarChart(mBarChartInfo);
+ mPreference.setBarViewInfos(barViewsInfo);
+ mPreference.updateLoadingState(false /* isLoading */);
+
+ mPreference.onBindViewHolder(mHolder);
+
+ assertThat(TextUtils.isEmpty(mTitleView.getText())).isFalse();
+ assertThat(TextUtils.isEmpty(mDetailsView.getText())).isFalse();
+ }
}