Make RunInLocale as top-level class

Change-Id: I07ed606b7aba098a5be85533e5baff300b21fb44
This commit is contained in:
Tadashi G. Takaoka 2013-07-24 15:58:11 +09:00
parent a410cb48ea
commit 0c3a9b549c
9 changed files with 68 additions and 50 deletions

View file

@ -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;
}

View file

@ -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;

View file

@ -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();

View file

@ -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();
/**

View file

@ -0,0 +1,55 @@
/*
* 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);
}
}
}
}
}

View file

@ -27,7 +27,6 @@ import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.latin.DictionaryFactory;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.LocaleUtils.RunInLocale;
import java.util.HashMap;
import java.util.Locale;

View file

@ -23,7 +23,7 @@ import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.LocaleUtils.RunInLocale;
import com.android.inputmethod.latin.utils.RunInLocale;
import java.lang.reflect.Field;
import java.util.ArrayList;

View file

@ -26,7 +26,7 @@ import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.utils.LocaleUtils.RunInLocale;
import com.android.inputmethod.latin.utils.RunInLocale;
import java.util.Arrays;
import java.util.Locale;

View file

@ -24,7 +24,6 @@ import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.RichInputMethodManager;
import com.android.inputmethod.latin.utils.LocaleUtils.RunInLocale;
import java.util.ArrayList;
import java.util.Locale;
@ -138,7 +137,7 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase {
public void testPredefinedSubtypesInEnglishSystemLocale() {
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
protected Void job(final Resources res) {
assertEquals("en_US", "English (US)",
SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(EN_US));
assertEquals("en_GB", "English (UK)",
@ -162,7 +161,7 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase {
public void testAdditionalSubtypesInEnglishSystemLocale() {
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
protected Void job(final Resources res) {
assertEquals("fr qwertz", "French (QWERTZ)",
SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(FR_QWERTZ));
assertEquals("de qwerty", "German (QWERTY)",
@ -202,7 +201,7 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase {
public void testPredefinedSubtypesInFrenchSystemLocale() {
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
protected Void job(final Resources res) {
assertEquals("en_US", "Anglais (États-Unis)",
SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(EN_US));
assertEquals("en_GB", "Anglais (Royaume-Uni)",
@ -226,7 +225,7 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase {
public void testAdditionalSubtypesInFrenchSystemLocale() {
final RunInLocale<Void> tests = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
protected Void job(final Resources res) {
assertEquals("fr qwertz", "Français (QWERTZ)",
SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(FR_QWERTZ));
assertEquals("de qwerty", "Allemand (QWERTY)",
@ -310,7 +309,7 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase {
private final RunInLocale<Void> testsPredefinedSubtypesForSpacebar = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
protected Void job(final Resources res) {
assertEquals("en_US", "English (US)", SubtypeLocaleUtils.getFullDisplayName(EN_US));
assertEquals("en_GB", "English (UK)", SubtypeLocaleUtils.getFullDisplayName(EN_GB));
assertEquals("es_US", "Español (EE.UU.)",
@ -342,7 +341,7 @@ public class SubtypeLocaleUtilsTests extends AndroidTestCase {
private final RunInLocale<Void> testsAdditionalSubtypesForSpacebar = new RunInLocale<Void>() {
@Override
protected Void job(Resources res) {
protected Void job(final Resources res) {
assertEquals("fr qwertz", "Français",
SubtypeLocaleUtils.getFullDisplayName(FR_QWERTZ));
assertEquals("de qwerty", "Deutsch",