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

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

Merge "Make RunInLocale as top-level class"

parents 9ef59af7 0c3a9b54
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -30,8 +30,8 @@ import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardId;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.LocaleUtils.RunInLocale;
import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.RunInLocale;
import com.android.inputmethod.latin.utils.StringUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import com.android.inputmethod.latin.utils.XmlParseUtils;
@@ -279,7 +279,7 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
            params.mTextsSet.setLanguage(language);
            final RunInLocale<Void> job = new RunInLocale<Void>() {
                @Override
                protected Void job(Resources res) {
                protected Void job(final Resources res) {
                    params.mTextsSet.loadStringResources(mContext);
                    return null;
                }
+1 −1
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ import com.android.inputmethod.latin.InputAttributes;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
import com.android.inputmethod.latin.utils.LocaleUtils;
import com.android.inputmethod.latin.utils.LocaleUtils.RunInLocale;
import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.RunInLocale;

import java.util.HashMap;
import java.util.Locale;
+2 −1
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.InputTypeUtils;
import com.android.inputmethod.latin.utils.RunInLocale;
import com.android.inputmethod.latin.utils.StringUtils;

import java.util.ArrayList;
@@ -39,7 +40,7 @@ import java.util.Arrays;

/**
 * When you call the constructor of this class, you may want to change the current system locale by
 * using {@link com.android.inputmethod.latin.utils.LocaleUtils.RunInLocale}.
 * using {@link com.android.inputmethod.latin.utils.RunInLocale}.
 */
public final class SettingsValues {
    private static final String TAG = SettingsValues.class.getSimpleName();
+0 −36
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.inputmethod.latin.utils;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.TextUtils;

import java.util.HashMap;
@@ -164,40 +162,6 @@ public final class LocaleUtils {
        return LOCALE_MATCH <= level;
    }

    static final Object sLockForRunInLocale = new Object();

    // TODO: Make this an external class
    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;
                final boolean needsChange = (newLocale != null && !newLocale.equals(oldLocale));
                try {
                    if (needsChange) {
                        conf.locale = newLocale;
                        res.updateConfiguration(conf, null);
                    }
                    return job(res);
                } finally {
                    if (needsChange) {
                        conf.locale = oldLocale;
                        res.updateConfiguration(conf, null);
                    }
                }
            }
        }
    }

    private static final HashMap<String, Locale> sLocaleCache = CollectionUtils.newHashMap();

    /**
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.latin.utils;

import android.content.res.Configuration;
import android.content.res.Resources;

import java.util.Locale;

public abstract class RunInLocale<T> {
    private static final Object sLockForRunInLocale = new Object();

    protected abstract T job(final Resources res);

    /**
     * Execute {@link #job(Resources)} method in specified system locale exclusively.
     *
     * @param res the resources to use.
     * @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;
            final boolean needsChange = (newLocale != null && !newLocale.equals(oldLocale));
            try {
                if (needsChange) {
                    conf.locale = newLocale;
                    res.updateConfiguration(conf, null);
                }
                return job(res);
            } finally {
                if (needsChange) {
                    conf.locale = oldLocale;
                    res.updateConfiguration(conf, null);
                }
            }
        }
    }
}
Loading