Apply locale updates correctly for appcompat-v1.2.0.

Fixes #9736

See https://developer.android.com/jetpack/androidx/releases/appcompat#1.2.0
for how the code is "correctly" applying a new configuration.

Co-authored-by: Cody Henthorne <cody@signal.org>
This commit is contained in:
Fumiaki Yoshimatsu
2020-09-26 10:35:18 -04:00
committed by Cody Henthorne
parent 7e347f5cce
commit a7d672f6b4
8 changed files with 70 additions and 74 deletions

View File

@@ -1,40 +0,0 @@
package org.thoughtcrime.securesms.util.dynamiclanguage;
import android.app.Activity;
import androidx.annotation.MainThread;
import androidx.core.os.ConfigurationCompat;
import org.thoughtcrime.securesms.logging.Log;
import java.util.Locale;
public final class DynamicLanguageActivityHelper {
private static final String TAG = Log.tag(DynamicLanguageActivityHelper.class);
private static String reentryProtection;
/**
* If the activity isn't in the specified language, it will restart the activity.
*/
@MainThread
public static void recreateIfNotInCorrectLanguage(Activity activity, String language) {
Locale currentActivityLocale = ConfigurationCompat.getLocales(activity.getResources().getConfiguration()).get(0);
Locale selectedLocale = LocaleParser.findBestMatchingLocaleForLanguage(language);
if (currentActivityLocale.equals(selectedLocale)) {
reentryProtection = "";
return;
}
String reentryKey = activity.getClass().getName() + ":" + selectedLocale;
if (!reentryKey.equals(reentryProtection)) {
reentryProtection = reentryKey;
Log.d(TAG, String.format("Activity Locale %s, Selected locale %s, restarting", currentActivityLocale, selectedLocale));
activity.recreate();
} else {
Log.d(TAG, String.format("Skipping recreate as looks like looping, Activity Locale %s, Selected locale %s", currentActivityLocale, selectedLocale));
}
}
}

View File

@@ -2,7 +2,8 @@ package org.thoughtcrime.securesms.util.dynamiclanguage;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import java.util.Locale;
@@ -10,25 +11,19 @@ import java.util.Locale;
* Updates a context with an alternative language.
*/
public final class DynamicLanguageContextWrapper {
private DynamicLanguageContextWrapper() {}
public static Context updateContext(Context context, String language) {
final Locale newLocale = LocaleParser.findBestMatchingLocaleForLanguage(language);
public static void prepareOverrideConfiguration(Context context, Configuration base) {
String language = TextSecurePreferences.getLanguage(context);
Locale newLocale = LocaleParser.findBestMatchingLocaleForLanguage(language);
Locale.setDefault(newLocale);
final Resources resources = context.getResources();
final Configuration config = resources.getConfiguration();
final Configuration newConfig = copyWithNewLocale(config, newLocale);
resources.updateConfiguration(newConfig, resources.getDisplayMetrics());
return context;
base.setLocale(newLocale);
}
private static Configuration copyWithNewLocale(Configuration config, Locale locale) {
final Configuration copy = new Configuration(config);
copy.setLocale(locale);
return copy;
}
public static void updateContext(Context base) {
Configuration config = base.getResources().getConfiguration();
prepareOverrideConfiguration(base, config);
}
}