diff options
4 files changed, 73 insertions, 163 deletions
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 89ebf4d1300a..d400159984a6 100644 --- a/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarChartPreference.java +++ b/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarChartPreference.java @@ -31,7 +31,7 @@ import androidx.preference.PreferenceViewHolder; import java.util.Arrays; /** - * This BarChartPreference shows four bar views in this preference at most. + * This BarChartPreference shows up to four bar views in this preference at most. * * <p>The following code sample shows a typical use, with an XML layout and code to initialize the * contents of the BarChartPreference: @@ -74,71 +74,28 @@ public class BarChartPreference extends Preference { }; private int mMaxBarHeight; - private @StringRes int mTitleId; - private @StringRes int mDetailsId; + @StringRes + private int mTitleId; + @StringRes + private int mDetailsId; private BarViewInfo[] mBarViewsInfo; private View.OnClickListener mDetailsOnClickListener; - /** - * Constructs a new BarChartPreference with the given context's theme. - * It sets a layout with settings bar chart style - * - * @param context The Context the view is running in, through which it can - * access the current theme, resources, etc. - */ public BarChartPreference(Context context) { super(context); init(); } - /** - * Constructs a new BarChartPreference with the given context's theme and the supplied - * attribute set. - * It sets a layout with settings bar chart style - * - * @param context the Context the view is running in - * @param attrs the attributes of the XML tag that is inflating the view. - */ public BarChartPreference(Context context, AttributeSet attrs) { super(context, attrs); init(); } - /** - * Constructs a new BarChartPreference with the given context's theme, the supplied - * attribute set, and default style attribute. - * It sets a layout with settings bar chart style - * - * @param context The Context the view is running in, through which it can - * access the current theme, resources, etc. - * @param attrs The attributes of the XML tag that is inflating the view. - * @param defStyleAttr An attribute in the current theme that contains a - * reference to a style resource that supplies default - * values for the view. Can be 0 to not look for - * defaults. - */ public BarChartPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } - /** - * Constructs a new BarChartPreference with the given context's theme, the supplied - * attribute set, and default styles. - * It sets a layout with settings bar chart style - * - * @param context The Context the view is running in, through which it can - * access the current theme, resources, etc. - * @param attrs The attributes of the XML tag that is inflating the view. - * @param defStyleAttr An attribute in the current theme that contains a - * reference to a style resource that supplies default - * values for the view. Can be 0 to not look for - * defaults. - * @param defStyleRes A resource identifier of a style resource that - * supplies default values for the view, used only if - * defStyleAttr is 0 or can not be found in the theme. - * Can be 0 to not look for defaults. - */ public BarChartPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); @@ -172,8 +129,6 @@ public class BarChartPreference extends Preference { /** * Set all bar view information which you'd like to show in preference. * - * <p>This method helps you do a sort by {@linkBarViewInfo#mBarNumber} in descending order. - * * @param barViewsInfo the barViewsInfo contain at least one {@link BarViewInfo}. */ public void setAllBarViewsInfo(@NonNull BarViewInfo[] barViewsInfo) { @@ -181,7 +136,7 @@ public class BarChartPreference extends Preference { // Do a sort in descending order, the first element would have max {@link // BarViewInfo#mBarNumber} Arrays.sort(mBarViewsInfo); - caculateAllBarViewsHeight(); + calculateAllBarViewHeights(); notifyChanged(); } @@ -224,19 +179,19 @@ public class BarChartPreference extends Preference { continue; } barView.setVisibility(View.VISIBLE); - barView.updateBarViewUI(mBarViewsInfo[index]); + barView.updateView(mBarViewsInfo[index]); } } - private void caculateAllBarViewsHeight() { + private void calculateAllBarViewHeights() { // Since we sorted this array in advance, the first element must have the max {@link - // BarViewInfo#mBarNumber}. - final int maxBarViewNumber = mBarViewsInfo[0].getBarNumber(); - // If the max number of bar view is zero, then we don't caculate the unit for bar height. - final int unit = maxBarViewNumber == 0 ? 0 : mMaxBarHeight / maxBarViewNumber; + // BarViewInfo#mHeight}. + final int maxBarHeight = mBarViewsInfo[0].getHeight(); + // If the max number of bar view is zero, then we don't calculate the unit for bar height. + final int unit = maxBarHeight == 0 ? 0 : mMaxBarHeight / maxBarHeight; for (BarViewInfo barView : mBarViewsInfo) { - barView.setBarHeight(barView.getBarNumber() * unit); + barView.setNormalizedHeight(barView.getHeight() * unit); } } } diff --git a/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarView.java b/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarView.java index 6243a2de387a..6bf61ae70312 100644 --- a/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarView.java +++ b/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarView.java @@ -30,7 +30,7 @@ import androidx.annotation.ColorInt; import androidx.annotation.VisibleForTesting; /** - * A extension view for bar chart. + * {@link View} for a single vertical bar with icon and summary. */ public class BarView extends LinearLayout { @@ -41,24 +41,11 @@ public class BarView extends LinearLayout { private TextView mBarTitle; private TextView mBarSummary; - /** - * Constructs a new BarView with the given context's theme. - * - * @param context The Context the view is running in, through which it can - * access the current theme, resources, etc. - */ public BarView(Context context) { super(context); init(); } - /** - * Constructs a new BarView with the given context's theme and the supplied - * attribute set. - * - * @param context the Context the view is running in - * @param attrs the attributes of the XML tag that is inflating the view. - */ public BarView(Context context, AttributeSet attrs) { super(context, attrs); init(); @@ -77,17 +64,16 @@ public class BarView extends LinearLayout { } /** - * This helps update the bar view UI with a {@link BarViewInfo}. - * - * @param barViewInfo A {@link BarViewInfo} saves bar view status. + * Updates the view with a {@link BarViewInfo}. */ - public void updateBarViewUI(BarViewInfo barViewInfo) { + void updateView(BarViewInfo barViewInfo) { + setOnClickListener(barViewInfo.getClickListener()); //Set height of bar view - mBarView.getLayoutParams().height = barViewInfo.getBarHeight(); + mBarView.getLayoutParams().height = barViewInfo.getNormalizedHeight(); mIcon.setImageDrawable(barViewInfo.getIcon()); // For now, we use the bar number as title. - mBarTitle.setText(Integer.toString(barViewInfo.getBarNumber())); - mBarSummary.setText(barViewInfo.getSummaryRes()); + mBarTitle.setText(Integer.toString(barViewInfo.getHeight())); + mBarSummary.setText(barViewInfo.getSummary()); } @VisibleForTesting @@ -106,9 +92,9 @@ public class BarView extends LinearLayout { setGravity(Gravity.CENTER); mBarView = findViewById(R.id.bar_view); - mIcon = (ImageView) findViewById(R.id.icon_view); - mBarTitle = (TextView) findViewById(R.id.bar_title); - mBarSummary = (TextView) findViewById(R.id.bar_summary); + mIcon = findViewById(R.id.icon_view); + mBarTitle = findViewById(R.id.bar_title); + mBarSummary = findViewById(R.id.bar_summary); } private void setOnClickListner(View.OnClickListener listener) { diff --git a/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarViewInfo.java b/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarViewInfo.java index aa83ce99d200..409f9ea05889 100644 --- a/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarViewInfo.java +++ b/packages/SettingsLib/BarChartPreference/src/com/android/settingslib/widget/BarViewInfo.java @@ -31,116 +31,71 @@ import java.util.Comparator; public class BarViewInfo implements Comparable<BarViewInfo> { private final Drawable mIcon; - private View.OnClickListener mListener; - private @StringRes int mSummaryRes; + private View.OnClickListener mClickListener; + @StringRes + private int mSummary; // A number indicates this bar's height. The larger number shows a higher bar view. - private int mBarNumber; + private int mHeight; // A real height of bar view. - private int mBarHeight; + private int mNormalizedHeight; /** * Construct a BarViewInfo instance. * - * @param icon the icon of bar view. - * @param barNumber the number of bar view. The larger number show a more height of bar view. - * @param summaryRes the resource identifier of the string resource to be displayed - * @return BarViewInfo object. + * @param icon The icon of bar view. + * @param barHeight The height of bar view. Larger number shows a higher bar view. + * @param summary The string resource id for summary. */ - public BarViewInfo(Drawable icon, @IntRange(from = 0) int barNumber, - @StringRes int summaryRes) { + public BarViewInfo(Drawable icon, @IntRange(from = 0) int barHeight, @StringRes int summary) { mIcon = icon; - mBarNumber = barNumber; - mSummaryRes = summaryRes; + mHeight = barHeight; + mSummary = summary; } /** - * Set number for bar view. - * - * @param barNumber the number of bar view. The larger number shows a higher bar view. + * Set a click listener for bar view. */ - public void setBarNumber(@IntRange(from = 0) int barNumber) { - mBarNumber = barNumber; + public void setClickListener(@Nullable View.OnClickListener listener) { + mClickListener = listener; } - /** - * Set summary resource for bar view - * - * @param resId the resource identifier of the string resource to be displayed - */ - public void setSummary(@StringRes int resId) { - mSummaryRes = resId; + @Override + public int compareTo(BarViewInfo other) { + // Descending order + return Comparator.comparingInt((BarViewInfo barViewInfo) -> barViewInfo.mHeight) + .compare(other, this); } - /** - * Set a click listner for bar view. - * - * @param listener the click listner is attached on bar view. - */ - public void setClickListener(@Nullable View.OnClickListener listener) { - mListener = listener; + void setHeight(@IntRange(from = 0) int height) { + mHeight = height; } - /** - * Get the icon of bar view. - * - * @return Drawable the icon of bar view. - */ - public Drawable getIcon() { - return mIcon; + void setSummary(@StringRes int resId) { + mSummary = resId; } - /** - * Get the OnClickListener of bar view. - * - * @return View.OnClickListener the click listner of bar view. - */ - public View.OnClickListener getListener() { - return mListener; + Drawable getIcon() { + return mIcon; } - /** - * Get the real height of bar view. - * - * @return the real height of bar view. - */ - public int getBarHeight() { - return mBarHeight; + int getHeight() { + return mHeight; } - /** - * Get summary resource of bar view. - * - * @return summary resource of bar view. - */ - public int getSummaryRes() { - return mSummaryRes; + View.OnClickListener getClickListener() { + return mClickListener; } - /** - * Get the number of app uses this permisssion. - * - * @return the number of app uses this permission. - */ - public int getBarNumber() { - return mBarNumber; + @StringRes + int getSummary() { + return mSummary; } - @Override - public int compareTo(BarViewInfo other) { - // Descending order - return Comparator.comparingInt((BarViewInfo barViewInfo) -> barViewInfo.mBarNumber) - .compare(other, this); + void setNormalizedHeight(@IntRange(from = 0) int barHeight) { + mNormalizedHeight = barHeight; } - /** - * Set a real height for bar view. - * - * <p>This method should not be called by outside. It usually should be called by - * {@link BarChartPreference#caculateAllBarViewsHeight} - * - * @param barHeight the real bar height for bar view. - */ - void setBarHeight(@IntRange(from = 0) int barHeight) { - mBarHeight = barHeight; + int getNormalizedHeight() { + return mNormalizedHeight; } } 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 371c3d46dfb4..d4e74810ea3f 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 @@ -208,4 +208,18 @@ public class BarChartPreferenceTest { assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); assertThat(mBarView1.getSummary()).isEqualTo(mContext.getText(R.string.debug_app)); } + + @Test + public void setAllBarViewsInfo_setClickListenerForBarView_barViewAttachClickListener() { + final BarViewInfo viewInfo = new BarViewInfo(mIcon, 30 /* barNumber */, R.string.debug_app); + viewInfo.setClickListener(v -> { + }); + final BarViewInfo[] barViewsInfo = new BarViewInfo[]{viewInfo}; + + mPreference.setAllBarViewsInfo(barViewsInfo); + mPreference.onBindViewHolder(mHolder); + + assertThat(mBarView1.getVisibility()).isEqualTo(View.VISIBLE); + assertThat(mBarView1.hasOnClickListeners()).isTrue(); + } } |