Refine configuration change (#1672)
This commit is contained in:
parent
e3a7256f01
commit
306468afb4
|
|
@ -28,6 +28,7 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Build;
|
||||
import android.os.Looper;
|
||||
import android.os.Process;
|
||||
|
|
@ -192,6 +193,15 @@ public class App extends Application {
|
|||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void onConfigurationChanged(@NonNull Configuration newConfig) {
|
||||
LocaleDelegate.setDefaultLocale(getLocale());
|
||||
newConfig.setLocale(getLocale());
|
||||
getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
|
||||
super.onConfigurationChanged(newConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
|
@ -209,7 +219,7 @@ public class App extends Application {
|
|||
}
|
||||
DayNightDelegate.setApplicationContext(this);
|
||||
DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme());
|
||||
LocaleDelegate.setDefaultLocale(getLocale());
|
||||
onConfigurationChanged(getResources().getConfiguration());
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction("org.lsposed.manager.NOTIFICATION");
|
||||
|
|
|
|||
|
|
@ -239,16 +239,6 @@ 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()) {
|
||||
|
|
@ -277,20 +267,6 @@ public class SettingsFragment extends BaseFragment {
|
|||
|
||||
SimpleMenuPreference language = findPreference("language");
|
||||
if (language != null) {
|
||||
language.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
var locale = SYSTEM.equals(newValue) ? LocaleDelegate.getSystemLocale() : Locale.forLanguageTag((String) newValue);
|
||||
LocaleDelegate.setDefaultLocale(locale);
|
||||
var res = App.getInstance().getResources();
|
||||
var config = res.getConfiguration();
|
||||
config.setLocale(locale);
|
||||
var metric = res.getDisplayMetrics();
|
||||
App.getInstance().getResources().updateConfiguration(config, metric);
|
||||
MainActivity activity = (MainActivity) getActivity();
|
||||
if (activity != null) {
|
||||
activity.restart();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
var tag = language.getValue();
|
||||
var userLocale = App.getLocale();
|
||||
var entries = new ArrayList<CharSequence>();
|
||||
|
|
@ -331,6 +307,24 @@ 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
|
||||
|
|
|
|||
Loading…
Reference in New Issue