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;
|
2021-09-08 18:30:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-09-08 18:30:20 +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
|
|
|
|
2021-09-08 18:30:20 +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);
|
2020-04-24 18:11:14 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
context = context.createConfigurationContext(configuration);
|
2020-04-24 18:11:14 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
} else if (VersionUtils.INSTANCE.hasLollipop()) {
|
|
|
|
configuration.setLocale(newLocale);
|
|
|
|
context = context.createConfigurationContext(configuration);
|
2020-04-24 18:11:14 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
} else {
|
|
|
|
configuration.locale = newLocale;
|
|
|
|
res.updateConfiguration(configuration, res.getDisplayMetrics());
|
2020-04-24 18:11:14 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
|
|
|
|
return new LanguageContextWrapper(context);
|
|
|
|
}
|
|
|
|
}
|