session-android/src/org/thoughtcrime/securesms/util/DynamicLanguage.java
Rhodey Orbits d8521637bb use Locale from DynamicLanguage for displaying dates
1) fixed DateUtils to use SimpleDateFormat for everything because it respects Locale
2) added getCurrentLocale() method to DynamicLanguage
3) allow PassphraseRequiredActionBarActivity.initFragment() to accept a Locale
4) updated classes that depend on DateUtils to pass down Locale from DynamicLanguage

Fixes #2684
Closes #2725
// FREEBIE
2015-04-23 13:56:31 -05:00

75 lines
2.1 KiB
Java

package org.thoughtcrime.securesms.util;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.text.TextUtils;
import java.util.Locale;
public class DynamicLanguage {
private static final String DEFAULT = "zz";
private Locale currentLocale;
public void onCreate(Activity activity) {
currentLocale = getSelectedLocale(activity);
setContextLocale(activity, currentLocale);
}
public void onResume(Activity activity) {
if (!currentLocale.equals(getSelectedLocale(activity))) {
Intent intent = activity.getIntent();
activity.finish();
OverridePendingTransition.invoke(activity);
activity.startActivity(intent);
OverridePendingTransition.invoke(activity);
}
}
public void updateServiceLocale(Service service) {
currentLocale = getSelectedLocale(service);
setContextLocale(service, currentLocale);
}
public Locale getCurrentLocale() {
return currentLocale;
}
private static void setContextLocale(Context context, Locale selectedLocale) {
Configuration configuration = context.getResources().getConfiguration();
if (!configuration.locale.equals(selectedLocale)) {
configuration.locale = selectedLocale;
context.getResources().updateConfiguration(configuration,
context.getResources().getDisplayMetrics());
}
}
private static Locale getActivityLocale(Activity activity) {
return activity.getResources().getConfiguration().locale;
}
private static Locale getSelectedLocale(Context context) {
String language[] = TextUtils.split(TextSecurePreferences.getLanguage(context), "_");
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]);
}
}
private static final class OverridePendingTransition {
static void invoke(Activity activity) {
activity.overridePendingTransition(0, 0);
}
}
}