From c44d2470c8bb1568dcb993e24fa9ebaa498b2afb Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Wed, 6 Feb 2013 14:21:40 +0100 Subject: [PATCH] Fix small issues and add possibility for multiple gpx files --- .../activities/DownloadIndexActivity.java | 6 +- .../plus/activities/MapActivityActions.java | 4 +- .../plus/activities/MapActivityLayers.java | 152 ++++++++++++------ .../osmand/plus/routing/RouteAnimation.java | 4 +- .../plus/views/YandexTrafficAdapter.java | 1 - 5 files changed, 104 insertions(+), 63 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/DownloadIndexActivity.java b/OsmAnd/src/net/osmand/plus/activities/DownloadIndexActivity.java index edc9b44261..c229411a9d 100644 --- a/OsmAnd/src/net/osmand/plus/activities/DownloadIndexActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/DownloadIndexActivity.java @@ -78,7 +78,6 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity { private static final int SELECT_ALL_ID = 1; private static final int DESELECT_ALL_ID = 2; private static final int FILTER_EXISTING_REGIONS = 3; - private static final int DOWNLOAD_FILES_TYPE = 4; /** dialogs **/ public static final int DIALOG_MAP_VERSION_UPDATE = 0; @@ -237,7 +236,6 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity { menu.add(0, SELECT_ALL_ID, 0, R.string.select_all); menu.add(0, DESELECT_ALL_ID, 0, R.string.deselect_all); menu.add(0, FILTER_EXISTING_REGIONS, 0, R.string.filter_existing_indexes); - menu.add(0, DOWNLOAD_FILES_TYPE, 0, R.string.download_select_map_types); } return true; } @@ -294,8 +292,6 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity { } listAdapter.setIndexFiles(filtered, IndexItemCategory.categorizeIndexItems(getClientContext(), filtered)); listAdapter.notifyDataSetChanged(); - } else if (item.getItemId() == DOWNLOAD_FILES_TYPE) { - selectDownloadType(); } else if(item.getItemId() == DESELECT_ALL_ID){ entriesToDownload.clear(); listAdapter.notifyDataSetInvalidated(); @@ -308,7 +304,7 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity { } - private void selectDownloadType() { + public void selectDownloadType() { Builder bld = new AlertDialog.Builder(this); final DownloadActivityType[] items = getDownloadTypes(); bld.setItems(toString(items), new DialogInterface.OnClickListener() { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index bbe0712f46..5f74584979 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -672,7 +672,7 @@ public class MapActivityActions implements DialogProvider { public void navigateUsingGPX(final ApplicationMode appMode) { final LatLon endForRouting = mapActivity.getPointToNavigate(); final MapActivityLayers mapLayers = mapActivity.getMapLayers(); - mapLayers.selectGPXFileLayer(new CallbackWithObject() { + mapLayers.selectGPXFileLayer(false, false, false, new CallbackWithObject() { @Override public boolean processResult(final GPXFile result) { @@ -726,7 +726,7 @@ public class MapActivityActions implements DialogProvider { builder.show(); return true; } - }, false, false); + }); } private ApplicationMode getAppMode(ToggleButton[] buttons, OsmandSettings settings){ diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java index 94f84fe364..fb469d2fe5 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityLayers.java @@ -348,7 +348,7 @@ public class MapActivityLayers { public void showGPXFileLayer(final OsmandMapTileView mapView){ final OsmandSettings settings = getApplication().getSettings(); - selectGPXFileLayer(new CallbackWithObject() { + selectGPXFileLayer(true, true, true, new CallbackWithObject() { @Override public boolean processResult(GPXFile result) { GPXFile toShow = result; @@ -375,13 +375,75 @@ public class MapActivityLayers { mapView.refreshMap(); return true; } - }, true, true); + }); } - public void selectGPXFileLayer(final CallbackWithObject callbackWithObject, final boolean convertCloudmade, - final boolean showCurrentGpx) { - final List list = new ArrayList(); + public void selectGPXFileLayer(final boolean convertCloudmade, + final boolean showCurrentGpx, final boolean multipleChoice, final CallbackWithObject callbackWithObject) { final File dir = getApplication().getAppPath(IndexConstants.GPX_INDEX_DIR); + final List list = getSortedGPXFilenames(dir); + if(list.isEmpty()){ + AccessibleToast.makeText(activity, R.string.gpx_files_not_found, Toast.LENGTH_LONG).show(); + } + if(!list.isEmpty() || showCurrentGpx){ + Builder builder = new AlertDialog.Builder(activity); + if(showCurrentGpx){ + list.add(0, getString(R.string.show_current_gpx_title)); + } + String[] items = list.toArray(new String[list.size()]); + if (multipleChoice) { + final boolean[] selected = new boolean[items.length]; + builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which, boolean isChecked) { + selected[which] = isChecked; + } + }); + builder.setPositiveButton(R.string.default_buttons_ok, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + if (showCurrentGpx && selected[0]) { + callbackWithObject.processResult(null); + } else { + List s = new ArrayList(); + for (int i = 0; i < selected.length; i++) { + if (selected[i]) { + s.add(list.get(i)); + } + } + loadGPXFileInDifferentThread(callbackWithObject, convertCloudmade, dir, s.toArray(new String[s.size()])); + } + } + }); + } else { + builder.setItems(items, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + if (showCurrentGpx && which == 0) { + callbackWithObject.processResult(null); + } else { + loadGPXFileInDifferentThread(callbackWithObject, convertCloudmade, dir, list.get(which)); + } + } + }); + } + + AlertDialog dlg = builder.show(); + try { + dlg.getListView().setFastScrollEnabled(true); + } catch(Exception e) { + // java.lang.ClassCastException: com.android.internal.widget.RoundCornerListAdapter + // Unknown reason but on some devices fail + } + } + } + + private List getSortedGPXFilenames(File dir) { + final List list = new ArrayList(); if (dir != null && dir.canRead()) { File[] files = dir.listFiles(); if (files != null) { @@ -405,56 +467,40 @@ public class MapActivityLayers { } } } - - if(list.isEmpty()){ - AccessibleToast.makeText(activity, R.string.gpx_files_not_found, Toast.LENGTH_LONG).show(); - } - if(!list.isEmpty() || showCurrentGpx){ - Builder builder = new AlertDialog.Builder(activity); - if(showCurrentGpx){ - list.add(0, getString(R.string.show_current_gpx_title)); - } - builder.setItems(list.toArray(new String[list.size()]), new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - if(showCurrentGpx && which == 0){ - callbackWithObject.processResult(null); - } else { - final ProgressDialog dlg = ProgressDialog.show(activity, getString(R.string.loading), - getString(R.string.loading_data)); - final File f = new File(dir, list.get(which)); - new Thread(new Runnable() { - @Override - public void run() { - final GPXFile res = GPXUtilities.loadGPXFile(activity.getMyApplication(), f, convertCloudmade); - dlg.dismiss(); - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - if (res.warning != null) { - AccessibleToast.makeText(activity, res.warning, Toast.LENGTH_LONG).show(); - } else { - callbackWithObject.processResult(res); - } - } - }); - } - - }, "Loading gpx").start(); //$NON-NLS-1$ - } + return list; + } + + private void loadGPXFileInDifferentThread(final CallbackWithObject callbackWithObject, + final boolean convertCloudmade, final File dir, final String... filename) { + final ProgressDialog dlg = ProgressDialog.show(activity, getString(R.string.loading), + getString(R.string.loading_data)); + new Thread(new Runnable() { + @Override + public void run() { + GPXFile r = null; + for(String fname : filename) { + final File f = new File(dir, fname); + GPXFile res = GPXUtilities.loadGPXFile(activity.getMyApplication(), f, convertCloudmade); + GPXUtilities.mergeGPXFileInto(res, r); + r = res; + } + final GPXFile res = r; + if (res != null) { + dlg.dismiss(); + activity.runOnUiThread(new Runnable() { + @Override + public void run() { + if (res.warning != null) { + AccessibleToast.makeText(activity, res.warning, Toast.LENGTH_LONG).show(); + } else { + callbackWithObject.processResult(res); + } + } + }); } - - }); - AlertDialog dlg = builder.show(); - try { - dlg.getListView().setFastScrollEnabled(true); - } catch(Exception e) { - // java.lang.ClassCastException: com.android.internal.widget.RoundCornerListAdapter - // Unknown reason but on some devices fail } - } + + }, "Loading gpx").start(); //$NON-NLS-1$ } private void selectPOIFilterLayer(final OsmandMapTileView mapView){ diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteAnimation.java b/OsmAnd/src/net/osmand/plus/routing/RouteAnimation.java index d9764ab453..641ef4c34a 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteAnimation.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteAnimation.java @@ -41,7 +41,7 @@ public class RouteAnimation { @Override public void onClick(DialogInterface dialog, int which) { - ma.getMapLayers().selectGPXFileLayer(new CallbackWithObject() { + ma.getMapLayers().selectGPXFileLayer(true, false, false, new CallbackWithObject() { @Override public boolean processResult(GPXUtilities.GPXFile result) { @@ -50,7 +50,7 @@ public class RouteAnimation { startAnimationThread(routingHelper, ma, prms.points, true, speedup.getProgress() + 1); return true; } - }, true, false); + }); } }); diff --git a/OsmAnd/src/net/osmand/plus/views/YandexTrafficAdapter.java b/OsmAnd/src/net/osmand/plus/views/YandexTrafficAdapter.java index da439e92a9..64f799a561 100644 --- a/OsmAnd/src/net/osmand/plus/views/YandexTrafficAdapter.java +++ b/OsmAnd/src/net/osmand/plus/views/YandexTrafficAdapter.java @@ -33,7 +33,6 @@ public class YandexTrafficAdapter extends MapTileAdapter { @Override public void onInit() { - AccessibleToast.makeText(view.getContext(), R.string.thanks_yandex_traffic, Toast.LENGTH_LONG).show(); } @Override