PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/LanguageContextWrapper.java

39 lines
1.1 KiB
Java
Raw Normal View History

2020-04-24 18:11:14 +00:00
package code.name.monkey.retromusic;
import android.content.Context;
2020-06-06 18:57:28 +00:00
import android.content.ContextWrapper;
2020-04-24 18:11:14 +00:00
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.LocaleList;
import com.google.android.gms.common.annotation.KeepName;
2020-10-06 08:46:04 +00:00
import java.util.Locale;
2020-04-24 18:11:14 +00:00
import code.name.monkey.appthemehelper.util.VersionUtils;
2020-06-06 18:57:28 +00:00
public class LanguageContextWrapper extends ContextWrapper {
2020-04-24 18:11:14 +00:00
2020-10-06 08:46:04 +00:00
public LanguageContextWrapper(Context base) {
super(base);
}
2020-04-24 18:11:14 +00:00
@KeepName
2020-10-06 08:46:04 +00:00
public static LanguageContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
2020-04-24 18:11:14 +00:00
2020-10-06 08:46:04 +00:00
if (VersionUtils.INSTANCE.hasNougatMR()) {
configuration.setLocale(newLocale);
2020-04-24 18:11:14 +00:00
2020-10-06 08:46:04 +00:00
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
} else {
configuration.setLocale(newLocale);
2020-04-24 18:11:14 +00:00
}
context = context.createConfigurationContext(configuration);
2020-10-06 08:46:04 +00:00
return new LanguageContextWrapper(context);
}
}