From de86d1c24e59e1e569433f0e31f2282d563e1277 Mon Sep 17 00:00:00 2001 From: cepprice Date: Mon, 15 Feb 2021 13:15:27 +0500 Subject: [PATCH] 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); } } }