From f7dcf50f4b4cddf62d36b878316affc52e4f3b62 Mon Sep 17 00:00:00 2001 From: Nazar Date: Fri, 17 Jan 2020 10:31:44 +0200 Subject: [PATCH] Small fixes --- OsmAnd/res/values/strings.xml | 2 +- OsmAnd/src/net/osmand/AndroidUtils.java | 2 +- .../src/net/osmand/plus/AppInitializer.java | 46 ++++++++++++++++++- .../src/net/osmand/plus/SettingsHelper.java | 2 +- .../net/osmand/plus/helpers/ImportHelper.java | 45 ++---------------- 5 files changed, 52 insertions(+), 45 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index b9a97db57b..bbcbecff48 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,7 +11,7 @@ Thx - Hardy --> - \'%1$s\' file doesn\'t contain routing rules, please choose another file. + \'%1$s\' file doesn\'t contain routing rules, please choose another file. Not supported file type. You need to select a file with %1$s extension. Import from file Import routing file diff --git a/OsmAnd/src/net/osmand/AndroidUtils.java b/OsmAnd/src/net/osmand/AndroidUtils.java index 1c6bd9d8d5..9e733fa186 100644 --- a/OsmAnd/src/net/osmand/AndroidUtils.java +++ b/OsmAnd/src/net/osmand/AndroidUtils.java @@ -690,7 +690,7 @@ public class AndroidUtils { return TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL; } - public static String createFileNameWithIncreasedNumber(String oldName) { + public static String createNewFileName(String oldName) { int firstDotIndex = oldName.indexOf('.'); String nameWithoutExt = oldName.substring(0, firstDotIndex); String ext = oldName.substring(firstDotIndex); diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index f2340c22b3..ce7bb80b77 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -11,6 +11,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; +import android.os.AsyncTask; import android.os.Build; import android.support.v7.app.AlertDialog; @@ -31,7 +32,6 @@ import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask; import net.osmand.plus.helpers.AvoidSpecificRoads; -import net.osmand.plus.helpers.ImportHelper; import net.osmand.plus.helpers.LockHelper; import net.osmand.plus.helpers.WaypointHelper; import net.osmand.plus.inapp.InAppPurchaseHelper; @@ -57,11 +57,16 @@ import net.osmand.plus.voice.MediaCommandPlayerImpl; import net.osmand.plus.voice.TTSCommandPlayerImpl; import net.osmand.plus.wikivoyage.data.TravelDbHelper; import net.osmand.render.RenderingRulesStorage; +import net.osmand.router.RoutingConfiguration; import net.osmand.util.Algorithms; import net.osmand.util.OpeningHoursParser; +import org.xmlpull.v1.XmlPullParserException; + import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -135,6 +140,10 @@ public class AppInitializer implements IProgress { void onFinish(AppInitializer init); } + + public interface LoadRoutingFilesCallback { + void onRoutingFilesLoaded(); + } public AppInitializer(OsmandApplication app) { @@ -573,7 +582,7 @@ public class AppInitializer implements IProgress { @SuppressLint("StaticFieldLeak") private void getLazyRoutingConfig() { - ImportHelper.loadRoutingFiles(app, new ImportHelper.LoadRoutingFilesCallback() { + loadRoutingFiles(app, new LoadRoutingFilesCallback() { @Override public void onRoutingFilesLoaded() { notifyEvent(InitEvents.ROUTING_CONFIG_INITIALIZED); @@ -581,6 +590,39 @@ public class AppInitializer implements IProgress { }); } + public static void loadRoutingFiles(final OsmandApplication app, final LoadRoutingFilesCallback callback) { + new AsyncTask() { + + @Override + protected RoutingConfiguration.Builder doInBackground(Void... voids) { + File routingFolder = app.getAppPath(IndexConstants.ROUTING_PROFILES_DIR); + RoutingConfiguration.Builder builder = RoutingConfiguration.getDefault(); + if (routingFolder.isDirectory()) { + File[] fl = routingFolder.listFiles(); + if (fl != null && fl.length > 0) { + for (File f : fl) { + if (f.isFile() && f.getName().endsWith(".xml") && f.canRead()) { + try { + RoutingConfiguration.parseFromInputStream(new FileInputStream(f), f.getName(), builder); + } catch (XmlPullParserException | IOException e) { + throw new IllegalStateException(e); + } + } + } + } + } + return builder; + } + + @Override + protected void onPostExecute(RoutingConfiguration.Builder builder) { + super.onPostExecute(builder); + app.updateRoutingConfig(builder); + callback.onRoutingFilesLoaded(); + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + public synchronized void initVoiceDataInDifferentThread(final Activity uiContext, final ApplicationMode applicationMode, diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 08c7969dfb..61509da462 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -83,7 +83,7 @@ public class SettingsHelper { private ImportAsyncTask importTask; public interface SettingsImportListener { - void onSettingsImportFinished(boolean succeed, boolean empty, List items); + void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items); } public interface SettingsExportListener { diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index 9f835cfbae..8997c4069a 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -76,6 +76,7 @@ import java.util.zip.ZipInputStream; import static android.app.Activity.RESULT_OK; import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT; import static net.osmand.IndexConstants.ROUTING_FILE_EXT; +import static net.osmand.plus.AppInitializer.loadRoutingFiles; import static net.osmand.plus.myplaces.FavoritesActivity.FAV_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; @@ -113,10 +114,6 @@ public class ImportHelper { public interface OnGpxImportCompleteListener { void onComplete(boolean success); } - - public interface LoadRoutingFilesCallback { - void onRoutingFilesLoaded(); - } public ImportHelper(final AppCompatActivity activity, final OsmandApplication app, final OsmandMapTileView mapView) { this.activity = activity; @@ -609,38 +606,6 @@ public class ImportHelper { }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - public static void loadRoutingFiles(final OsmandApplication app, final LoadRoutingFilesCallback callback) { - new AsyncTask() { - @Override - protected RoutingConfiguration.Builder doInBackground(Void... voids) { - File routingFolder = app.getAppPath(IndexConstants.ROUTING_PROFILES_DIR); - RoutingConfiguration.Builder builder = RoutingConfiguration.getDefault(); - if (routingFolder.isDirectory()) { - File[] fl = routingFolder.listFiles(); - if (fl != null && fl.length > 0) { - for (File f : fl) { - if (f.isFile() && f.getName().endsWith(".xml") && f.canRead()) { - try { - RoutingConfiguration.parseFromInputStream(new FileInputStream(f), f.getName(), builder); - } catch (XmlPullParserException | IOException e) { - throw new IllegalStateException(e); - } - } - } - } - } - return builder; - } - - @Override - protected void onPostExecute(RoutingConfiguration.Builder builder) { - super.onPostExecute(builder); - app.updateRoutingConfig(builder); - callback.onRoutingFilesLoaded(); - } - }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - } - public void chooseFileToImport(final ImportType importType, final CallbackWithObject callback) { final MapActivity mapActivity = getMapActivity(); if (mapActivity == null) { @@ -716,7 +681,7 @@ public class ImportHelper { } File dest = new File(routingDir, mFileName); while (dest.exists()) { - mFileName = AndroidUtils.createFileNameWithIncreasedNumber(mFileName); + mFileName = AndroidUtils.createNewFileName(mFileName); dest = new File(routingDir, mFileName); } return copyFile(app, dest, uri, true); @@ -727,7 +692,7 @@ public class ImportHelper { File routingDir = app.getAppPath(IndexConstants.ROUTING_PROFILES_DIR); final File file = new File(routingDir, mFileName); if (error == null && file.exists()) { - loadRoutingFiles(app, new LoadRoutingFilesCallback() { + loadRoutingFiles(app, new AppInitializer.LoadRoutingFilesCallback() { @Override public void onRoutingFilesLoaded() { if (isActivityNotDestroyed(activity)) { @@ -740,7 +705,7 @@ public class ImportHelper { callback.processResult(profileKey); } } else { - app.showToastMessage(app.getString(R.string.file_does_not_contains_routing_rules, mFileName)); + app.showToastMessage(app.getString(R.string.file_does_not_contain_routing_rules, mFileName)); } } }); @@ -808,7 +773,7 @@ public class ImportHelper { if (error == null && file.exists()) { app.getSettingsHelper().importSettings(file, latestChanges, version, new SettingsImportListener() { @Override - public void onSettingsImportFinished(boolean succeed, boolean empty, List items) { + public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { if (isActivityNotDestroyed(activity)) { progress.dismiss(); }