diff options
author | Ben Murdoch <benm@google.com> | 2011-02-25 17:11:02 +0000 |
---|---|---|
committer | Ben Murdoch <benm@google.com> | 2011-02-28 14:19:05 +0000 |
commit | d2e91d1dc7c9fcbf792cbc21d283a5915b32cb54 (patch) | |
tree | 349696a6dc618ff7b83a51c14fa65f12ccb54647 | |
parent | b8460ba428e9114aedd9249e187dea6bf3104d78 (diff) | |
download | frameworks_base-d2e91d1dc7c9fcbf792cbc21d283a5915b32cb54.zip frameworks_base-d2e91d1dc7c9fcbf792cbc21d283a5915b32cb54.tar.gz frameworks_base-d2e91d1dc7c9fcbf792cbc21d283a5915b32cb54.tar.bz2 |
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
-rw-r--r-- | core/java/android/webkit/L10nUtils.java | 35 | ||||
-rw-r--r-- | core/java/android/webkit/WebView.java | 2 |
2 files changed, 25 insertions, 12 deletions
diff --git a/core/java/android/webkit/L10nUtils.java b/core/java/android/webkit/L10nUtils.java index 43a747a..f59d7d0 100644 --- a/core/java/android/webkit/L10nUtils.java +++ b/core/java/android/webkit/L10nUtils.java @@ -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(); } } diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index 98fc290..9be911c 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -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 |