session-android/src/org/thoughtcrime/securesms/util/DynamicLanguage.java

72 lines
2.1 KiB
Java
Raw Normal View History

2013-06-30 01:03:55 +00:00
package org.thoughtcrime.securesms.util;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
2014-03-11 17:47:35 +00:00
import android.text.TextUtils;
2013-06-30 01:03:55 +00:00
import java.util.Locale;
public class DynamicLanguage {
private static final String DEFAULT = "zz";
private Locale currentLocale;
public void onCreate(Activity activity) {
currentLocale = getSelectedLocale(activity);
setActivityLocale(activity, currentLocale);
}
public void onResume(Activity activity) {
2014-03-11 17:47:35 +00:00
if (!currentLocale.equals(getSelectedLocale(activity))) {
2013-06-30 01:03:55 +00:00
Intent intent = activity.getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
activity.startActivity(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
OverridePendingTransition.invoke(activity);
}
activity.finish();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
OverridePendingTransition.invoke(activity);
}
}
}
private static void setActivityLocale(Activity activity, Locale selectedLocale) {
Configuration configuration = activity.getResources().getConfiguration();
2014-03-11 17:47:35 +00:00
if (!configuration.locale.equals(selectedLocale)) {
2013-06-30 01:03:55 +00:00
configuration.locale = selectedLocale;
activity.getResources().updateConfiguration(configuration,
activity.getResources().getDisplayMetrics());
}
}
private static Locale getActivityLocale(Activity activity) {
return activity.getResources().getConfiguration().locale;
}
private static Locale getSelectedLocale(Activity activity) {
2014-03-11 17:47:35 +00:00
String language[] = TextUtils.split(TextSecurePreferences.getLanguage(activity), "_");
if (language[0].equals(DEFAULT)) {
return Locale.getDefault();
} else if (language.length == 2) {
return new Locale(language[0], language[1]);
} else {
return new Locale(language[0]);
}
2013-06-30 01:03:55 +00:00
}
private static final class OverridePendingTransition {
static void invoke(Activity activity) {
activity.overridePendingTransition(0, 0);
}
}
}