Search map for context menu in background

This commit is contained in:
Alexey Kulish 2017-08-01 11:18:48 +03:00
parent 8e300271a2
commit f2b037d3c2
3 changed files with 205 additions and 146 deletions

View file

@ -305,6 +305,9 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
appModeChanged = false; appModeChanged = false;
if (needAcquireMenuController) { if (needAcquireMenuController) {
if (menuController != null) {
menuController.setMapContextMenu(null);
}
if (!acquireMenuController(restorePrevious)) { if (!acquireMenuController(restorePrevious)) {
active = false; active = false;
clearSelectedObject(object); clearSelectedObject(object);
@ -380,6 +383,13 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
} }
} }
public void rebuildMenu() {
WeakReference<MapContextMenuFragment> fragmentRef = findMenuFragment();
if (fragmentRef != null) {
fragmentRef.get().rebuildMenu();
}
}
public void showOrUpdate(LatLon latLon, PointDescription pointDescription, Object object) { public void showOrUpdate(LatLon latLon, PointDescription pointDescription, Object object) {
if (isVisible() && this.object != null && this.object.equals(object)) { if (isVisible() && this.object != null && this.object.equals(object)) {
update(latLon, pointDescription, object); update(latLon, pointDescription, object);
@ -526,6 +536,7 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
} }
menuController = MenuController.getMenuController(mapActivity, latLon, pointDescription, object, MenuType.STANDARD); menuController = MenuController.getMenuController(mapActivity, latLon, pointDescription, object, MenuType.STANDARD);
if (menuController.setActive(true)) { if (menuController.setActive(true)) {
menuController.setMapContextMenu(this);
if (menuData != null && (object != menuData.getObject()) if (menuData != null && (object != menuData.getObject())
&& (menuController.hasBackAction() || menuData.hasBackAction())) { && (menuController.hasBackAction() || menuData.hasBackAction())) {
historyStack.add(menuData); historyStack.add(menuData);
@ -599,16 +610,18 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
public void onSingleTapOnMap() { public void onSingleTapOnMap() {
if (menuController == null || !menuController.handleSingleTapOnMap()) { if (menuController == null || !menuController.handleSingleTapOnMap()) {
hide(); hide();
if (mapActivity.getMapLayers().getMapQuickActionLayer().isLayerOn()) if (mapActivity.getMapLayers().getMapQuickActionLayer().isLayerOn()) {
mapActivity.getMapLayers().getMapQuickActionLayer().refreshLayer(); mapActivity.getMapLayers().getMapQuickActionLayer().refreshLayer();
} }
} }
}
@Override @Override
public void onSearchAddressDone() { public void onSearchAddressDone() {
WeakReference<MapContextMenuFragment> fragmentRef = findMenuFragment(); WeakReference<MapContextMenuFragment> fragmentRef = findMenuFragment();
if (fragmentRef != null) if (fragmentRef != null) {
fragmentRef.get().refreshTitle(); fragmentRef.get().refreshTitle();
}
if (searchDoneAction != null) { if (searchDoneAction != null) {
if (searchDoneAction.dlg != null) { if (searchDoneAction.dlg != null) {

View file

@ -67,12 +67,6 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
ImageView fabView; ImageView fabView;
private MapContextMenu menu; private MapContextMenu menu;
private TitleButtonController leftTitleButtonController;
private TitleButtonController rightTitleButtonController;
private TitleButtonController topRightTitleButtonController;
private TitleButtonController leftDownloadButtonController;
private TitleButtonController rightDownloadButtonController;
private TitleProgressController titleProgressController;
private int menuTopViewHeight; private int menuTopViewHeight;
private int menuTopShadowHeight; private int menuTopShadowHeight;
@ -130,14 +124,6 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
nightMode = menu.isNightMode(); nightMode = menu.isNightMode();
mainView = view.findViewById(R.id.context_menu_main); mainView = view.findViewById(R.id.context_menu_main);
leftTitleButtonController = menu.getLeftTitleButtonController();
rightTitleButtonController = menu.getRightTitleButtonController();
topRightTitleButtonController = menu.getTopRightTitleButtonController();
leftDownloadButtonController = menu.getLeftDownloadButtonController();
rightDownloadButtonController = menu.getRightDownloadButtonController();
titleProgressController = menu.getTitleProgressController();
map = getMapActivity().getMapView(); map = getMapActivity().getMapView();
RotatedTileBox box = map.getCurrentRotatedTileBox().copy(); RotatedTileBox box = map.getCurrentRotatedTileBox().copy();
customMapCenter = menu.getMapCenter() != null; customMapCenter = menu.getMapCenter() != null;
@ -162,71 +148,77 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
// Left title button // Left title button
final Button leftTitleButton = (Button) view.findViewById(R.id.title_button); final Button leftTitleButton = (Button) view.findViewById(R.id.title_button);
if (leftTitleButtonController != null) {
leftTitleButton.setOnClickListener(new View.OnClickListener() { leftTitleButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
TitleButtonController leftTitleButtonController = menu.getLeftTitleButtonController();
if (leftTitleButtonController != null) {
leftTitleButtonController.buttonPressed(); leftTitleButtonController.buttonPressed();
} }
});
} }
});
// Right title button // Right title button
final Button rightTitleButton = (Button) view.findViewById(R.id.title_button_right); final Button rightTitleButton = (Button) view.findViewById(R.id.title_button_right);
if (rightTitleButtonController != null) {
rightTitleButton.setOnClickListener(new View.OnClickListener() { rightTitleButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
TitleButtonController rightTitleButtonController = menu.getRightTitleButtonController();
if (rightTitleButtonController != null) {
rightTitleButtonController.buttonPressed(); rightTitleButtonController.buttonPressed();
} }
});
} }
});
// Left download button // Left download button
final Button leftDownloadButton = (Button) view.findViewById(R.id.download_button_left); final Button leftDownloadButton = (Button) view.findViewById(R.id.download_button_left);
if (leftDownloadButtonController != null) {
leftDownloadButton.setOnClickListener(new View.OnClickListener() { leftDownloadButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
TitleButtonController leftDownloadButtonController = menu.getLeftDownloadButtonController();
if (leftDownloadButtonController != null) {
leftDownloadButtonController.buttonPressed(); leftDownloadButtonController.buttonPressed();
} }
});
} }
});
// Right download button // Right download button
final Button rightDownloadButton = (Button) view.findViewById(R.id.download_button_right); final Button rightDownloadButton = (Button) view.findViewById(R.id.download_button_right);
if (rightDownloadButtonController != null) {
rightDownloadButton.setOnClickListener(new View.OnClickListener() { rightDownloadButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
TitleButtonController rightDownloadButtonController = menu.getRightDownloadButtonController();
if (rightDownloadButtonController != null) {
rightDownloadButtonController.buttonPressed(); rightDownloadButtonController.buttonPressed();
} }
});
} }
});
// Top Right title button // Top Right title button
final Button topRightTitleButton = (Button) view.findViewById(R.id.title_button_top_right); final Button topRightTitleButton = (Button) view.findViewById(R.id.title_button_top_right);
if (topRightTitleButtonController != null) {
topRightTitleButton.setOnClickListener(new View.OnClickListener() { topRightTitleButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
TitleButtonController topRightTitleButtonController = menu.getTopRightTitleButtonController();
if (topRightTitleButtonController != null) {
topRightTitleButtonController.buttonPressed(); topRightTitleButtonController.buttonPressed();
} }
});
} }
});
// Progress bar // Progress bar
if (titleProgressController != null) {
final ImageView progressButton = (ImageView) view.findViewById(R.id.progressButton); final ImageView progressButton = (ImageView) view.findViewById(R.id.progressButton);
progressButton.setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_remove_dark, progressButton.setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_remove_dark,
!nightMode ? R.color.icon_color : R.color.dashboard_subheader_text_dark)); !nightMode ? R.color.icon_color : R.color.dashboard_subheader_text_dark));
progressButton.setOnClickListener(new View.OnClickListener() { progressButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
TitleProgressController titleProgressController = menu.getTitleProgressController();
if (titleProgressController != null) {
titleProgressController.buttonPressed(); titleProgressController.buttonPressed();
} }
});
} }
});
menu.updateData(); menu.updateData();
updateButtonsAndProgress(); updateButtonsAndProgress();
@ -583,6 +575,13 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
private void updateButtonsAndProgress() { private void updateButtonsAndProgress() {
if (view != null) { if (view != null) {
TitleButtonController leftTitleButtonController = menu.getLeftTitleButtonController();
TitleButtonController rightTitleButtonController = menu.getRightTitleButtonController();
TitleButtonController topRightTitleButtonController = menu.getTopRightTitleButtonController();
TitleButtonController leftDownloadButtonController = menu.getLeftDownloadButtonController();
TitleButtonController rightDownloadButtonController = menu.getRightDownloadButtonController();
TitleProgressController titleProgressController = menu.getTitleProgressController();
// Title buttons // Title buttons
boolean showTitleButtonsContainer = (leftTitleButtonController != null || rightTitleButtonController != null); boolean showTitleButtonsContainer = (leftTitleButtonController != null || rightTitleButtonController != null);
final View titleButtonsContainer = view.findViewById(R.id.title_button_container); final View titleButtonsContainer = view.findViewById(R.id.title_button_container);
@ -848,12 +847,30 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents {
line2LineHeight = line2.getLineHeight(); line2LineHeight = line2.getLineHeight();
line2MeasuredHeight = line2.getMeasuredHeight(); line2MeasuredHeight = line2.getMeasuredHeight();
} }
int dp16 = dpToPx(16f);
int titleButtonHeight = 0;
View titleButtonContainer = view.findViewById(R.id.title_button_container);
if (titleButtonContainer.getVisibility() == View.VISIBLE) {
titleButtonHeight = titleButtonContainer.getMeasuredHeight() - dp16;
}
int downloadButtonsHeight = 0;
View downloadButtonsContainer = view.findViewById(R.id.download_buttons_container);
if (downloadButtonsContainer.getVisibility() == View.VISIBLE) {
downloadButtonsHeight = downloadButtonsContainer.getMeasuredHeight() - dp16;
}
int titleProgressHeight = 0;
View titleProgressContainer = view.findViewById(R.id.title_progress_container);
if (titleProgressContainer.getVisibility() == View.VISIBLE) {
titleProgressHeight = titleProgressContainer.getMeasuredHeight() - dp16;
}
if (menuTopViewHeight != 0) { if (menuTopViewHeight != 0) {
int titleHeight = line1.getLineCount() * line1.getLineHeight() + line2LineCount * line2LineHeight + menuTitleTopBottomPadding; int titleHeight = line1.getLineCount() * line1.getLineHeight() + line2LineCount * line2LineHeight + menuTitleTopBottomPadding;
if (titleHeight < line1.getMeasuredHeight() + line2MeasuredHeight) { if (titleHeight < line1.getMeasuredHeight() + line2MeasuredHeight) {
titleHeight = line1.getMeasuredHeight() + line2MeasuredHeight; titleHeight = line1.getMeasuredHeight() + line2MeasuredHeight;
} }
newMenuTopViewHeight = menuTopViewHeightExcludingTitle + titleHeight; newMenuTopViewHeight = menuTopViewHeightExcludingTitle + titleHeight + titleButtonHeight + downloadButtonsHeight + titleProgressHeight;
dy = Math.max(0, newMenuTopViewHeight - menuTopViewHeight - (newMenuTopShadowAllHeight - menuTopShadowAllHeight)); dy = Math.max(0, newMenuTopViewHeight - menuTopViewHeight - (newMenuTopShadowAllHeight - menuTopShadowAllHeight));
} else { } else {
menuTopViewHeightExcludingTitle = newMenuTopViewHeight - line1.getMeasuredHeight() - line2MeasuredHeight; menuTopViewHeightExcludingTitle = newMenuTopViewHeight - line1.getMeasuredHeight() - line2MeasuredHeight;

View file

@ -1,6 +1,7 @@
package net.osmand.plus.mapcontextmenu; package net.osmand.plus.mapcontextmenu;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@ -86,6 +87,7 @@ public abstract class MenuController extends BaseMenuController {
MULTI_LINE MULTI_LINE
} }
protected MapContextMenu mapContextMenu;
protected MenuBuilder builder; protected MenuBuilder builder;
private int currentMenuState; private int currentMenuState;
private MenuType menuType = MenuType.STANDARD; private MenuType menuType = MenuType.STANDARD;
@ -117,6 +119,10 @@ public abstract class MenuController extends BaseMenuController {
this.builder.setLight(isLight()); this.builder.setLight(isLight());
} }
public void setMapContextMenu(MapContextMenu mapContextMenu) {
this.mapContextMenu = mapContextMenu;
}
public void build(View rootView) { public void build(View rootView) {
for (OsmandPlugin plugin : OsmandPlugin.getEnabledPlugins()) { for (OsmandPlugin plugin : OsmandPlugin.getEnabledPlugins()) {
if (plugin.isMenuControllerSupported(this.getClass())) { if (plugin.isMenuControllerSupported(this.getClass())) {
@ -531,13 +537,25 @@ public abstract class MenuController extends BaseMenuController {
} }
} }
public void buildMapDownloadButton(LatLon latLon) { public void buildMapDownloadButton(final LatLon latLon) {
new AsyncTask<Void, Void, BinaryMapDataObject>() {
ResourceManager rm;
OsmandRegions osmandRegions;
String selectedFullName = "";
@Override
protected void onPreExecute() {
rm = getMapActivity().getMyApplication().getResourceManager();
osmandRegions = rm.getOsmandRegions();
}
@Override
protected BinaryMapDataObject doInBackground(Void... voids) {
int point31x = MapUtils.get31TileNumberX(latLon.getLongitude()); int point31x = MapUtils.get31TileNumberX(latLon.getLongitude());
int point31y = MapUtils.get31TileNumberY(latLon.getLatitude()); int point31y = MapUtils.get31TileNumberY(latLon.getLatitude());
ResourceManager rm = getMapActivity().getMyApplication().getResourceManager();
OsmandRegions osmandRegions = rm.getOsmandRegions();
List<BinaryMapDataObject> mapDataObjects = null; List<BinaryMapDataObject> mapDataObjects = null;
try { try {
mapDataObjects = osmandRegions.queryBbox(point31x, point31x, point31y, point31y); mapDataObjects = osmandRegions.queryBbox(point31x, point31x, point31y, point31y);
@ -545,6 +563,7 @@ public abstract class MenuController extends BaseMenuController {
e.printStackTrace(); e.printStackTrace();
} }
BinaryMapDataObject binaryMapDataObject = null;
if (mapDataObjects != null) { if (mapDataObjects != null) {
Iterator<BinaryMapDataObject> it = mapDataObjects.iterator(); Iterator<BinaryMapDataObject> it = mapDataObjects.iterator();
while (it.hasNext()) { while (it.hasNext()) {
@ -563,15 +582,13 @@ public abstract class MenuController extends BaseMenuController {
} }
} }
} }
String selectedFullName = "";
double smallestArea = -1; double smallestArea = -1;
downloadMapDataObject = null;
for (BinaryMapDataObject o : mapDataObjects) { for (BinaryMapDataObject o : mapDataObjects) {
String downloadName = osmandRegions.getDownloadName(o); String downloadName = osmandRegions.getDownloadName(o);
if (!Algorithms.isEmpty(downloadName)) { if (!Algorithms.isEmpty(downloadName)) {
boolean downloaded = checkIfObjectDownloaded(rm, downloadName); boolean downloaded = checkIfObjectDownloaded(rm, downloadName);
if (downloaded) { if (downloaded) {
downloadMapDataObject = null; binaryMapDataObject = null;
break; break;
} else { } else {
String fullName = osmandRegions.getFullName(o); String fullName = osmandRegions.getFullName(o);
@ -581,17 +598,24 @@ public abstract class MenuController extends BaseMenuController {
if (smallestArea == -1) { if (smallestArea == -1) {
smallestArea = area; smallestArea = area;
selectedFullName = fullName; selectedFullName = fullName;
downloadMapDataObject = o; binaryMapDataObject = o;
} else if (area < smallestArea) { } else if (area < smallestArea) {
smallestArea = area; smallestArea = area;
selectedFullName = fullName; selectedFullName = fullName;
downloadMapDataObject = o; binaryMapDataObject = o;
}
} }
} }
} }
} }
} }
return binaryMapDataObject;
}
@Override
protected void onPostExecute(BinaryMapDataObject binaryMapDataObject) {
downloadMapDataObject = binaryMapDataObject;
downloaded = downloadMapDataObject == null; downloaded = downloadMapDataObject == null;
if (!downloaded) { if (!downloaded) {
downloadThread = getMapActivity().getMyApplication().getDownloadThread(); downloadThread = getMapActivity().getMyApplication().getDownloadThread();
@ -636,11 +660,16 @@ public abstract class MenuController extends BaseMenuController {
} }
} }
updateData(); if (mapContextMenu != null) {
mapContextMenu.updateMenuUI();
} }
} }
} }
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private boolean checkIfObjectDownloaded(ResourceManager rm, String downloadName) { private boolean checkIfObjectDownloaded(ResourceManager rm, String downloadName) {
final String regionName = Algorithms.capitalizeFirstLetterAndLowercase(downloadName) final String regionName = Algorithms.capitalizeFirstLetterAndLowercase(downloadName)
+ IndexConstants.BINARY_MAP_INDEX_EXT; + IndexConstants.BINARY_MAP_INDEX_EXT;