Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3305bce7 authored by Victor Chang's avatar Victor Chang
Browse files

Revert "Remove use of MeasureUnit.internalGetInstance"

This reverts commit aa5629e6.

Test: m
Bug: 70005649
Bug: 36994779
Change-Id: I4591870f564567c40fa450866c3050fd5a7a61ae
parent 23012115
Loading
Loading
Loading
Loading
+6 −23
Original line number Original line Diff line number Diff line
@@ -32,7 +32,6 @@ import android.text.BidiFormatter;
import android.text.TextUtils;
import android.text.TextUtils;
import android.view.View;
import android.view.View;


import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.Locale;


@@ -195,29 +194,13 @@ public final class Formatter {


    /**
    /**
     * ICU doesn't support PETABYTE yet. Fake it so that we can treat all units the same way.
     * ICU doesn't support PETABYTE yet. Fake it so that we can treat all units the same way.
     * {@hide}
     */
     */
    private static final MeasureUnit PETABYTE = createPetaByte();
    public static final MeasureUnit PETABYTE = MeasureUnit.internalGetInstance(
            "digital", "petabyte");


    /**
    /** {@hide} */
     * Create a petabyte MeasureUnit without registering it with ICU.
    public static class RoundedBytesResult {
     * ICU doesn't support user-create MeasureUnit and the only public (but hidden) method to do so
     * is {@link MeasureUnit#internalGetInstance(String, String)} which also registers the unit as
     * an available type and thus leaks it to code that doesn't expect or support it.
     * <p>This method uses reflection to create an instance of MeasureUnit to avoid leaking it. This
     * instance is <b>only</b> to be used in this class.
     */
    private static MeasureUnit createPetaByte() {
        try {
            Constructor<MeasureUnit> constructor = MeasureUnit.class
                    .getDeclaredConstructor(String.class, String.class);
            constructor.setAccessible(true);
            return constructor.newInstance("digital", "petabyte");
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException("Failed to create petabyte MeasureUnit", e);
        }
    }

    private static class RoundedBytesResult {
        public final float value;
        public final float value;
        public final MeasureUnit units;
        public final MeasureUnit units;
        public final int fractionDigits;
        public final int fractionDigits;
@@ -235,7 +218,7 @@ public final class Formatter {
         * Returns a RoundedBytesResult object based on the input size in bytes and the rounding
         * Returns a RoundedBytesResult object based on the input size in bytes and the rounding
         * flags. The result can be used for formatting.
         * flags. The result can be used for formatting.
         */
         */
        static RoundedBytesResult roundBytes(long sizeBytes, int flags) {
        public static RoundedBytesResult roundBytes(long sizeBytes, int flags) {
            final boolean isNegative = (sizeBytes < 0);
            final boolean isNegative = (sizeBytes < 0);
            float result = isNegative ? -sizeBytes : sizeBytes;
            float result = isNegative ? -sizeBytes : sizeBytes;
            MeasureUnit units = MeasureUnit.BYTE;
            MeasureUnit units = MeasureUnit.BYTE;
+0 −21
Original line number Original line Diff line number Diff line
@@ -17,7 +17,6 @@
package android.text.format;
package android.text.format;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;


import android.content.Context;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Configuration;
@@ -210,24 +209,4 @@ public class FormatterTest {


        Locale.setDefault(locale);
        Locale.setDefault(locale);
    }
    }

    /**
     * Verifies that Formatter doesn't "leak" the locally defined petabyte unit into ICU via the
     * {@link MeasureUnit} registry. This test can fail for two reasons:
     * 1. we regressed and started leaking again. In this case the code needs to be fixed.
     * 2. ICU started supporting petabyte as a unit, in which case change one needs to revert this
     * change (I494fb59a3b3742f35cbdf6b8705817f404a2c6b0), remove Formatter.PETABYTE and replace any
     * usages of that field with just MeasureUnit.PETABYTE.
     */
    // http://b/65632959
    @Test
    public void doesNotLeakPetabyte() {
        // Ensure that the Formatter class is loaded when we call .getAvailable().
        Formatter.formatFileSize(mContext, Long.MAX_VALUE);
        Set<MeasureUnit> digitalUnits = MeasureUnit.getAvailable("digital");
        for (MeasureUnit unit : digitalUnits) {
            // This assert can fail if we don't leak PETABYTE, but ICU has added it, see #2 above.
            assertNotEquals("petabyte", unit.getSubtype());
        }
    }
}
}