Merge pull request #651 from krabaey/feature-favourites-import

option to import favourites.gpx as favourites
This commit is contained in:
vshcherb 2014-05-25 02:25:44 +02:00
commit f68a84b8f7
2 changed files with 98 additions and 0 deletions

View file

@ -1482,6 +1482,8 @@ Afghanistan, Albania, Algeria, Andorra, Angola, Anguilla, Antigua and Barbuda, A
<string name="rotate_map_to_bearing">Map orientation</string> <string name="rotate_map_to_bearing">Map orientation</string>
<string name="show_route">Route details</string> <string name="show_route">Route details</string>
<string name="fav_imported_sucessfully">Favorites successfully imported</string> <string name="fav_imported_sucessfully">Favorites successfully imported</string>
<string name="import_file_favourites">Save as GPX file or import into favourites?</string>
<string name="import_save">Save</string>
<string name="fav_file_to_load_not_found">GPX file containing favorites is not found at {0}</string> <string name="fav_file_to_load_not_found">GPX file containing favorites is not found at {0}</string>
<string name="fav_saved_sucessfully">Favorites successfully saved to {0}</string> <string name="fav_saved_sucessfully">Favorites successfully saved to {0}</string>
<string name="no_fav_to_save">No favorite points to save</string> <string name="no_fav_to_save">No favorite points to save</string>

View file

@ -1,6 +1,9 @@
package net.osmand.plus.helpers; package net.osmand.plus.helpers;
import android.app.AlertDialog;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -9,6 +12,8 @@ import android.provider.OpenableColumns;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.access.AccessibleToast; 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;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -24,7 +29,9 @@ import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* @author Koen Rabaey * @author Koen Rabaey
@ -52,6 +59,8 @@ public class GpxImportHelper {
public void handleFileImport(final Uri intentUri, final String fileName) { public void handleFileImport(final Uri intentUri, final String fileName) {
if (fileName != null && fileName.endsWith(KML_SUFFIX)) { if (fileName != null && fileName.endsWith(KML_SUFFIX)) {
handleKmlImport(intentUri, fileName); handleKmlImport(intentUri, fileName);
} else if (fileName != null && fileName.endsWith("favourites.gpx")) {
handleFavouritesImport(intentUri, fileName);
} else { } else {
handleGpxImport(intentUri, fileName); handleGpxImport(intentUri, fileName);
} }
@ -107,6 +116,44 @@ public class GpxImportHelper {
}.execute(); }.execute();
} }
private void handleFavouritesImport(final Uri gpxFile, final String fileName) {
new AsyncTask<Void, Void, GPXUtilities.GPXFile>() {
ProgressDialog progress = null;
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(mapActivity, application.getString(R.string.loading), application.getString(R.string.loading_data));
}
@Override
protected GPXUtilities.GPXFile doInBackground(Void... nothing) {
InputStream is = null;
try {
final ParcelFileDescriptor pFD = application.getContentResolver().openFileDescriptor(gpxFile, "r");
if (pFD != null) {
is = new FileInputStream(pFD.getFileDescriptor());
return GPXUtilities.loadGPXFile(application, is);
}
} catch (FileNotFoundException e) {
//
} finally {
if (is != null) try {
is.close();
} catch (IOException ignore) {
}
}
return null;
}
@Override
protected void onPostExecute(final GPXUtilities.GPXFile result) {
progress.dismiss();
importFavourites(result, fileName);
}
}.execute();
}
private void handleKmlImport(final Uri kmlFile, final String name) { private void handleKmlImport(final Uri kmlFile, final String name) {
new AsyncTask<Void, Void, GPXUtilities.GPXFile>() { new AsyncTask<Void, Void, GPXUtilities.GPXFile>() {
ProgressDialog progress = null; ProgressDialog progress = null;
@ -227,4 +274,53 @@ public class GpxImportHelper {
mapView.refreshMap(); mapView.refreshMap();
} }
} }
private void importFavourites(final GPXUtilities.GPXFile gpxFile, final String fileName) {
final DialogInterface.OnClickListener importFavouritesListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
final List<FavouritePoint> 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);
break;
case DialogInterface.BUTTON_NEGATIVE:
handleResult(gpxFile, fileName );
break;
}
}
};
new AlertDialog.Builder(mapActivity)
.setMessage(R.string.import_file_favourites)
.setPositiveButton(R.string.import_fav, importFavouritesListener)
.setNegativeButton(R.string.import_save, importFavouritesListener)
.show();
}
private List<FavouritePoint> asFavourites(final List<GPXUtilities.WptPt> wptPts) {
final List<FavouritePoint> favourites = new ArrayList<FavouritePoint>();
for (GPXUtilities.WptPt p : wptPts) {
if (p.category != null) {
favourites.add(new FavouritePoint(p.lat, p.lon, p.name, p.category));
} else if (p.name != null) {
int c;
if ((c = p.name.lastIndexOf('_')) != -1) {
favourites.add(new FavouritePoint(p.lat, p.lon, p.name.substring(0, c), p.name.substring(c + 1)));
}
}
}
return favourites;
}
} }