aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/LocaleUtils.java
diff options
context:
space:
mode:
authorTadashi G. Takaoka <takaoka@google.com>2012-04-03 14:28:56 +0900
committerTadashi G. Takaoka <takaoka@google.com>2012-04-03 17:43:45 +0900
commit16c6f355700ee5cdaa029f4a25b8b3d40718e6ab (patch)
treeb3863cd867379b6f3a1c15b19c16364d3b44676e /java/src/com/android/inputmethod/latin/LocaleUtils.java
parent78e333594bbc97e56ad105ce3888192e78771626 (diff)
downloadlatinime-16c6f355700ee5cdaa029f4a25b8b3d40718e6ab.tar.gz
latinime-16c6f355700ee5cdaa029f4a25b8b3d40718e6ab.tar.xz
latinime-16c6f355700ee5cdaa029f4a25b8b3d40718e6ab.zip
Add RunInLocale class to guard locale switching
Bug: 6128216 Change-Id: I8d9c75c773c3de886183b291ada7a3836295839b
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LocaleUtils.java')
-rw-r--r--java/src/com/android/inputmethod/latin/LocaleUtils.java43
1 files changed, 29 insertions, 14 deletions
diff --git a/java/src/com/android/inputmethod/latin/LocaleUtils.java b/java/src/com/android/inputmethod/latin/LocaleUtils.java
index cf60089c5..f19c59a6a 100644
--- a/java/src/com/android/inputmethod/latin/LocaleUtils.java
+++ b/java/src/com/android/inputmethod/latin/LocaleUtils.java
@@ -161,21 +161,36 @@ public class LocaleUtils {
return LOCALE_MATCH <= level;
}
- /**
- * Sets the system locale for this process.
- *
- * @param res the resources to use. Pass current resources.
- * @param newLocale the locale to change to.
- * @return the old locale.
- */
- public static synchronized Locale setSystemLocale(final Resources res, final Locale newLocale) {
- final Configuration conf = res.getConfiguration();
- final Locale oldLocale = conf.locale;
- if (newLocale != null && !newLocale.equals(oldLocale)) {
- conf.locale = newLocale;
- res.updateConfiguration(conf, res.getDisplayMetrics());
+ static final Object sLockForRunInLocale = new Object();
+
+ public abstract static class RunInLocale<T> {
+ protected abstract T job(Resources res);
+
+ /**
+ * Execute {@link #job(Resources)} method in specified system locale exclusively.
+ *
+ * @param res the resources to use. Pass current resources.
+ * @param newLocale the locale to change to
+ * @return the value returned from {@link #job(Resources)}.
+ */
+ public T runInLocale(final Resources res, final Locale newLocale) {
+ synchronized (sLockForRunInLocale) {
+ final Configuration conf = res.getConfiguration();
+ final Locale oldLocale = conf.locale;
+ try {
+ if (newLocale != null && !newLocale.equals(oldLocale)) {
+ conf.locale = newLocale;
+ res.updateConfiguration(conf, res.getDisplayMetrics());
+ }
+ return job(res);
+ } finally {
+ if (newLocale != null && !newLocale.equals(oldLocale)) {
+ conf.locale = oldLocale;
+ res.updateConfiguration(conf, res.getDisplayMetrics());
+ }
+ }
+ }
}
- return oldLocale;
}
private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>();