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

Commit 9a43e6cb authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Stop ellipsize in system server

System server don't have UI but doing ellipsize with real font with
hard coded font size and hard coded width. This is the only font
usage in the system server, so let's remove this for preparing
skiping font initialization in the system server.

In the system server, this method is used for loading label. During
loading labels, the hard coded font size 42 px and 1000px width has
been used. This is equivalent to about 41 characters in Roboto.

No behavior changes to the this public API users except for system
server.

Bug: 291239366
Test: Manually done by booting device (Pixel 5)
Change-Id: I71c940de4b76bf32c14ead66a7912dfb8f25a384
parent d09ec365
Loading
Loading
Loading
Loading
+22 −5
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.annotation.PluralsRes;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.icu.lang.UCharacter;
import android.icu.text.CaseMap;
import android.icu.text.Edits;
@@ -2481,15 +2482,31 @@ public class TextUtils {

        if (ellipsizeDip == 0) {
            return gettingCleaned.toString();
        } else {
            final float assumedFontSizePx = 42;
            if (Typeface.getSystemFontMap().isEmpty()) {
                // In the system server, the font files may not be loaded, so unable to perform
                // ellipsize, so use the estimated char count for the ellipsize.

                // The median of glyph widths of the Roboto is 0.57em, so use it as a reference
                // of the glyph width.
                final float assumedCharWidthInEm = 0.57f;
                final float assumedCharWidthInPx = assumedFontSizePx * assumedCharWidthInEm;

                // Even if the argument name is `ellipsizeDip`, the unit of this argument is pixels.
                final int charCount = (int) ((ellipsizeDip + 0.5f) / assumedCharWidthInPx);
                return TextUtils.trimToSize(gettingCleaned.toString(), charCount)
                        + getEllipsisString(TruncateAt.END);
            } else {
                // Truncate
                final TextPaint paint = new TextPaint();
            paint.setTextSize(42);
                paint.setTextSize(assumedFontSizePx);

                return TextUtils.ellipsize(gettingCleaned.toString(), paint, ellipsizeDip,
                        TextUtils.TruncateAt.END);
            }
        }
    }

    /**
     * A special string manipulation class. Just records removals and executes the when onString()