From 920b369dfd62fd7d8b768dc759fbc42290794826 Mon Sep 17 00:00:00 2001 From: vshcherb Date: Tue, 18 Mar 2014 01:56:39 +0100 Subject: [PATCH] Update GPX selection dialog --- OsmAnd/res/layout/list_menu_item.xml | 9 + OsmAnd/res/layout/list_menu_item_native.xml | 12 +- .../net/osmand/plus/ContextMenuAdapter.java | 4 + .../net/osmand/plus/helpers/GpxUiHelper.java | 205 +++++++++++++----- .../controls/MapRoutePreferencesControl.java | 24 +- 5 files changed, 196 insertions(+), 58 deletions(-) diff --git a/OsmAnd/res/layout/list_menu_item.xml b/OsmAnd/res/layout/list_menu_item.xml index 6939d9963f..e76581a339 100644 --- a/OsmAnd/res/layout/list_menu_item.xml +++ b/OsmAnd/res/layout/list_menu_item.xml @@ -7,6 +7,15 @@ android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:background="@color/color_white"> + + - - + + + s = new ArrayList(); - for (int i = (showCurrentGpx ? 1 : 0); i < selected.length; i++) { - if (selected[i]) { - s.add(list.get(i)); - } - } - loadGPXFileInDifferentThread(activity, callbackWithObject, dir, currentGPX, - 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(activity, callbackWithObject, dir, null, list.get(which)); - } - } - }); + for(String s : list) { + if (s.endsWith(".gpx")) { + s = s.substring(0, s.length() - ".gpx".length()); + } + s = s.replace('_', ' '); + adapter.item(s).selected(multipleChoice ? 0 : -1) + .icons(R.drawable.ic_action_info_dark, R.drawable.ic_action_info_light).reg(); } + createDialog(activity, showCurrentGpx, multipleChoice, callbackWithObject, list, adapter); + } + } + + private static void setDescripionInDialog(final ArrayAdapter adapter, final ContextMenuAdapter cmAdapter, Activity activity, + final File dir, String filename, final int position) { + final Application app = activity.getApplication(); + final File f = new File(dir, filename); + loadGPXFileInDifferentThread(activity, new CallbackWithObject() { - 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 + @Override + public boolean processResult(GPXFile result) { + cmAdapter.setItemName(position, getDescription((OsmandApplication) app, result, f)); + adapter.notifyDataSetInvalidated(); + return true; } + }, dir, null, filename); + } + + private static void createDialog(final Activity activity, final boolean showCurrentGpx, + final boolean multipleChoice, final CallbackWithObject callbackWithObject, + final List list, final ContextMenuAdapter adapter) { + final OsmandApplication app = (OsmandApplication) activity.getApplication(); + final File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR); + Builder b = new AlertDialog.Builder(activity); + // final int padding = (int) (12 * activity.getResources().getDisplayMetrics().density + 0.5f); + final boolean light = app.getSettings().isLightContentMenu(); + final int layout; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { + layout = R.layout.list_menu_item; + } else { + layout = R.layout.list_menu_item_native; + } + + final ArrayAdapter listAdapter = new ArrayAdapter(activity, layout, R.id.title, + adapter.getItemNames()) { + @Override + public View getView(final int position, View convertView, ViewGroup parent) { + // User super class to create the View + View v = activity.getLayoutInflater().inflate(layout, null); + ImageView icon = (ImageView) v.findViewById(R.id.icon); + icon.setImageResource(adapter.getImageId(position, light)); + final ArrayAdapter arrayAdapter = this; + icon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (showCurrentGpx && position == 0) { + return; + } + setDescripionInDialog(arrayAdapter, adapter, activity, dir, list.get(position), position); + } + + }); + icon.setVisibility(View.VISIBLE); + TextView tv = (TextView) v.findViewById(R.id.title); + tv.setText(adapter.getItemName(position)); + tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); + + // Put the image on the TextView + // if(adapter.getImageId(position, light) != 0) { + // tv.setCompoundDrawablesWithIntrinsicBounds(adapter.getImageId(position, light), 0, 0, 0); + // } + // tv.setCompoundDrawablePadding(padding); + final CheckBox ch = ((CheckBox) v.findViewById(R.id.check_item)); + if (adapter.getSelection(position) == -1) { + ch.setVisibility(View.INVISIBLE); + } else { + ch.setOnCheckedChangeListener(null); + ch.setChecked(adapter.getSelection(position) > 0); + ch.setOnCheckedChangeListener(new OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + } + }); + } + return v; + } + }; + + OnClickListener onClickListener = new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int position) { + } + }; + b.setAdapter(listAdapter, onClickListener); + if (multipleChoice) { + b.setPositiveButton(R.string.default_buttons_ok, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + GPXFile currentGPX = null; + if (showCurrentGpx && adapter.getSelection(0) > 0) { + currentGPX = new GPXFile(); + currentGPX.showCurrentTrack = true; + } + List s = new ArrayList(); + for (int i = (showCurrentGpx ? 1 : 0); i < adapter.length(); i++) { + if (adapter.getSelection(i) > 0) { + s.add(list.get(i)); + } + } + loadGPXFileInDifferentThread(activity, callbackWithObject, dir, currentGPX, + s.toArray(new String[s.size()])); + } + }); + } + + final AlertDialog dlg = b.create(); + dlg.setCanceledOnTouchOutside(true); + dlg.getListView().setOnItemClickListener(new OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + if (multipleChoice) { + adapter.setSelection(position, adapter.getSelection(position) > 0 ? 0 : 1); + listAdapter.notifyDataSetInvalidated(); + } else { + dlg.dismiss(); + if (showCurrentGpx && position == 0) { + callbackWithObject.processResult(null); + } else { + loadGPXFileInDifferentThread(activity, callbackWithObject, dir, null, list.get(position)); + } + } + } + }); + dlg.show(); + try { + dlg.getListView().setFastScrollEnabled(true); + } catch (Exception e) { + // java.lang.ClassCastException: com.android.internal.widget.RoundCornerListAdapter + // Unknown reason but on some devices fail } } diff --git a/OsmAnd/src/net/osmand/plus/views/controls/MapRoutePreferencesControl.java b/OsmAnd/src/net/osmand/plus/views/controls/MapRoutePreferencesControl.java index 48387ccc73..92679c929f 100644 --- a/OsmAnd/src/net/osmand/plus/views/controls/MapRoutePreferencesControl.java +++ b/OsmAnd/src/net/osmand/plus/views/controls/MapRoutePreferencesControl.java @@ -1,12 +1,16 @@ package net.osmand.plus.views.controls; +import java.io.File; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; +import net.osmand.CallbackWithObject; import net.osmand.data.RotatedTileBox; import net.osmand.plus.ApplicationMode; +import net.osmand.plus.GPXUtilities; +import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings.CommonPreference; import net.osmand.plus.R; @@ -14,6 +18,7 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.activities.SettingsNavigationActivity; import net.osmand.plus.activities.actions.AppModeDialog; +import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.views.OsmandMapLayer.DrawSettings; import net.osmand.router.GeneralRouter; import net.osmand.router.GeneralRouter.RoutingParameter; @@ -30,11 +35,11 @@ import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; +import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; -import android.widget.AdapterView; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.ListView; @@ -46,7 +51,7 @@ public class MapRoutePreferencesControl extends MapControls { private OsmandSettings settings; private int cachedId; private Dialog dialog; - private String selectedGPXFile = null; + private GPXFile selectedGPXFile = null; public MapRoutePreferencesControl(MapActivity mapActivity, Handler showUIHandler, float scaleCoefficient) { super(mapActivity, showUIHandler, scaleCoefficient); @@ -195,9 +200,16 @@ public class MapRoutePreferencesControl extends MapControls { }); } - protected void openGPXFileSelection(Spinner gpxSpinner) { - // TODO Auto-generated method stub - updateSpinnerItems(gpxSpinner); + protected void openGPXFileSelection(final Spinner gpxSpinner) { + GpxUiHelper.selectGPXFile(mapActivity, false, false, new CallbackWithObject() { + + @Override + public boolean processResult(GPXFile result) { + selectedGPXFile = result; + updateSpinnerItems(gpxSpinner); + return true; + } + }); } private void updateSpinnerItems(Spinner gpxSpinner) { @@ -205,7 +217,7 @@ public class MapRoutePreferencesControl extends MapControls { gpxActions.add(mapActivity.getString(R.string.default_none)); gpxActions.add(mapActivity.getString(R.string.select_gpx)); if(selectedGPXFile != null) { - gpxActions.add(selectedGPXFile); + gpxActions.add(new File(selectedGPXFile.path).getName()); } ArrayAdapter gpxAdapter = new ArrayAdapter(mapActivity,