surround the system-locale-getter by try/catch to protect against errors, on some past versions or in the future; as a default, use Locale.getDefault() as in the past

This commit is contained in:
B. Petersen
2020-06-04 00:23:10 +02:00
parent 3fd04125a7
commit 04a98d515b
@@ -70,14 +70,18 @@ public class DynamicLanguage {
// Beware that Locale.getDefault() returns the locale the App was STARTED in, not the locale of the system.
// It just happens to be the same for the majority of use cases.
private static Locale getDefaultLocale() {
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
locale = Resources.getSystem().getConfiguration().locale;
Locale locale = null;
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
locale = Resources.getSystem().getConfiguration().locale;
}
} catch(Exception e) {
e.printStackTrace();
}
return locale;
return locale != null ? locale : Locale.getDefault();
}
public static Locale getSelectedLocale(Context context) {