Refactor to lighten OsmandApplication

This commit is contained in:
cepprice 2021-02-14 13:41:30 +05:00
parent 9453a14fe4
commit 14ce40ddda
6 changed files with 95 additions and 72 deletions

View file

@ -36,7 +36,6 @@ import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask; import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask;
import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.AvoidSpecificRoads;
import net.osmand.plus.helpers.DayNightHelper; import net.osmand.plus.helpers.DayNightHelper;
import net.osmand.plus.helpers.LocaleHelper;
import net.osmand.plus.helpers.LockHelper; import net.osmand.plus.helpers.LockHelper;
import net.osmand.plus.helpers.WaypointHelper; import net.osmand.plus.helpers.WaypointHelper;
import net.osmand.plus.inapp.InAppPurchaseHelperImpl; import net.osmand.plus.inapp.InAppPurchaseHelperImpl;
@ -472,7 +471,6 @@ public class AppInitializer implements IProgress {
app.osmOAuthHelper = startupInit(new OsmOAuthHelper(app), OsmOAuthHelper.class); app.osmOAuthHelper = startupInit(new OsmOAuthHelper(app), OsmOAuthHelper.class);
app.oprAuthHelper = startupInit(new OprAuthHelper(app), OprAuthHelper.class); app.oprAuthHelper = startupInit(new OprAuthHelper(app), OprAuthHelper.class);
app.onlineRoutingHelper = startupInit(new OnlineRoutingHelper(app), OnlineRoutingHelper.class); app.onlineRoutingHelper = startupInit(new OnlineRoutingHelper(app), OnlineRoutingHelper.class);
app.localeHelper = startupInit(new LocaleHelper(app), LocaleHelper.class);
initOpeningHoursParser(); initOpeningHoursParser();
} }

View file

@ -166,12 +166,9 @@ public class OsmandApplication extends MultiDexApplication {
OprAuthHelper oprAuthHelper; OprAuthHelper oprAuthHelper;
MeasurementEditingContext measurementEditingContext; MeasurementEditingContext measurementEditingContext;
OnlineRoutingHelper onlineRoutingHelper; OnlineRoutingHelper onlineRoutingHelper;
LocaleHelper localeHelper; LocaleHelper localeHelper = new LocaleHelper(this);
private Resources localizedResources;
private Map<String, Builder> customRoutingConfigs = new ConcurrentHashMap<>(); private Map<String, Builder> customRoutingConfigs = new ConcurrentHashMap<>();
private Locale preferredLocale;
private Locale defaultLocale;
private File externalStorageDirectory; private File externalStorageDirectory;
private boolean externalStorageDirectoryReadOnly; private boolean externalStorageDirectoryReadOnly;
@ -220,7 +217,7 @@ public class OsmandApplication extends MultiDexApplication {
removeSqliteDbTravelFiles(); removeSqliteDbTravelFiles();
} }
checkPreferredLocale(); localeHelper.checkPreferredLocale();
appInitializer.onCreateApplication(); appInitializer.onCreateApplication();
// if(!osmandSettings.FOLLOW_THE_ROUTE.get()) { // if(!osmandSettings.FOLLOW_THE_ROUTE.get()) {
// targetPointsHelper.clearPointToNavigate(false); // targetPointsHelper.clearPointToNavigate(false);
@ -423,6 +420,7 @@ public class OsmandApplication extends MultiDexApplication {
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
Locale preferredLocale = localeHelper.getPreferredLocale();
if (preferredLocale != null && !newConfig.locale.getLanguage().equals(preferredLocale.getLanguage())) { if (preferredLocale != null && !newConfig.locale.getLanguage().equals(preferredLocale.getLanguage())) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
// ugly fix ! On devices after 4.0 screen is blinking when you rotate device! // ugly fix ! On devices after 4.0 screen is blinking when you rotate device!
@ -436,47 +434,6 @@ public class OsmandApplication extends MultiDexApplication {
} }
} }
public void checkPreferredLocale() {
Configuration config = getBaseContext().getResources().getConfiguration();
String pl = osmandSettings.PREFERRED_LOCALE.get();
String[] split = pl.split("_");
String lang = split[0];
String country = (split.length > 1) ? split[1] : "";
if (defaultLocale == null) {
defaultLocale = Locale.getDefault();
}
if (!Algorithms.isEmpty(lang)) {
if (!Algorithms.isEmpty(country)) {
preferredLocale = new Locale(lang, country);
} else {
preferredLocale = new Locale(lang);
}
}
Locale selectedLocale = null;
if (!Algorithms.isEmpty(lang) && !config.locale.equals(preferredLocale)) {
selectedLocale = preferredLocale;
} else if (Algorithms.isEmpty(lang) && defaultLocale != null && Locale.getDefault() != defaultLocale) {
selectedLocale = defaultLocale;
preferredLocale = null;
}
if (selectedLocale != null) {
Locale.setDefault(selectedLocale);
config.locale = selectedLocale;
config.setLayoutDirection(selectedLocale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
if (android.os.Build.VERSION.SDK_INT >= 17) {
Configuration conf = new Configuration(config);
conf.locale = selectedLocale;
localizedResources = createConfigurationContext(conf).getResources();
}
}
}
public static final int PROGRESS_DIALOG = 5; public static final int PROGRESS_DIALOG = 5;
public void checkApplicationIsBeingInitialized(Activity activity, AppInitializeListener listener) { public void checkApplicationIsBeingInitialized(Activity activity, AppInitializeListener listener) {
@ -828,7 +785,7 @@ public class OsmandApplication extends MultiDexApplication {
themeResId = R.style.OsmandLightTheme; themeResId = R.style.OsmandLightTheme;
} }
} }
setLanguage(c); localeHelper.setLanguage(c);
c.setTheme(themeResId); c.setTheme(themeResId);
} }
@ -855,25 +812,9 @@ public class OsmandApplication extends MultiDexApplication {
return s; return s;
} }
public void setLanguage(Context context) {
if (preferredLocale != null) {
Configuration config = context.getResources().getConfiguration();
String lang = preferredLocale.getLanguage();
if (!Algorithms.isEmpty(lang) && !config.locale.getLanguage().equals(lang)) {
preferredLocale = new Locale(lang);
Locale.setDefault(preferredLocale);
config.locale = preferredLocale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
} else if (Algorithms.isEmpty(lang) && defaultLocale != null && Locale.getDefault() != defaultLocale) {
Locale.setDefault(defaultLocale);
config.locale = defaultLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
}
public String getCountry() { public String getCountry() {
String country; String country;
Locale preferredLocale = localeHelper.getPreferredLocale();
if (preferredLocale != null) { if (preferredLocale != null) {
country = preferredLocale.getCountry(); country = preferredLocale.getCountry();
} else { } else {
@ -884,6 +825,7 @@ public class OsmandApplication extends MultiDexApplication {
public String getLanguage() { public String getLanguage() {
String lang; String lang;
Locale preferredLocale = localeHelper.getPreferredLocale();
if (preferredLocale != null) { if (preferredLocale != null) {
lang = preferredLocale.getLanguage(); lang = preferredLocale.getLanguage();
} else { } else {
@ -897,11 +839,12 @@ public class OsmandApplication extends MultiDexApplication {
@Override @Override
public AssetManager getAssets() { public AssetManager getAssets() {
return localizedResources != null ? localizedResources.getAssets() : super.getAssets(); return getResources() != null ? getResources().getAssets() : super.getAssets();
} }
@Override @Override
public Resources getResources() { public Resources getResources() {
Resources localizedResources = localeHelper.getLocalizedResources();
return localizedResources != null ? localizedResources : super.getResources(); return localizedResources != null ? localizedResources : super.getResources();
} }

View file

@ -1,10 +1,21 @@
package net.osmand.plus.helpers; package net.osmand.plus.helpers;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import net.osmand.StateChangedListener; import net.osmand.StateChangedListener;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.util.Algorithms;
import java.util.Locale;
public class LocaleHelper implements StateChangedListener<String> { public class LocaleHelper implements StateChangedListener<String> {
private Resources localizedResources;
private Locale preferredLocale;
private Locale defaultLocale;
private boolean needRestart = false; private boolean needRestart = false;
private OsmandApplication app; private OsmandApplication app;
@ -13,6 +24,65 @@ public class LocaleHelper implements StateChangedListener<String> {
this.app = app; this.app = app;
} }
public void checkPreferredLocale() {
Configuration config = app.getBaseContext().getResources().getConfiguration();
String pl = app.getSettings().PREFERRED_LOCALE.get();
String[] split = pl.split("_");
String lang = split[0];
String country = (split.length > 1) ? split[1] : "";
if (defaultLocale == null) {
defaultLocale = Locale.getDefault();
}
if (!Algorithms.isEmpty(lang)) {
if (!Algorithms.isEmpty(country)) {
preferredLocale = new Locale(lang, country);
} else {
preferredLocale = new Locale(lang);
}
}
Locale selectedLocale = null;
if (!Algorithms.isEmpty(lang) && !config.locale.equals(preferredLocale)) {
selectedLocale = preferredLocale;
} else if (Algorithms.isEmpty(lang) && defaultLocale != null && Locale.getDefault() != defaultLocale) {
selectedLocale = defaultLocale;
preferredLocale = null;
}
if (selectedLocale != null) {
Locale.setDefault(selectedLocale);
config.locale = selectedLocale;
config.setLayoutDirection(selectedLocale);
Resources resources = app.getBaseContext().getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
if (android.os.Build.VERSION.SDK_INT >= 17) {
Configuration conf = new Configuration(config);
conf.locale = selectedLocale;
localizedResources = app.createConfigurationContext(conf).getResources();
}
}
}
public void setLanguage(Context context) {
if (preferredLocale != null) {
Configuration config = context.getResources().getConfiguration();
String lang = preferredLocale.getLanguage();
if (!Algorithms.isEmpty(lang) && !config.locale.getLanguage().equals(lang)) {
preferredLocale = new Locale(lang);
Locale.setDefault(preferredLocale);
config.locale = preferredLocale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
} else if (Algorithms.isEmpty(lang) && defaultLocale != null && Locale.getDefault() != defaultLocale) {
Locale.setDefault(defaultLocale);
config.locale = defaultLocale;
Resources resources = app.getBaseContext().getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
}
}
public void listenLocaleChanges() { public void listenLocaleChanges() {
app.getSettings().PREFERRED_LOCALE.addListener(this); app.getSettings().PREFERRED_LOCALE.addListener(this);
} }
@ -29,9 +99,21 @@ public class LocaleHelper implements StateChangedListener<String> {
this.needRestart = needRestart; this.needRestart = needRestart;
} }
public Resources getLocalizedResources() {
return localizedResources;
}
public Locale getPreferredLocale() {
return preferredLocale;
}
public Locale getDefaultLocale() {
return defaultLocale;
}
@Override @Override
public void stateChanged(String change) { public void stateChanged(String change) {
needRestart = true; needRestart = true;
app.checkPreferredLocale(); checkPreferredLocale();
} }
} }

View file

@ -108,7 +108,7 @@ public class GlobalSettingsFragment extends BaseSettingsFragment
Activity activity = getActivity(); Activity activity = getActivity();
OsmandApplication app = getMyApplication(); OsmandApplication app = getMyApplication();
if (app != null && activity != null) { if (app != null && activity != null) {
app.checkPreferredLocale(); app.getLocaleHelper().checkPreferredLocale();
app.restartApp(activity); app.restartApp(activity);
} }
} else if (prefId.equals(settings.SPEED_CAMERAS_UNINSTALLED.getId())) { } else if (prefId.equals(settings.SPEED_CAMERAS_UNINSTALLED.getId())) {

View file

@ -40,7 +40,7 @@ public class WebViewEx extends WebView {
// also see: https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce // also see: https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
OsmandApplication app = (OsmandApplication) ctx.getApplicationContext(); OsmandApplication app = (OsmandApplication) ctx.getApplicationContext();
app.checkPreferredLocale(); app.getLocaleHelper().checkPreferredLocale();
ctx.getResources().updateConfiguration( ctx.getResources().updateConfiguration(
new Configuration(app.getResources().getConfiguration()), new Configuration(app.getResources().getConfiguration()),
ctx.getResources().getDisplayMetrics()); ctx.getResources().getDisplayMetrics());

View file

@ -71,7 +71,7 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
nightMode = !settings.isLightContent(); nightMode = !settings.isLightContent();
int themeId = nightMode ? R.style.OsmandDarkTheme_NoActionbar : R.style.OsmandLightTheme_NoActionbar_LightStatusBar; int themeId = nightMode ? R.style.OsmandDarkTheme_NoActionbar : R.style.OsmandLightTheme_NoActionbar_LightStatusBar;
app.setLanguage(this); app.getLocaleHelper().setLanguage(this);
setTheme(themeId); setTheme(themeId);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);