Small refactoring

This commit is contained in:
cepprice 2021-02-15 13:15:27 +05:00
parent e1b7175919
commit de86d1c24e
6 changed files with 35 additions and 47 deletions

View file

@ -237,7 +237,7 @@ public class CustomOsmandPlugin extends OsmandPlugin {
final SettingsHelper.SettingsImportListener importListener = new SettingsHelper.SettingsImportListener() {
@Override
public void onSettingsImportFinished(boolean succeed, @NonNull List<SettingsItem> items) {
public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List<SettingsItem> items) {
if (AndroidUtils.isActivityNotDestroyed(activity)) {
progress.dismiss();
}

View file

@ -10,7 +10,7 @@ import net.osmand.util.Algorithms;
import java.util.Locale;
public class LocaleHelper implements StateChangedListener<String> {
public class LocaleHelper {
private final OsmandApplication app;
@ -18,8 +18,6 @@ public class LocaleHelper implements StateChangedListener<String> {
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<String> {
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) {

View file

@ -134,7 +134,7 @@ class SettingsImportTask extends BaseLoadAsyncTask<Void, Void, String> {
private SettingsImportListener getImportListener(final File file) {
return new SettingsImportListener() {
@Override
public void onSettingsImportFinished(boolean succeed, @NonNull List<SettingsItem> items) {
public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List<SettingsItem> items) {
if (succeed) {
app.getRendererRegistry().updateExternalRenderers();
app.getPoiFilters().loadSelectedPoiFilters();
@ -150,7 +150,7 @@ class SettingsImportTask extends BaseLoadAsyncTask<Void, Void, String> {
}
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<Void, Void, String> {
final SettingsImportListener importListener = new SettingsImportListener() {
@Override
public void onSettingsImportFinished(boolean succeed, @NonNull List<SettingsItem> items) {
public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List<SettingsItem> items) {
FragmentActivity activity = activityRef.get();
if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) {
progress.dismiss();

View file

@ -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<File, ExportAsyncTask> exportAsyncTasks = new HashMap<>();
public interface SettingsImportListener {
void onSettingsImportFinished(boolean succeed, @NonNull List<SettingsItem> items);
void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List<SettingsItem> items);
}
public interface SettingsCollectListener {
@ -173,7 +174,7 @@ public class SettingsHelper {
}
}
private void finishImport(@Nullable SettingsImportListener listener, boolean success, @NonNull List<SettingsItem> items) {
private void finishImport(@Nullable SettingsImportListener listener, boolean success, @NonNull List<SettingsItem> items, boolean needRestart) {
importTask = null;
List<String> 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<Void, Void, Boolean> {
private SettingsImporter importer;
private File file;
private SettingsImportListener listener;
private List<SettingsItem> items;
private LocaleHelper localeHelper;
private final SettingsImporter importer;
private final File file;
private final SettingsImportListener listener;
private final List<SettingsItem> items;
private final StateChangedListener<String> 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<String>() {
@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;
}

View file

@ -52,14 +52,16 @@ public class ImportCompleteFragment extends BaseOsmAndFragment {
private RecyclerView recyclerView;
private List<SettingsItem> settingsItems;
private String fileName;
private boolean needRestart;
private boolean nightMode;
public static void showInstance(FragmentManager fm, @NonNull List<SettingsItem> 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;
}
}

View file

@ -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<SettingsItem> items) {
public void onSettingsImportFinished(boolean succeed, boolean needRestart, @NonNull List<SettingsItem> 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);
}
}
}