Skip to content

Commit ae3fefd

Browse files
committed
Fix locale-related warnings + allow variants in language tags
1 parent 476203d commit ae3fefd

File tree

5 files changed

+99
-52
lines changed

5 files changed

+99
-52
lines changed

app/src/main/java/org/thunderdog/challegram/U.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,7 @@ public static int getVideoRotation (String path) {
25292529

25302530
public static String getUsefulMetadata (@Nullable Tdlib tdlib) {
25312531
AppBuildInfo buildInfo = org.thunderdog.challegram.unsorted.Settings.instance().getCurrentBuildInformation();
2532-
String locale = UI.getAppContext().getResources().getConfiguration().locale.toString();
2532+
String locale = Lang.getConfigurationLocale().toString();
25332533
String appLocale = Lang.locale().toString();
25342534
String metadata = Lang.getAppBuildAndVersion(tdlib) + " (" + BuildConfig.COMMIT + ")\n" +
25352535
(!buildInfo.getPullRequests().isEmpty() ? "PRs: " + buildInfo.pullRequestsList() + "\n" : "") +
@@ -2685,7 +2685,7 @@ public static boolean awaitLatch (CountDownLatch latch, long time, TimeUnit unit
26852685

26862686
public static Locale getDisplayLocaleOfSubtypeLocale (@NonNull final String localeString) {
26872687
if (NO_LANGUAGE.equals(localeString)) {
2688-
return UI.getResources().getConfiguration().locale;
2688+
return Lang.getPrimaryLocale(UI.getResources().getConfiguration());
26892689
}
26902690
return constructLocaleFromString(localeString);
26912691
}
@@ -2696,16 +2696,7 @@ public static Locale constructLocaleFromString (@Nullable final String localeStr
26962696
// TODO: Should this be Locale.ROOT?
26972697
return null;
26982698
}
2699-
final String[] localeParams = localeStr.split("_", 3);
2700-
if (localeParams.length == 1) {
2701-
return new Locale(localeParams[0]);
2702-
} else if (localeParams.length == 2) {
2703-
return new Locale(localeParams[0], localeParams[1]);
2704-
} else if (localeParams.length == 3) {
2705-
return new Locale(localeParams[0], localeParams[1], localeParams[2]);
2706-
}
2707-
// TODO: Should return Locale.ROOT instead of null?
2708-
return null;
2699+
return Lang.obtainLocale(localeStr);
27092700
}
27102701

27112702
public static @Nullable Bitmap tryDecodeVideoThumb (String path, long timeUs, int dstWidth, int dstHeight, @Nullable int[] rotation) {
@@ -3773,7 +3764,7 @@ public static String[] getInputLanguages () {
37733764
inputLanguages.add(code);
37743765
}
37753766
} else {
3776-
String code = LocaleUtils.toBcp47Language(Resources.getSystem().getConfiguration().locale);
3767+
String code = LocaleUtils.toBcp47Language(Lang.getPrimaryLocale(Resources.getSystem().getConfiguration()));
37773768
if (!StringUtils.isEmpty(code)) {
37783769
inputLanguages.add(code);
37793770
}
@@ -3795,12 +3786,18 @@ private static String toLanguageCode (InputMethodSubtype ims) {
37953786
return languageTag;
37963787
}
37973788
}
3798-
String locale = ims.getLocale();
3799-
if (!StringUtils.isEmpty(locale)) {
3800-
Locale l = U.getDisplayLocaleOfSubtypeLocale(locale);
3801-
if (l != null) {
3802-
return LocaleUtils.toBcp47Language(l);
3803-
}
3789+
return toLanguageCodePreNougat(ims);
3790+
}
3791+
return null;
3792+
}
3793+
3794+
@SuppressWarnings("deprecation")
3795+
private static String toLanguageCodePreNougat (InputMethodSubtype ims) {
3796+
String locale = ims.getLocale();
3797+
if (!StringUtils.isEmpty(locale)) {
3798+
Locale l = U.getDisplayLocaleOfSubtypeLocale(locale);
3799+
if (l != null) {
3800+
return LocaleUtils.toBcp47Language(l);
38043801
}
38053802
}
38063803
return null;

app/src/main/java/org/thunderdog/challegram/core/Lang.java

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.thunderdog.challegram.core;
1616

1717
import android.content.Context;
18+
import android.content.res.Configuration;
1819
import android.content.res.Resources;
1920
import android.os.Build;
2021
import android.text.Spannable;
@@ -3427,20 +3428,89 @@ public static void checkLanguageSettings () {
34273428
checkLanguageSettings(true);
34283429
}
34293430

3431+
@SuppressWarnings("deprecation")
3432+
public static Locale getPrimaryLocale (Configuration configuration) {
3433+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
3434+
android.os.LocaleList list = configuration.getLocales();
3435+
return list.get(0);
3436+
} else {
3437+
return configuration.locale;
3438+
}
3439+
}
3440+
3441+
public static Locale getSystemLocale () {
3442+
Configuration configuration = Resources.getSystem().getConfiguration();
3443+
return getPrimaryLocale(configuration);
3444+
}
3445+
3446+
public static Locale getConfigurationLocale () {
3447+
Configuration configuration = UI.getAppContext().getResources().getConfiguration();
3448+
return getPrimaryLocale(configuration);
3449+
}
3450+
3451+
public static Locale obtainLocale (String languageTag) {
3452+
String language = Lang.cleanLanguageCode(languageTag);
3453+
String country;
3454+
if (languageTag.length() > language.length()) {
3455+
country = Lang.cleanLanguageCode(languageTag.substring(language.length() + 1));
3456+
} else {
3457+
country = null;
3458+
}
3459+
String variant;
3460+
if (!StringUtils.isEmpty(country) && languageTag.length() > country.length() + language.length() + 2) {
3461+
variant = Lang.cleanLanguageCode(languageTag.substring(language.length() + country.length() + 2));
3462+
} else {
3463+
variant = null;
3464+
}
3465+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA) {
3466+
if (!StringUtils.isEmpty(variant)) {
3467+
return Locale.of(language, country, variant);
3468+
} else if (!StringUtils.isEmpty(country)) {
3469+
return Locale.of(language, country);
3470+
} else {
3471+
return Locale.of(language);
3472+
}
3473+
} else {
3474+
return obtainLocalePreBaklava(languageTag, language, country, variant);
3475+
}
3476+
}
3477+
3478+
@SuppressWarnings("deprecation")
3479+
private static Locale obtainLocalePreBaklava (String languageTag, String language, String country, String variant) {
3480+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
3481+
try {
3482+
Locale.Builder b = new Locale.Builder()
3483+
.setLanguage(language);
3484+
if (!StringUtils.isEmpty(country)) {
3485+
b.setRegion(country);
3486+
}
3487+
if (!StringUtils.isEmpty(variant)) {
3488+
b.setVariant(variant);
3489+
}
3490+
return b.build();
3491+
} catch (RuntimeException illformedLocaleException) {
3492+
return Locale.forLanguageTag(languageTag);
3493+
}
3494+
} else if (!StringUtils.isEmpty(variant)) {
3495+
return new Locale(language, country,variant);
3496+
} else if (!StringUtils.isEmpty(country)) {
3497+
return new Locale(language, country);
3498+
} else {
3499+
return new Locale(language);
3500+
}
3501+
}
3502+
34303503
private static void checkLanguageSettings (boolean sendEvents) {
34313504
setLanguageAllowLowercase(!"1".equals(Lang.getString(R.string.language_disable_lowercase)), sendEvents);
34323505
setLanguageRtl(Settings.instance().needRtl(packId(), getLanguageDirection() == LANGUAGE_DIRECTION_RTL), sendEvents);
34333506
Locale dateLocale = null;
34343507
String dateFormatLocale = Lang.getString(R.string.language_dateFormatLocale);
34353508
if (!StringUtils.isEmpty(dateFormatLocale) && !"0".equals(dateFormatLocale)) {
34363509
try {
3437-
String language = Lang.cleanLanguageCode(dateFormatLocale);
3438-
if (language.length() == dateFormatLocale.length()) {
3439-
dateLocale = new Locale(language);
3440-
} else {
3441-
dateLocale = new Locale(language, Lang.cleanLanguageCode(dateFormatLocale.substring(language.length() + 1)));
3442-
}
3443-
} catch (Throwable ignored) { }
3510+
dateLocale = obtainLocale(dateFormatLocale);
3511+
} catch (Throwable t) {
3512+
Log.v("Unable to obtain locale for tag %s", t, dateFormatLocale);
3513+
}
34443514
}
34453515
setDateLocale(dateLocale, sendEvents);
34463516
languageSettingsLoaded = true;

app/src/main/java/org/thunderdog/challegram/telegram/TdlibManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.thunderdog.challegram.config.Config;
4141
import org.thunderdog.challegram.core.Background;
4242
import org.thunderdog.challegram.core.BaseThread;
43+
import org.thunderdog.challegram.core.Lang;
4344
import org.thunderdog.challegram.core.WatchDog;
4445
import org.thunderdog.challegram.core.WatchDogContext;
4546
import org.thunderdog.challegram.data.TD;
@@ -2189,7 +2190,7 @@ public static String getSystemVersion () {
21892190
public static String getSystemLanguageCode () {
21902191
String languageCode = "en-US";
21912192
try {
2192-
languageCode = LocaleUtils.toBcp47Language(UI.getConfigurationLocale());
2193+
languageCode = LocaleUtils.toBcp47Language(Lang.getConfigurationLocale());
21932194
} catch (Throwable ignored) { }
21942195
return languageCode;
21952196
}

app/src/main/java/org/thunderdog/challegram/telegram/TdlibSettingsManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,9 @@ public int chatStyle () {
526526
try {
527527
String language = Locale.getDefault().getLanguage();
528528
if (!StringUtils.isEmpty(language)) {
529-
if (language.equals(new Locale("ja").getLanguage()) ||
530-
language.equals(new Locale("ko").getLanguage()) ||
531-
language.equals(new Locale("zh").getLanguage())) {
529+
if (language.equals(Locale.JAPANESE.getLanguage()) ||
530+
language.equals(Locale.KOREAN.getLanguage()) ||
531+
language.equals(Locale.CHINESE.getLanguage())) {
532532
defaultStyle = ThemeManager.CHAT_STYLE_MODERN;
533533
}
534534
}

app/src/main/java/org/thunderdog/challegram/tool/UI.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import androidx.annotation.Nullable;
3838
import androidx.annotation.StringRes;
3939
import androidx.core.content.ContextCompat;
40-
import androidx.core.view.WindowCompat;
4140

4241
import org.drinkless.tdlib.TdApi;
4342
import org.thunderdog.challegram.BaseActivity;
@@ -432,26 +431,6 @@ public static Resources getResources () {
432431
return appContext.getResources();
433432
}
434433

435-
@SuppressWarnings("deprecation")
436-
public static Locale getLocale (Configuration configuration) {
437-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
438-
android.os.LocaleList list = configuration.getLocales();
439-
return list.get(0);
440-
} else {
441-
return configuration.locale;
442-
}
443-
}
444-
445-
public static Locale getSystemLocale () {
446-
Configuration configuration = Resources.getSystem().getConfiguration();
447-
return getLocale(configuration);
448-
}
449-
450-
public static Locale getConfigurationLocale () {
451-
Configuration configuration = getAppContext().getResources().getConfiguration();
452-
return getLocale(configuration);
453-
}
454-
455434
public static void removePendingRunnable (Runnable runnable) {
456435
getAppHandler().removeCallbacks(runnable);
457436
}
@@ -859,7 +838,7 @@ public static String[] getInputLanguages () {
859838
inputLanguages.add(code);
860839
}
861840
} else {
862-
String code = LocaleUtils.toBcp47Language(getSystemLocale());
841+
String code = LocaleUtils.toBcp47Language(Lang.getSystemLocale());
863842
if (!StringUtils.isEmpty(code)) {
864843
inputLanguages.add(code);
865844
}

0 commit comments

Comments
 (0)