Skip to content

Instantly share code, notes, and snippets.

@kmansoft
Last active August 29, 2015 14:23
Show Gist options
  • Save kmansoft/afad60576712fc5931ef to your computer and use it in GitHub Desktop.
Save kmansoft/afad60576712fc5931ef to your computer and use it in GitHub Desktop.
pyler's AppLocaleManager with a few changes...
package org.kman.AquaMail.core;
import java.util.Locale;
import org.kman.Compat.util.MyLog;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
public class AppLocaleManager {
private static final String TAG = "AppLocaleManager";
public AppLocaleManager(Context context) {
mContext = context.getApplicationContext();
}
public void applyFromPrefs(SharedPreferences prefs, String key) {
final String newLocaleCode = prefs.getString(key, null);
applyImpl(newLocaleCode);
}
public void applyFromValue(String newLocaleCode) {
applyImpl(newLocaleCode);
}
public Locale getSystemLocale() {
if (gSavedSystemLocale != null) {
return gSavedSystemLocale;
}
return Locale.getDefault();
}
private boolean applyImpl(String newLocaleCode) {
MyLog.i(TAG, "applyImpl: newLocaleCode = %s, savedSystemLocale = %s", newLocaleCode, gSavedSystemLocale);
Locale newLocale = null;
if (TextUtils.isEmpty(newLocaleCode)) {
// System locale
if (gSavedSystemLocale == null) {
// We've never changed it
return true;
}
newLocale = gSavedSystemLocale;
} else {
// Non-system, explicit locale
if (gSavedSystemLocale == null) {
gSavedSystemLocale = Locale.getDefault();
}
try {
final String[] split = newLocaleCode.split("_");
if (split != null && split.length >= 2) {
newLocale = new Locale(split[0], split[1]);
} else {
newLocale = new Locale(newLocaleCode);
}
} catch (Exception x) {
// Just in case
MyLog.w(TAG, x);
newLocale = null;
}
}
MyLog.i(TAG, "applyImpl: newLocale = %s", newLocale);
if (newLocale == null) {
return false;
}
try {
final Resources resources = mContext.getResources();
if (resources == null) {
return false;
}
final Configuration config = resources.getConfiguration();
if (config == null) {
return false;
}
config.locale = newLocale;
final ConfigCompat configCompat = ConfigCompat.factory();
configCompat.setLayoutDirection(config, newLocale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
Locale.setDefault(newLocale);
} catch (Exception x) {
// Just in case
MyLog.w(TAG, x);
}
return true;
}
private static class ConfigCompat {
static ConfigCompat factory() {
if (Build.VERSION.SDK_INT >= 17) {
return new ConfigCompat_api17();
}
return new ConfigCompat();
}
void setLayoutDirection(Configuration config, Locale locale) {
}
}
@TargetApi(17)
private static class ConfigCompat_api17 extends ConfigCompat {
@Override
void setLayoutDirection(Configuration config, Locale locale) {
config.setLayoutDirection(locale);
}
}
private Context mContext;
private static Locale gSavedSystemLocale;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment