Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search screen – Android Auto (WIP) #19216

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions OsmAnd/src/net/osmand/plus/auto/SearchHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,12 @@ private void updateSearchHint(@NonNull SearchSettings searchSettings) {
}

public void runSearch(@NonNull String query) {
runSearch(query, setupSearchSettings(false));
}

public void runSearch(@NonNull String query, @NonNull SearchSettings searchSettings) {
searching = true;
searchQuery = query;
SearchSettings searchSettings = setupSearchSettings(false);
searchUICore.setOnResultsComplete(() -> {
ItemList.Builder itemList = new ItemList.Builder();
SearchUICore.SearchResultCollection resultCollection = searchUICore.getCurrentSearchResult();
Expand Down Expand Up @@ -225,7 +228,7 @@ public void runSearch(@NonNull String query) {
builder.addText(app.getString(R.string.nothing_found_in_radius) + " "
+ OsmAndFormatter.getFormattedDistance((float) rd, app, false));
}
builder.setOnClickListener(this::onClickSearchMore);
builder.setOnClickListener(() -> onClickSearchMore(searchSettings));
builder.setBrowsable(true);
itemList.addItem(builder.build());
}
Expand All @@ -243,13 +246,21 @@ public void runSearch(@NonNull String query) {
}

public void completeQueryWithObject(@NonNull SearchResult result) {
completeQueryWithObject(result, setupSearchSettings(false), null);
}

public void completeQueryWithObject(@NonNull SearchResult result, @NonNull SearchSettings searchSettings, @Nullable String additionalQuery) {
searchUICore.resetPhrase();
SearchUtils.selectSearchResult(app, result);

String searchQuery = searchUICore.getPhrase().getText(true);
if(additionalQuery != null) {
searchQuery += additionalQuery;
}
if (searchRadiusLevel != 1) {
searchRadiusLevel = minSearchRadiusLevel;
}
runSearch(searchQuery);
runSearch(searchQuery, searchSettings);
}

@Nullable
Expand Down Expand Up @@ -281,10 +292,10 @@ public Row.Builder buildSearchRow(@Nullable LatLon searchLocation, @Nullable Lat
return builder;
}

private void onClickSearchMore() {
private void onClickSearchMore(@NonNull SearchSettings searchSettings) {
searchRadiusLevel++;
if (!Algorithms.isEmpty(searchQuery)) {
runSearch(searchQuery);
runSearch(searchQuery, searchSettings);
}
if (listener != null) {
listener.onClickSearchMore();
Expand Down
169 changes: 143 additions & 26 deletions OsmAnd/src/net/osmand/plus/auto/screens/SearchScreen.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package net.osmand.plus.auto.screens;

import static net.osmand.plus.auto.screens.SearchScreen.SearchType.ADDRESS_HOUSE;
import static net.osmand.plus.auto.screens.SearchScreen.SearchType.ADDRESS_CITY;
import static net.osmand.plus.auto.screens.SearchScreen.SearchType.ADDRESS_STREET;
import static net.osmand.plus.auto.screens.SearchScreen.SearchType.POI;
import static net.osmand.plus.auto.screens.SearchScreen.SearchType.HISTORY;
import static net.osmand.plus.auto.screens.SearchScreen.SearchType.MAIN;

import android.graphics.drawable.Drawable;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.car.app.CarContext;
import androidx.car.app.model.Action;
import androidx.car.app.model.CarIcon;
import androidx.car.app.model.Item;
import androidx.car.app.model.ItemList;
import androidx.car.app.model.Row;
import androidx.car.app.model.SearchTemplate;
import androidx.car.app.model.SearchTemplate.SearchCallback;
import androidx.car.app.model.Template;
import androidx.core.graphics.drawable.IconCompat;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

Expand All @@ -22,6 +34,7 @@
import net.osmand.plus.AppInitializer.AppInitializeListener;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.auto.NavigationSession;
import net.osmand.plus.auto.SearchHelper;
import net.osmand.plus.auto.SearchHelper.SearchHelperListener;
import net.osmand.plus.helpers.SearchHistoryHelper;
Expand All @@ -30,14 +43,18 @@
import net.osmand.plus.mapmarkers.MapMarker;
import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.search.SearchUtils;
import net.osmand.plus.search.listitems.QuickSearchListItem;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.preferences.OsmandPreference;
import net.osmand.plus.settings.enums.HistorySource;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.plus.utils.UiUtilities;
import net.osmand.search.SearchUICore;
import net.osmand.search.core.ObjectType;
import net.osmand.search.core.SearchPhrase;
import net.osmand.search.core.SearchResult;
import net.osmand.search.core.SearchSettings;
import net.osmand.search.core.SearchWord;
import net.osmand.util.Algorithms;

Expand All @@ -49,30 +66,55 @@
public final class SearchScreen extends BaseSearchScreen implements DefaultLifecycleObserver,
AppInitializeListener, SearchHelperListener {

public enum SearchType {
MAIN, HISTORY, ADDRESS_CITY, ADDRESS_STREET, ADDRESS_HOUSE, POI
}

private static final Log LOG = PlatformUtil.getLog(SearchScreen.class);
private static final int MAP_MARKERS_LIMIT = 3;


@NonNull
private final Action settingsAction;
@NonNull
private final SearchType searchType;

private ItemList itemList = withNoResults(new ItemList.Builder()).build();

private String searchText;
private boolean loading;
private boolean destroyed;
private List<SearchResult> recentResults;
private boolean showResult;
private final SearchResult selectedSearchResult;

public SearchScreen(@NonNull CarContext carContext, @NonNull Action settingsAction) {
this(carContext, settingsAction, MAIN, null);
}

public SearchScreen(@NonNull CarContext carContext, @NonNull Action settingsAction, @NonNull SearchType searchType, @Nullable SearchResult selectedSearchResult) {
super(carContext);
this.searchType = searchType;
this.settingsAction = settingsAction;
this.selectedSearchResult = selectedSearchResult;

getLifecycle().addObserver(this);
getApp().getAppInitializer().addListener(this);
reloadHistory();

reloadItems();
}

private void reloadItems() {
if (selectedSearchResult != null) {
showResult = true;
getSearchHelper().completeQueryWithObject(selectedSearchResult, getSearchSettings(), null);
} else {
if (searchType == HISTORY) {
reloadHistory();
} else {
startSearch(searchType);
}
}
}

@NonNull
public SearchUICore getSearchUICore() {
Expand All @@ -89,14 +131,16 @@ public void onDestroy(@NonNull LifecycleOwner owner) {
@NonNull
@Override
public Template onGetTemplate() {
String searchQuery = getSearchHelper().getSearchQuery();
String searchHint = getSearchHelper().getSearchHint();
SearchTemplate.Builder builder = new SearchTemplate.Builder(new SearchCallback() {
@Override
public void onSearchTextChanged(@NonNull String searchText) {
SearchScreen.this.searchText = searchText;
getSearchHelper().resetSearchRadius();
doSearch(searchText);
if (selectedSearchResult != null) {
getSearchHelper().completeQueryWithObject(selectedSearchResult, getSearchSettings(), searchText);
} else {
getSearchHelper().resetSearchRadius();
doSearch(searchText);
}
}

@Override
Expand All @@ -111,8 +155,8 @@ public void onSearchSubmitted(@NonNull String searchTerm) {
});

builder.setHeaderAction(Action.BACK)
.setShowKeyboardByDefault(false)
.setInitialSearchText(searchQuery == null ? "" : searchQuery);
.setInitialSearchText("")
.setShowKeyboardByDefault(false);
if (!Algorithms.isEmpty(searchHint)) {
builder.setSearchHint(searchHint);
}
Expand All @@ -130,13 +174,8 @@ public void onSearchSubmitted(@NonNull String searchTerm) {

private void doSearch(String searchText) {
if (!getApp().isApplicationInitializing()) {
if (searchText.isEmpty()) {
showRecents();
} else {
getSearchHelper().runSearch(searchText);
}
getSearchHelper().runSearch(searchText, getSearchSettings());
}
invalidate();
}

public void onClickSearchResult(@NonNull SearchResult sr) {
Expand All @@ -150,18 +189,48 @@ public void onClickSearchResult(@NonNull SearchResult sr) {
|| sr.objectType == ObjectType.WPT
|| sr.objectType == ObjectType.STREET_INTERSECTION
|| sr.objectType == ObjectType.GPX_TRACK) {

showResult(sr);
} else {
if (sr.objectType == ObjectType.CITY || sr.objectType == ObjectType.VILLAGE || sr.objectType == ObjectType.STREET) {
showResult = true;
SearchType newSearchType = null;
if (sr.objectType == ObjectType.CITY || sr.objectType == ObjectType.VILLAGE) {
newSearchType = ADDRESS_STREET;
}
getSearchHelper().completeQueryWithObject(sr);
if (sr.object instanceof AbstractPoiType || sr.object instanceof PoiUIFilter) {
reloadHistory();
if (sr.objectType == ObjectType.STREET) {
newSearchType = ADDRESS_HOUSE;
}
invalidate();
if (sr.objectType == ObjectType.POI_TYPE) {
newSearchType = POI;
}
if (newSearchType != null) {
showSearch(newSearchType, sr);
} else {
if (sr.object instanceof AbstractPoiType || sr.object instanceof PoiUIFilter) {
reloadHistory();
}
}
}
}

@NonNull
private SearchSettings getSearchSettings() {
return getSearchSettings(searchType);
}

@NonNull
private SearchSettings getSearchSettings(SearchType searchType) {
SearchSettings settings = getSearchHelper().setupSearchSettings(false);
if (searchType == ADDRESS_CITY) {
settings = SearchUtils.setupCitySearchSettings(settings);
} else if (searchType == MAIN) {
settings = SearchUtils.setupPOITypeSearchSettings(settings);
} else if (searchType == ADDRESS_STREET) {
settings = SearchUtils.setupStreetSearchSettings(settings);
} else if (searchType == POI) {
settings = SearchUtils.setupPOISearchSettings(settings);
} else if (searchType == ADDRESS_HOUSE) {
settings = SearchUtils.setupHouseSearchSettings(settings);
}
return settings;
}

private void showResult(SearchResult sr) {
Expand All @@ -184,7 +253,20 @@ public void onSearchDone(@NonNull SearchPhrase phrase, @Nullable List<SearchResu
if (resultsCount > 0) {
showResult = false;
}
this.itemList = itemList;
loading = false;
if (searchType == MAIN) {
ItemList.Builder fullItemList = new ItemList.Builder();
fullItemList.addItem(createRowItem(R.drawable.ic_action_history, R.string.shared_string_history, HISTORY));
fullItemList.addItem(createRowItem(R.drawable.ic_action_building2, R.string.shared_string_address, ADDRESS_CITY));
if (itemList != null) {
for (Item item : itemList.getItems()) {
fullItemList.addItem(item);
}
}
this.itemList = fullItemList.build();
} else {
this.itemList = itemList;
}
invalidate();
}
}
Expand Down Expand Up @@ -309,6 +391,43 @@ private String getPointName(TargetPoint targetPoint) {
return name;
}

@NonNull
private Row createRowItem(@DrawableRes Integer iconId, @StringRes int titleRes, @NonNull SearchType nextStep) {
Row.Builder builder = new Row.Builder();
UiUtilities uiUtilities = getApp().getUIUtilities();
Drawable icon = uiUtilities.getActiveIcon(iconId, true);
builder.setImage(new CarIcon.Builder(IconCompat.createWithBitmap(AndroidUtils.drawableToBitmap(icon))).build());
builder.setTitle(getApp().getString(titleRes));
builder.setBrowsable(true);
builder.setOnClickListener(() -> showSearch(nextStep));
return builder.build();
}

private void showSearch(@NonNull SearchType searchType) {
showSearch(searchType, null);
}

private void showSearch(@NonNull SearchType searchType, @Nullable SearchResult selectedSearchResult) {
Action settingsAction = getSettingsAction();
if (settingsAction != null) {
showSearchStep(new SearchScreen(getCarContext(), settingsAction, searchType, selectedSearchResult));
}
}

@Nullable
private Action getSettingsAction() {
NavigationSession session = getApp().getCarNavigationSession();
return session != null ? session.getSettingsAction() : null;
}

private void showSearchStep(@NonNull SearchScreen searchScreen) {
getScreenManager().push(searchScreen);
}

private void startSearch(@NonNull SearchType searchType) {
getSearchHelper().runSearch("", getSearchSettings(searchType));
}

private void showRecents() {
OsmandApplication app = getApp();
ItemList.Builder itemList = withNoResults(new ItemList.Builder());
Expand Down Expand Up @@ -343,11 +462,9 @@ private void showRecents() {
public void onFinish(@NonNull AppInitializer init) {
loading = false;
if (!destroyed) {
reloadHistoryInternal();
if (!Algorithms.isEmpty(searchText)) {
getSearchHelper().runSearch(searchText);
if (searchType == MAIN) {
reloadHistoryInternal();
}
invalidate();
}
}

Expand Down
Loading