diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index 6df76e1f7e..aadeed5671 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -35,6 +35,8 @@ import net.osmand.plus.dialogs.PluginInstalledBottomSheetDialog; import net.osmand.plus.download.IndexItem; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuController; +import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; +import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask.GetImageCardsListener; import net.osmand.plus.mapillary.MapillaryPlugin; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.myplaces.FavoritesActivity; @@ -209,6 +211,10 @@ public abstract class OsmandPlugin { return Collections.emptyList(); } + protected List getImageCards(@Nullable GetImageCardsListener listener) { + return Collections.emptyList(); + } + /** * Plugin was installed */ @@ -736,7 +742,6 @@ public abstract class OsmandPlugin { return l; } - public static void onMapActivityCreate(MapActivity activity) { for (OsmandPlugin plugin : getEnabledPlugins()) { plugin.mapActivityCreate(activity); @@ -870,6 +875,12 @@ public abstract class OsmandPlugin { return collection; } + public static void populateImageCards(@NonNull List imageCards, @Nullable GetImageCardsListener listener) { + for (OsmandPlugin p : getEnabledPlugins()) { + imageCards.addAll(p.getImageCards(listener)); + } + } + public static boolean isPackageInstalled(String packageInfo, Context ctx) { if (packageInfo == null) { return false; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java index 5cfdd0a281..ca11b8a456 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java @@ -51,6 +51,7 @@ import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.ActivityResultListener; import net.osmand.plus.activities.ActivityResultListener.OnActivityResultListener; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.FontCache; import net.osmand.plus.mapcontextmenu.UploadPhotosAsyncTask.UploadPhotosListener; import net.osmand.plus.mapcontextmenu.builders.cards.AbstractCard; @@ -62,7 +63,6 @@ import net.osmand.plus.mapcontextmenu.controllers.AmenityMenuController; import net.osmand.plus.mapcontextmenu.controllers.TransportStopController; import net.osmand.plus.openplacereviews.AddPhotosBottomSheetDialogFragment; import net.osmand.plus.openplacereviews.OPRConstants; -import net.osmand.plus.openplacereviews.OpenPlaceReviewsPlugin; import net.osmand.plus.openplacereviews.OprStartFragment; import net.osmand.plus.osmedit.opr.OpenDBAPI; import net.osmand.plus.poi.PoiFiltersHelper; @@ -138,16 +138,14 @@ public class MenuBuilder implements UploadPhotosListener { } @Override - public void onPlaceIdAcquired(String[] placeId) { + public void onPlaceIdAcquired(final String[] placeId) { MenuBuilder.this.placeId = placeId; - if (placeId.length < 2 || OsmandPlugin.getEnabledPlugin(OpenPlaceReviewsPlugin.class) == null) { - app.runInUIThread(new Runnable() { - @Override - public void run() { - photoButton.setVisibility(View.GONE); - } - }); - } + app.runInUIThread(new Runnable() { + @Override + public void run() { + AndroidUiHelper.updateVisibility(photoButton, placeId.length >= 2); + } + }); } @Override @@ -479,9 +477,7 @@ public class MenuBuilder implements UploadPhotosListener { } } }); - if (OsmandPlugin.getEnabledPlugin(OpenPlaceReviewsPlugin.class) == null) { - view.setVisibility(View.GONE); - } + AndroidUiHelper.updateVisibility(view, false); photoButton = view; return view; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java index a8eea711fa..1c2339648e 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -29,8 +29,6 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapillary.MapillaryContributeCard; import net.osmand.plus.mapillary.MapillaryImageCard; -import net.osmand.plus.openplacereviews.OPRConstants; -import net.osmand.plus.openplacereviews.OpenPlaceReviewsPlugin; import net.osmand.plus.wikimedia.WikiImageHelper; import net.osmand.util.Algorithms; @@ -43,9 +41,7 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Collections; import java.util.Date; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; @@ -412,28 +408,6 @@ public abstract class ImageCard extends AbstractCard { } } - private static String[] getIdFromResponse(String response) { - try { - JSONArray obj = new JSONObject(response).getJSONArray("objects"); - JSONArray images = (JSONArray) ((JSONObject) obj.get(0)).get("id"); - return toStringArray(images); - } catch (JSONException e) { - e.printStackTrace(); - } - return new String[0]; - } - - private static String[] toStringArray(JSONArray array) { - if (array == null) - return null; - - String[] arr = new String[array.length()]; - for (int i = 0; i < arr.length; i++) { - arr[i] = array.optString(i); - } - return arr; - } - public static class GetImageCardsTask extends AsyncTask> { private MapActivity mapActivity; @@ -465,20 +439,7 @@ public abstract class ImageCard extends AbstractCard { protected List doInBackground(Void... params) { TrafficStats.setThreadStatsTag(GET_IMAGE_CARD_THREAD_ID); List result = new ArrayList<>(); - Object o = mapActivity.getMapLayers().getContextMenuLayer().getSelectedObject(); - if (o instanceof Amenity && OsmandPlugin.getEnabledPlugin(OpenPlaceReviewsPlugin.class) != null) { - Amenity am = (Amenity) o; - long amenityId = am.getId() >> 1; - String baseUrl = OPRConstants.getBaseUrl(app); - String url = baseUrl + "api/objects-by-index?type=opr.place&index=osmid&key=" + amenityId; - String response = AndroidNetworkUtils.sendRequest(app, url, Collections.emptyMap(), - "Requesting location images...", false, false); - if (response != null) { - getPicturesForPlace(result, response); - String[] id = getIdFromResponse(response); - listener.onPlaceIdAcquired(id); - } - } + OsmandPlugin.populateImageCards(result, listener); try { final Map pms = new LinkedHashMap<>(); pms.put("lat", "" + (float) latLon.getLatitude()); @@ -539,36 +500,6 @@ public abstract class ImageCard extends AbstractCard { return result; } - private void getPicturesForPlace(List result, String response) { - try { - if (!Algorithms.isEmpty(response)) { - JSONArray obj = new JSONObject(response).getJSONArray("objects"); - JSONObject imagesWrapper = ((JSONObject) ((JSONObject) obj.get(0)).get("images")); - Iterator it = imagesWrapper.keys(); - while (it.hasNext()) { - JSONArray images = imagesWrapper.getJSONArray(it.next()); - if (images.length() > 0) { - for (int i = 0; i < images.length(); i++) { - try { - JSONObject imageObject = (JSONObject) images.get(i); - if (imageObject != JSONObject.NULL) { - ImageCard imageCard = ImageCard.createCardOpr(mapActivity, imageObject); - if (imageCard != null) { - result.add(imageCard); - } - } - } catch (JSONException e) { - LOG.error(e); - } - } - } - } - } - } catch (Exception e) { - LOG.error(e); - } - } - @Override protected void onPostExecute(List cardList) { result = cardList; diff --git a/OsmAnd/src/net/osmand/plus/openplacereviews/OpenPlaceReviewsPlugin.java b/OsmAnd/src/net/osmand/plus/openplacereviews/OpenPlaceReviewsPlugin.java index 263f62a570..abf4d23b0e 100644 --- a/OsmAnd/src/net/osmand/plus/openplacereviews/OpenPlaceReviewsPlugin.java +++ b/OsmAnd/src/net/osmand/plus/openplacereviews/OpenPlaceReviewsPlugin.java @@ -2,15 +2,38 @@ package net.osmand.plus.openplacereviews; import android.graphics.drawable.Drawable; +import androidx.annotation.Nullable; + +import net.osmand.AndroidNetworkUtils; +import net.osmand.PlatformUtil; +import net.osmand.data.Amenity; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; +import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask.GetImageCardsListener; import net.osmand.plus.settings.fragments.BaseSettingsFragment.SettingsScreenType; +import net.osmand.util.Algorithms; + +import org.apache.commons.logging.Log; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; public class OpenPlaceReviewsPlugin extends OsmandPlugin { + private static final Log LOG = PlatformUtil.getLog(OpenPlaceReviewsPlugin.class); + private static final String ID = "osmand.openplacereviews"; + private MapActivity mapActivity; + public OpenPlaceReviewsPlugin(OsmandApplication app) { super(app); } @@ -46,6 +69,96 @@ public class OpenPlaceReviewsPlugin extends OsmandPlugin { return app.getUIUtilities().getIcon(R.drawable.img_plugin_openplacereviews); } + @Override + public void mapActivityResume(MapActivity activity) { + this.mapActivity = activity; + } + + @Override + public void mapActivityResumeOnTop(MapActivity activity) { + this.mapActivity = activity; + } + + @Override + public void mapActivityPause(MapActivity activity) { + this.mapActivity = null; + } + + @Override + protected List getImageCards(@Nullable GetImageCardsListener listener) { + List imageCards = new ArrayList<>(); + if (mapActivity != null) { + Object object = mapActivity.getMapLayers().getContextMenuLayer().getSelectedObject(); + if (object instanceof Amenity) { + Amenity am = (Amenity) object; + long amenityId = am.getId() >> 1; + String baseUrl = OPRConstants.getBaseUrl(app); + String url = baseUrl + "api/objects-by-index?type=opr.place&index=osmid&key=" + amenityId; + String response = AndroidNetworkUtils.sendRequest(app, url, Collections.emptyMap(), + "Requesting location images...", false, false); + if (response != null) { + getPicturesForPlace(imageCards, response); + if (listener != null) { + listener.onPlaceIdAcquired(getIdFromResponse(response)); + } + } + } + } + return imageCards; + } + + private void getPicturesForPlace(List result, String response) { + try { + if (!Algorithms.isEmpty(response)) { + JSONArray obj = new JSONObject(response).getJSONArray("objects"); + JSONObject imagesWrapper = ((JSONObject) ((JSONObject) obj.get(0)).get("images")); + Iterator it = imagesWrapper.keys(); + while (it.hasNext()) { + JSONArray images = imagesWrapper.getJSONArray(it.next()); + if (images.length() > 0) { + for (int i = 0; i < images.length(); i++) { + try { + JSONObject imageObject = (JSONObject) images.get(i); + if (imageObject != JSONObject.NULL) { + ImageCard imageCard = ImageCard.createCardOpr(mapActivity, imageObject); + if (imageCard != null) { + result.add(imageCard); + } + } + } catch (JSONException e) { + LOG.error(e); + } + } + } + } + } + } catch (Exception e) { + LOG.error(e); + } + } + + private static String[] getIdFromResponse(String response) { + try { + JSONArray obj = new JSONObject(response).getJSONArray("objects"); + JSONArray images = (JSONArray) ((JSONObject) obj.get(0)).get("id"); + return toStringArray(images); + } catch (JSONException e) { + e.printStackTrace(); + } + return new String[0]; + } + + private static String[] toStringArray(JSONArray array) { + if (array == null) + return null; + + String[] arr = new String[array.length()]; + for (int i = 0; i < arr.length; i++) { + arr[i] = array.optString(i); + } + return arr; + } + @Override public void disable(OsmandApplication app) { if (app.getSettings().OPR_USE_DEV_URL.get()) {