Added non webgl image dialog

This commit is contained in:
Alexey Kulish 2017-05-08 20:11:59 +03:00
parent 66178eb756
commit 54dc44bcd4
9 changed files with 423 additions and 284 deletions

View file

@ -13,7 +13,7 @@
android:orientation="vertical">
<LinearLayout
android:id="@+id/image_layout"
android:id="@+id/dialog_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/dashboard_map_toolbar"
@ -22,7 +22,7 @@
android:orientation="horizontal">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/image_close_button"
android:id="@+id/close_button"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="56dp"
android:layout_height="56dp"
@ -32,7 +32,7 @@
android:src="@drawable/ic_action_remove_dark"/>
<LinearLayout
android:id="@+id/image_title_layout"
android:id="@+id/title_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -42,7 +42,7 @@
android:paddingTop="10dp">
<TextView
android:id="@+id/image_title"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
@ -55,7 +55,7 @@
tools:text="Title"/>
<TextView
android:id="@+id/image_description"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
@ -69,7 +69,7 @@
</LinearLayout>
<android.support.v7.widget.AppCompatImageView
android:id="@+id/image_refresh_button"
android:id="@+id/refresh_button"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="48dp"
android:layout_height="56dp"
@ -81,7 +81,7 @@
</LinearLayout>
<LinearLayout
android:id="@+id/image_viewer"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/map_widget_dark"

View file

@ -7,7 +7,7 @@
android:background="@android:color/transparent">
<LinearLayout
android:id="@+id/image_layout"
android:id="@+id/dialog_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/dashboard_map_toolbar"
@ -16,7 +16,7 @@
android:orientation="horizontal">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/image_close_button"
android:id="@+id/close_button"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="56dp"
android:layout_height="56dp"
@ -26,7 +26,7 @@
android:src="@drawable/ic_action_remove_dark"/>
<LinearLayout
android:id="@+id/image_title_layout"
android:id="@+id/title_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
@ -36,7 +36,7 @@
android:paddingTop="10dp">
<TextView
android:id="@+id/image_title"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
@ -49,7 +49,7 @@
tools:text="Title"/>
<TextView
android:id="@+id/image_description"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
@ -63,7 +63,7 @@
</LinearLayout>
<android.support.v7.widget.AppCompatImageView
android:id="@+id/image_refresh_button"
android:id="@+id/refresh_button"
style="@style/Widget.AppCompat.ActionButton"
android:layout_width="48dp"
android:layout_height="56dp"
@ -75,14 +75,14 @@
</LinearLayout>
<LinearLayout
android:id="@+id/image_viewer"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/map_widget_dark"
android:orientation="vertical"/>
<FrameLayout
android:id="@+id/image_shadow"
android:id="@+id/shadow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@drawable/bg_contextmenu_shadow"

View file

@ -55,6 +55,8 @@ public abstract class ImageCard extends AbstractCard {
private String url;
// Image bitmap url
private String imageUrl;
// Image high resolution bitmap url
private String imageHiresUrl;
private int defaultImageLayoutId = R.layout.context_menu_card_image;
@ -101,6 +103,9 @@ public abstract class ImageCard extends AbstractCard {
if (imageObject.has("imageUrl")) {
this.imageUrl = imageObject.getString("imageUrl");
}
if (imageObject.has("imageHiresUrl")) {
this.imageHiresUrl = imageObject.getString("imageHiresUrl");
}
} catch (JSONException e) {
e.printStackTrace();
}
@ -157,6 +162,10 @@ public abstract class ImageCard extends AbstractCard {
return imageUrl;
}
public String getImageHiresUrl() {
return imageHiresUrl;
}
public int getDefaultImageLayoutId() {
return defaultImageLayoutId;
}

View file

@ -0,0 +1,119 @@
package net.osmand.plus.mapcontextmenu.builders.cards.dialogs;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapillary.MapillaryImageDialog;
import net.osmand.plus.views.OsmandMapTileView;
public abstract class ContextMenuCardDialog {
private MapActivity mapActivity;
private static final String KEY_CARD_DIALOG_TYPE = "key_card_dialog_type";
private static final String KEY_CARD_DIALOG_TITLE = "key_card_dialog_title";
private static final String KEY_CARD_DIALOG_DESCRIPTION = "key_card_dialog_description";
private CardDialogType type;
protected String title;
protected String description;
private int prevMapPosition = OsmandSettings.CENTER_CONSTANT;
private boolean portrait;
public enum CardDialogType {
REGULAR,
MAPILLARY
}
protected ContextMenuCardDialog(MapActivity mapActivity, @NonNull CardDialogType type) {
this.mapActivity = mapActivity;
this.type = type;
this.portrait = AndroidUiHelper.isOrientationPortrait(mapActivity);
}
public MapActivity getMapActivity() {
return mapActivity;
}
public boolean isPortrait() {
return portrait;
}
public CardDialogType getType() {
return type;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public void saveMenu(Bundle bundle) {
bundle.putString(KEY_CARD_DIALOG_TYPE, type.name());
if (title != null) {
bundle.putString(KEY_CARD_DIALOG_TITLE, title);
}
if (description != null) {
bundle.putString(KEY_CARD_DIALOG_DESCRIPTION, description);
}
}
protected void restoreFields(Bundle bundle) {
this.title = bundle.getString(KEY_CARD_DIALOG_TITLE);
this.description = bundle.getString(KEY_CARD_DIALOG_TITLE);
}
public static ContextMenuCardDialog restoreMenu(@NonNull Bundle bundle, @NonNull MapActivity mapActivity) {
try {
CardDialogType type = CardDialogType.valueOf(bundle.getString(KEY_CARD_DIALOG_TYPE));
ContextMenuCardDialog dialog = null;
switch (type) {
case MAPILLARY:
dialog = new MapillaryImageDialog(mapActivity, bundle);
break;
}
return dialog;
} catch (Exception e) {
return null;
}
}
public void onResume() {
shiftMapPosition();
}
public void onPause() {
restoreMapPosition();
}
private void shiftMapPosition() {
OsmandMapTileView mapView = mapActivity.getMapView();
if (AndroidUiHelper.isOrientationPortrait(mapActivity)) {
if (mapView.getMapPosition() != OsmandSettings.MIDDLE_CONSTANT) {
prevMapPosition = mapView.getMapPosition();
mapView.setMapPosition(OsmandSettings.MIDDLE_CONSTANT);
}
} else {
mapView.setMapPositionX(1);
}
}
private void restoreMapPosition() {
if (AndroidUiHelper.isOrientationPortrait(mapActivity)) {
mapActivity.getMapView().setMapPosition(prevMapPosition);
} else {
mapActivity.getMapView().setMapPositionX(0);
}
}
public abstract View getContentView();
}

View file

@ -1,4 +1,4 @@
package net.osmand.plus.mapcontextmenu.other;
package net.osmand.plus.mapcontextmenu.builders.cards.dialogs;
import android.os.Bundle;
import android.support.annotation.Nullable;
@ -15,42 +15,41 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.util.Algorithms;
public class WebImageMenuFragment extends Fragment {
public static final String TAG = "WebImageMenuFragment";
public class ContextMenuCardDialogFragment extends Fragment {
public static final String TAG = "ContextMenuCardDialogFragment";
private WebImageMenu menu;
private View view;
private LinearLayout viewerLayout;
private ContextMenuCardDialog dialog;
private LinearLayout contentLayout;
private View contentView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null && getActivity() instanceof MapActivity) {
menu = WebImageMenu.restoreMenu(savedInstanceState, (MapActivity) getActivity());
dialog = ContextMenuCardDialog.restoreMenu(savedInstanceState, (MapActivity) getActivity());
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.map_image_view, container, false);
viewerLayout = (LinearLayout) view.findViewById(R.id.image_viewer);
contentView = menu.getContentView();
View view = inflater.inflate(R.layout.context_menu_card_dialog, container, false);
contentLayout = (LinearLayout) view.findViewById(R.id.content);
contentView = dialog.getContentView();
if (contentView != null) {
viewerLayout.addView(contentView);
contentLayout.addView(contentView);
}
view.findViewById(R.id.image_close_button).setOnClickListener(new View.OnClickListener() {
view.findViewById(R.id.close_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
if (!Algorithms.isEmpty(menu.getTitle())) {
((TextView) view.findViewById(R.id.image_title)).setText(menu.getTitle());
if (!Algorithms.isEmpty(dialog.getTitle())) {
((TextView) view.findViewById(R.id.title)).setText(dialog.getTitle());
}
if (!Algorithms.isEmpty(menu.getDescription())) {
((TextView) view.findViewById(R.id.image_description)).setText(menu.getDescription());
if (!Algorithms.isEmpty(dialog.getDescription())) {
((TextView) view.findViewById(R.id.description)).setText(dialog.getDescription());
}
return view;
}
@ -58,24 +57,24 @@ public class WebImageMenuFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
if (menu != null) {
menu.onResume();
if (dialog != null) {
dialog.onResume();
}
}
@Override
public void onPause() {
super.onPause();
if (menu != null) {
menu.onPause();
if (dialog != null) {
dialog.onPause();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
if (viewerLayout != null && contentView != null) {
viewerLayout.removeView(contentView);
if (contentLayout != null && contentView != null) {
contentLayout.removeView(contentView);
if (contentView instanceof WebView) {
((WebView) contentView).loadUrl("about:blank");
}
@ -84,19 +83,19 @@ public class WebImageMenuFragment extends Fragment {
@Override
public void onSaveInstanceState(Bundle outState) {
menu.saveMenu(outState);
dialog.saveMenu(outState);
}
public static void showInstance(WebImageMenu menu) {
WebImageMenuFragment fragment = new WebImageMenuFragment();
fragment.menu = menu;
public static void showInstance(ContextMenuCardDialog menu) {
ContextMenuCardDialogFragment fragment = new ContextMenuCardDialogFragment();
fragment.dialog = menu;
menu.getMapActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.topFragmentContainer, fragment, TAG)
.addToBackStack(TAG).commit();
}
public void dismiss() {
MapActivity activity = menu.getMapActivity();
MapActivity activity = dialog.getMapActivity();
if (activity != null) {
try {
activity.getSupportFragmentManager().popBackStack(TAG,

View file

@ -1,239 +0,0 @@
package net.osmand.plus.mapcontextmenu.other;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.LinearLayout;
import net.osmand.AndroidUtils;
import net.osmand.data.LatLon;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.mapillary.MapillaryLayer;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.Algorithms;
public class WebImageMenu {
private MapActivity mapActivity;
private static final String KEY_WEB_IMAGE_MENU_TYPE = "key_web_image_menu_type";
private static final String KEY_WEB_IMAGE_MENU_VIEWER_URL = "key_web_image_menu_viewer_url";
private static final String KEY_WEB_IMAGE_MENU_LATLON = "key_web_image_menu_latlon";
private static final String KEY_WEB_IMAGE_MENU_CA = "key_web_image_menu_ca";
private static final String KEY_WEB_IMAGE_MENU_TITLE = "key_web_image_menu_title";
private static final String KEY_WEB_IMAGE_MENU_DESCRIPTION = "key_web_image_menu_description";
private static final String MAPILLARY_VIEWER_URL_TEMPLATE =
"https://osmand.net/api/mapillary/photo-viewer.php?photo_id=";
private WebImageType type;
private String viewerUrl;
private LatLon latLon;
private double ca = Double.NaN;
private String title;
private String description;
private int prevMapPosition = OsmandSettings.CENTER_CONSTANT;
public enum WebImageType {
MAPILLARY
}
private WebImageMenu(MapActivity mapActivity, @NonNull WebImageType type, @NonNull String viewerUrl,
LatLon latLon, double ca, String title, String description) {
this.mapActivity = mapActivity;
this.type = type;
this.viewerUrl = viewerUrl;
this.latLon = latLon;
this.ca = ca;
this.title = title;
this.description = description;
}
public MapActivity getMapActivity() {
return mapActivity;
}
public WebImageType getType() {
return type;
}
public String getViewerUrl() {
return viewerUrl;
}
public LatLon getLatLon() {
return latLon;
}
public double getCa() {
return ca;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public void saveMenu(Bundle bundle) {
bundle.putString(KEY_WEB_IMAGE_MENU_TYPE, type.name());
bundle.putSerializable(KEY_WEB_IMAGE_MENU_VIEWER_URL, viewerUrl);
bundle.putSerializable(KEY_WEB_IMAGE_MENU_LATLON, latLon);
bundle.putString(KEY_WEB_IMAGE_MENU_TITLE, title);
bundle.putDouble(KEY_WEB_IMAGE_MENU_CA, ca);
if (description != null) {
bundle.putString(KEY_WEB_IMAGE_MENU_DESCRIPTION, description);
}
}
public static WebImageMenu restoreMenu(Bundle bundle, MapActivity mapActivity) {
try {
WebImageType type = WebImageType.valueOf(bundle.getString(KEY_WEB_IMAGE_MENU_TYPE));
String viewerUrl = bundle.getString(KEY_WEB_IMAGE_MENU_VIEWER_URL);
LatLon latLon = null;
String title = bundle.getString(KEY_WEB_IMAGE_MENU_TITLE);
Object latLonObj = bundle.getSerializable(KEY_WEB_IMAGE_MENU_LATLON);
if (latLonObj != null) {
latLon = (LatLon) latLonObj;
}
Double ca = bundle.getDouble(KEY_WEB_IMAGE_MENU_CA, Double.NaN);
String description = bundle.getString(KEY_WEB_IMAGE_MENU_TITLE);
if (viewerUrl != null) {
return new WebImageMenu(mapActivity, type, viewerUrl, latLon, ca, title, description);
} else {
return null;
}
} catch (IllegalArgumentException e) {
return null;
}
}
public void onResume() {
setImageLocation(latLon, ca, true);
}
public void onPause() {
restoreMapPosition();
setImageLocation(null, Double.NaN, false);
}
public void setImageLocation(LatLon latLon, double ca, boolean animated) {
switch (type) {
case MAPILLARY:
MapillaryLayer layer = mapActivity.getMapView().getLayerByClass(MapillaryLayer.class);
if (layer != null) {
layer.setSelectedImageLocation(latLon);
if (!Double.isNaN(ca)) {
layer.setSelectedImageCameraAngle((float) ca);
} else {
layer.setSelectedImageCameraAngle(null);
}
}
break;
}
if (latLon != null) {
shiftMapPosition();
if (animated) {
mapActivity.getMapView().getAnimatedDraggingThread().startMoving(
latLon.getLatitude(), latLon.getLongitude(), mapActivity.getMapView().getZoom(), true);
} else {
mapActivity.setMapLocation(latLon.getLatitude(), latLon.getLongitude());
}
} else {
mapActivity.refreshMap();
}
}
private void shiftMapPosition() {
OsmandMapTileView mapView = mapActivity.getMapView();
if (AndroidUiHelper.isOrientationPortrait(mapActivity)) {
if (mapView.getMapPosition() != OsmandSettings.MIDDLE_CONSTANT) {
prevMapPosition = mapView.getMapPosition();
mapView.setMapPosition(OsmandSettings.MIDDLE_CONSTANT);
}
} else {
mapView.setMapPositionX(1);
}
}
private void restoreMapPosition() {
if (AndroidUiHelper.isOrientationPortrait(mapActivity)) {
mapActivity.getMapView().setMapPosition(prevMapPosition);
} else {
mapActivity.getMapView().setMapPositionX(0);
}
}
public View getContentView() {
switch (type) {
case MAPILLARY:
return getWebView(viewerUrl);
default:
return null;
}
}
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
private WebView getWebView(String url) {
final WebView webView = new WebView(mapActivity);
webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
//webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
webView.setScrollContainer(false);
webView.getSettings().setJavaScriptEnabled(true);
if (type == WebImageType.MAPILLARY) {
webView.addJavascriptInterface(new MapillaryWebAppInterface(mapActivity), "Android");
}
boolean portrait = AndroidUiHelper.isOrientationPortrait(mapActivity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
portrait ? ViewGroup.LayoutParams.MATCH_PARENT : AndroidUtils.dpToPx(mapActivity, 360f),
portrait ? AndroidUtils.dpToPx(mapActivity, 270f) : ViewGroup.LayoutParams.MATCH_PARENT);
webView.setLayoutParams(lp);
webView.loadUrl(url);
return webView;
}
private class MapillaryWebAppInterface {
Context mContext;
MapillaryWebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void onNodeChanged(double latitude, double longitude, double ca, String key) {
LatLon latLon = null;
if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
latLon = new LatLon(latitude, longitude);
WebImageMenu.this.latLon = latLon;
WebImageMenu.this.ca = ca;
if (!Algorithms.isEmpty(key)) {
WebImageMenu.this.viewerUrl = MAPILLARY_VIEWER_URL_TEMPLATE + key;
}
}
setImageLocation(latLon, ca, false);
}
}
public static WebImageMenu show(MapActivity mapActivity, WebImageType type, String viewerUrl, LatLon latLon, double ca,
String title, String description) {
WebImageMenu menu = new WebImageMenu(mapActivity, type, viewerUrl, latLon, ca, title, description);
WebImageMenuFragment.showInstance(menu);
return menu;
}
}

View file

@ -5,8 +5,6 @@ import android.view.View;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard;
import net.osmand.plus.mapcontextmenu.other.WebImageMenu;
import net.osmand.plus.mapcontextmenu.other.WebImageMenu.WebImageType;
import org.json.JSONObject;
@ -19,7 +17,7 @@ public class MapillaryImageCard extends ImageCard {
@Override
public void onClick(View v) {
getMapActivity().getContextMenu().hideMenues();
WebImageMenu.show(getMapActivity(), WebImageType.MAPILLARY, getUrl(), getLocation(),
MapillaryImageDialog.show(getMapActivity(), getImageHiresUrl(), getUrl(), getLocation(),
getCa(), getMyApplication().getString(R.string.mapillary), null);
}
};

View file

@ -0,0 +1,243 @@
package net.osmand.plus.mapillary;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import net.osmand.AndroidNetworkUtils;
import net.osmand.AndroidUtils;
import net.osmand.data.LatLon;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard;
import net.osmand.plus.mapcontextmenu.builders.cards.dialogs.ContextMenuCardDialog;
import net.osmand.plus.mapcontextmenu.builders.cards.dialogs.ContextMenuCardDialogFragment;
import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.Algorithms;
public class MapillaryImageDialog extends ContextMenuCardDialog {
private static final String KEY_MAPILLARY_DIALOG_IMAGE_URL = "key_mapillary_dialog_image_url";
private static final String KEY_MAPILLARY_DIALOG_VIEWER_URL = "key_mapillary_dialog_viewer_url";
private static final String KEY_MAPILLARY_DIALOG_LATLON = "key_mapillary_dialog_latlon";
private static final String KEY_MAPILLARY_DIALOG_CA = "key_mapillary_dialog_ca";
private static final String MAPILLARY_VIEWER_URL_TEMPLATE =
"https://osmand.net/api/mapillary/photo-viewer.php?photo_id=";
private static final String MAPILLARY_HIRES_IMAGE_URL_TEMPLATE =
"https://osmand.net/api/mapillary/get_photo.php?hires=true&photo_id=";
private static final String WEBGL_ERROR_MESSAGE = "Error creating WebGL context";
private String imageUrl;
private String viewerUrl;
private LatLon latLon;
private double ca = Double.NaN;
public MapillaryImageDialog(@NonNull MapActivity mapActivity, @NonNull Bundle bundle) {
super(mapActivity, CardDialogType.MAPILLARY);
restoreFields(bundle);
}
public MapillaryImageDialog(MapActivity mapActivity, String imageUrl, String viewerUrl, LatLon latLon, double ca,
String title, String description) {
super(mapActivity, CardDialogType.MAPILLARY);
this.title = title;
this.description = description;
this.imageUrl = imageUrl;
this.viewerUrl = viewerUrl;
this.latLon = latLon;
this.ca = ca;
}
public String getViewerUrl() {
return viewerUrl;
}
public LatLon getLatLon() {
return latLon;
}
public double getCa() {
return ca;
}
public void saveMenu(Bundle bundle) {
super.saveMenu(bundle);
bundle.putSerializable(KEY_MAPILLARY_DIALOG_IMAGE_URL, imageUrl);
bundle.putSerializable(KEY_MAPILLARY_DIALOG_VIEWER_URL, viewerUrl);
bundle.putSerializable(KEY_MAPILLARY_DIALOG_LATLON, latLon);
bundle.putDouble(KEY_MAPILLARY_DIALOG_CA, ca);
}
@Override
protected void restoreFields(Bundle bundle) {
super.restoreFields(bundle);
this.imageUrl = bundle.getString(KEY_MAPILLARY_DIALOG_IMAGE_URL);
this.viewerUrl = bundle.getString(KEY_MAPILLARY_DIALOG_VIEWER_URL);
Object latLonObj = bundle.getSerializable(KEY_MAPILLARY_DIALOG_LATLON);
if (latLonObj != null) {
this.latLon = (LatLon) latLonObj;
}
this.ca = bundle.getDouble(KEY_MAPILLARY_DIALOG_CA, Double.NaN);
}
public void onResume() {
super.onResume();
setImageLocation(latLon, ca, true);
}
public void onPause() {
super.onPause();
setImageLocation(null, Double.NaN, false);
}
private void setImageLocation(LatLon latLon, double ca, boolean animated) {
OsmandMapTileView mapView = getMapActivity().getMapView();
MapillaryLayer layer = mapView.getLayerByClass(MapillaryLayer.class);
if (layer != null) {
layer.setSelectedImageLocation(latLon);
if (!Double.isNaN(ca)) {
layer.setSelectedImageCameraAngle((float) ca);
} else {
layer.setSelectedImageCameraAngle(null);
}
}
if (latLon != null) {
if (animated) {
mapView.getAnimatedDraggingThread().startMoving(
latLon.getLatitude(), latLon.getLongitude(), mapView.getZoom(), true);
} else {
getMapActivity().setMapLocation(latLon.getLatitude(), latLon.getLongitude());
}
} else {
getMapActivity().refreshMap();
}
}
public View getContentView() {
if (MapillaryPlugin.isWebGlSupported()) {
return getWebView();
} else {
return getStaticImageView();
}
}
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
private WebView getWebView() {
final WebView webView = new WebView(getMapActivity());
webView.setBackgroundColor(Color.argb(1, 0, 0, 0));
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
webView.setScrollContainer(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MapillaryWebAppInterface(), "Android");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
isPortrait() ? ViewGroup.LayoutParams.MATCH_PARENT : AndroidUtils.dpToPx(getMapActivity(), 360f),
isPortrait() ? AndroidUtils.dpToPx(getMapActivity(), 270f) : ViewGroup.LayoutParams.MATCH_PARENT);
webView.setLayoutParams(lp);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
if (!Algorithms.isEmpty(consoleMessage.message()) && consoleMessage.message().contains(WEBGL_ERROR_MESSAGE)) {
MapillaryPlugin.setWebGlSupported(false);
show(getMapActivity(), imageUrl, viewerUrl, getLatLon(), getCa(), getTitle(), getDescription());
}
return false;
}
});
webView.loadUrl(viewerUrl);
return webView;
}
private class MapillaryWebAppInterface {
@JavascriptInterface
public void onNodeChanged(double latitude, double longitude, double ca, String key) {
LatLon latLon = null;
if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
latLon = new LatLon(latitude, longitude);
MapillaryImageDialog.this.latLon = latLon;
MapillaryImageDialog.this.ca = ca;
if (!Algorithms.isEmpty(key)) {
MapillaryImageDialog.this.imageUrl = MAPILLARY_HIRES_IMAGE_URL_TEMPLATE + key;
MapillaryImageDialog.this.viewerUrl = MAPILLARY_VIEWER_URL_TEMPLATE + key;
}
}
setImageLocation(latLon, ca, false);
}
}
private View getStaticImageView() {
LinearLayout ll = new LinearLayout(getMapActivity());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
isPortrait() ? ViewGroup.LayoutParams.MATCH_PARENT : AndroidUtils.dpToPx(getMapActivity(), 360f),
isPortrait() ? AndroidUtils.dpToPx(getMapActivity(), 270f) : ViewGroup.LayoutParams.MATCH_PARENT);
ll.setGravity(Gravity.CENTER);
ll.setLayoutParams(lp);
ProgressBar progressBar = new ProgressBar(getMapActivity());
progressBar.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ll.addView(progressBar);
ImageView imageView = new ImageView(getMapActivity());
imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
ll.addView(imageView);
if (!Algorithms.isEmpty(imageUrl)) {
ImageCard.execute(new DownloadImageTask(progressBar, imageView));
}
return ll;
}
public static MapillaryImageDialog show(MapActivity mapActivity, String imageUrl, String viewerUrl, LatLon latLon, double ca,
String title, String description) {
MapillaryImageDialog dialog = new MapillaryImageDialog(mapActivity, imageUrl, viewerUrl, latLon, ca, title, description);
ContextMenuCardDialogFragment.showInstance(dialog);
return dialog;
}
private class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> {
private ProgressBar progressBar;
private ImageView imageView;
public DownloadImageTask(ProgressBar progressBar, ImageView imageView) {
this.progressBar = progressBar;
this.imageView = imageView;
}
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Bitmap doInBackground(Void... params) {
return AndroidNetworkUtils.downloadImage(getMapActivity().getMyApplication(), imageUrl);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
progressBar.setVisibility(View.GONE);
if (bitmap != null) {
imageView.setImageDrawable(new BitmapDrawable(getMapActivity().getResources(), bitmap));
}
}
}
}

View file

@ -45,6 +45,8 @@ public class MapillaryPlugin extends OsmandPlugin {
private TextInfoWidget mapillaryControl;
private MapWidgetRegInfo mapillaryWidgetRegInfo;
private static boolean webGlSupported = true;
public MapillaryPlugin(OsmandApplication app) {
this.app = app;
settings = app.getSettings();
@ -81,6 +83,14 @@ public class MapillaryPlugin extends OsmandPlugin {
registerWidget(activity);
}
public static boolean isWebGlSupported() {
return webGlSupported;
}
public static void setWebGlSupported(boolean webGlSupported) {
MapillaryPlugin.webGlSupported = webGlSupported;
}
private void createLayers() {
rasterLayer = new MapillaryLayer();
}