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 com.android.build.gradle.internal.dsl.BuildType
|
||||||
import java.io.PrintStream
|
|
||||||
import java.nio.file.Paths
|
import java.nio.file.Paths
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id("org.gradle.idea")
|
|
||||||
id("com.android.application")
|
id("com.android.application")
|
||||||
id("androidx.navigation.safeargs")
|
id("androidx.navigation.safeargs")
|
||||||
|
id("dev.rikka.tools.autoresconfig")
|
||||||
}
|
}
|
||||||
|
|
||||||
val androidTargetSdkVersion: Int by rootProject.extra
|
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 optimizeReleaseRes = task("optimizeReleaseRes").doLast {
|
||||||
val aapt2 = File(
|
val aapt2 = File(
|
||||||
androidComponents.sdkComponents.sdkDirectory.get().asFile,
|
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 {
|
dependencies {
|
||||||
val glideVersion = "4.13.1"
|
val glideVersion = "4.13.1"
|
||||||
val navVersion: String by rootProject.extra
|
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
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
|
@ -221,7 +210,12 @@ public class App extends Application {
|
||||||
}
|
}
|
||||||
DayNightDelegate.setApplicationContext(this);
|
DayNightDelegate.setApplicationContext(this);
|
||||||
DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme());
|
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 intentFilter = new IntentFilter();
|
||||||
intentFilter.addAction("org.lsposed.manager.NOTIFICATION");
|
intentFilter.addAction("org.lsposed.manager.NOTIFICATION");
|
||||||
|
|
@ -288,11 +282,15 @@ public class App extends Application {
|
||||||
return okHttpCache;
|
return okHttpCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Locale getLocale() {
|
public static Locale getLocale(String tag) {
|
||||||
String tag = getPreferences().getString("language", null);
|
|
||||||
if (TextUtils.isEmpty(tag) || "SYSTEM".equals(tag)) {
|
if (TextUtils.isEmpty(tag) || "SYSTEM".equals(tag)) {
|
||||||
return Locale.getDefault();
|
return LocaleDelegate.getSystemLocale();
|
||||||
}
|
}
|
||||||
return Locale.forLanguageTag(tag);
|
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");
|
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");
|
SwitchPreference prefShowHiddenIcons = findPreference("show_hidden_icon_apps_enabled");
|
||||||
if (prefShowHiddenIcons != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
if (prefShowHiddenIcons != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
if (ConfigManager.isBinderAlive()) {
|
if (ConfigManager.isBinderAlive()) {
|
||||||
|
|
@ -270,7 +280,7 @@ public class SettingsFragment extends BaseFragment {
|
||||||
var tag = language.getValue();
|
var tag = language.getValue();
|
||||||
var userLocale = App.getLocale();
|
var userLocale = App.getLocale();
|
||||||
var entries = new ArrayList<CharSequence>();
|
var entries = new ArrayList<CharSequence>();
|
||||||
var lstLang = LangList.LANG_LIST;
|
var lstLang = LangList.LOCALES;
|
||||||
for (var lang : lstLang) {
|
for (var lang : lstLang) {
|
||||||
if (lang.equals(SYSTEM)) {
|
if (lang.equals(SYSTEM)) {
|
||||||
entries.add(getString(rikka.core.R.string.follow_system));
|
entries.add(getString(rikka.core.R.string.follow_system));
|
||||||
|
|
@ -287,6 +297,21 @@ public class SettingsFragment extends BaseFragment {
|
||||||
var locale = Locale.forLanguageTag(tag);
|
var locale = Locale.forLanguageTag(tag);
|
||||||
language.setSummary(!TextUtils.isEmpty(locale.getScript()) ? locale.getDisplayScript(userLocale) : locale.getDisplayName(userLocale));
|
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");
|
Preference translation = findPreference("translation");
|
||||||
|
|
@ -307,24 +332,6 @@ public class SettingsFragment extends BaseFragment {
|
||||||
translation_contributors.setSummary(translators);
|
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
|
@NonNull
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ buildscript {
|
||||||
val agpVersion by extra("7.1.2")
|
val agpVersion by extra("7.1.2")
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath("com.android.tools.build:gradle:$agpVersion")
|
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("org.eclipse.jgit:org.eclipse.jgit:6.0.0.202111291000-r")
|
||||||
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$navVersion")
|
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$navVersion")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue