Refactor to lighten OsmandApplication
This commit is contained in:
parent
9453a14fe4
commit
14ce40ddda
6 changed files with 95 additions and 72 deletions
|
@ -36,7 +36,6 @@ import net.osmand.plus.download.DownloadActivity;
|
|||
import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask;
|
||||
import net.osmand.plus.helpers.AvoidSpecificRoads;
|
||||
import net.osmand.plus.helpers.DayNightHelper;
|
||||
import net.osmand.plus.helpers.LocaleHelper;
|
||||
import net.osmand.plus.helpers.LockHelper;
|
||||
import net.osmand.plus.helpers.WaypointHelper;
|
||||
import net.osmand.plus.inapp.InAppPurchaseHelperImpl;
|
||||
|
@ -472,7 +471,6 @@ public class AppInitializer implements IProgress {
|
|||
app.osmOAuthHelper = startupInit(new OsmOAuthHelper(app), OsmOAuthHelper.class);
|
||||
app.oprAuthHelper = startupInit(new OprAuthHelper(app), OprAuthHelper.class);
|
||||
app.onlineRoutingHelper = startupInit(new OnlineRoutingHelper(app), OnlineRoutingHelper.class);
|
||||
app.localeHelper = startupInit(new LocaleHelper(app), LocaleHelper.class);
|
||||
|
||||
initOpeningHoursParser();
|
||||
}
|
||||
|
|
|
@ -166,12 +166,9 @@ public class OsmandApplication extends MultiDexApplication {
|
|||
OprAuthHelper oprAuthHelper;
|
||||
MeasurementEditingContext measurementEditingContext;
|
||||
OnlineRoutingHelper onlineRoutingHelper;
|
||||
LocaleHelper localeHelper;
|
||||
LocaleHelper localeHelper = new LocaleHelper(this);
|
||||
|
||||
private Resources localizedResources;
|
||||
private Map<String, Builder> customRoutingConfigs = new ConcurrentHashMap<>();
|
||||
private Locale preferredLocale;
|
||||
private Locale defaultLocale;
|
||||
private File externalStorageDirectory;
|
||||
private boolean externalStorageDirectoryReadOnly;
|
||||
|
||||
|
@ -220,7 +217,7 @@ public class OsmandApplication extends MultiDexApplication {
|
|||
removeSqliteDbTravelFiles();
|
||||
}
|
||||
|
||||
checkPreferredLocale();
|
||||
localeHelper.checkPreferredLocale();
|
||||
appInitializer.onCreateApplication();
|
||||
// if(!osmandSettings.FOLLOW_THE_ROUTE.get()) {
|
||||
// targetPointsHelper.clearPointToNavigate(false);
|
||||
|
@ -423,6 +420,7 @@ public class OsmandApplication extends MultiDexApplication {
|
|||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
Locale preferredLocale = localeHelper.getPreferredLocale();
|
||||
if (preferredLocale != null && !newConfig.locale.getLanguage().equals(preferredLocale.getLanguage())) {
|
||||
super.onConfigurationChanged(newConfig);
|
||||
// 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 void checkApplicationIsBeingInitialized(Activity activity, AppInitializeListener listener) {
|
||||
|
@ -828,7 +785,7 @@ public class OsmandApplication extends MultiDexApplication {
|
|||
themeResId = R.style.OsmandLightTheme;
|
||||
}
|
||||
}
|
||||
setLanguage(c);
|
||||
localeHelper.setLanguage(c);
|
||||
c.setTheme(themeResId);
|
||||
}
|
||||
|
||||
|
@ -855,25 +812,9 @@ public class OsmandApplication extends MultiDexApplication {
|
|||
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() {
|
||||
String country;
|
||||
Locale preferredLocale = localeHelper.getPreferredLocale();
|
||||
if (preferredLocale != null) {
|
||||
country = preferredLocale.getCountry();
|
||||
} else {
|
||||
|
@ -884,6 +825,7 @@ public class OsmandApplication extends MultiDexApplication {
|
|||
|
||||
public String getLanguage() {
|
||||
String lang;
|
||||
Locale preferredLocale = localeHelper.getPreferredLocale();
|
||||
if (preferredLocale != null) {
|
||||
lang = preferredLocale.getLanguage();
|
||||
} else {
|
||||
|
@ -897,11 +839,12 @@ public class OsmandApplication extends MultiDexApplication {
|
|||
|
||||
@Override
|
||||
public AssetManager getAssets() {
|
||||
return localizedResources != null ? localizedResources.getAssets() : super.getAssets();
|
||||
return getResources() != null ? getResources().getAssets() : super.getAssets();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resources getResources() {
|
||||
Resources localizedResources = localeHelper.getLocalizedResources();
|
||||
return localizedResources != null ? localizedResources : super.getResources();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,21 @@
|
|||
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.plus.OsmandApplication;
|
||||
import net.osmand.util.Algorithms;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class LocaleHelper implements StateChangedListener<String> {
|
||||
|
||||
private Resources localizedResources;
|
||||
private Locale preferredLocale;
|
||||
private Locale defaultLocale;
|
||||
|
||||
private boolean needRestart = false;
|
||||
|
||||
private OsmandApplication app;
|
||||
|
@ -13,6 +24,65 @@ public class LocaleHelper implements StateChangedListener<String> {
|
|||
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() {
|
||||
app.getSettings().PREFERRED_LOCALE.addListener(this);
|
||||
}
|
||||
|
@ -29,9 +99,21 @@ public class LocaleHelper implements StateChangedListener<String> {
|
|||
this.needRestart = needRestart;
|
||||
}
|
||||
|
||||
public Resources getLocalizedResources() {
|
||||
return localizedResources;
|
||||
}
|
||||
|
||||
public Locale getPreferredLocale() {
|
||||
return preferredLocale;
|
||||
}
|
||||
|
||||
public Locale getDefaultLocale() {
|
||||
return defaultLocale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateChanged(String change) {
|
||||
needRestart = true;
|
||||
app.checkPreferredLocale();
|
||||
checkPreferredLocale();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -108,7 +108,7 @@ public class GlobalSettingsFragment extends BaseSettingsFragment
|
|||
Activity activity = getActivity();
|
||||
OsmandApplication app = getMyApplication();
|
||||
if (app != null && activity != null) {
|
||||
app.checkPreferredLocale();
|
||||
app.getLocaleHelper().checkPreferredLocale();
|
||||
app.restartApp(activity);
|
||||
}
|
||||
} else if (prefId.equals(settings.SPEED_CAMERAS_UNINSTALLED.getId())) {
|
||||
|
|
|
@ -40,7 +40,7 @@ public class WebViewEx extends WebView {
|
|||
// also see: https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||
OsmandApplication app = (OsmandApplication) ctx.getApplicationContext();
|
||||
app.checkPreferredLocale();
|
||||
app.getLocaleHelper().checkPreferredLocale();
|
||||
ctx.getResources().updateConfiguration(
|
||||
new Configuration(app.getResources().getConfiguration()),
|
||||
ctx.getResources().getDisplayMetrics());
|
||||
|
|
|
@ -71,7 +71,7 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
|
|||
nightMode = !settings.isLightContent();
|
||||
|
||||
int themeId = nightMode ? R.style.OsmandDarkTheme_NoActionbar : R.style.OsmandLightTheme_NoActionbar_LightStatusBar;
|
||||
app.setLanguage(this);
|
||||
app.getLocaleHelper().setLanguage(this);
|
||||
setTheme(themeId);
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
|
|
Loading…
Reference in a new issue