Merge "Fix a concurrency bug."

main
Jean Chalard 2013-08-02 11:28:45 +00:00 committed by Android (Google) Code Review
commit ba2f4f84ca
1 changed files with 27 additions and 14 deletions

View File

@ -27,12 +27,14 @@ import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
import com.android.inputmethod.latin.InputAttributes; import com.android.inputmethod.latin.InputAttributes;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
import com.android.inputmethod.latin.utils.DebugLogUtils;
import com.android.inputmethod.latin.utils.LocaleUtils; import com.android.inputmethod.latin.utils.LocaleUtils;
import com.android.inputmethod.latin.utils.ResourceUtils; import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.RunInLocale; import com.android.inputmethod.latin.utils.RunInLocale;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.concurrent.locks.ReentrantLock;
public final class Settings implements SharedPreferences.OnSharedPreferenceChangeListener { public final class Settings implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = Settings.class.getSimpleName(); private static final String TAG = Settings.class.getSimpleName();
@ -94,6 +96,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
private Resources mRes; private Resources mRes;
private SharedPreferences mPrefs; private SharedPreferences mPrefs;
private SettingsValues mSettingsValues; private SettingsValues mSettingsValues;
private final ReentrantLock mSettingsValuesLock = new ReentrantLock();
private static final Settings sInstance = new Settings(); private static final Settings sInstance = new Settings();
@ -121,24 +124,34 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
@Override @Override
public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) {
if (mSettingsValues == null) { mSettingsValuesLock.lock();
// TODO: Introduce a static function to register this class and ensure that try {
// loadSettings must be called before "onSharedPreferenceChanged" is called. if (mSettingsValues == null) {
Log.w(TAG, "onSharedPreferenceChanged called before loadSettings."); // TODO: Introduce a static function to register this class and ensure that
return; // loadSettings must be called before "onSharedPreferenceChanged" is called.
Log.w(TAG, "onSharedPreferenceChanged called before loadSettings.");
return;
}
loadSettings(mSettingsValues.mLocale, mSettingsValues.mInputAttributes);
} finally {
mSettingsValuesLock.unlock();
} }
loadSettings(mSettingsValues.mLocale, mSettingsValues.mInputAttributes);
} }
public void loadSettings(final Locale locale, final InputAttributes inputAttributes) { public void loadSettings(final Locale locale, final InputAttributes inputAttributes) {
final SharedPreferences prefs = mPrefs; mSettingsValuesLock.lock();
final RunInLocale<SettingsValues> job = new RunInLocale<SettingsValues>() { try {
@Override final SharedPreferences prefs = mPrefs;
protected SettingsValues job(final Resources res) { final RunInLocale<SettingsValues> job = new RunInLocale<SettingsValues>() {
return new SettingsValues(prefs, locale, res, inputAttributes); @Override
} protected SettingsValues job(final Resources res) {
}; return new SettingsValues(prefs, locale, res, inputAttributes);
mSettingsValues = job.runInLocale(mRes, locale); }
};
mSettingsValues = job.runInLocale(mRes, locale);
} finally {
mSettingsValuesLock.unlock();
}
} }
// TODO: Remove this method and add proxy method to SettingsValues. // TODO: Remove this method and add proxy method to SettingsValues.