Fix language switch (#1741)
This commit is contained in:
parent
6fff15e968
commit
dc6a599f00
|
|
@ -18,15 +18,13 @@
|
|||
*/
|
||||
|
||||
import com.android.build.gradle.internal.dsl.BuildType
|
||||
import java.io.PrintStream
|
||||
import java.nio.file.Paths
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
|
||||
plugins {
|
||||
id("org.gradle.idea")
|
||||
id("com.android.application")
|
||||
id("androidx.navigation.safeargs")
|
||||
id("dev.rikka.tools.autoresconfig")
|
||||
}
|
||||
|
||||
val androidTargetSdkVersion: Int by rootProject.extra
|
||||
|
|
@ -123,6 +121,13 @@ android {
|
|||
}
|
||||
}
|
||||
|
||||
autoResConfig {
|
||||
generateClass.set(true)
|
||||
generateRes.set(false)
|
||||
generatedClassFullName.set("org.lsposed.manager.util.LangList")
|
||||
generatedArrayFirstItem.set("SYSTEM")
|
||||
}
|
||||
|
||||
val optimizeReleaseRes = task("optimizeReleaseRes").doLast {
|
||||
val aapt2 = File(
|
||||
androidComponents.sdkComponents.sdkDirectory.get().asFile,
|
||||
|
|
@ -158,38 +163,6 @@ tasks.whenTaskAdded {
|
|||
}
|
||||
}
|
||||
|
||||
afterEvaluate {
|
||||
android.applicationVariants.forEach { variant ->
|
||||
val outSrcDir = file("$buildDir/generated/source/langList/${variant.name}")
|
||||
val outSrc = file("$outSrcDir/org/lsposed/manager/util/LangList.java")
|
||||
val genLangList =
|
||||
tasks.register("generate${variant.name.capitalize(Locale.ROOT)}LangList") {
|
||||
inputs.files("src/main/res")
|
||||
outputs.file(outSrc)
|
||||
doLast {
|
||||
val langList = File(projectDir, "src/main/res").listFiles { dir ->
|
||||
dir.name.startsWith("values-") && File(dir, "strings.xml").exists()
|
||||
}.orEmpty().sorted().map {
|
||||
it.name.substring(7).split("-", limit = 2)
|
||||
}.map {
|
||||
if (it.size == 1) Locale(it[0])
|
||||
else Locale(it[0], it[1].substring(1))
|
||||
}.map { it.toLanguageTag() }
|
||||
PrintStream(outSrc).print(
|
||||
"""
|
||||
|package org.lsposed.manager.util;
|
||||
|public final class LangList {
|
||||
| public static final String[] LANG_LIST = {"SYSTEM", ${
|
||||
langList.joinToString(", ") { """"$it"""" }
|
||||
}};
|
||||
|}""".trimMargin()
|
||||
)
|
||||
}
|
||||
}
|
||||
variant.registerJavaGeneratingTask(genLangList, outSrcDir)
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
val glideVersion = "4.13.1"
|
||||
val navVersion: String by rootProject.extra
|
||||
|
|
|
|||
|
|
@ -193,17 +193,6 @@ public class App extends Application {
|
|||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void onConfigurationChanged(@NonNull Configuration newConfig) {
|
||||
if (getResources().getConfiguration().diff(newConfig) != 0) {
|
||||
LocaleDelegate.setDefaultLocale(getLocale());
|
||||
newConfig.setLocale(getLocale());
|
||||
getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
|
||||
}
|
||||
super.onConfigurationChanged(newConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
|
@ -221,7 +210,12 @@ public class App extends Application {
|
|||
}
|
||||
DayNightDelegate.setApplicationContext(this);
|
||||
DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme());
|
||||
onConfigurationChanged(getResources().getConfiguration());
|
||||
LocaleDelegate.setDefaultLocale(getLocale());
|
||||
var res = getResources();
|
||||
var config = res.getConfiguration();
|
||||
config.setLocale(LocaleDelegate.getDefaultLocale());
|
||||
//noinspection deprecation
|
||||
res.updateConfiguration(config, res.getDisplayMetrics());
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction("org.lsposed.manager.NOTIFICATION");
|
||||
|
|
@ -288,11 +282,15 @@ public class App extends Application {
|
|||
return okHttpCache;
|
||||
}
|
||||
|
||||
public static Locale getLocale() {
|
||||
String tag = getPreferences().getString("language", null);
|
||||
public static Locale getLocale(String tag) {
|
||||
if (TextUtils.isEmpty(tag) || "SYSTEM".equals(tag)) {
|
||||
return Locale.getDefault();
|
||||
return LocaleDelegate.getSystemLocale();
|
||||
}
|
||||
return Locale.forLanguageTag(tag);
|
||||
}
|
||||
|
||||
public static Locale getLocale() {
|
||||
String tag = getPreferences().getString("language", null);
|
||||
return getLocale(tag);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -239,6 +239,16 @@ public class SettingsFragment extends BaseFragment {
|
|||
}
|
||||
|
||||
Preference primary_color = findPreference("theme_color");
|
||||
if (primary_color != null) {
|
||||
primary_color.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
MainActivity activity = (MainActivity) getActivity();
|
||||
if (activity != null) {
|
||||
activity.restart();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
SwitchPreference prefShowHiddenIcons = findPreference("show_hidden_icon_apps_enabled");
|
||||
if (prefShowHiddenIcons != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
if (ConfigManager.isBinderAlive()) {
|
||||
|
|
@ -270,7 +280,7 @@ public class SettingsFragment extends BaseFragment {
|
|||
var tag = language.getValue();
|
||||
var userLocale = App.getLocale();
|
||||
var entries = new ArrayList<CharSequence>();
|
||||
var lstLang = LangList.LANG_LIST;
|
||||
var lstLang = LangList.LOCALES;
|
||||
for (var lang : lstLang) {
|
||||
if (lang.equals(SYSTEM)) {
|
||||
entries.add(getString(rikka.core.R.string.follow_system));
|
||||
|
|
@ -287,6 +297,21 @@ public class SettingsFragment extends BaseFragment {
|
|||
var locale = Locale.forLanguageTag(tag);
|
||||
language.setSummary(!TextUtils.isEmpty(locale.getScript()) ? locale.getDisplayScript(userLocale) : locale.getDisplayName(userLocale));
|
||||
}
|
||||
language.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
var app = App.getInstance();
|
||||
var locale = App.getLocale((String)newValue);
|
||||
var res = app.getResources();
|
||||
var config = res.getConfiguration();
|
||||
config.setLocale(locale);
|
||||
LocaleDelegate.setDefaultLocale(locale);
|
||||
//noinspection deprecation
|
||||
res.updateConfiguration(config, res.getDisplayMetrics());
|
||||
MainActivity activity = (MainActivity) getActivity();
|
||||
if (activity != null) {
|
||||
activity.restart();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
Preference translation = findPreference("translation");
|
||||
|
|
@ -307,24 +332,6 @@ public class SettingsFragment extends BaseFragment {
|
|||
translation_contributors.setSummary(translators);
|
||||
}
|
||||
}
|
||||
|
||||
App.getPreferences().registerOnSharedPreferenceChangeListener((sharedPreferences, key) -> {
|
||||
var newValue = sharedPreferences.getAll().getOrDefault(key, null);
|
||||
if (newValue == null) return;
|
||||
switch (key) {
|
||||
case "language":
|
||||
var app = App.getInstance();
|
||||
var config = app.getResources().getConfiguration();
|
||||
app.onConfigurationChanged(config);
|
||||
case "theme_color":
|
||||
case "follow_system_accent":
|
||||
MainActivity activity = (MainActivity) getActivity();
|
||||
if (activity != null) {
|
||||
activity.restart();
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ buildscript {
|
|||
val agpVersion by extra("7.1.2")
|
||||
dependencies {
|
||||
classpath("com.android.tools.build:gradle:$agpVersion")
|
||||
classpath("dev.rikka.tools.autoresconfig:gradle-plugin:1.0.0")
|
||||
classpath("org.eclipse.jgit:org.eclipse.jgit:6.0.0.202111291000-r")
|
||||
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$navVersion")
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue