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

Commit d2e91d1d authored by Ben Murdoch's avatar Ben Murdoch
Browse files

Improve caching of localised strings for Chromium

We read localised strings for Chromium (e.g. for Autofill)
from Java. Now we've added more large strings, keep them
in WeakReferences and load them lazily rather than on startup.

Change-Id: Ibedcaab5ee781699b41b5c03aae866d1d82bb570
parent b8460ba4
Loading
Loading
Loading
Loading
+24 −11
Original line number Diff line number Diff line
@@ -18,8 +18,9 @@ package android.webkit;

import android.content.Context;

import java.util.List;
import java.util.Vector;
import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.HashMap;

/**
 * @hide
@@ -71,20 +72,32 @@ public class L10nUtils {
        com.android.internal.R.string.autofill_card_ignored_re              // IDS_AUTOFILL_CARD_IGNORED_RE
    };

    private static List<String> mStrings;
    private static Context mApplicationContext;
    private static Map<Integer, SoftReference<String> > mStrings;

    public static void loadStrings(Context context) {
        if (mStrings != null) {
            return;
    public static void setApplicationContext(Context applicationContext) {
        mApplicationContext = applicationContext.getApplicationContext();
    }

        mStrings = new Vector<String>(mIdsArray.length);
        for (int i = 0; i < mIdsArray.length; i++) {
            mStrings.add(context.getResources().getString(mIdsArray[i]));
    private static String loadString(int id) {
        if (mStrings == null) {
            mStrings = new HashMap<Integer, SoftReference<String> >(mIdsArray.length);
        }

        String localisedString = mApplicationContext.getResources().getString(mIdsArray[id]);
        mStrings.put(id, new SoftReference<String>(localisedString));
        return localisedString;
    }

    public static String getLocalisedString(int id) {
        return mStrings.get(id);
        if (mStrings == null) {
            // This is the first time we need a localised string.
            // loadString will create the Map.
            return loadString(id);
        }

        SoftReference<String> ref = mStrings.get(id);
        boolean needToLoad = ref == null || ref.get() == null;
        return needToLoad ? loadString(id) : ref.get();
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -987,7 +987,7 @@ public class WebView extends AbsoluteLayout

        mCallbackProxy = new CallbackProxy(context, this);
        mViewManager = new ViewManager(this);
        L10nUtils.loadStrings(context);
        L10nUtils.setApplicationContext(context.getApplicationContext());
        mWebViewCore = new WebViewCore(context, this, mCallbackProxy, javaScriptInterfaces);
        mDatabase = WebViewDatabase.getInstance(context);
        mScroller = new OverScroller(context, null, 0, 0, false); //TODO Use OverScroller's flywheel