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:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user