2013-06-30 01:03:55 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2015-11-26 18:04:27 +00:00
|
|
|
import android.annotation.TargetApi;
|
2013-06-30 01:03:55 +00:00
|
|
|
import android.app.Activity;
|
2014-12-11 23:04:32 +00:00
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Context;
|
2013-06-30 01:03:55 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.res.Configuration;
|
2015-11-26 18:04:27 +00:00
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
2017-09-25 16:05:41 +00:00
|
|
|
import android.support.annotation.RequiresApi;
|
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);
|
2014-12-11 23:04:32 +00:00
|
|
|
setContextLocale(activity, currentLocale);
|
2013-06-30 01:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onResume(Activity activity) {
|
2014-03-11 17:47:35 +00:00
|
|
|
if (!currentLocale.equals(getSelectedLocale(activity))) {
|
2014-05-23 20:22:23 +00:00
|
|
|
Intent intent = activity.getIntent();
|
|
|
|
activity.finish();
|
|
|
|
OverridePendingTransition.invoke(activity);
|
|
|
|
activity.startActivity(intent);
|
|
|
|
OverridePendingTransition.invoke(activity);
|
2013-06-30 01:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:32 +00:00
|
|
|
public void updateServiceLocale(Service service) {
|
|
|
|
currentLocale = getSelectedLocale(service);
|
|
|
|
setContextLocale(service, currentLocale);
|
|
|
|
}
|
|
|
|
|
2015-03-19 20:08:48 +00:00
|
|
|
public Locale getCurrentLocale() {
|
|
|
|
return currentLocale;
|
|
|
|
}
|
|
|
|
|
2017-09-25 16:05:41 +00:00
|
|
|
@RequiresApi(VERSION_CODES.JELLY_BEAN_MR1)
|
2015-11-26 18:04:27 +00:00
|
|
|
public static int getLayoutDirection(Context context) {
|
|
|
|
Configuration configuration = context.getResources().getConfiguration();
|
|
|
|
return configuration.getLayoutDirection();
|
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:32 +00:00
|
|
|
private static void setContextLocale(Context context, Locale selectedLocale) {
|
|
|
|
Configuration configuration = context.getResources().getConfiguration();
|
2013-06-30 01:03:55 +00:00
|
|
|
|
2014-03-11 17:47:35 +00:00
|
|
|
if (!configuration.locale.equals(selectedLocale)) {
|
2013-06-30 01:03:55 +00:00
|
|
|
configuration.locale = selectedLocale;
|
2015-11-26 18:04:27 +00:00
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
|
|
|
|
configuration.setLayoutDirection(selectedLocale);
|
|
|
|
}
|
2014-12-11 23:04:32 +00:00
|
|
|
context.getResources().updateConfiguration(configuration,
|
2015-11-26 18:04:27 +00:00
|
|
|
context.getResources().getDisplayMetrics());
|
2013-06-30 01:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Locale getActivityLocale(Activity activity) {
|
|
|
|
return activity.getResources().getConfiguration().locale;
|
|
|
|
}
|
|
|
|
|
2014-12-11 23:04:32 +00:00
|
|
|
private static Locale getSelectedLocale(Context context) {
|
|
|
|
String language[] = TextUtils.split(TextSecurePreferences.getLanguage(context), "_");
|
2014-03-11 17:47:35 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|