diff options
author | 2012-04-03 01:53:05 -0700 | |
---|---|---|
committer | 2012-04-03 01:53:05 -0700 | |
commit | d2c5533fd9dba55df8b2726b449232338dcaf93f (patch) | |
tree | 274890f722c82658677a693bcbb71bd94312270c /java/src/com/android/inputmethod/latin/LocaleUtils.java | |
parent | 56dda78d529478b939ffc8ca5d7e8f6998d79af9 (diff) | |
parent | 16c6f355700ee5cdaa029f4a25b8b3d40718e6ab (diff) | |
download | latinime-d2c5533fd9dba55df8b2726b449232338dcaf93f.tar.gz latinime-d2c5533fd9dba55df8b2726b449232338dcaf93f.tar.xz latinime-d2c5533fd9dba55df8b2726b449232338dcaf93f.zip |
Merge "Add RunInLocale class to guard locale switching"
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LocaleUtils.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/LocaleUtils.java | 43 |
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>(); |