From 9453a14fe458e0f6931f05b0cbf51f8140b40d45 Mon Sep 17 00:00:00 2001 From: cepprice Date: Wed, 10 Feb 2021 12:44:44 +0500 Subject: [PATCH 1/5] Add restart button after settings import if language was changed --- .../res/layout/fragment_import_complete.xml | 62 +++++++++++++++---- OsmAnd/res/values/strings.xml | 1 + .../src/net/osmand/plus/AppInitializer.java | 2 + .../net/osmand/plus/OsmandApplication.java | 7 ++- .../net/osmand/plus/helpers/LocaleHelper.java | 37 +++++++++++ .../backend/backup/SettingsHelper.java | 9 +++ .../fragments/ImportCompleteFragment.java | 25 ++++++++ 7 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java diff --git a/OsmAnd/res/layout/fragment_import_complete.xml b/OsmAnd/res/layout/fragment_import_complete.xml index 6e9a5fa22b..e9d6180784 100644 --- a/OsmAnd/res/layout/fragment_import_complete.xml +++ b/OsmAnd/res/layout/fragment_import_complete.xml @@ -66,28 +66,64 @@ - + android:orientation="horizontal" > - + android:background="?attr/dlg_btn_secondary" + android:layout_weight="1"> - + + + + + + + + + + + + + diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index a15fb40cbd..3f5841204a 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -12,6 +12,7 @@ --> + Restart Login to OpenPlaceReviews Use test.openplacereviews.org OpenPlaceReviews diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 065c7aba2e..13b3102c93 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -36,6 +36,7 @@ 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; @@ -471,6 +472,7 @@ 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(); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 9d2524d30c..285a0135eb 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -59,6 +59,7 @@ import net.osmand.plus.download.DownloadService; import net.osmand.plus.download.IndexItem; import net.osmand.plus.helpers.AvoidSpecificRoads; import net.osmand.plus.helpers.DayNightHelper; +import net.osmand.plus.helpers.LocaleHelper; import net.osmand.plus.helpers.LocationServiceHelper; import net.osmand.plus.helpers.LockHelper; import net.osmand.plus.helpers.WaypointHelper; @@ -165,6 +166,7 @@ public class OsmandApplication extends MultiDexApplication { OprAuthHelper oprAuthHelper; MeasurementEditingContext measurementEditingContext; OnlineRoutingHelper onlineRoutingHelper; + LocaleHelper localeHelper; private Resources localizedResources; private Map customRoutingConfigs = new ConcurrentHashMap<>(); @@ -402,6 +404,10 @@ public class OsmandApplication extends MultiDexApplication { return oprAuthHelper; } + public LocaleHelper getLocaleHelper() { + return localeHelper; + } + public synchronized DownloadIndexesThread getDownloadThread() { if (downloadIndexesThread == null) { downloadIndexesThread = new DownloadIndexesThread(this); @@ -717,7 +723,6 @@ public class OsmandApplication extends MultiDexApplication { } } - public TargetPointsHelper getTargetPointsHelper() { return targetPointsHelper; } diff --git a/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java new file mode 100644 index 0000000000..02250098bc --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java @@ -0,0 +1,37 @@ +package net.osmand.plus.helpers; + +import net.osmand.StateChangedListener; +import net.osmand.plus.OsmandApplication; + +public class LocaleHelper implements StateChangedListener { + + private boolean needRestart = false; + + private OsmandApplication app; + + public LocaleHelper(OsmandApplication app) { + this.app = app; + } + + public void listenLocaleChanges() { + app.getSettings().PREFERRED_LOCALE.addListener(this); + } + + public void stopListeningLocaleChanges() { + app.getSettings().PREFERRED_LOCALE.removeListener(this); + } + + public boolean needRestart() { + return needRestart; + } + + public void setNeedRestart(boolean needRestart) { + this.needRestart = needRestart; + } + + @Override + public void stateChanged(String change) { + needRestart = true; + app.checkPreferredLocale(); + } +} diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java index 11fe377040..2e548f0c2b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java @@ -29,6 +29,7 @@ import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXInfo; +import net.osmand.plus.helpers.LocaleHelper; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry; import net.osmand.plus.mapmarkers.MapMarker; @@ -193,6 +194,7 @@ public class SettingsHelper { private File file; private SettingsImportListener listener; private List items; + private LocaleHelper localeHelper; ImportItemsAsyncTask(@NonNull File file, @Nullable SettingsImportListener listener, @@ -201,6 +203,12 @@ public class SettingsHelper { this.file = file; this.listener = listener; this.items = items; + localeHelper = app.getLocaleHelper(); + } + + @Override + protected void onPreExecute() { + localeHelper.listenLocaleChanges(); } @Override @@ -218,6 +226,7 @@ public class SettingsHelper { @Override protected void onPostExecute(Boolean success) { + localeHelper.stopListeningLocaleChanges(); finishImport(listener, success, items); } } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java index 0d40a3c2f1..ddee0e1393 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java @@ -15,6 +15,7 @@ import android.widget.TextView; import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -28,6 +29,7 @@ import net.osmand.plus.audionotes.AudioVideoNotesPlugin; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.dialogs.SelectMapStyleBottomSheetDialogFragment; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.myplaces.FavoritesActivity; import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.plus.quickaction.QuickActionListFragment; @@ -97,6 +99,10 @@ public class ImportCompleteFragment extends BaseOsmAndFragment { dismissFragment(); } }); + if (app.getLocaleHelper().needRestart()) { + app.getLocaleHelper().setNeedRestart(false); + setupRestartButton(root); + } if (Build.VERSION.SDK_INT >= 21) { AndroidUtils.addStatusBarPadding21v(app, root); } @@ -239,6 +245,25 @@ public class ImportCompleteFragment extends BaseOsmAndFragment { } } + private void setupRestartButton(View root) { + View buttonsDivider = root.findViewById(R.id.buttons_divider); + View buttonContainer = root.findViewById(R.id.button_restart_container); + AndroidUiHelper.setVisibility(View.VISIBLE, buttonsDivider, buttonContainer); + + TextView btnRestart = root.findViewById(R.id.button_restart); + btnRestart.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + FragmentActivity activity = getActivity(); + if (activity instanceof MapActivity) { + MapActivity.doRestart(activity); + } else { + android.os.Process.killProcess(android.os.Process.myPid()); + } + } + }); + } + @Override public int getStatusBarColorId() { return nightMode ? R.color.status_bar_color_dark : R.color.status_bar_color_light; From 14ce40ddda771171e91a267b4dff1809853d1587 Mon Sep 17 00:00:00 2001 From: cepprice Date: Sun, 14 Feb 2021 13:41:30 +0500 Subject: [PATCH 2/5] Refactor to lighten OsmandApplication --- .../src/net/osmand/plus/AppInitializer.java | 2 - .../net/osmand/plus/OsmandApplication.java | 73 ++-------------- .../net/osmand/plus/helpers/LocaleHelper.java | 86 ++++++++++++++++++- .../fragments/GlobalSettingsFragment.java | 2 +- .../net/osmand/plus/widgets/WebViewEx.java | 2 +- .../explore/WikivoyageExploreActivity.java | 2 +- 6 files changed, 95 insertions(+), 72 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 13b3102c93..065c7aba2e 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -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(); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 285a0135eb..83386b481b 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -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 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(); } diff --git a/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java index 02250098bc..a0bf32fb8e 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java @@ -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 { + 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 { 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 { 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(); } -} +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java index 071d7be709..8114874547 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java @@ -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())) { diff --git a/OsmAnd/src/net/osmand/plus/widgets/WebViewEx.java b/OsmAnd/src/net/osmand/plus/widgets/WebViewEx.java index c83e1110cb..747e1bff57 100644 --- a/OsmAnd/src/net/osmand/plus/widgets/WebViewEx.java +++ b/OsmAnd/src/net/osmand/plus/widgets/WebViewEx.java @@ -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()); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreActivity.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreActivity.java index dae47525aa..64a1de6dee 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreActivity.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreActivity.java @@ -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); From e1b7175919a1d0d63a36b2ab790ae3a48d6dffc3 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Sun, 14 Feb 2021 12:55:50 +0200 Subject: [PATCH 3/5] Small fixes --- .../src/net/osmand/plus/AppInitializer.java | 2 +- .../net/osmand/plus/OsmandApplication.java | 31 ++--------- .../net/osmand/plus/helpers/LocaleHelper.java | 55 +++++++++++++------ 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 065c7aba2e..819590a516 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -514,7 +514,7 @@ public class AppInitializer implements IProgress { return null; } }); - app.regions.setLocale(app.getLanguage(), app.getCountry()); + app.regions.setLocale(app.getLanguage(), app.getLocaleHelper().getCountry()); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 83386b481b..165a9a9c07 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -127,6 +127,7 @@ public class OsmandApplication extends MultiDexApplication { private final SQLiteAPI sqliteAPI = new SQLiteAPIImpl(this); private final OsmAndTaskManager taskManager = new OsmAndTaskManager(this); private final UiUtilities iconsCache = new UiUtilities(this); + private final LocaleHelper localeHelper = new LocaleHelper(this); // start variables ResourceManager resourceManager; @@ -166,7 +167,6 @@ public class OsmandApplication extends MultiDexApplication { OprAuthHelper oprAuthHelper; MeasurementEditingContext measurementEditingContext; OnlineRoutingHelper onlineRoutingHelper; - LocaleHelper localeHelper = new LocaleHelper(this); private Map customRoutingConfigs = new ConcurrentHashMap<>(); private File externalStorageDirectory; @@ -419,7 +419,7 @@ public class OsmandApplication extends MultiDexApplication { } @Override - public void onConfigurationChanged(Configuration newConfig) { + public void onConfigurationChanged(@NonNull Configuration newConfig) { Locale preferredLocale = localeHelper.getPreferredLocale(); if (preferredLocale != null && !newConfig.locale.getLanguage().equals(preferredLocale.getLanguage())) { super.onConfigurationChanged(newConfig); @@ -434,12 +434,10 @@ public class OsmandApplication extends MultiDexApplication { } } - public static final int PROGRESS_DIALOG = 5; - public void checkApplicationIsBeingInitialized(Activity activity, AppInitializeListener listener) { // start application if it was previously closed startApplication(); - if(listener != null) { + if (listener != null) { appInitializer.addListener(listener); } } @@ -812,29 +810,8 @@ public class OsmandApplication extends MultiDexApplication { return s; } - public String getCountry() { - String country; - Locale preferredLocale = localeHelper.getPreferredLocale(); - if (preferredLocale != null) { - country = preferredLocale.getCountry(); - } else { - country = Locale.getDefault().getCountry(); - } - return country; - } - public String getLanguage() { - String lang; - Locale preferredLocale = localeHelper.getPreferredLocale(); - if (preferredLocale != null) { - lang = preferredLocale.getLanguage(); - } else { - lang = Locale.getDefault().getLanguage(); - } - if (lang != null && lang.length() > 3) { - lang = lang.substring(0, 2).toLowerCase(); - } - return lang; + return localeHelper.getLanguage(); } @Override diff --git a/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java index a0bf32fb8e..a0facaf61c 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java @@ -12,14 +12,14 @@ import java.util.Locale; public class LocaleHelper implements StateChangedListener { - private Resources localizedResources; - private Locale preferredLocale; + private final OsmandApplication app; + private Locale defaultLocale; + private Locale preferredLocale; + private Resources localizedResources; private boolean needRestart = false; - private OsmandApplication app; - public LocaleHelper(OsmandApplication app) { this.app = app; } @@ -83,6 +83,18 @@ public class LocaleHelper implements StateChangedListener { } } + public Resources getLocalizedResources() { + return localizedResources; + } + + public Locale getPreferredLocale() { + return preferredLocale; + } + + public Locale getDefaultLocale() { + return defaultLocale; + } + public void listenLocaleChanges() { app.getSettings().PREFERRED_LOCALE.addListener(this); } @@ -99,21 +111,32 @@ public class LocaleHelper implements StateChangedListener { 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; checkPreferredLocale(); } + + public String getCountry() { + String country; + if (preferredLocale != null) { + country = preferredLocale.getCountry(); + } else { + country = Locale.getDefault().getCountry(); + } + return country; + } + + public String getLanguage() { + String lang; + if (preferredLocale != null) { + lang = preferredLocale.getLanguage(); + } else { + lang = Locale.getDefault().getLanguage(); + } + if (lang != null && lang.length() > 3) { + lang = lang.substring(0, 2).toLowerCase(); + } + return lang; + } } \ No newline at end of file From de86d1c24e59e1e569433f0e31f2282d563e1277 Mon Sep 17 00:00:00 2001 From: cepprice Date: Mon, 15 Feb 2021 13:15:27 +0500 Subject: [PATCH 4/5] Small refactoring --- .../net/osmand/plus/CustomOsmandPlugin.java | 2 +- .../net/osmand/plus/helpers/LocaleHelper.java | 26 +-------------- .../plus/importfiles/SettingsImportTask.java | 6 ++-- .../backend/backup/SettingsHelper.java | 33 +++++++++++-------- .../fragments/ImportCompleteFragment.java | 11 +++++-- .../fragments/ImportSettingsFragment.java | 4 +-- 6 files changed, 35 insertions(+), 47 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java index 9f3003e925..8c8a1dafa0 100644 --- a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java @@ -237,7 +237,7 @@ public class CustomOsmandPlugin extends OsmandPlugin { final SettingsHelper.SettingsImportListener importListener = new SettingsHelper.SettingsImportListener() { @Override - public void onSettingsImportFinished(boolean succeed, @NonNull List items) { + public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List items) { if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } diff --git a/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java index a0facaf61c..bdacd02c62 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/LocaleHelper.java @@ -10,7 +10,7 @@ import net.osmand.util.Algorithms; import java.util.Locale; -public class LocaleHelper implements StateChangedListener { +public class LocaleHelper { private final OsmandApplication app; @@ -18,8 +18,6 @@ public class LocaleHelper implements StateChangedListener { private Locale preferredLocale; private Resources localizedResources; - private boolean needRestart = false; - public LocaleHelper(OsmandApplication app) { this.app = app; } @@ -95,28 +93,6 @@ public class LocaleHelper implements StateChangedListener { return defaultLocale; } - public void listenLocaleChanges() { - app.getSettings().PREFERRED_LOCALE.addListener(this); - } - - public void stopListeningLocaleChanges() { - app.getSettings().PREFERRED_LOCALE.removeListener(this); - } - - public boolean needRestart() { - return needRestart; - } - - public void setNeedRestart(boolean needRestart) { - this.needRestart = needRestart; - } - - @Override - public void stateChanged(String change) { - needRestart = true; - checkPreferredLocale(); - } - public String getCountry() { String country; if (preferredLocale != null) { diff --git a/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java index 1f886a23bb..a80ed45126 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java @@ -134,7 +134,7 @@ class SettingsImportTask extends BaseLoadAsyncTask { private SettingsImportListener getImportListener(final File file) { return new SettingsImportListener() { @Override - public void onSettingsImportFinished(boolean succeed, @NonNull List items) { + public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List items) { if (succeed) { app.getRendererRegistry().updateExternalRenderers(); app.getPoiFilters().loadSelectedPoiFilters(); @@ -150,7 +150,7 @@ class SettingsImportTask extends BaseLoadAsyncTask { } if (!silentImport && file != null && activity != null) { FragmentManager fm = activity.getSupportFragmentManager(); - ImportCompleteFragment.showInstance(fm, items, file.getName()); + ImportCompleteFragment.showInstance(fm, items, file.getName(), needRestart); } } } @@ -174,7 +174,7 @@ class SettingsImportTask extends BaseLoadAsyncTask { final SettingsImportListener importListener = new SettingsImportListener() { @Override - public void onSettingsImportFinished(boolean succeed, @NonNull List items) { + public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List items) { FragmentActivity activity = activityRef.get(); if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java index 2e548f0c2b..4204fded0b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java @@ -11,6 +11,7 @@ import net.osmand.Collator; import net.osmand.IndexConstants; import net.osmand.OsmAndCollator; import net.osmand.PlatformUtil; +import net.osmand.StateChangedListener; import net.osmand.data.LatLon; import net.osmand.map.ITileSource; import net.osmand.map.TileSourceManager; @@ -111,7 +112,7 @@ public class SettingsHelper { private Map exportAsyncTasks = new HashMap<>(); public interface SettingsImportListener { - void onSettingsImportFinished(boolean succeed, @NonNull List items); + void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List items); } public interface SettingsCollectListener { @@ -173,7 +174,7 @@ public class SettingsHelper { } } - private void finishImport(@Nullable SettingsImportListener listener, boolean success, @NonNull List items) { + private void finishImport(@Nullable SettingsImportListener listener, boolean success, @NonNull List items, boolean needRestart) { importTask = null; List warnings = new ArrayList<>(); for (SettingsItem item : items) { @@ -183,18 +184,19 @@ public class SettingsHelper { app.showToastMessage(AndroidUtils.formatWarnings(warnings).toString()); } if (listener != null) { - listener.onSettingsImportFinished(success, items); + listener.onSettingsImportFinished(success, needRestart, items); } } @SuppressLint("StaticFieldLeak") private class ImportItemsAsyncTask extends AsyncTask { - private SettingsImporter importer; - private File file; - private SettingsImportListener listener; - private List items; - private LocaleHelper localeHelper; + private final SettingsImporter importer; + private final File file; + private final SettingsImportListener listener; + private final List items; + private final StateChangedListener localeListener; + private boolean needRestart = false; ImportItemsAsyncTask(@NonNull File file, @Nullable SettingsImportListener listener, @@ -203,12 +205,17 @@ public class SettingsHelper { this.file = file; this.listener = listener; this.items = items; - localeHelper = app.getLocaleHelper(); + localeListener = new StateChangedListener() { + @Override + public void stateChanged(String change) { + needRestart = true; + } + }; } @Override protected void onPreExecute() { - localeHelper.listenLocaleChanges(); + app.getSettings().PREFERRED_LOCALE.addListener(localeListener); } @Override @@ -226,8 +233,8 @@ public class SettingsHelper { @Override protected void onPostExecute(Boolean success) { - localeHelper.stopListeningLocaleChanges(); - finishImport(listener, success, items); + app.getSettings().PREFERRED_LOCALE.removeListener(localeListener); + finishImport(listener, success, items, needRestart); } } @@ -348,7 +355,7 @@ public class SettingsHelper { protected void onPreExecute() { ImportAsyncTask importTask = SettingsHelper.this.importTask; if (importTask != null && !importTask.importDone) { - finishImport(importListener, false, items); + finishImport(importListener, false, items, false); } SettingsHelper.this.importTask = this; } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java index ddee0e1393..abcd5afc0a 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java @@ -52,14 +52,16 @@ public class ImportCompleteFragment extends BaseOsmAndFragment { private RecyclerView recyclerView; private List settingsItems; private String fileName; + private boolean needRestart; private boolean nightMode; public static void showInstance(FragmentManager fm, @NonNull List settingsItems, - @NonNull String fileName) { + @NonNull String fileName, boolean needRestart) { ImportCompleteFragment fragment = new ImportCompleteFragment(); fragment.setSettingsItems(settingsItems); fragment.setFileName(fileName); fragment.setRetainInstance(true); + fragment.setNeedRestart(needRestart); fm.beginTransaction() .replace(R.id.fragmentContainer, fragment, TAG) .addToBackStack(SETTINGS_LIST_TAG) @@ -99,8 +101,7 @@ public class ImportCompleteFragment extends BaseOsmAndFragment { dismissFragment(); } }); - if (app.getLocaleHelper().needRestart()) { - app.getLocaleHelper().setNeedRestart(false); + if (needRestart) { setupRestartButton(root); } if (Build.VERSION.SDK_INT >= 21) { @@ -276,4 +277,8 @@ public class ImportCompleteFragment extends BaseOsmAndFragment { public void setFileName(String fileName) { this.fileName = fileName; } + + public void setNeedRestart(boolean needRestart) { + this.needRestart = needRestart; + } } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java index b614bc57dd..3cae444157 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java @@ -155,7 +155,7 @@ public class ImportSettingsFragment extends BaseSettingsListFragment { public SettingsHelper.SettingsImportListener getImportListener() { return new SettingsHelper.SettingsImportListener() { @Override - public void onSettingsImportFinished(boolean succeed, @NonNull List items) { + public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List items) { if (succeed) { app.getRendererRegistry().updateExternalRenderers(); AppInitializer.loadRoutingFiles(app, null); @@ -166,7 +166,7 @@ public class ImportSettingsFragment extends BaseSettingsListFragment { } FragmentManager fm = getFragmentManager(); if (fm != null && file != null) { - ImportCompleteFragment.showInstance(fm, items, file.getName()); + ImportCompleteFragment.showInstance(fm, items, file.getName(), needRestart); } } } From 5c59f65e1575e4cf2529a8e317cdce65e4c79904 Mon Sep 17 00:00:00 2001 From: cepprice Date: Mon, 15 Feb 2021 13:17:42 +0500 Subject: [PATCH 5/5] Add missing file --- .../plus/settings/fragments/ConfigureProfileFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureProfileFragment.java index cea9b0e17c..e421a981b9 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureProfileFragment.java @@ -202,7 +202,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co private void importBackupSettingsItems(File file, List items) { app.getSettingsHelper().importSettings(file, items, "", 1, new SettingsHelper.SettingsImportListener() { @Override - public void onSettingsImportFinished(boolean succeed, @NonNull List items) { + public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List items) { app.showToastMessage(R.string.profile_prefs_reset_successful); updateCopiedOrResetPrefs(); }