From 0a669f3713716cd9105dd216cc41236d8525bcb3 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sat, 16 Jul 2016 17:06:05 +0300 Subject: [PATCH] [Quick search] added compass to list --- OsmAnd/res/layout/search_list_item.xml | 30 +++++-- .../search/QuickSearchDialogFragment.java | 76 +++++++++++++++-- .../osmand/plus/search/SearchListAdapter.java | 85 +++++++++++++++---- .../osmand/plus/search/SearchListItem.java | 10 +-- 4 files changed, 161 insertions(+), 40 deletions(-) diff --git a/OsmAnd/res/layout/search_list_item.xml b/OsmAnd/res/layout/search_list_item.xml index 304bf98861..e17a434043 100644 --- a/OsmAnd/res/layout/search_list_item.xml +++ b/OsmAnd/res/layout/search_list_item.xml @@ -41,14 +41,30 @@ android:textSize="@dimen/default_list_text_size" tools:text="Amsterdam"/> - + android:layout_height="match_parent" + android:orientation="horizontal"> + + + + + + diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index be562cadd8..d485e27262 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -19,15 +19,19 @@ import android.widget.ListView; import android.widget.ProgressBar; import net.osmand.AndroidUtils; +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.plus.OsmAndLocationProvider.OsmAndCompassListener; +import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; 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.dashboard.DashLocationFragment; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry; import net.osmand.plus.resources.RegionAddressRepository; @@ -46,7 +50,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -public class QuickSearchDialogFragment extends DialogFragment { +public class QuickSearchDialogFragment extends DialogFragment implements OsmAndCompassListener, OsmAndLocationListener { public static final String TAG = "QuickSearchDialogFragment"; private static final String QUICK_SEARCH_QUERY_KEY = "quick_search_query_key"; @@ -60,6 +64,11 @@ public class QuickSearchDialogFragment extends DialogFragment { private String searchQuery = ""; private SearchResultCollection resultCollection; + private net.osmand.Location location = null; + private Float heading = null; + + private int screenOrientation; + public static final int SEARCH_FAVORITE_API_PRIORITY = 3; public static final int SEARCH_FAVORITE_OBJECT_PRIORITY = 10; public static final int SEARCH_HISTORY_API_PRIORITY = 3; @@ -102,7 +111,7 @@ public class QuickSearchDialogFragment extends DialogFragment { setupSearch(mapActivity); listView = (ListView) view.findViewById(android.R.id.list); - listAdapter = new SearchListAdapter(getMyApplication()); + listAdapter = new SearchListAdapter(getMyApplication(), getActivity()); listView.setAdapter(listAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override @@ -308,12 +317,28 @@ public class QuickSearchDialogFragment extends DialogFragment { @Override public void onResume() { super.onResume(); + screenOrientation = DashLocationFragment.getScreenOrientation(getActivity()); + listAdapter.setScreenOrientation(screenOrientation); + OsmandApplication app = getMyApplication(); + app.getLocationProvider().addCompassListener(this); + app.getLocationProvider().addLocationListener(this); + location = app.getLocationProvider().getLastKnownLocation(); + updateLocation(location); + if (!Algorithms.isEmpty(searchQuery)) { searchEditText.setText(searchQuery); searchEditText.setSelection(searchQuery.length()); } } + @Override + public void onPause() { + super.onPause(); + OsmandApplication app = getMyApplication(); + app.getLocationProvider().removeLocationListener(this); + app.getLocationProvider().removeCompassListener(this); + } + private void showProgressBar() { clearButton.setVisibility(View.GONE); progressBar.setVisibility(View.VISIBLE); @@ -412,12 +437,7 @@ public class QuickSearchDialogFragment extends DialogFragment { rows.add(moreListItem); } for (final SearchResult sr : res.getCurrentSearchResults()) { - SearchListItem listItem = new SearchListItem(app, sr); - if (sr.location != null) { - LatLon location = res.getPhrase().getLastTokenLocation(); - listItem.setDistance(MapUtils.getDistance(location, sr.location)); - } - rows.add(listItem); + rows.add(new SearchListItem(app, sr)); } } updateListAdapter(rows); @@ -516,4 +536,44 @@ public class QuickSearchDialogFragment extends DialogFragment { return (OsmandApplication) getActivity().getApplication(); } + @Override + public void updateCompassValue(final float value) { + // 99 in next line used to one-time initialize arrows (with reference vs. fixed-north direction) + // on non-compass devices + float lastHeading = heading != null ? heading : 99; + heading = value; + if (Math.abs(MapUtils.degreesDiff(lastHeading, heading)) > 5) { + final Location location = this.location; + getMyApplication().runInUIThread(new Runnable() { + @Override + public void run() { + updateLocationUI(location, value); + } + }); + } else { + heading = lastHeading; + } + } + + @Override + public void updateLocation(final Location location) { + this.location = location; + final Float heading = this.heading; + getMyApplication().runInUIThread(new Runnable() { + @Override + public void run() { + updateLocationUI(location, heading); + } + }); + } + + private void updateLocationUI(Location location, Float heading) { + LatLon latLon = null; + if (location != null) { + latLon = new LatLon(location.getLatitude(), location.getLongitude()); + } + listAdapter.setLocation(latLon); + listAdapter.setHeading(heading); + listAdapter.notifyDataSetChanged(); + } } diff --git a/OsmAnd/src/net/osmand/plus/search/SearchListAdapter.java b/OsmAnd/src/net/osmand/plus/search/SearchListAdapter.java index 320938d7ac..4feb4ed22d 100644 --- a/OsmAnd/src/net/osmand/plus/search/SearchListAdapter.java +++ b/OsmAnd/src/net/osmand/plus/search/SearchListAdapter.java @@ -1,5 +1,6 @@ package net.osmand.plus.search; +import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; @@ -9,21 +10,52 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; -import net.osmand.plus.OsmAndFormatter; +import net.osmand.Location; +import net.osmand.data.LatLon; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.util.Algorithms; import java.util.List; public class SearchListAdapter extends ArrayAdapter { - private OsmandApplication ctx; + private OsmandApplication app; + private Activity activity; + private LatLon location; + private Float heading; private boolean hasSearchMoreItem; + private int screenOrientation; - public SearchListAdapter(OsmandApplication ctx) { - super(ctx, R.layout.search_list_item); - this.ctx = ctx; + public SearchListAdapter(OsmandApplication app, Activity activity) { + super(app, R.layout.search_list_item); + this.app = app; + this.activity = activity; + } + + public int getScreenOrientation() { + return screenOrientation; + } + + public void setScreenOrientation(int screenOrientation) { + this.screenOrientation = screenOrientation; + } + + public LatLon getLocation() { + return location; + } + + public void setLocation(LatLon location) { + this.location = location; + } + + public Float getHeading() { + return heading; + } + + public void setHeading(Float heading) { + this.heading = heading; } public void setListItems(List items) { @@ -57,7 +89,7 @@ public class SearchListAdapter extends ArrayAdapter { LinearLayout view; if (viewType == 0) { if (convertView == null) { - LayoutInflater inflater = (LayoutInflater) ctx + LayoutInflater inflater = (LayoutInflater) app .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = (LinearLayout) inflater.inflate( R.layout.search_more_list_item, null); @@ -68,7 +100,7 @@ public class SearchListAdapter extends ArrayAdapter { ((TextView) view.findViewById(R.id.title)).setText(listItem.getName()); } else { if (convertView == null) { - LayoutInflater inflater = (LayoutInflater) ctx + LayoutInflater inflater = (LayoutInflater) app .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = (LinearLayout) inflater.inflate( R.layout.search_list_item, null); @@ -79,7 +111,6 @@ public class SearchListAdapter extends ArrayAdapter { ImageView imageView = (ImageView) view.findViewById(R.id.imageView); TextView title = (TextView) view.findViewById(R.id.title); TextView subtitle = (TextView) view.findViewById(R.id.subtitle); - TextView distance = (TextView) view.findViewById(R.id.distance); imageView.setImageDrawable(listItem.getIcon()); String name = listItem.getName(); @@ -91,15 +122,37 @@ public class SearchListAdapter extends ArrayAdapter { } else { subtitle.setVisibility(View.GONE); } - float dist = (float) listItem.getDistance(); - if (dist == 0) { - distance.setText(""); - distance.setVisibility(View.INVISIBLE); - } else { - distance.setText(OsmAndFormatter.getFormattedDistance(dist, ctx)); - distance.setVisibility(View.VISIBLE); - } + updateCompassVisibility(view, listItem); } return view; } + + private void updateCompassVisibility(View view, SearchListItem listItem) { + View compassView = view.findViewById(R.id.compass_layout); + Location ll = app.getLocationProvider().getLastKnownLocation(); + boolean showCompass = location != null && listItem.getSearchResult().location != null; + boolean gpsFixed = ll != null && System.currentTimeMillis() - ll.getTime() < 1000 * 60 * 60 * 20; + if (gpsFixed && showCompass) { + updateDistanceDirection(view, listItem); + compassView.setVisibility(View.VISIBLE); + } else { + if (!showCompass) { + compassView.setVisibility(View.GONE); + } else { + compassView.setVisibility(View.INVISIBLE); + } + } + } + + private void updateDistanceDirection(View view, SearchListItem listItem) { + 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, + listItem.getSearchResult().location.getLatitude(), + listItem.getSearchResult().location.getLongitude(), + screenOrientation, app, activity); + } } diff --git a/OsmAnd/src/net/osmand/plus/search/SearchListItem.java b/OsmAnd/src/net/osmand/plus/search/SearchListItem.java index d09e794bb1..2ff82dfd13 100644 --- a/OsmAnd/src/net/osmand/plus/search/SearchListItem.java +++ b/OsmAnd/src/net/osmand/plus/search/SearchListItem.java @@ -7,6 +7,7 @@ import net.osmand.data.Amenity; import net.osmand.data.City; import net.osmand.data.City.CityType; import net.osmand.data.FavouritePoint; +import net.osmand.data.LatLon; import net.osmand.data.Street; import net.osmand.osm.AbstractPoiType; import net.osmand.osm.PoiCategory; @@ -26,7 +27,6 @@ public class SearchListItem { protected OsmandApplication app; private SearchResult searchResult; - private double distance; public SearchListItem(OsmandApplication app, SearchResult searchResult) { this.app = app; @@ -228,12 +228,4 @@ public class SearchListItem { } return null; } - - public double getDistance() { - return distance; - } - - public void setDistance(double distance) { - this.distance = distance; - } }