Add UsageProgressBarPreference
This preference contains an usage summary and a total summary and a horizontal progress bar. The number of usage summary will show with enlarged font size. Bug: 174964885 Test: atest UsageProgressBarPreferenceTest Change-Id: I97fc27ac2b8d08202e776713bc035bd9b80bbbbc
This commit is contained in:
@@ -56,6 +56,7 @@ java_defaults {
|
||||
"SettingsLibTopIntroPreference",
|
||||
"SettingsLibBannerMessagePreference",
|
||||
"SettingsLibFooterPreference",
|
||||
"SettingsLibUsageProgressBarPreference",
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
13
packages/SettingsLib/UsageProgressBarPreference/Android.bp
Normal file
13
packages/SettingsLib/UsageProgressBarPreference/Android.bp
Normal file
@@ -0,0 +1,13 @@
|
||||
android_library {
|
||||
name: "SettingsLibUsageProgressBarPreference",
|
||||
|
||||
srcs: ["src/**/*.java"],
|
||||
resource_dirs: ["res"],
|
||||
|
||||
static_libs: [
|
||||
"androidx.preference_preference",
|
||||
],
|
||||
|
||||
sdk_version: "system_current",
|
||||
min_sdk_version: "21",
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2021 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.android.settingslib.widget">
|
||||
|
||||
<uses-sdk android:minSdkVersion="21" />
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2021 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:paddingTop="32dp"
|
||||
android:paddingBottom="32dp">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<TextView
|
||||
android:id="@+id/usage_summary"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_alignBaseline="@id/total_summary"
|
||||
android:singleLine="true"
|
||||
android:ellipsize="marquee"
|
||||
android:fontFamily="@*android:string/config_headlineFontFamily"
|
||||
android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Display1"
|
||||
android:textSize="20sp"/>
|
||||
<TextView
|
||||
android:id="@+id/total_summary"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:singleLine="true"
|
||||
android:ellipsize="marquee"
|
||||
android:textAppearance="@*android:style/TextAppearance.DeviceDefault.Body1"/>
|
||||
</RelativeLayout>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@android:id/progress"
|
||||
style="?android:attr/progressBarStyleHorizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:scaleY="2"
|
||||
android:layout_marginTop="4dp"
|
||||
android:max="100"/>
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Progres bar preference with a usage summary and a total summary.
|
||||
* This preference shows number in usage summary with enlarged font size.
|
||||
*/
|
||||
public class UsageProgressBarPreference extends Preference {
|
||||
|
||||
private final Pattern mNumberPattern = Pattern.compile("[\\d]*\\.?[\\d]+");
|
||||
|
||||
private CharSequence mUsageSummary;
|
||||
private CharSequence mTotalSummary;
|
||||
private int mPercent = -1;
|
||||
|
||||
/**
|
||||
* Perform inflation from XML and apply a class-specific base style.
|
||||
*
|
||||
* @param context The {@link Context} this is associated with, through which it can
|
||||
* access the current theme, resources, {@link SharedPreferences}, etc.
|
||||
* @param attrs The attributes of the XML tag that is inflating the preference
|
||||
* @param defStyle 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 UsageProgressBarPreference(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
setLayoutResource(R.layout.preference_usage_progress_bar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform inflation from XML and apply a class-specific base style.
|
||||
*
|
||||
* @param context The {@link Context} this is associated with, through which it can
|
||||
* access the current theme, resources, {@link SharedPreferences}, etc.
|
||||
* @param attrs The attributes of the XML tag that is inflating the preference
|
||||
*/
|
||||
public UsageProgressBarPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setLayoutResource(R.layout.preference_usage_progress_bar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor to create a preference.
|
||||
*
|
||||
* @param context The Context this is associated with.
|
||||
*/
|
||||
public UsageProgressBarPreference(Context context) {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
/** Set usage summary, number in the summary will show with enlarged font size. */
|
||||
public void setUsageSummary(CharSequence usageSummary) {
|
||||
if (TextUtils.equals(mUsageSummary, usageSummary)) {
|
||||
return;
|
||||
}
|
||||
mUsageSummary = usageSummary;
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
/** Set total summary. */
|
||||
public void setTotalSummary(CharSequence totalSummary) {
|
||||
if (TextUtils.equals(mTotalSummary, totalSummary)) {
|
||||
return;
|
||||
}
|
||||
mTotalSummary = totalSummary;
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
/** Set percentage of the progress bar. */
|
||||
public void setPercent(long usage, long total) {
|
||||
if (total == 0L || usage > total) {
|
||||
return;
|
||||
}
|
||||
final int percent = (int) (usage / (double) total * 100);
|
||||
if (mPercent == percent) {
|
||||
return;
|
||||
}
|
||||
mPercent = percent;
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* Binds the created View to the data for this preference.
|
||||
*
|
||||
* <p>This is a good place to grab references to custom Views in the layout and set
|
||||
* properties on them.
|
||||
*
|
||||
* <p>Make sure to call through to the superclass's implementation.
|
||||
*
|
||||
* @param holder The ViewHolder that provides references to the views to fill in. These views
|
||||
* will be recycled, so you should not hold a reference to them after this method
|
||||
* returns.
|
||||
*/
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder holder) {
|
||||
super.onBindViewHolder(holder);
|
||||
|
||||
final TextView usageSummary = (TextView) holder.findViewById(R.id.usage_summary);
|
||||
usageSummary.setText(enlargeFontOfNumber(mUsageSummary));
|
||||
|
||||
final TextView totalSummary = (TextView) holder.findViewById(R.id.total_summary);
|
||||
if (mTotalSummary != null) {
|
||||
totalSummary.setText(mTotalSummary);
|
||||
}
|
||||
|
||||
final ProgressBar progressBar = (ProgressBar) holder.findViewById(android.R.id.progress);
|
||||
if (mPercent < 0) {
|
||||
progressBar.setIndeterminate(true);
|
||||
} else {
|
||||
progressBar.setIndeterminate(false);
|
||||
progressBar.setProgress(mPercent);
|
||||
}
|
||||
}
|
||||
|
||||
private CharSequence enlargeFontOfNumber(CharSequence summary) {
|
||||
if (TextUtils.isEmpty(summary)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
final Matcher matcher = mNumberPattern.matcher(summary);
|
||||
if (matcher.find()) {
|
||||
final SpannableString spannableSummary = new SpannableString(summary);
|
||||
spannableSummary.setSpan(new RelativeSizeSpan(2.4f), matcher.start(),
|
||||
matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
return spannableSummary;
|
||||
}
|
||||
return summary;
|
||||
}
|
||||
}
|
||||
@@ -33,10 +33,12 @@ android_test {
|
||||
test_suites: ["device-tests"],
|
||||
|
||||
static_libs: [
|
||||
"androidx.test.core",
|
||||
"androidx.test.rules",
|
||||
"androidx.test.espresso.core",
|
||||
"mockito-target-minus-junit4",
|
||||
"truth-prebuilt",
|
||||
"SettingsLibUsageProgressBarPreference",
|
||||
],
|
||||
|
||||
dxflags: ["--multi-dex"],
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (C) 2021 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.widget;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.SpannedString;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class UsageProgressBarPreferenceTest {
|
||||
|
||||
private UsageProgressBarPreference mUsageProgressBarPreference;
|
||||
private PreferenceViewHolder mViewHolder;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
final Context context = ApplicationProvider.getApplicationContext();
|
||||
mUsageProgressBarPreference = new UsageProgressBarPreference(context);
|
||||
final LayoutInflater inflater = LayoutInflater.from(context);
|
||||
final View rootView = inflater.inflate(mUsageProgressBarPreference.getLayoutResource(),
|
||||
new LinearLayout(context), false /* attachToRoot */);
|
||||
mViewHolder = PreferenceViewHolder.createInstanceForTests(rootView);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUsageSummary_noNumber_noRelativeSizeSpan() {
|
||||
mUsageProgressBarPreference.setUsageSummary("test");
|
||||
|
||||
mUsageProgressBarPreference.onBindViewHolder(mViewHolder);
|
||||
|
||||
final TextView usageSummary = (TextView) mViewHolder.findViewById(R.id.usage_summary);
|
||||
final SpannedString summary = new SpannedString(usageSummary.getText());
|
||||
assertThat(summary.getSpans(0, summary.length(), RelativeSizeSpan.class).length)
|
||||
.isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUsageSummary_integerNumber_findRelativeSizeSpan() {
|
||||
mUsageProgressBarPreference.setUsageSummary("10Test");
|
||||
|
||||
mUsageProgressBarPreference.onBindViewHolder(mViewHolder);
|
||||
|
||||
final TextView usageSummary = (TextView) mViewHolder.findViewById(R.id.usage_summary);
|
||||
final SpannedString summary = new SpannedString(usageSummary.getText());
|
||||
assertThat(summary.getSpans(0, summary.length(), RelativeSizeSpan.class).length)
|
||||
.isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setUsageSummary_floatNumber_findRelativeSizeSpan() {
|
||||
mUsageProgressBarPreference.setUsageSummary("3.14Test");
|
||||
|
||||
mUsageProgressBarPreference.onBindViewHolder(mViewHolder);
|
||||
|
||||
final TextView usageSummary = (TextView) mViewHolder.findViewById(R.id.usage_summary);
|
||||
final SpannedString summary = new SpannedString(usageSummary.getText());
|
||||
assertThat(summary.getSpans(0, summary.length(), RelativeSizeSpan.class).length)
|
||||
.isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setPercent_getCorrectProgress() {
|
||||
mUsageProgressBarPreference.setPercent(31, 80);
|
||||
|
||||
mUsageProgressBarPreference.onBindViewHolder(mViewHolder);
|
||||
|
||||
final ProgressBar progressBar = (ProgressBar) mViewHolder
|
||||
.findViewById(android.R.id.progress);
|
||||
assertThat(progressBar.getProgress()).isEqualTo((int) (31.0f / 80 * 100));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user