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
This commit is contained in:
Ben Murdoch
2011-02-25 17:11:02 +00:00
parent b8460ba428
commit d2e91d1dc7
2 changed files with 25 additions and 12 deletions

View File

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

View File

@@ -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