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;
if (needAcquireMenuController) {
if (menuController != null) {
menuController.setMapContextMenu(null);
}
if (!acquireMenuController(restorePrevious)) {
active = false;
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) {
if (isVisible() && this.object != null && this.object.equals(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);
if (menuController.setActive(true)) {
menuController.setMapContextMenu(this);
if (menuData != null && (object != menuData.getObject())
&& (menuController.hasBackAction() || menuData.hasBackAction())) {
historyStack.add(menuData);
@ -599,16 +610,18 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL
public void onSingleTapOnMap() {
if (menuController == null || !menuController.handleSingleTapOnMap()) {
hide();
if (mapActivity.getMapLayers().getMapQuickActionLayer().isLayerOn())
if (mapActivity.getMapLayers().getMapQuickActionLayer().isLayerOn()) {
mapActivity.getMapLayers().getMapQuickActionLayer().refreshLayer();
}
}
}
@Override
public void onSearchAddressDone() {
WeakReference<MapContextMenuFragment> fragmentRef = findMenuFragment();
if (fragmentRef != null)
if (fragmentRef != null) {
fragmentRef.get().refreshTitle();
}
if (searchDoneAction != null) {
if (searchDoneAction.dlg != null) {

View file

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

View file

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