Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a980fb39 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

Merge "Refactor RunInLocale a bit"

parents 8180b02b 5c490e5d
Loading
Loading
Loading
Loading
+10 −12
Original line number Diff line number Diff line
@@ -30,25 +30,23 @@ public abstract class RunInLocale<T> {
     * Execute {@link #job(Resources)} method in specified system locale exclusively.
     *
     * @param res the resources to use.
     * @param newLocale the locale to change to.
     * @param newLocale the locale to change to. Run in system locale if null.
     * @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;
            final boolean needsChange = (newLocale != null && !newLocale.equals(oldLocale));
            try {
                if (needsChange) {
                    conf.locale = newLocale;
                    res.updateConfiguration(conf, null);
            final Configuration savedConf = res.getConfiguration();
            if (newLocale == null || newLocale.equals(savedConf.locale)) {
                return job(res);
            }
            final Configuration newConf = new Configuration();
            newConf.setTo(savedConf);
            newConf.setLocale(newLocale);
            try {
                res.updateConfiguration(newConf, null);
                return job(res);
            } finally {
                if (needsChange) {
                    conf.locale = oldLocale;
                    res.updateConfiguration(conf, null);
                }
                res.updateConfiguration(savedConf, null);
            }
        }
    }