[Quick search] added compass to list

This commit is contained in:
Alexey Kulish 2016-07-16 17:06:05 +03:00
parent 343b850ea9
commit 0a669f3713
4 changed files with 161 additions and 40 deletions

View file

@ -41,14 +41,30 @@
android:textSize="@dimen/default_list_text_size" android:textSize="@dimen/default_list_text_size"
tools:text="Amsterdam"/> tools:text="Amsterdam"/>
<TextView <LinearLayout
android:id="@+id/distance" android:id="@+id/compass_layout"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:gravity="right" android:orientation="horizontal">
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_desc_text_size" <ImageView
tools:text="100 km"/> android:id="@+id/direction"
android:layout_width="@dimen/dashFavDirectionSize"
android:layout_height="@dimen/dashFavDirectionSize"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_desc_text_size"
tools:text="100500 km"/>
</LinearLayout>
</LinearLayout> </LinearLayout>

View file

@ -19,15 +19,19 @@ import android.widget.ListView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.Location;
import net.osmand.ResultMatcher; import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader; import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; 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.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.dashboard.DashLocationFragment;
import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper;
import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry; import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry;
import net.osmand.plus.resources.RegionAddressRepository; import net.osmand.plus.resources.RegionAddressRepository;
@ -46,7 +50,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
public class QuickSearchDialogFragment extends DialogFragment { public class QuickSearchDialogFragment extends DialogFragment implements OsmAndCompassListener, OsmAndLocationListener {
public static final String TAG = "QuickSearchDialogFragment"; public static final String TAG = "QuickSearchDialogFragment";
private static final String QUICK_SEARCH_QUERY_KEY = "quick_search_query_key"; 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 String searchQuery = "";
private SearchResultCollection resultCollection; 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_API_PRIORITY = 3;
public static final int SEARCH_FAVORITE_OBJECT_PRIORITY = 10; public static final int SEARCH_FAVORITE_OBJECT_PRIORITY = 10;
public static final int SEARCH_HISTORY_API_PRIORITY = 3; public static final int SEARCH_HISTORY_API_PRIORITY = 3;
@ -102,7 +111,7 @@ public class QuickSearchDialogFragment extends DialogFragment {
setupSearch(mapActivity); setupSearch(mapActivity);
listView = (ListView) view.findViewById(android.R.id.list); listView = (ListView) view.findViewById(android.R.id.list);
listAdapter = new SearchListAdapter(getMyApplication()); listAdapter = new SearchListAdapter(getMyApplication(), getActivity());
listView.setAdapter(listAdapter); listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override @Override
@ -308,12 +317,28 @@ public class QuickSearchDialogFragment extends DialogFragment {
@Override @Override
public void onResume() { public void onResume() {
super.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)) { if (!Algorithms.isEmpty(searchQuery)) {
searchEditText.setText(searchQuery); searchEditText.setText(searchQuery);
searchEditText.setSelection(searchQuery.length()); searchEditText.setSelection(searchQuery.length());
} }
} }
@Override
public void onPause() {
super.onPause();
OsmandApplication app = getMyApplication();
app.getLocationProvider().removeLocationListener(this);
app.getLocationProvider().removeCompassListener(this);
}
private void showProgressBar() { private void showProgressBar() {
clearButton.setVisibility(View.GONE); clearButton.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
@ -412,12 +437,7 @@ public class QuickSearchDialogFragment extends DialogFragment {
rows.add(moreListItem); rows.add(moreListItem);
} }
for (final SearchResult sr : res.getCurrentSearchResults()) { for (final SearchResult sr : res.getCurrentSearchResults()) {
SearchListItem listItem = new SearchListItem(app, sr); rows.add(new SearchListItem(app, sr));
if (sr.location != null) {
LatLon location = res.getPhrase().getLastTokenLocation();
listItem.setDistance(MapUtils.getDistance(location, sr.location));
}
rows.add(listItem);
} }
} }
updateListAdapter(rows); updateListAdapter(rows);
@ -516,4 +536,44 @@ public class QuickSearchDialogFragment extends DialogFragment {
return (OsmandApplication) getActivity().getApplication(); 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();
}
} }

View file

@ -1,5 +1,6 @@
package net.osmand.plus.search; package net.osmand.plus.search;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -9,21 +10,52 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; 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.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.dashboard.DashLocationFragment;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.util.List; import java.util.List;
public class SearchListAdapter extends ArrayAdapter<SearchListItem> { public class SearchListAdapter extends ArrayAdapter<SearchListItem> {
private OsmandApplication ctx; private OsmandApplication app;
private Activity activity;
private LatLon location;
private Float heading;
private boolean hasSearchMoreItem; private boolean hasSearchMoreItem;
private int screenOrientation;
public SearchListAdapter(OsmandApplication ctx) { public SearchListAdapter(OsmandApplication app, Activity activity) {
super(ctx, R.layout.search_list_item); super(app, R.layout.search_list_item);
this.ctx = ctx; 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<SearchListItem> items) { public void setListItems(List<SearchListItem> items) {
@ -57,7 +89,7 @@ public class SearchListAdapter extends ArrayAdapter<SearchListItem> {
LinearLayout view; LinearLayout view;
if (viewType == 0) { if (viewType == 0) {
if (convertView == null) { if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) ctx LayoutInflater inflater = (LayoutInflater) app
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate( view = (LinearLayout) inflater.inflate(
R.layout.search_more_list_item, null); R.layout.search_more_list_item, null);
@ -68,7 +100,7 @@ public class SearchListAdapter extends ArrayAdapter<SearchListItem> {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName()); ((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
} else { } else {
if (convertView == null) { if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) ctx LayoutInflater inflater = (LayoutInflater) app
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate( view = (LinearLayout) inflater.inflate(
R.layout.search_list_item, null); R.layout.search_list_item, null);
@ -79,7 +111,6 @@ public class SearchListAdapter extends ArrayAdapter<SearchListItem> {
ImageView imageView = (ImageView) view.findViewById(R.id.imageView); ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView title = (TextView) view.findViewById(R.id.title); TextView title = (TextView) view.findViewById(R.id.title);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle); TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
TextView distance = (TextView) view.findViewById(R.id.distance);
imageView.setImageDrawable(listItem.getIcon()); imageView.setImageDrawable(listItem.getIcon());
String name = listItem.getName(); String name = listItem.getName();
@ -91,15 +122,37 @@ public class SearchListAdapter extends ArrayAdapter<SearchListItem> {
} else { } else {
subtitle.setVisibility(View.GONE); subtitle.setVisibility(View.GONE);
} }
float dist = (float) listItem.getDistance(); updateCompassVisibility(view, listItem);
if (dist == 0) {
distance.setText("");
distance.setVisibility(View.INVISIBLE);
} else {
distance.setText(OsmAndFormatter.getFormattedDistance(dist, ctx));
distance.setVisibility(View.VISIBLE);
}
} }
return view; 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);
}
} }

View file

@ -7,6 +7,7 @@ import net.osmand.data.Amenity;
import net.osmand.data.City; import net.osmand.data.City;
import net.osmand.data.City.CityType; import net.osmand.data.City.CityType;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.data.Street; import net.osmand.data.Street;
import net.osmand.osm.AbstractPoiType; import net.osmand.osm.AbstractPoiType;
import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiCategory;
@ -26,7 +27,6 @@ public class SearchListItem {
protected OsmandApplication app; protected OsmandApplication app;
private SearchResult searchResult; private SearchResult searchResult;
private double distance;
public SearchListItem(OsmandApplication app, SearchResult searchResult) { public SearchListItem(OsmandApplication app, SearchResult searchResult) {
this.app = app; this.app = app;
@ -228,12 +228,4 @@ public class SearchListItem {
} }
return null; return null;
} }
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
} }