From c597739b3ec06a5d0e6db8e4cf38c4c3a6c155ca Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 13 Jul 2014 21:54:45 +0200 Subject: [PATCH] Speedup import favorites --- .../net/osmand/plus/FavouritesDbHelper.java | 22 +++++++--- .../osmand/plus/helpers/GpxImportHelper.java | 44 ++++++++++++++----- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index 975ee8e1a6..627075703b 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -113,8 +113,11 @@ public class FavouritesDbHelper { saveCurrentPointsIntoFile(); } - public boolean deleteFavourite(FavouritePoint p) { + return deleteFavourite(p, true); + } + + public boolean deleteFavourite(FavouritePoint p, boolean saveImmediately) { if (p != null) { FavoriteGroup group = flatGroups.get(p.getCategory()); if (group != null) { @@ -122,12 +125,17 @@ public class FavouritesDbHelper { } cachedFavoritePoints.remove(p); } - saveCurrentPointsIntoFile(); + if (saveImmediately) { + saveCurrentPointsIntoFile(); + } return true; } - - + public boolean addFavourite(FavouritePoint p) { + return addFavourite(p, true); + } + + public boolean addFavourite(FavouritePoint p, boolean saveImmediately) { if (p.getName().equals("") && flatGroups.containsKey(p.getCategory())) { return true; } @@ -138,7 +146,9 @@ public class FavouritesDbHelper { group.points.add(p); cachedFavoritePoints.add(p); } - saveCurrentPointsIntoFile(); + if (saveImmediately) { + saveCurrentPointsIntoFile(); + } return true; } @@ -171,7 +181,7 @@ public class FavouritesDbHelper { return true; } - private void saveCurrentPointsIntoFile() { + public void saveCurrentPointsIntoFile() { try { Map ex = new LinkedHashMap(); loadGPXFile(getInternalFile(), ex); diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxImportHelper.java index 65b6047bd7..a224b133b6 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxImportHelper.java @@ -15,6 +15,7 @@ import net.osmand.access.AccessibleToast; import net.osmand.data.FavouritePoint; import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.GPXUtilities; +import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -158,6 +159,37 @@ public class GpxImportHelper { } }.execute(); } + + private void importFavoritesImpl(final GPXFile gpxFile) { + new AsyncTask() { + ProgressDialog progress = null; + + @Override + protected void onPreExecute() { + progress = ProgressDialog.show(mapActivity, application.getString(R.string.loading_smth, ""), application.getString(R.string.loading_data)); + } + + @Override + protected GPXUtilities.GPXFile doInBackground(Void... nothing) { + final List favourites = asFavourites(gpxFile.points); + final FavouritesDbHelper favoritesHelper = application.getFavorites(); + for (final FavouritePoint favourite : favourites) { + favoritesHelper.deleteFavourite(favourite, false); + favoritesHelper.addFavourite(favourite, false); + } + favoritesHelper.saveCurrentPointsIntoFile(); + AccessibleToast.makeText(mapActivity, R.string.fav_imported_sucessfully, Toast.LENGTH_LONG).show(); + return null; + } + + @Override + protected void onPostExecute(GPXUtilities.GPXFile result) { + progress.dismiss(); + final Intent newIntent = new Intent(mapActivity, application.getAppCustomization().getFavoritesActivity()); + mapActivity.startActivity(newIntent); + } + }.execute(); + } private void handleKmlImport(final Uri kmlFile, final String name, final boolean save) { new AsyncTask() { @@ -296,17 +328,7 @@ public class GpxImportHelper { public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: - final List favourites = asFavourites(gpxFile.points); - final FavouritesDbHelper favorites = application.getFavorites(); - - for (final FavouritePoint favourite : favourites) { - favorites.deleteFavourite(favourite); - favorites.addFavourite(favourite); - } - - AccessibleToast.makeText(mapActivity, R.string.fav_imported_sucessfully, Toast.LENGTH_LONG).show(); - final Intent newIntent = new Intent(mapActivity, application.getAppCustomization().getFavoritesActivity()); - mapActivity.startActivity(newIntent); + importFavoritesImpl(gpxFile); break; case DialogInterface.BUTTON_NEGATIVE: handleResult(gpxFile, fileName, save);