From 5cffe02d9804a59d5cfd106a02746cf5aad6bde6 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Mon, 1 Aug 2016 16:08:39 +0300 Subject: [PATCH] Fix #2705 --- .../res/layout/map_context_menu_fragment.xml | 4 +- OsmAnd/res/values-large/sizes.xml | 2 +- OsmAnd/res/values/sizes.xml | 2 +- .../AudioVideoNoteMenuController.java | 2 + .../plus/mapcontextmenu/MapContextMenu.java | 8 + .../MapContextMenuFragment.java | 23 +- .../plus/mapcontextmenu/MenuController.java | 212 +++++++++++++++++- .../controllers/MapDataMenuController.java | 6 +- .../plus/osmedit/OsmBugMenuController.java | 1 + .../osmand/plus/osmo/OsMoMenuController.java | 2 + 10 files changed, 245 insertions(+), 17 deletions(-) diff --git a/OsmAnd/res/layout/map_context_menu_fragment.xml b/OsmAnd/res/layout/map_context_menu_fragment.xml index 17b488ca3b..106ecab995 100644 --- a/OsmAnd/res/layout/map_context_menu_fragment.xml +++ b/OsmAnd/res/layout/map_context_menu_fragment.xml @@ -167,6 +167,7 @@ android:id="@+id/title_button_container" android:layout_width="fill_parent" android:layout_height="wrap_content" + android:minHeight="@dimen/context_menu_action_buttons_h" android:layout_marginTop="@dimen/context_menu_buttons_top_margin" android:clickable="true" android:orientation="horizontal" @@ -217,6 +218,7 @@ android:id="@+id/download_buttons_container" android:layout_width="fill_parent" android:layout_height="wrap_content" + android:minHeight="@dimen/context_menu_action_buttons_h" android:layout_marginTop="@dimen/context_menu_buttons_top_margin" android:clickable="true" android:orientation="vertical" @@ -273,7 +275,7 @@ android:layout_marginTop="@dimen/context_menu_buttons_top_margin" android:clickable="true" android:gravity="center_vertical" - android:minHeight="@dimen/context_menu_progress_min_height" + android:minHeight="@dimen/context_menu_action_buttons_h" android:orientation="horizontal" android:paddingBottom="@dimen/context_menu_buttons_padding_bottom" android:paddingLeft="72dp" diff --git a/OsmAnd/res/values-large/sizes.xml b/OsmAnd/res/values-large/sizes.xml index 37553b7427..e4783dc327 100644 --- a/OsmAnd/res/values-large/sizes.xml +++ b/OsmAnd/res/values-large/sizes.xml @@ -55,7 +55,7 @@ 15dp -24dp 6dp - 78dp + 80dp 66dp 80dp diff --git a/OsmAnd/res/values/sizes.xml b/OsmAnd/res/values/sizes.xml index ca162e6c44..d38209dbee 100644 --- a/OsmAnd/res/values/sizes.xml +++ b/OsmAnd/res/values/sizes.xml @@ -117,7 +117,7 @@ 10dp -16dp 4dp - 52dp + 54dp 44dp 54dp diff --git a/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNoteMenuController.java b/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNoteMenuController.java index e10f4637b8..51f7afa00b 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNoteMenuController.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/AudioVideoNoteMenuController.java @@ -126,6 +126,8 @@ public class AudioVideoNoteMenuController extends MenuController { @Override public void updateData() { + super.updateData(); + if (!mIsFileAvailable) { return; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 22dd98e7ce..5ad8a414b5 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -36,6 +36,7 @@ import net.osmand.plus.mapcontextmenu.MenuController.MenuState; import net.osmand.plus.mapcontextmenu.MenuController.MenuType; import net.osmand.plus.mapcontextmenu.MenuController.TitleButtonController; import net.osmand.plus.mapcontextmenu.MenuController.TitleProgressController; +import net.osmand.plus.mapcontextmenu.controllers.MapDataMenuController; import net.osmand.plus.mapcontextmenu.editors.FavoritePointEditor; import net.osmand.plus.mapcontextmenu.editors.PointEditor; import net.osmand.plus.mapcontextmenu.editors.WptPtEditor; @@ -457,6 +458,9 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL menuController.onAcquireNewController(pointDescription, object); } menuController = MenuController.getMenuController(mapActivity, pointDescription, object, MenuType.STANDARD); + if (!(menuController instanceof MapDataMenuController)) { + menuController.buildMapDownloadButton(latLon); + } } public void onSingleTapOnMap() { @@ -834,6 +838,10 @@ public class MapContextMenu extends MenuTitleController implements StateChangedL return menuController != null && menuController.displayDistanceDirection(); } + public boolean isMapDownloaded() { + return menuController != null && menuController.isMapDownloaded(); + } + public void updateData() { if (menuController != null) { menuController.updateData(); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 5ba238d3ff..a13a0f1314 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -646,11 +646,23 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents { } // Download buttons - boolean showDownloadButtonsContainer = (leftDownloadButtonController != null || rightDownloadButtonController != null) + boolean showDownloadButtonsContainer = + ((leftDownloadButtonController != null && leftDownloadButtonController.visible) + || (rightDownloadButtonController != null && rightDownloadButtonController.visible)) && (titleProgressController == null || !titleProgressController.visible); final View downloadButtonsContainer = view.findViewById(R.id.download_buttons_container); downloadButtonsContainer.setVisibility(showDownloadButtonsContainer ? View.VISIBLE : View.GONE); + if (showDownloadButtonsContainer) { + view.findViewById(R.id.download_buttons_top_border).setVisibility(showTitleButtonsContainer ? View.VISIBLE : View.INVISIBLE); + if (showTitleButtonsContainer) { + LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) downloadButtonsContainer.getLayoutParams(); + if (ll.topMargin != 0) { + ll.setMargins(0, 0, 0, 0); + } + } + } + // Left download button final Button leftDownloadButton = (Button) view.findViewById(R.id.download_button_left); if (leftDownloadButtonController != null) { @@ -683,6 +695,12 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents { final View titleProgressContainer = view.findViewById(R.id.title_progress_container); if (titleProgressController != null) { titleProgressContainer.setVisibility(titleProgressController.visible ? View.VISIBLE : View.GONE); + if (titleProgressController.visible && showTitleButtonsContainer) { + LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) titleProgressContainer.getLayoutParams(); + if (ll.topMargin != 0) { + ll.setMargins(0, 0, 0, 0); + } + } final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar); final TextView progressTitle = (TextView) view.findViewById(R.id.progressTitle); @@ -1204,6 +1222,9 @@ public class MapContextMenuFragment extends Fragment implements DownloadEvents { @Override public void downloadHasFinished() { updateOnDownload(); + if (menu != null && menu.isVisible() && menu.isMapDownloaded()) { + rebuildMenu(); + } } private void updateOnDownload() { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java index 4d24a71ca5..d7af702112 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java @@ -4,12 +4,17 @@ import android.graphics.drawable.Drawable; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; + +import net.osmand.IndexConstants; +import net.osmand.binary.BinaryMapDataObject; import net.osmand.binary.RouteDataObject; import net.osmand.data.Amenity; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.data.TransportStop; +import net.osmand.map.OsmandRegions; +import net.osmand.map.WorldRegion; import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem; import net.osmand.plus.MapMarkersHelper.MapMarker; @@ -19,6 +24,10 @@ import net.osmand.plus.TargetPointsHelper.TargetPoint; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.audionotes.AudioVideoNoteMenuController; import net.osmand.plus.audionotes.AudioVideoNotesPlugin.Recording; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadIndexesThread; +import net.osmand.plus.download.DownloadValidationManager; +import net.osmand.plus.download.IndexItem; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.mapcontextmenu.controllers.AmenityMenuController; import net.osmand.plus.mapcontextmenu.controllers.FavouritePointMenuController; @@ -42,7 +51,15 @@ import net.osmand.plus.osmedit.OsmPoint; import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice; import net.osmand.plus.osmo.OsMoMenuController; import net.osmand.plus.parkingpoint.ParkingPositionMenuController; +import net.osmand.plus.resources.ResourceManager; import net.osmand.plus.views.DownloadedRegionsLayer.DownloadMapObject; +import net.osmand.util.Algorithms; +import net.osmand.util.MapUtils; + +import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; public abstract class MenuController extends BaseMenuController { @@ -70,6 +87,12 @@ public abstract class MenuController extends BaseMenuController { protected TitleButtonController rightDownloadButtonController; protected TitleProgressController titleProgressController; + protected IndexItem indexItem; + protected boolean downloaded; + private BinaryMapDataObject downloadMapDataObject; + private WorldRegion downloadRegion; + private DownloadIndexesThread downloadThread; + public MenuController(MenuBuilder builder, PointDescription pointDescription, MapActivity mapActivity) { super(mapActivity); this.pointDescription = pointDescription; @@ -281,27 +304,95 @@ public abstract class MenuController extends BaseMenuController { return false; } - public int getLeftIconId() { return 0; } + public int getLeftIconId() { + return 0; + } - public Drawable getLeftIcon() { return null; } + public Drawable getLeftIcon() { + return null; + } - public Drawable getSecondLineTypeIcon() { return null; } + public Drawable getSecondLineTypeIcon() { + return null; + } - public int getFavActionIconId() { return R.drawable.map_action_fav_dark; } + public int getFavActionIconId() { + return R.drawable.map_action_fav_dark; + } - public int getFavActionStringId() { return R.string.shared_string_add_to_favorites; } + public int getFavActionStringId() { + return R.string.shared_string_add_to_favorites; + } - public String getTypeStr() { return ""; } + public String getTypeStr() { + return ""; + } - public String getCommonTypeStr() { return ""; } + public String getCommonTypeStr() { + return ""; + } - public String getNameStr() { return pointDescription.getName(); } + public String getNameStr() { + return pointDescription.getName(); + } public void share(LatLon latLon, String title) { ShareMenu.show(latLon, title, getMapActivity()); } public void updateData() { + if (downloadMapDataObject != null) { + if (indexItem == null) { + List indexItems = new LinkedList<>(downloadThread.getIndexes().getIndexItems(downloadRegion)); + for (IndexItem item : indexItems) { + if (item.getType() == DownloadActivityType.NORMAL_FILE) { + indexItem = item; + break; + } + } + } + + if (indexItem != null) { + downloaded = indexItem.isDownloaded(); + } + + leftDownloadButtonController.visible = !downloaded; + leftDownloadButtonController.leftIconId = R.drawable.ic_action_import; + + boolean downloadIndexes = getMapActivity().getMyApplication().getSettings().isInternetConnectionAvailable() + && !downloadThread.getIndexes().isDownloadedFromInternet + && !downloadThread.getIndexes().downloadFromInternetFailed; + + boolean isDownloading = indexItem != null && downloadThread.isDownloading(indexItem); + if (isDownloading) { + titleProgressController.setMapDownloadMode(); + if (downloadThread.getCurrentDownloadingItem() == indexItem) { + titleProgressController.indeterminate = false; + titleProgressController.progress = downloadThread.getCurrentDownloadingItemProgress(); + } else { + titleProgressController.indeterminate = true; + titleProgressController.progress = 0; + } + double mb = indexItem.getArchiveSizeMB(); + String v; + if (titleProgressController.progress != -1) { + v = getMapActivity().getString(R.string.value_downloaded_of_max, mb * titleProgressController.progress / 100, mb); + } else { + v = getMapActivity().getString(R.string.file_size_in_mb, mb); + } + if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { + titleProgressController.caption = indexItem.getType().getString(getMapActivity()) + " • " + v; + } else { + titleProgressController.caption = v; + } + titleProgressController.visible = true; + } else if (downloadIndexes) { + titleProgressController.setIndexesDownloadMode(); + titleProgressController.visible = true; + } else { + titleProgressController.visible = false; + } + } } public boolean hasCustomAddressLine() { @@ -353,10 +444,109 @@ public abstract class MenuController extends BaseMenuController { public void onHide() { } - + public void onClose() { } - - public void onAcquireNewController(PointDescription pointDescription2, Object object) { + + public void onAcquireNewController(PointDescription pointDescription, Object object) { } + + public boolean isMapDownloaded() { + return downloaded; + } + + public void buildMapDownloadButton(LatLon latLon) { + int point31x = MapUtils.get31TileNumberX(latLon.getLongitude()); + int point31y = MapUtils.get31TileNumberY(latLon.getLatitude()); + + ResourceManager rm = getMapActivity().getMyApplication().getResourceManager(); + OsmandRegions osmandRegions = rm.getOsmandRegions(); + + List mapDataObjects = null; + try { + mapDataObjects = osmandRegions.queryBbox(point31x, point31x, point31y, point31y); + } catch (IOException e) { + e.printStackTrace(); + } + + if (mapDataObjects != null) { + Iterator it = mapDataObjects.iterator(); + while (it.hasNext()) { + BinaryMapDataObject o = it.next(); + if (!osmandRegions.contain(o, point31x, point31y)) { + it.remove(); + } + } + String selectedFullName = ""; + for (BinaryMapDataObject o : mapDataObjects) { + boolean downloaded = checkIfObjectDownloaded(rm, osmandRegions.getDownloadName(o)); + if (downloaded) { + downloadMapDataObject = null; + break; + } else { + String fullName = osmandRegions.getFullName(o); + if (fullName.length() > selectedFullName.length()) { + selectedFullName = fullName; + downloadMapDataObject = o; + } + } + } + + downloaded = downloadMapDataObject == null; + if (!downloaded) { + downloadThread = getMapActivity().getMyApplication().getDownloadThread(); + downloadRegion = osmandRegions.getRegionData(selectedFullName); + if (downloadRegion != null && downloadRegion.isRegionMapDownload()) { + List indexItems = downloadThread.getIndexes().getIndexItems(downloadRegion); + for (IndexItem item : indexItems) { + if (item.getType() == DownloadActivityType.NORMAL_FILE + && (item.isDownloaded() || downloadThread.isDownloading(item))) { + indexItem = item; + } + } + } + + leftDownloadButtonController = new TitleButtonController() { + @Override + public void buttonPressed() { + if (indexItem != null) { + if (indexItem.getType() == DownloadActivityType.NORMAL_FILE) { + new DownloadValidationManager(getMapActivity().getMyApplication()) + .startDownload(getMapActivity(), indexItem); + } + } + } + }; + leftDownloadButtonController.caption = + downloadRegion != null ? downloadRegion.getLocaleName() : getMapActivity().getString(R.string.shared_string_download); + leftDownloadButtonController.leftIconId = R.drawable.ic_action_import; + + titleProgressController = new TitleProgressController() { + @Override + public void buttonPressed() { + if (indexItem != null) { + downloadThread.cancelDownload(indexItem); + } + } + }; + + if (!downloadThread.getIndexes().isDownloadedFromInternet) { + if (getMapActivity().getMyApplication().getSettings().isInternetConnectionAvailable()) { + downloadThread.runReloadIndexFiles(); + } + } + + updateData(); + } + } + } + + private boolean checkIfObjectDownloaded(ResourceManager rm, String downloadName) { + final String regionName = Algorithms.capitalizeFirstLetterAndLowercase(downloadName) + + IndexConstants.BINARY_MAP_INDEX_EXT; + final String roadsRegionName = Algorithms.capitalizeFirstLetterAndLowercase(downloadName) + ".road" + + IndexConstants.BINARY_MAP_INDEX_EXT; + return rm.getIndexFileNames().containsKey(regionName) || rm.getIndexFileNames().containsKey(roadsRegionName); + } + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapDataMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapDataMenuController.java index 3e97128e47..2774a7a091 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapDataMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/MapDataMenuController.java @@ -43,13 +43,11 @@ import java.util.Map; public class MapDataMenuController extends MenuController { private DownloadMapObject mapObject; - private IndexItem indexItem; private List otherIndexItems; private LocalIndexInfo localIndexInfo; private List otherLocalIndexInfos; private boolean srtmDisabled; private boolean srtmNeedsInstallation; - private boolean downloaded; private boolean backuped; private DownloadIndexesThread downloadThread; @@ -317,6 +315,10 @@ public class MapDataMenuController extends MenuController { } } + if (indexItem != null) { + downloaded = indexItem.isDownloaded(); + } + leftDownloadButtonController.visible = true; leftDownloadButtonController.leftIconId = R.drawable.ic_action_import; if (backuped) { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugMenuController.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugMenuController.java index a7fd05407a..53815a1a2e 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugMenuController.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugMenuController.java @@ -102,6 +102,7 @@ public class OsmBugMenuController extends MenuController { @Override public void updateData() { + super.updateData(); rightTitleButtonController.visible = bug.isOpened(); } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoMenuController.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoMenuController.java index c6182f8189..6689b2a4bb 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoMenuController.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoMenuController.java @@ -69,6 +69,8 @@ public class OsMoMenuController extends MenuController { @Override public void updateData() { + super.updateData(); + if (OsMoPositionLayer.getFollowDestinationId() != null) { leftTitleButtonController.caption = getMapActivity().getString(R.string.shared_string_cancel); leftTitleButtonController.leftIconId = R.drawable.ic_action_remove_dark;