From b2c6bf49c496851d90de283180823de04e7c7ab6 Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Tue, 3 Sep 2024 11:39:20 +0800 Subject: [PATCH] Clean up FileSizeFormatter Which is not used after Change I37c7b3a4b5860323cb55581b23a90f583f4af216 Fix: 364129275 Flag: EXEMPT clean up Test: robotests Change-Id: I0db29c77a1dd938bff53df9e5f33c95cc0fbaad4 --- .../deviceinfo/StorageItemPreference.java | 2 + .../settings/utils/FileSizeFormatter.java | 172 ------------------ .../deviceinfo/StorageItemPreferenceTest.java | 3 +- .../storage/NonCurrentUserControllerTest.java | 3 +- .../StorageItemPreferenceControllerTest.java | 7 +- 5 files changed, 8 insertions(+), 179 deletions(-) delete mode 100644 src/com/android/settings/utils/FileSizeFormatter.java diff --git a/src/com/android/settings/deviceinfo/StorageItemPreference.java b/src/com/android/settings/deviceinfo/StorageItemPreference.java index 91102a031eb..62c0a6a8be6 100644 --- a/src/com/android/settings/deviceinfo/StorageItemPreference.java +++ b/src/com/android/settings/deviceinfo/StorageItemPreference.java @@ -22,6 +22,7 @@ import android.content.Context; import android.util.AttributeSet; import android.widget.ProgressBar; +import androidx.annotation.VisibleForTesting; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; @@ -48,6 +49,7 @@ public class StorageItemPreference extends Preference { setLayoutResource(R.layout.storage_item); } + @VisibleForTesting public void setStorageSize(long size, long total) { setStorageSize(size, total, false /* animate */); } diff --git a/src/com/android/settings/utils/FileSizeFormatter.java b/src/com/android/settings/utils/FileSizeFormatter.java deleted file mode 100644 index 5950d5d0ce6..00000000000 --- a/src/com/android/settings/utils/FileSizeFormatter.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (C) 2017 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.settings.utils; - -import android.content.Context; -import android.icu.text.DecimalFormat; -import android.icu.text.MeasureFormat; -import android.icu.text.NumberFormat; -import android.icu.util.Measure; -import android.icu.util.MeasureUnit; -import android.text.BidiFormatter; -import android.text.TextUtils; -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import java.math.BigDecimal; -import java.util.Locale; - -/** - * Utility class to aid in formatting file sizes always with the same unit. This is modified from - * android.text.format.Formatter to fit this purpose. - */ -public final class FileSizeFormatter { - public static final long KILOBYTE_IN_BYTES = 1000; - public static final long MEGABYTE_IN_BYTES = KILOBYTE_IN_BYTES * 1000; - public static final long GIGABYTE_IN_BYTES = MEGABYTE_IN_BYTES * 1000; - - private static class RoundedBytesResult { - public final float value; - public final MeasureUnit units; - public final int fractionDigits; - public final long roundedBytes; - - private RoundedBytesResult( - float value, MeasureUnit units, int fractionDigits, long roundedBytes) { - this.value = value; - this.units = units; - this.fractionDigits = fractionDigits; - this.roundedBytes = roundedBytes; - } - } - - private static Locale localeFromContext(@NonNull Context context) { - return context.getResources().getConfiguration().locale; - } - - private static String bidiWrap(@NonNull Context context, String source) { - final Locale locale = localeFromContext(context); - if (TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL) { - return BidiFormatter.getInstance(true /* RTL*/).unicodeWrap(source); - } else { - return source; - } - } - - private static NumberFormat getNumberFormatter(Locale locale, int fractionDigits) { - final NumberFormat numberFormatter = NumberFormat.getInstance(locale); - numberFormatter.setMinimumFractionDigits(fractionDigits); - numberFormatter.setMaximumFractionDigits(fractionDigits); - numberFormatter.setGroupingUsed(false); - if (numberFormatter instanceof DecimalFormat) { - // We do this only for DecimalFormat, since in the general NumberFormat case, calling - // setRoundingMode may throw an exception. - numberFormatter.setRoundingMode(BigDecimal.ROUND_HALF_UP); - } - return numberFormatter; - } - - private static String formatMeasureShort(Locale locale, NumberFormat numberFormatter, - float value, MeasureUnit units) { - final MeasureFormat measureFormatter = MeasureFormat.getInstance( - locale, MeasureFormat.FormatWidth.SHORT, numberFormatter); - return measureFormatter.format(new Measure(value, units)); - } - - private static String formatRoundedBytesResult( - @NonNull Context context, @NonNull RoundedBytesResult input) { - final Locale locale = localeFromContext(context); - final NumberFormat numberFormatter = getNumberFormatter(locale, input.fractionDigits); - return formatMeasureShort(locale, numberFormatter, input.value, input.units); - } - - /** - * Formats a content size to be in the form of bytes, kilobytes, megabytes, etc. - * - *

As of O, the prefixes are used in their standard meanings in the SI system, so kB = 1000 - * bytes, MB = 1,000,000 bytes, etc. - * - *

In {@link android.os.Build.VERSION_CODES#N} and earlier, powers of 1024 are - * used instead, with KB = 1024 bytes, MB = 1,048,576 bytes, etc. - * - *

If the context has a right-to-left locale, the returned string is wrapped in bidi - * formatting characters to make sure it's displayed correctly if inserted inside a - * right-to-left string. (This is useful in cases where the unit strings, like "MB", are - * left-to-right, but the locale is right-to-left.) - * - * @param context Context to use to load the localized units - * @param sizeBytes size value to be formatted, in bytes - * @param unit The unit used for formatting. - * @param mult Amount of bytes in the unit. - * @return formatted string with the number - */ - public static String formatFileSize( - @Nullable Context context, long sizeBytes, MeasureUnit unit, long mult) { - if (context == null) { - return ""; - } - final RoundedBytesResult res = formatBytes(sizeBytes, unit, mult); - return bidiWrap(context, formatRoundedBytesResult(context, res)); - } - - /** - * A simplified version of the SettingsLib file size formatter. The primary difference is that - * this version always assumes it is doing a "short file size" and allows for a suffix to be - * provided. - * - * @param res Resources to fetch strings with. - * @param sizeBytes File size in bytes to format. - * @param suffix String id for the unit suffix. - * @param mult Amount of bytes in the unit. - */ - private static RoundedBytesResult formatBytes( - long sizeBytes, MeasureUnit unit, long mult) { - final boolean isNegative = (sizeBytes < 0); - float result = isNegative ? -sizeBytes : sizeBytes; - result = result / mult; - // Note we calculate the rounded long by ourselves, but still let String.format() - // compute the rounded value. String.format("%f", 0.1) might not return "0.1" due to - // floating point errors. - final int roundFactor; - final int roundDigits; - if (mult == 1) { - roundFactor = 1; - roundDigits = 0; - } else if (result < 1) { - roundFactor = 100; - roundDigits = 2; - } else if (result < 10) { - roundFactor = 10; - roundDigits = 1; - } else { // 10 <= result < 100 - roundFactor = 1; - roundDigits = 0; - } - - if (isNegative) { - result = -result; - } - - // Note this might overflow if abs(result) >= Long.MAX_VALUE / 100, but that's like 80PB so - // it's okay (for now)... - final long roundedBytes = (((long) Math.round(result * roundFactor)) * mult / roundFactor); - - return new RoundedBytesResult(result, unit, roundDigits, roundedBytes); - } -} diff --git a/tests/robotests/src/com/android/settings/deviceinfo/StorageItemPreferenceTest.java b/tests/robotests/src/com/android/settings/deviceinfo/StorageItemPreferenceTest.java index 984d945dac6..eba8e11f57a 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/StorageItemPreferenceTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/StorageItemPreferenceTest.java @@ -15,8 +15,6 @@ */ package com.android.settings.deviceinfo; -import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES; - import static com.google.common.truth.Truth.assertThat; import android.content.Context; @@ -36,6 +34,7 @@ import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) public class StorageItemPreferenceTest { + private static final long MEGABYTE_IN_BYTES = 1000_000; private Context mContext; private StorageItemPreference mPreference; diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/NonCurrentUserControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/NonCurrentUserControllerTest.java index 61d3bed91c7..34cc4fdb20c 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/storage/NonCurrentUserControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/NonCurrentUserControllerTest.java @@ -16,8 +16,6 @@ package com.android.settings.deviceinfo.storage; -import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES; - import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.anyString; @@ -61,6 +59,7 @@ public class NonCurrentUserControllerTest { private static final String TEST_NAME = "Fred"; private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users"; + private static final long MEGABYTE_IN_BYTES = 1000_000; @Mock private UserManager mUserManager; @Mock diff --git a/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java index 306d80c5c17..a9e9eacaa3c 100644 --- a/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/deviceinfo/storage/StorageItemPreferenceControllerTest.java @@ -16,9 +16,6 @@ package com.android.settings.deviceinfo.storage; import static com.android.settings.applications.manageapplications.ManageApplications.EXTRA_WORK_ID; -import static com.android.settings.utils.FileSizeFormatter.GIGABYTE_IN_BYTES; -import static com.android.settings.utils.FileSizeFormatter.KILOBYTE_IN_BYTES; -import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES; import static com.google.common.truth.Truth.assertThat; @@ -74,6 +71,10 @@ import org.robolectric.annotation.Config; }) public class StorageItemPreferenceControllerTest { + private static final long KILOBYTE_IN_BYTES = 1000; + private static final long MEGABYTE_IN_BYTES = KILOBYTE_IN_BYTES * 1000; + private static final long GIGABYTE_IN_BYTES = MEGABYTE_IN_BYTES * 1000; + private Context mContext; private VolumeInfo mVolume; @Mock