From 64e9950ed9802f3ddad0d6d06d695ca619ddcdef Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Mon, 25 Jul 2016 20:51:25 +0300 Subject: [PATCH] [Quick search] in progress --- OsmAnd/res/layout/search_dialog_fragment.xml | 2 +- OsmAnd/res/values/strings.xml | 2 +- OsmAnd/src/net/osmand/plus/GPXUtilities.java | 131 ------- .../osmand/plus/activities/MapActivity.java | 10 +- .../editors/PointEditorFragment.java | 31 +- .../search/QuickSearchDialogFragment.java | 362 ++++++++++-------- .../plus/search/QuickSearchListAdapter.java | 14 +- .../plus/search/QuickSearchListFragment.java | 1 + .../plus/search/QuickSearchListItem.java | 13 +- .../mapwidgets/MapInfoWidgetsFactory.java | 5 +- .../mapwidgets/MapMarkersWidgetsFactory.java | 2 +- 11 files changed, 245 insertions(+), 328 deletions(-) diff --git a/OsmAnd/res/layout/search_dialog_fragment.xml b/OsmAnd/res/layout/search_dialog_fragment.xml index e49f008df5..a786f8ba17 100644 --- a/OsmAnd/res/layout/search_dialog_fragment.xml +++ b/OsmAnd/res/layout/search_dialog_fragment.xml @@ -43,7 +43,7 @@ android:id="@+id/searchProgressBar" android:layout_width="32dp" android:layout_height="32dp" - android:layout_marginRight="16dp" + android:layout_marginRight="8dp" android:indeterminate="true" android:visibility="gone"/> diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 87adc9ec11..b6c3dea7e7 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -18,7 +18,7 @@ \u2022 Many other improvements and bug fixes.\n\n and moreā€¦ - %1$s away + Search %1$s away Show on the map shared via OsmAnd Categories diff --git a/OsmAnd/src/net/osmand/plus/GPXUtilities.java b/OsmAnd/src/net/osmand/plus/GPXUtilities.java index de04ceab16..d504c3fb1d 100644 --- a/OsmAnd/src/net/osmand/plus/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/plus/GPXUtilities.java @@ -1182,137 +1182,6 @@ public class GPXUtilities { return res; } - public static GPXFile loadWptPt(Context ctx, File f) { - FileInputStream fis = null; - try { - fis = new FileInputStream(f); - GPXFile file = loadWptPt(ctx, fis); - file.path = f.getAbsolutePath(); - try { - fis.close(); - } catch (IOException e) { - } - return file; - } catch (FileNotFoundException e) { - GPXFile res = new GPXFile(); - res.path = f.getAbsolutePath(); - log.error("Error reading gpx", e); //$NON-NLS-1$ - res.warning = ctx.getString(R.string.error_reading_gpx); - return res; - } finally { - try { - if (fis != null) - fis.close(); - } catch (IOException ignore) { - // ignore - } - } - } - - public static GPXFile loadWptPt(Context ctx, InputStream f) { - GPXFile res = new GPXFile(); - SimpleDateFormat format = new SimpleDateFormat(GPX_TIME_FORMAT, Locale.US); - format.setTimeZone(TimeZone.getTimeZone("UTC")); - try { - XmlPullParser parser = PlatformUtil.newXMLPullParser(); - parser.setInput(getUTF8Reader(f)); //$NON-NLS-1$ - Stack parserState = new Stack(); - boolean extensionReadMode = false; - parserState.push(res); - int tok; - while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) { - if (tok == XmlPullParser.START_TAG) { - Object parse = parserState.peek(); - String tag = parser.getName(); - if (extensionReadMode && parse instanceof GPXExtensions) { - String value = readText(parser, tag); - if (value != null) { - ((GPXExtensions) parse).getExtensionsToWrite().put(tag, value); - if (tag.equals("speed") && parse instanceof WptPt) { - try { - ((WptPt) parse).speed = Float.parseFloat(value); - } catch (NumberFormatException e) { - } - } - } - - } else if (parse instanceof GPXExtensions && tag.equals("extensions")) { - extensionReadMode = true; - } else { - if (parse instanceof GPXFile) { - if (parser.getName().equals("wpt")) { - WptPt wptPt = parseWptAttributes(parser); - ((GPXFile) parse).points.add(wptPt); - parserState.push(wptPt); - } - } else if (parse instanceof WptPt) { - if (parser.getName().equals("name")) { - ((WptPt) parse).name = readText(parser, "name"); - } else if (parser.getName().equals("desc")) { - ((WptPt) parse).desc = readText(parser, "desc"); - } else if (parser.getName().equals("link")) { - ((WptPt) parse).link = parser.getAttributeValue("", "href"); - } else if (tag.equals("category")) { - ((WptPt) parse).category = readText(parser, "category"); - } else if (tag.equals("type")) { - if (((WptPt) parse).category == null) { - ((WptPt) parse).category = readText(parser, "type"); - } - } else if (parser.getName().equals("ele")) { - String text = readText(parser, "ele"); - if (text != null) { - try { - ((WptPt) parse).ele = Float.parseFloat(text); - } catch (NumberFormatException e) { - } - } - } else if (parser.getName().equals("hdop")) { - String text = readText(parser, "hdop"); - if (text != null) { - try { - ((WptPt) parse).hdop = Float.parseFloat(text); - } catch (NumberFormatException e) { - } - } - } else if (parser.getName().equals("time")) { - String text = readText(parser, "time"); - if (text != null) { - try { - ((WptPt) parse).time = format.parse(text).getTime(); - } catch (ParseException e) { - } - } - } - } - } - - } else if (tok == XmlPullParser.END_TAG) { - Object parse = parserState.peek(); - String tag = parser.getName(); - if (parse instanceof GPXExtensions && tag.equals("extensions")) { - extensionReadMode = false; - } - - if (tag.equals("wpt")) { - Object pop = parserState.pop(); - assert pop instanceof WptPt; - } - } - } - } catch (RuntimeException e) { - log.error("Error reading gpx", e); //$NON-NLS-1$ - res.warning = ctx.getString(R.string.error_reading_gpx) + " " + e.getMessage(); - } catch (XmlPullParserException e) { - log.error("Error reading gpx", e); //$NON-NLS-1$ - res.warning = ctx.getString(R.string.error_reading_gpx) + " " + e.getMessage(); - } catch (IOException e) { - log.error("Error reading gpx", e); //$NON-NLS-1$ - res.warning = ctx.getString(R.string.error_reading_gpx) + " " + e.getMessage(); - } - - return res; - } - private static Reader getUTF8Reader(InputStream f) throws IOException { BufferedInputStream bis = new BufferedInputStream(f); assert bis.markSupported(); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index a586c8621c..f6262bc471 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -168,7 +168,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven private boolean permissionGranted; private boolean mIsDestroyed = false; - private boolean quickSearchActive = false; + private boolean quickSearchTopbarActive = false; @Override public void onCreate(Bundle savedInstanceState) { @@ -1381,11 +1381,11 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven return fragment!= null && !fragment.isDetached() && !fragment.isRemoving() ? (QuickSearchDialogFragment) fragment : null; } - public boolean isQuickSearchDialogActive() { - return quickSearchActive && getQuickSearchDialogFragment() != null; + public boolean isQuickSearchTopbarActive() { + return quickSearchTopbarActive && getQuickSearchDialogFragment() != null; } - public void setQuickSearchActive(boolean quickSearchActive) { - this.quickSearchActive = quickSearchActive; + public void setQuickSearchTopbarActive(boolean quickSearchTopbarActive) { + this.quickSearchTopbarActive = quickSearchTopbarActive; } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java index 7a663778a1..6781ea71a1 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/PointEditorFragment.java @@ -15,6 +15,7 @@ import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import android.view.ViewTreeObserver; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; @@ -130,15 +131,29 @@ public abstract class PointEditorFragment extends Fragment { descriptionEdit.setText(getDescriptionInitValue()); } - view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { - @Override - public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { - if (descriptionEdit.isFocused()) { - ScrollView scrollView = (ScrollView) view.findViewById(R.id.editor_scroll_view); - scrollView.scrollTo(0, bottom); + if (Build.VERSION.SDK_INT >= 11) { + view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { + @Override + public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { + if (descriptionEdit.isFocused()) { + ScrollView scrollView = (ScrollView) view.findViewById(R.id.editor_scroll_view); + scrollView.scrollTo(0, bottom); + } } - } - }); + }); + } else { + ViewTreeObserver vto = view.getViewTreeObserver(); + vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + + @Override + public void onGlobalLayout() { + if (descriptionEdit.isFocused()) { + ScrollView scrollView = (ScrollView) view.findViewById(R.id.editor_scroll_view); + scrollView.scrollTo(0, view.getBottom()); + } + } + }); + } ImageView nameImage = (ImageView) view.findViewById(R.id.name_image); nameImage.setImageDrawable(getNameIcon()); diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 906f3e6e48..0084d651f6 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -1,12 +1,14 @@ package net.osmand.plus.search; import android.annotation.SuppressLint; +import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; import android.os.AsyncTask; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.design.widget.TabLayout; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; @@ -30,28 +32,29 @@ import android.widget.ImageView; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; + import net.osmand.AndroidUtils; -import net.osmand.IndexConstants; import net.osmand.Location; import net.osmand.ResultMatcher; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; +import net.osmand.data.PointDescription; import net.osmand.osm.AbstractPoiType; import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.WptPt; +import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.LockableViewPager; +import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener; import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; -import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry; -import net.osmand.plus.myplaces.AvailableGPXFragment.GpxInfo; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.resources.RegionAddressRepository; import net.osmand.search.SearchUICore; @@ -71,7 +74,6 @@ import net.osmand.util.MapUtils; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -107,6 +109,8 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC private net.osmand.Location location = null; private Float heading = null; + private boolean useMapCenter; + private boolean paused; public static final int SEARCH_FAVORITE_API_PRIORITY = 2; public static final int SEARCH_FAVORITE_OBJECT_PRIORITY = 10; @@ -154,11 +158,16 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC view.findViewById(R.id.buttonToolbar).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { + OsmandSettings settings = app.getSettings(); SearchPhrase searchPhrase = searchUICore.getPhrase(); - if (searchPhrase.isLastWord(ObjectType.POI_TYPE)) { - OsmandSettings settings = app.getSettings(); - AbstractPoiType abstractPoiType = (AbstractPoiType) searchPhrase.getLastSelectedWord().getResult().object; - PoiUIFilter filter = new PoiUIFilter(abstractPoiType, app, ""); + if (searchPhrase.isNoSelectedType() || searchPhrase.isLastWord(ObjectType.POI_TYPE)) { + PoiUIFilter filter; + if (searchPhrase.isNoSelectedType()) { + filter = new PoiUIFilter(null, app, ""); + } else { + AbstractPoiType abstractPoiType = (AbstractPoiType) searchPhrase.getLastSelectedWord().getResult().object; + filter = new PoiUIFilter(abstractPoiType, app, ""); + } if (!Algorithms.isEmpty(searchPhrase.getUnknownSearchWord())) { filter.setFilterByName(searchPhrase.getUnknownSearchWord()); } @@ -167,8 +176,28 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC if (location != null) { settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), 15); } + getMapActivity().setQuickSearchTopbarActive(searchPhrase.isNoSelectedType()); MapActivity.launchMapActivityMoveToTop(getActivity()); - hide(); + if (searchPhrase.isNoSelectedType()) { + hide(); + } else { + dismiss(); + } + } else { + SearchWord word = searchPhrase.getLastSelectedWord(); + if (word != null && word.getLocation() != null) { + SearchResult searchResult = word.getResult(); + String name = QuickSearchListItem.getName(app, searchResult); + String typeName = QuickSearchListItem.getTypeName(app, searchResult); + PointDescription pointDescription = new PointDescription(PointDescription.POINT_TYPE_ADDRESS, typeName, name); + getMyApplication().getSettings().setMapLocationToShow( + searchResult.location.getLatitude(), searchResult.location.getLongitude(), + searchResult.preferredZoom, pointDescription, true, searchResult.object); + + getMapActivity().setQuickSearchTopbarActive(true); + MapActivity.launchMapActivityMoveToTop(getActivity()); + hide(); + } } } }); @@ -212,28 +241,30 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC view.findViewById(R.id.deleteButton).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - /*) - final AlertDialog.Builder builder = new AlertDialog.Builder(app); - builder.setTitle(R.string.confirmation_to_delete_history_items); - builder.setView(view); - builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { + + new DialogFragment() { + @NonNull @Override - public void onClick(DialogInterface dialog, int which) { - */ - SearchHistoryHelper helper = SearchHistoryHelper.getInstance(app); - List selectedItems = historySearchFragment.getListAdapter().getSelectedItems(); - for (QuickSearchListItem searchListItem : selectedItems) { - HistoryEntry historyEntry = (HistoryEntry) searchListItem.getSearchResult().object; - helper.remove(historyEntry); - } - reloadHistory(); - enableSelectionMode(false, -1); - /* + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(R.string.confirmation_to_delete_history_items) + .setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + SearchHistoryHelper helper = SearchHistoryHelper.getInstance(app); + List selectedItems = historySearchFragment.getListAdapter().getSelectedItems(); + for (QuickSearchListItem searchListItem : selectedItems) { + HistoryEntry historyEntry = (HistoryEntry) searchListItem.getSearchResult().object; + helper.remove(historyEntry); + } + reloadHistory(); + enableSelectionMode(false, -1); + } + }) + .setNegativeButton(R.string.shared_string_no, null); + return builder.create(); } - }); - builder.setNegativeButton(R.string.shared_string_no, null); - builder.create().show(); - */ + }.show(getChildFragmentManager(), "DeleteHistoryConfirmationFragment"); } }); @@ -257,6 +288,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC @Override public void afterTextChanged(Editable s) { String newQueryText = s.toString(); + updateClearButtonAndHint(); updateClearButtonVisibility(newQueryText.length() > 0); updateTabbarVisibility(newQueryText.length() == 0); if (!searchQuery.equalsIgnoreCase(newQueryText)) { @@ -282,21 +314,22 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } else { buttonToolbarText.setText(app.getString(R.string.show_on_map).toUpperCase()); } - } else { - searchEditText.setHint(R.string.search_poi_category_hint); - clearButton.setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_action_remove_dark)); - if(location != null && searchUICore != null) { - LatLon centerLatLon = new LatLon(location.getLatitude(), location.getLongitude()); - SearchSettings ss = searchUICore.getSearchSettings().setOriginalLocation( - new LatLon(centerLatLon.getLatitude(), centerLatLon.getLongitude())); - searchUICore.updateSettings(ss); - } - - } + } else if (useMapCenter && location != null) { + useMapCenter = false; + updateUseMapCenterUI(); + startLocationUpdate(); + LatLon centerLatLon = new LatLon(location.getLatitude(), location.getLongitude()); + SearchSettings ss = searchUICore.getSearchSettings().setOriginalLocation( + new LatLon(centerLatLon.getLatitude(), centerLatLon.getLongitude())); + searchUICore.updateSettings(ss); + updateClearButtonAndHint(); + } } }); + setupSearch(mapActivity); + updateClearButtonAndHint(); addMainSearchFragment(); searchEditText.requestFocus(); @@ -310,6 +343,14 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } public void show() { + getMapActivity().setQuickSearchTopbarActive(false); + if (useMapCenter) { + LatLon mapCenter = getMapActivity().getMapView().getCurrentRotatedTileBox().getCenterLatLon(); + SearchSettings ss = searchUICore.getSearchSettings().setOriginalLocation( + new LatLon(mapCenter.getLatitude(), mapCenter.getLongitude())); + searchUICore.updateSettings(ss); + updateLocationUI(mapCenter, null); + } getDialog().show(); } @@ -347,37 +388,16 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } searchUICore = new SearchUICore(app.getPoiTypes(), locale, binaryMapIndexReaderArray); - /* - List files = new ArrayList<>(); - File file = new File(Environment.getExternalStorageDirectory() + "/osmand"); - if (file.exists() && file.listFiles() != null) { - for (File obf : file.listFiles()) { - if (!obf.isDirectory() && obf.getName().endsWith(".obf")) { - try { - BinaryMapIndexReader bmir = new BinaryMapIndexReader(new RandomAccessFile(obf, "r"), obf); - files.add(bmir); - } catch (Exception e1) { - e1.printStackTrace(); - } - } - } - } - - searchUICore = new SearchUICore(app.getPoiTypes(), locale, files.toArray(new BinaryMapIndexReader[files.size()])); - */ - + location = app.getLocationProvider().getLastKnownLocation(); LatLon clt = mapActivity.getMapView().getCurrentRotatedTileBox().getCenterLatLon(); LatLon centerLatLon = clt; searchEditText.setHint(R.string.search_poi_category_hint); if (location != null) { double d = MapUtils.getDistance(clt, location.getLatitude(), location.getLongitude()); - if(d < DISTANCE_THRESHOLD) { + if (d < DISTANCE_THRESHOLD) { centerLatLon = new LatLon(location.getLatitude(), location.getLongitude()); } else { - String n = getString(R.string.search_poi_category_hint); - String dist = OsmAndFormatter.getFormattedDistance((float) d, mapActivity.getMyApplication()); - searchEditText.setHint(n +", " + getString(R.string.dist_away_from_my_location, dist)); - clearButton.setImageDrawable(app.getIconsCache().getIcon(R.drawable.ic_action_get_my_location, R.color.color_myloc_distance)); + useMapCenter = true; } } SearchSettings settings = searchUICore.getPhrase().getSettings().setOriginalLocation( @@ -415,6 +435,8 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC resultMatcher.publish(sr); } else if (phrase.getNameStringMatcher().matches(sr.localeName)) { resultMatcher.publish(sr); + } else if (point.getCategory() != null && phrase.getNameStringMatcher().matches(point.getCategory())) { + resultMatcher.publish(sr); } } return true; @@ -447,31 +469,48 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC @Override public void onResume() { super.onResume(); - OsmandApplication app = getMyApplication(); - app.getLocationProvider().addCompassListener(this); - app.getLocationProvider().addLocationListener(this); - location = app.getLocationProvider().getLastKnownLocation(); - updateLocation(location); - getMapActivity().setQuickSearchActive(true); + if (!useMapCenter) { + startLocationUpdate(); + } + paused = false; } @Override public void onPause() { super.onPause(); - OsmandApplication app = getMyApplication(); - getMapActivity().setQuickSearchActive(false); + paused = true; + stopLocationUpdate(); + } + + @Override + public void onDismiss(DialogInterface dialog) { + getMapActivity().setQuickSearchTopbarActive(false); getChildFragmentManager().popBackStack(); - app.getLocationProvider().removeLocationListener(this); - app.getLocationProvider().removeCompassListener(this); - mainSearchFragment = null; - historySearchFragment = null; - categoriesSearchFragment = null; + super.onDismiss(dialog); } public Toolbar getToolbar() { return toolbar; } + public boolean isUseMapCenter() { + return useMapCenter; + } + + private void startLocationUpdate() { + OsmandApplication app = getMyApplication(); + app.getLocationProvider().addCompassListener(this); + app.getLocationProvider().addLocationListener(this); + location = app.getLocationProvider().getLastKnownLocation(); + updateLocation(location); + } + + private void stopLocationUpdate() { + OsmandApplication app = getMyApplication(); + app.getLocationProvider().removeLocationListener(this); + app.getLocationProvider().removeCompassListener(this); + } + private void showProgressBar() { updateClearButtonVisibility(false); progressBar.setVisibility(View.VISIBLE); @@ -482,9 +521,23 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC progressBar.setVisibility(View.GONE); } + private void updateClearButtonAndHint() { + OsmandApplication app = getMyApplication(); + if (useMapCenter && searchEditText.length() == 0) { + LatLon latLon = searchUICore.getSearchSettings().getOriginalLocation(); + double d = MapUtils.getDistance(latLon, location.getLatitude(), location.getLongitude()); + String dist = OsmAndFormatter.getFormattedDistance((float) d, app); + searchEditText.setHint(getString(R.string.dist_away_from_my_location, dist)); + clearButton.setImageDrawable(app.getIconsCache().getIcon(R.drawable.ic_action_get_my_location, R.color.color_myloc_distance)); + } else { + searchEditText.setHint(R.string.search_poi_category_hint); + clearButton.setImageDrawable(app.getIconsCache().getThemedIcon(R.drawable.ic_action_remove_dark)); + } + } + private void updateClearButtonVisibility(boolean show) { if (show) { - clearButton.setVisibility(searchEditText.length() > 0 ? View.VISIBLE : View.GONE); + clearButton.setVisibility(searchEditText.length() > 0 || useMapCenter ? View.VISIBLE : View.GONE); } else { clearButton.setVisibility(View.GONE); } @@ -505,7 +558,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } public void onSearchListFragmentResume(QuickSearchListFragment searchListFragment) { - SearchPhrase sp; switch (searchListFragment.getType()) { case HISTORY: historySearchFragment = (QuickSearchHistoryListFragment) searchListFragment; @@ -526,6 +578,10 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } break; } + LatLon mapCenter = getMapActivity().getMapView().getCurrentRotatedTileBox().getCenterLatLon(); + if (useMapCenter) { + searchListFragment.updateLocation(mapCenter, null); + } } private void reloadCategories() { @@ -613,7 +669,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC @Override public boolean publish(SearchResult object) { - if (mainSearchFragment == null) { + if (paused) { return false; } @@ -689,7 +745,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC @Override public boolean isCancelled() { - return mainSearchFragment == null; + return paused; } }); } @@ -719,7 +775,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } }); - if (mainSearchFragment != null) { + if (!paused && mainSearchFragment != null) { mainSearchFragment.addListItem(moreListItem); } } @@ -727,15 +783,14 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC private void updateSearchResult(SearchResultCollection res, boolean appended) { - OsmandApplication app = getMyApplication(); - - List rows = new ArrayList<>(); - if (res.getCurrentSearchResults().size() > 0) { - for (final SearchResult sr : res.getCurrentSearchResults()) { - rows.add(new QuickSearchListItem(app, sr)); + if (!paused && mainSearchFragment != null) { + OsmandApplication app = getMyApplication(); + List rows = new ArrayList<>(); + if (res.getCurrentSearchResults().size() > 0) { + for (final SearchResult sr : res.getCurrentSearchResults()) { + rows.add(new QuickSearchListItem(app, sr)); + } } - } - if (mainSearchFragment != null) { mainSearchFragment.updateListAdapter(rows, appended); } } @@ -844,14 +899,34 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC if (location != null) { latLon = new LatLon(location.getLatitude(), location.getLongitude()); } - if (mainSearchFragment != null) { - mainSearchFragment.updateLocation(latLon, heading); + updateLocationUI(latLon, heading); + } + + private void updateLocationUI(LatLon latLon, Float heading) { + if (latLon != null && !paused) { + if (mainSearchFragment != null) { + mainSearchFragment.updateLocation(latLon, heading); + } + if (historySearchFragment != null) { + historySearchFragment.updateLocation(latLon, heading); + } + if (categoriesSearchFragment != null) { + categoriesSearchFragment.updateLocation(latLon, heading); + } } - if (historySearchFragment != null) { - historySearchFragment.updateLocation(latLon, heading); - } - if (categoriesSearchFragment != null) { - categoriesSearchFragment.updateLocation(latLon, heading); + } + + private void updateUseMapCenterUI() { + if (!paused) { + if (mainSearchFragment != null) { + mainSearchFragment.getListAdapter().setUseMapCenter(useMapCenter); + } + if (historySearchFragment != null) { + historySearchFragment.getListAdapter().setUseMapCenter(useMapCenter); + } + if (categoriesSearchFragment != null) { + categoriesSearchFragment.getListAdapter().setUseMapCenter(useMapCenter); + } } } @@ -1069,35 +1144,36 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC public static class SearchWptAPI extends SearchBaseAPI { private OsmandApplication app; - private LoadGpxTask asyncLoader = new LoadGpxTask(); public SearchWptAPI(OsmandApplication app) { this.app = app; - asyncLoader.execute(); } @Override public boolean search(SearchPhrase phrase, SearchResultMatcher resultMatcher) { - if (asyncLoader.getResult() == null) { + + if (phrase.isEmpty()) { return false; } - List infos = asyncLoader.getResult(); - for (GpxInfo info : infos) { - for (WptPt point : info.gpx.points) { - SearchResult sr = new SearchResult(phrase); - sr.localeName = point.getPointDescription(app).getName(); - sr.object = point; - sr.priority = SEARCH_WPT_OBJECT_PRIORITY; - sr.objectType = ObjectType.WPT; - sr.location = new LatLon(point.getLatitude(), point.getLongitude()); - sr.localeRelatedObjectName = app.getRegions().getCountryName(sr.location); - sr.relatedObject = info; - sr.preferredZoom = 17; - if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) { - resultMatcher.publish(sr); - } else if (phrase.getNameStringMatcher().matches(sr.localeName)) { - resultMatcher.publish(sr); + List list = app.getSelectedGpxHelper().getSelectedGPXFiles(); + for (SelectedGpxFile selectedGpx : list) { + if (selectedGpx != null) { + for (WptPt point : selectedGpx.getGpxFile().points) { + SearchResult sr = new SearchResult(phrase); + sr.localeName = point.getPointDescription(app).getName(); + sr.object = point; + sr.priority = SEARCH_WPT_OBJECT_PRIORITY; + sr.objectType = ObjectType.WPT; + sr.location = new LatLon(point.getLatitude(), point.getLongitude()); + sr.localeRelatedObjectName = app.getRegions().getCountryName(sr.location); + sr.relatedObject = selectedGpx.getGpxFile(); + sr.preferredZoom = 17; + if (phrase.getUnknownSearchWordLength() <= 1 && phrase.isNoSelectedType()) { + resultMatcher.publish(sr); + } else if (phrase.getNameStringMatcher().matches(sr.localeName)) { + resultMatcher.publish(sr); + } } } } @@ -1111,59 +1187,5 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } return SEARCH_WPT_API_PRIORITY; } - - - public class LoadGpxTask extends AsyncTask> { - - private List result; - - @Override - protected List doInBackground(Void... params) { - List result = new ArrayList<>(); - loadGPXData(app.getAppPath(IndexConstants.GPX_INDEX_DIR), result, this); - return result; - } - - @Override - protected void onPostExecute(List result) { - this.result = result; - } - - private File[] listFilesSorted(File dir) { - File[] listFiles = dir.listFiles(); - if (listFiles == null) { - return new File[0]; - } - Arrays.sort(listFiles); - return listFiles; - } - - private void loadGPXData(File mapPath, List result, LoadGpxTask loadTask) { - if (mapPath.canRead()) { - loadGPXFolder(mapPath, result, loadTask, ""); - } - } - - private void loadGPXFolder(File mapPath, List result, LoadGpxTask loadTask, - String gpxSubfolder) { - for (File gpxFile : listFilesSorted(mapPath)) { - if (gpxFile.isDirectory()) { - String sub = gpxSubfolder.length() == 0 ? gpxFile.getName() : gpxSubfolder + "/" - + gpxFile.getName(); - loadGPXFolder(gpxFile, result, loadTask, sub); - } else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) { - GpxInfo info = new GpxInfo(); - info.subfolder = gpxSubfolder; - info.file = gpxFile; - info.gpx = GPXUtilities.loadWptPt(app, gpxFile); - result.add(info); - } - } - } - - public List getResult() { - return result; - } - } } } diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchListAdapter.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchListAdapter.java index a57b8d7307..a45f129d06 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchListAdapter.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchListAdapter.java @@ -33,6 +33,7 @@ public class QuickSearchListAdapter extends ArrayAdapter { private LatLon location; private Float heading; + private boolean useMapCenter; private int searchMoreItemPosition; private int selectAllItemPosition; @@ -97,6 +98,14 @@ public class QuickSearchListAdapter extends ArrayAdapter { this.heading = heading; } + public boolean isUseMapCenter() { + return useMapCenter; + } + + public void setUseMapCenter(boolean useMapCenter) { + this.useMapCenter = useMapCenter; + } + public boolean isSelectionMode() { return selectionMode; } @@ -376,9 +385,8 @@ public class QuickSearchListAdapter extends ArrayAdapter { TextView distanceText = (TextView) view.findViewById(R.id.distance); ImageView direction = (ImageView) view.findViewById(R.id.direction); - float myHeading = heading == null ? 0f : heading; - DashLocationFragment.updateLocationView(false, location, - myHeading, direction, distanceText, + DashLocationFragment.updateLocationView(useMapCenter, location, + heading, direction, distanceText, listItem.getSearchResult().location.getLatitude(), listItem.getSearchResult().location.getLongitude(), screenOrientation, app, activity); diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchListFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchListFragment.java index 2c17554eb2..d2ce1d03c2 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchListFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchListFragment.java @@ -87,6 +87,7 @@ public abstract class QuickSearchListFragment extends OsmAndListFragment { super.onActivityCreated(savedInstanceState); dialogFragment = (QuickSearchDialogFragment) getParentFragment(); listAdapter = new QuickSearchListAdapter(getMyApplication(), getActivity()); + listAdapter.setUseMapCenter(dialogFragment.isUseMapCenter()); setListAdapter(listAdapter); ListView listView = getListView(); listView.setBackgroundColor(getResources().getColor( diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchListItem.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchListItem.java index 6a89649fa4..412a5e7269 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchListItem.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchListItem.java @@ -15,18 +15,22 @@ import net.osmand.osm.AbstractPoiType; import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiFilter; import net.osmand.osm.PoiType; +import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.WptPt; +import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.search.SearchHistoryFragment; import net.osmand.plus.base.FavoriteImageDrawable; import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry; -import net.osmand.plus.myplaces.AvailableGPXFragment.GpxInfo; +import net.osmand.plus.myplaces.AvailableGPXFragment; import net.osmand.plus.render.RenderingIcons; import net.osmand.search.core.SearchResult; import net.osmand.util.Algorithms; +import java.io.File; + public class QuickSearchListItem { protected OsmandApplication app; @@ -207,16 +211,15 @@ public class QuickSearchListItem { } case WPT: StringBuilder sb = new StringBuilder(); - WptPt wpt = (WptPt) searchResult.object; - GpxInfo gpxInfo = (GpxInfo) searchResult.relatedObject; + GPXFile gpx = (GPXFile) searchResult.relatedObject; if (!Algorithms.isEmpty(searchResult.localeRelatedObjectName)) { sb.append(searchResult.localeRelatedObjectName); } - if (gpxInfo != null && !Algorithms.isEmpty(gpxInfo.getFileName())) { + if (gpx != null && !Algorithms.isEmpty(gpx.path)) { if (sb.length() > 0) { sb.append(", "); } - sb.append(gpxInfo.getFileName()); + sb.append(new File(gpx.path).getName()); } return sb.toString(); case UNKNOWN_NAME_FILTER: diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapInfoWidgetsFactory.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapInfoWidgetsFactory.java index 669b7ffb31..fe071ca9a4 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapInfoWidgetsFactory.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapInfoWidgetsFactory.java @@ -15,7 +15,6 @@ import android.widget.TextView; import net.osmand.Location; import net.osmand.ValueHolder; import net.osmand.binary.RouteDataObject; -import net.osmand.plus.ApplicationMode; import net.osmand.plus.NavigationService; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndLocationProvider; @@ -210,7 +209,7 @@ public class MapInfoWidgetsFactory { } public void updateInfo() { - boolean isQuickSearchActive = map.isQuickSearchDialogActive(); + boolean isQuickSearchActive = map.isQuickSearchTopbarActive(); if (isQuickSearchActive) { QuickSearchDialogFragment fragment = map.getQuickSearchDialogFragment(); if (fragment != null) { @@ -335,7 +334,7 @@ public class MapInfoWidgetsFactory { text = "\u2316+ " + text; } } - if (map.isQuickSearchDialogActive()) { + if (map.isQuickSearchTopbarActive()) { updateVisibility(false); } else if (!showNextTurn && updateWaypoint()) { updateVisibility(true); diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidgetsFactory.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidgetsFactory.java index 05c304fa25..a02d404674 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidgetsFactory.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidgetsFactory.java @@ -203,7 +203,7 @@ public class MapMarkersWidgetsFactory { || map.getMyApplication().getRoutingHelper().isRoutePlanningMode() || map.getMapLayers().getMapControlsLayer().getMapRouteInfoMenu().isVisible() || addressTopBar.getVisibility() == View.VISIBLE - || map.isQuickSearchDialogActive()) { + || map.isQuickSearchTopbarActive()) { updateVisibility(false); return; }