summaryrefslogtreecommitdiff
path: root/core/java/android/widget/ProgressBar.java
diff options
context:
space:
mode:
authorChristopher Tate <ctate@google.com>2020-07-16 10:31:02 -0700
committerChristopher Tate <ctate@google.com>2020-07-16 11:31:54 -0700
commitacd4d23f1e86cd670bc6b2fe0ea8e11b4695a9bc (patch)
tree8d448c526038f043c3bcdc2021a46770c60ab6b0 /core/java/android/widget/ProgressBar.java
parent25ec4f6855022e44388d9f9fcc195bb516f5a122 (diff)
Avoid churning NumberFormat instances in ProgressBar
Don't construct a new locale-specific NumberFormat every time a progress bar's state is refreshed or updated. We now do this only when necessary due to a locale change. Bug: 161264248 Test: atest ProgressBarTest Change-Id: I349ec28268a3471184e5746b164f81428ad80cc8
Diffstat (limited to 'core/java/android/widget/ProgressBar.java')
-rw-r--r--core/java/android/widget/ProgressBar.java15
1 files changed, 13 insertions, 2 deletions
diff --git a/core/java/android/widget/ProgressBar.java b/core/java/android/widget/ProgressBar.java
index 4b32e10d083b..35605c4c6f6e 100644
--- a/core/java/android/widget/ProgressBar.java
+++ b/core/java/android/widget/ProgressBar.java
@@ -69,6 +69,7 @@ import com.android.internal.R;
import java.text.NumberFormat;
import java.util.ArrayList;
+import java.util.Locale;
/**
* <p>
@@ -249,6 +250,9 @@ public class ProgressBar extends View {
private ObjectAnimator mLastProgressAnimator;
+ private NumberFormat mPercentFormat;
+ private Locale mCachedLocale;
+
/**
* Create a new progress bar with range 0...100 and initial progress of 0.
* @param context the application environment
@@ -1591,8 +1595,15 @@ public class ProgressBar extends View {
* @return state description based on progress
*/
private CharSequence formatStateDescription(int progress) {
- return NumberFormat.getPercentInstance(mContext.getResources().getConfiguration().locale)
- .format(getPercent(progress));
+ // Cache the locale-appropriate NumberFormat. Configuration locale is guaranteed
+ // non-null, so the first time this is called we will always get the appropriate
+ // NumberFormat, then never regenerate it unless the locale changes on the fly.
+ final Locale curLocale = mContext.getResources().getConfiguration().getLocales().get(0);
+ if (!curLocale.equals(mCachedLocale)) {
+ mCachedLocale = curLocale;
+ mPercentFormat = NumberFormat.getPercentInstance(curLocale);
+ }
+ return mPercentFormat.format(getPercent(progress));
}
/**