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

Commit f1f9902b authored by Raph Levien's avatar Raph Levien Committed by Android (Google) Code Review
Browse files

Merge "Load hyphenation data at Zygote init"

parents 3544e6fa c3dd1c1b
Loading
Loading
Loading
Loading
+22 −11
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ import java.util.Locale;
 *
 * @hide
 */
/* package */ class Hyphenator {
public class Hyphenator {
    // This class has deliberately simple lifetime management (no finalizer) because in
    // the common case a process will use a very small number of locales.

@@ -45,20 +45,14 @@ import java.util.Locale;
    }

    public static long get(Locale locale) {
        synchronized (sMap) {
        Hyphenator result = sMap.get(locale);
            if (result == null) {
                result = loadHyphenator(locale);
                sMap.put(locale, result);
            }
        return result == null ? 0 : result.mNativePtr;
    }
    }

    private static Hyphenator loadHyphenator(Locale locale) {
        // TODO: find pattern dictionary (from system location) that best matches locale
        if (Locale.US.equals(locale)) {
            File f = new File("/data/local/tmp/hyph-en-us.pat.txt");
            File f = new File(getSystemHyphenatorLocation(), "hyph-en-us.pat.txt");
            try {
                RandomAccessFile rf = new RandomAccessFile(f, "r");
                byte[] buf = new byte[(int)rf.length()];
@@ -68,9 +62,26 @@ import java.util.Locale;
                long nativePtr = StaticLayout.nLoadHyphenator(patternData);
                return new Hyphenator(nativePtr);
            } catch (IOException e) {
                Log.e(TAG, "error loading hyphenation " + f);
                Log.e(TAG, "error loading hyphenation " + f, e);
            }
        }
        return null;
    }

    private static File getSystemHyphenatorLocation() {
        // TODO: move to a sensible location under system
        return new File("/system/usr/hyphen-data");
    }

    /**
     * Load hyphenation patterns at initialization time. We want to have patterns
     * for all locales loaded and ready to use so we don't have to do any file IO
     * on the UI thread when drawing text in different locales.
     *
     * @hide
     */
    public static void init() {
        Locale l = Locale.US;
        sMap.put(l, loadHyphenator(l));
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import android.system.StructPollfd;
import android.text.Hyphenator;
import android.util.EventLog;
import android.util.Log;
import android.webkit.WebViewFactory;
@@ -182,6 +183,7 @@ public class ZygoteInit {
        preloadResources();
        preloadOpenGL();
        preloadSharedLibraries();
        preloadTextResources();
        // Ask the WebViewFactory to do any initialization that must run in the zygote process,
        // for memory sharing purposes.
        WebViewFactory.prepareWebViewInZygote();
@@ -201,6 +203,10 @@ public class ZygoteInit {
        }
    }

    private static void preloadTextResources() {
        Hyphenator.init();
    }

    /**
     * Performs Zygote process initialization. Loads and initializes
     * commonly used classes.