From 3fd04125a74128619ca0559c17926b320f259059 Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Wed, 3 Jun 2020 16:16:27 +0200 Subject: [PATCH 1/3] fixes 1209 Language.getDefault() does not do what one expects it to. --- .../securesms/util/DynamicLanguage.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/util/DynamicLanguage.java b/src/org/thoughtcrime/securesms/util/DynamicLanguage.java index 83643fd56..66a3e440e 100644 --- a/src/org/thoughtcrime/securesms/util/DynamicLanguage.java +++ b/src/org/thoughtcrime/securesms/util/DynamicLanguage.java @@ -5,6 +5,8 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import androidx.annotation.RequiresApi; @@ -65,11 +67,24 @@ public class DynamicLanguage { return activity.getResources().getConfiguration().locale; } + // 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; + } + return locale; + } + public static Locale getSelectedLocale(Context context) { String language[] = TextUtils.split(Prefs.getLanguage(context), "_"); if (language[0].equals(DEFAULT)) { - return Locale.getDefault(); + return getDefaultLocale(); } else if (language.length == 2) { return new Locale(language[0], language[1]); } else { From 04a98d515ba964dab8b01fa27bdbc2ce52530b70 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Thu, 4 Jun 2020 00:23:10 +0200 Subject: [PATCH 2/3] 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 --- .../securesms/util/DynamicLanguage.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/util/DynamicLanguage.java b/src/org/thoughtcrime/securesms/util/DynamicLanguage.java index 66a3e440e..8dfdc6d2d 100644 --- a/src/org/thoughtcrime/securesms/util/DynamicLanguage.java +++ b/src/org/thoughtcrime/securesms/util/DynamicLanguage.java @@ -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) { From 353d03fba3035815d4dda7c4235a4388569fc35b Mon Sep 17 00:00:00 2001 From: Angelo Fuchs Date: Fri, 5 Jun 2020 18:07:37 +0200 Subject: [PATCH 3/3] #1209 cleanup of Exception and using prebuilt compat instead of writing code ourselves. --- .../securesms/util/DynamicLanguage.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/util/DynamicLanguage.java b/src/org/thoughtcrime/securesms/util/DynamicLanguage.java index 8dfdc6d2d..0dda20f17 100644 --- a/src/org/thoughtcrime/securesms/util/DynamicLanguage.java +++ b/src/org/thoughtcrime/securesms/util/DynamicLanguage.java @@ -10,12 +10,17 @@ import android.os.Build; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import androidx.annotation.RequiresApi; +import androidx.core.os.ConfigurationCompat; + import android.text.TextUtils; +import android.util.Log; import java.util.Locale; public class DynamicLanguage { + private static final String TAG = DynamicLanguage.class.getSimpleName(); + private static final String DEFAULT = "zz"; private Locale currentLocale; @@ -72,14 +77,9 @@ public class DynamicLanguage { private static Locale getDefaultLocale() { 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; - } + locale = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0); } catch(Exception e) { - e.printStackTrace(); + Log.e(TAG, "could not determine the system locale.", e); } return locale != null ? locale : Locale.getDefault(); }