/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using ElmSharp;
using Layout = ElmSharp.Layout;
using EvasObject = ElmSharp.EvasObject;
using System.Collections.Generic;
namespace Tizen.Maps
{
///
/// Map View class to show a map on the screen.
///
/// 3
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public class MapView : Layout, IDisposable
{
internal Interop.ViewHandle handle;
private MapService _service;
private Dictionary _handleToObjectTable = new Dictionary();
private Interop.ViewOnEventCallback _gestureEventCallback;
private Interop.ViewOnEventCallback _objectEventCallback;
private Interop.ViewOnEventCallback _viewReadyEventCallback;
private event EventHandler _scrolledEventHandler;
private event EventHandler _twoFingerZoomedEventHandler;
private event EventHandler _clickedEventHandler;
private event EventHandler _doubleClickedEventHandler;
private event EventHandler _twoFingerClickedEventHandler;
private event EventHandler _twoFingerRotatedEventHandler;
private event EventHandler _longPressedEventHandler;
private event EventHandler _viewReadyEventHandler;
///
/// Creates a view and links it to the instance of a map service.
///
/// 3
/// An instance of object for which a map view will be drawn.
/// An instance of object.
/// http://tizen.org/privilege/mapservice
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when parameters are invalid.
/// Thrown when a native operation failed to allocate memory, and connect to the service.
public MapView(EvasObject parent, MapService service) : base(parent)
{
handle = new Interop.ViewHandle(service.handle, this);
service.handle.HasOwnership = false;
Log.Info(string.Format("MapView is created"));
_service = service;
this.Resize(1, 1);
// We need to keep Gesture Tap event enabled for object event to work
handle.SetGestureEnabled(Interop.ViewGesture.Click, true);
SetObjectEventCallback();
}
///
/// Destroy the MapView object.
///
~MapView()
{
Dispose(false);
}
///
/// Adds or removes event handlers to deliver a scrolled gesture event.
///
/// Event handlers to get a scrolled gesture event.
/// 3
/// Thrown when the required feature is not supported.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public event EventHandler Scrolled
{
add
{
SetGestureEventCallback();
handle.SetGestureEnabled(Interop.ViewGesture.Scroll, true);
_scrolledEventHandler += value;
Log.Info(string.Format("Scrolled event handler is added"));
}
remove
{
_scrolledEventHandler -= value;
Log.Info(string.Format("Scrolled event handler is removed"));
if (_scrolledEventHandler == null)
{
handle.SetGestureEnabled(Interop.ViewGesture.Scroll, false);
UnsetGestureEventCallback();
}
}
}
///
/// Adds or removes event handlers to deliver a zoomed gesture event.
///
/// Event handlers to get a zoomed gesture event.
/// 3
/// Thrown when the required feature is not supported.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public event EventHandler TwoFingerZoomed
{
add
{
SetGestureEventCallback();
handle.SetGestureEnabled(Interop.ViewGesture.Zoom, true);
_twoFingerZoomedEventHandler += value;
Log.Info(string.Format("TwoFingerZoomed event handler is added"));
}
remove
{
_twoFingerZoomedEventHandler -= value;
Log.Info(string.Format("TwoFingerZoomed event handler is removed"));
if (_twoFingerZoomedEventHandler == null)
{
handle.SetGestureEnabled(Interop.ViewGesture.Zoom, false);
UnsetGestureEventCallback();
}
}
}
///
/// Adds or removes event handlers to deliver a clicked gesture event.
///
/// Event handlers to get a clicked gesture event.
/// 3
/// Thrown when the required feature is not supported.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public event EventHandler Clicked
{
add
{
SetGestureEventCallback();
//handle.SetGestureEnabled(Interop.ViewGesture.Click, true);
_clickedEventHandler += value;
Log.Info(string.Format("Clicked event handler is added"));
}
remove
{
_clickedEventHandler -= value;
Log.Info(string.Format("Clicked event handler is removed"));
if (_clickedEventHandler == null)
{
//handle.SetGestureEnabled(Interop.ViewGesture.Click, false);
UnsetGestureEventCallback();
}
}
}
///
/// Adds or removes event handlers to deliver a double-clicked gesture event.
///
/// Event handlers to get a double-clicked gesture event.
/// 3
/// Thrown when the required feature is not supported.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public event EventHandler DoubleClicked
{
add
{
SetGestureEventCallback();
handle.SetGestureEnabled(Interop.ViewGesture.DoubleClick, true);
_doubleClickedEventHandler += value;
Log.Info(string.Format("DoubleClicked event handler is removed"));
}
remove
{
_doubleClickedEventHandler -= value;
Log.Info(string.Format("DoubleClicked event handler is removed"));
if (_doubleClickedEventHandler == null)
{
handle.SetGestureEnabled(Interop.ViewGesture.DoubleClick, false);
UnsetGestureEventCallback();
}
}
}
///
/// Adds or removes event handlers to deliver a clicked gesture event with two-fingers.
///
/// Event handlers to get a clicked gesture event.
/// 3
/// Thrown when the required feature is not supported.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public event EventHandler TwoFingerClicked
{
add
{
SetGestureEventCallback();
handle.SetGestureEnabled(Interop.ViewGesture.TwoFingerClick, true);
_twoFingerClickedEventHandler += value;
Log.Info(string.Format("TwoFingerClicked event handler is added"));
}
remove
{
_twoFingerClickedEventHandler -= value;
Log.Info(string.Format("TwoFingerClicked event handler is removed"));
if (_twoFingerClickedEventHandler == null)
{
handle.SetGestureEnabled(Interop.ViewGesture.TwoFingerClick, false);
UnsetGestureEventCallback();
}
}
}
///
/// Adds or removes event handlers to deliver a rotated gesture event.
///
/// Event handlers to get a rotated gesture event.
/// 3
/// Thrown when the required feature is not supported.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public event EventHandler TwoFingerRotated
{
add
{
SetGestureEventCallback();
handle.SetGestureEnabled(Interop.ViewGesture.Rotation, true);
_twoFingerRotatedEventHandler += value;
Log.Info(string.Format("Rotated event handler is added"));
}
remove
{
_twoFingerRotatedEventHandler -= value;
Log.Info(string.Format("Rotated event handler is removed"));
if (_twoFingerRotatedEventHandler == null)
{
handle.SetGestureEnabled(Interop.ViewGesture.Rotation, false);
UnsetGestureEventCallback();
}
}
}
///
/// Adds or removes event handlers to deliver a long-pressed gesture event.
///
/// Event handlers to get a long-pressed gesture event.
/// 3
/// Thrown when the required feature is not supported.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public event EventHandler LongPressed
{
add
{
SetGestureEventCallback();
handle.SetGestureEnabled(Interop.ViewGesture.LongPress, true);
_longPressedEventHandler += value;
Log.Info(string.Format("LongPressed event handler is added"));
}
remove
{
_longPressedEventHandler -= value;
Log.Info(string.Format("LongPressed event handler is removed"));
if (_longPressedEventHandler == null)
{
handle.SetGestureEnabled(Interop.ViewGesture.LongPress, false);
UnsetGestureEventCallback();
}
}
}
///
/// Adds or removes event handlers to deliver an event representing a view ready to be used.
///
/// Event handlers to get a view ready event.
/// 3
/// Thrown when the required feature is not supported.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public event EventHandler ViewReady
{
add
{
SetViewReadyEventCallback();
_viewReadyEventHandler += value;
Log.Info(string.Format("ViewReady event handler is added"));
}
remove
{
_viewReadyEventHandler -= value;
Log.Info(string.Format("ViewReady event handler is removed"));
UnsetGestureEventCallback();
}
}
///
/// Gets or sets the current zoom level.
///
/// It is an integer value representing the current zoom level.
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public int ZoomLevel
{
get
{
return handle.ZoomLevel;
}
set
{
Log.Info(string.Format("ZoomLevel is changed from {0} to {1}", handle.ZoomLevel, value));
handle.ZoomLevel = value;
}
}
///
/// Gets or sets the minimum zoom level.
///
/// It is an integer value that limits minimal zoom level within a range of the current map plug-in support.
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public int MinimumZoomLevel
{
get
{
return handle.MinimumZoomLevel;
}
set
{
Log.Info(string.Format("MinimumZoomLevel is changed from {0} to {1}", handle.MinimumZoomLevel, value));
handle.MinimumZoomLevel = value;
}
}
///
/// Gets or sets the maximum zoom level.
///
/// It is an integer value that limits maximum zoom level within a range of the current map plug-in support.
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public int MaximumZoomLevel
{
get
{
return handle.MaximumZoomLevel;
}
set
{
Log.Info(string.Format("MaximumZoomLevel is changed from {0} to {1}", handle.MaximumZoomLevel, value));
handle.MaximumZoomLevel = value;
}
}
///
/// Gets or sets the orientation on the map view.
///
/// It is an integer value from 0 to 360 that indicates the orientation of the map view.
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public double Orientation
{
get
{
return handle.Orientation;
}
set
{
Log.Info(string.Format("Orientation is changed from {0} to {1}", handle.Orientation, value));
handle.Orientation = value;
}
}
///
/// Gets or sets theme type of the map view.
///
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public MapTypes MapType
{
get
{
return (MapTypes)handle.MapType;
}
set
{
Log.Info(string.Format("MapType is changed from {0} to {1}", handle.MapType, value));
handle.MapType = (Interop.ViewType)value;
}
}
///
/// Indicates whether the map should show the 3D buildings layer.
///
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public bool BuildingsEnabled
{
get
{
return handle.BuildingsEnabled;
}
set
{
Log.Info(string.Format("Showing the 3D buildings is {0}", (value ? "enabled" : "disabled")));
handle.BuildingsEnabled = value;
}
}
///
/// Indicates whether the map should show the traffic layer.
///
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public bool TrafficEnabled
{
get
{
return handle.TrafficEnabled;
}
set
{
Log.Info(string.Format("Showing the traffic is {0}", (value ? "enabled" : "disabled")));
handle.TrafficEnabled = value;
}
}
///
/// Indicates whether the map should show the public transit layer.
///
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public bool PublicTransitEnabled
{
get
{
return handle.PublicTransitEnabled;
}
set
{
Log.Info(string.Format("Showing the public transit is {0}", (value ? "enabled" : "disabled")));
handle.PublicTransitEnabled = value;
}
}
///
/// Indicates whether the scale-bar is enabled or not.
///
/// 4
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public bool ScaleBarEnabled
{
get
{
return handle.ScaleBarEnabled;
}
set
{
Log.Info(string.Format("Showing the scale-bar is {0}", (value ? "enabled" : "disabled")));
handle.ScaleBarEnabled = value;
}
}
///
/// Sets language of map view.
///
/// The display language in the map.
/// A language is specified as an ISO 3166 alpha-2 two letter country-code
/// followed by ISO 639-1 for the two-letter language code.
/// Each language tag is composed of one or more "subtags" separated by hyphens (-).
/// Each subtag is composed of basic Latin letters or digits only.
/// For example, "ko-KR" for Korean, "en-US" for American English.
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when the value is invalid.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public string Language
{
get
{
return handle.Language;
}
set
{
Log.Info(string.Format("Language is changed from {0} to {1}", handle.Language, value));
handle.Language = value;
}
}
///
/// Gets or sets geographical coordinates for map view's center.
///
/// 3
/// http://tizen.org/privilege/mapservice
/// http://tizen.org/privilege/internet
/// http://tizen.org/privilege/network.get
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when the value is invalid.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public Geocoordinates Center
{
get
{
return new Geocoordinates(handle.Center);
}
set
{
Log.Info(string.Format("Center is changed from {0} to {1}", handle.Center.ToString(), value.ToString()));
handle.Center = value.handle;
}
}
///
/// Gets a list of the map object added to map view.
///
/// 3
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public new IEnumerable Children
{
get
{
return _handleToObjectTable.Values;
}
}
///
/// Changes the geographical coordinates to screen coordinates.
///
/// 3
/// Geographical coordinates.
/// Returns an instance of the screen coordinates on the current screen.
/// http://tizen.org/privilege/mapservice
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when the value is invalid.
/// Thrown when a native operation failed to allocate memory and connect to the service.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public Point GeolocationToScreen(Geocoordinates coordinates)
{
return handle.GeolocationToScreen(coordinates.handle);
}
///
/// Changes the screen coordinates to geographical coordinates.
///
/// 3
/// Screen coordinates.
/// Returns an instance of the geographical coordinates object.
/// http://tizen.org/privilege/mapservice
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when the value is invalid.
/// Thrown when a native operation failed to allocate memory and connect to the service.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public Geocoordinates ScreenToGeolocation(Point screenCoordinates)
{
return new Geocoordinates(handle.ScreenToGeolocation(screenCoordinates));
}
///
/// Adds a map object to map view.
///
/// 3
/// An instance of the map object to be added.
/// http://tizen.org/privilege/mapservice
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when the value is invalid.
/// Thrown when a native operation failed to allocate memory and connect to the service.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public void Add(MapObject child)
{
Log.Info(string.Format("Add a object"));
var objectHandle = child.GetHandle();
if (!_handleToObjectTable.ContainsKey(objectHandle))
{
_handleToObjectTable[objectHandle] = child;
handle.AddObject(objectHandle);
// MapView take ownership of added map objects
objectHandle.HasOwnership = false;
}
}
///
/// Removes a map object from the map view.
///
/// 3
/// An instance of the map object to be removed.
/// Once removed, the child object will be become invalid.
/// http://tizen.org/privilege/mapservice
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when the value is invalid.
/// Thrown when native operation failed to allocate memory and connect to the service.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public void Remove(MapObject child)
{
Log.Info(string.Format("Remove a object"));
var objectHandle = child.GetHandle();
if (_handleToObjectTable.Remove(objectHandle))
{
handle.RemoveObject(child.GetHandle());
// The object handle will be released automatically by the View, once RemoveObject call is successful
child.InvalidateMapObject();
}
}
///
/// Removes all map objects from the map view.
///
/// 3
/// http://tizen.org/privilege/mapservice
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when a native operation failed to allocate memory and connect to the service.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public void RemoveAll()
{
Log.Info(string.Format("Remove all of objects"));
foreach (var child in _handleToObjectTable.Values)
{
child.InvalidateMapObject();
}
_handleToObjectTable.Clear();
handle.RemoveAllObjects();
}
///
/// Captures a snapshot of the map view.
///
/// 3
/// Type of file format.
/// An integer value representing the quality for encoding from 1 to 100.
/// A string representing the file path for a snapshot.
/// http://tizen.org/privilege/mapservice
/// Thrown when the required feature is not supported.
/// Thrown when application does not have some privilege to access this method.
/// Thrown when the value is invalid.
/// Thrown when a native operation failed to allocate memory and connect to the service.
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public void CaptureSnapshot(SnapshotType type, int quality, string path)
{
var err = Interop.ViewSnapshot.ViewCaptureSnapshot(handle, (Interop.ViewSnapshotFormatType)type, quality, path);
err.ThrowIfFailed("Failed to create snapshot for the view");
}
private void SetGestureEventCallback()
{
if (_gestureEventCallback == null)
{
_gestureEventCallback = (type, eventData, userData) =>
{
if (type != Interop.ViewEventType.Gesture) return;
var eventArg = new MapGestureEventArgs(eventData);
switch (eventArg.GestureType)
{
case GestureType.Scroll: _scrolledEventHandler?.Invoke(this, eventArg); break;
case GestureType.Zoom: _twoFingerZoomedEventHandler?.Invoke(this, eventArg); break;
case GestureType.Click: _clickedEventHandler?.Invoke(this, eventArg); break;
case GestureType.DoubleClick: _doubleClickedEventHandler?.Invoke(this, eventArg); break;
case GestureType.TwoFingerClick: _twoFingerClickedEventHandler?.Invoke(this, eventArg); break;
case GestureType.Rotation: _twoFingerRotatedEventHandler?.Invoke(this, eventArg); break;
case GestureType.LongPress: _longPressedEventHandler?.Invoke(this, eventArg); break;
}
};
handle.SetEventCb(Interop.ViewEventType.Gesture, _gestureEventCallback, IntPtr.Zero);
Log.Info(string.Format("Gesture event callback is set"));
}
}
private void UnsetGestureEventCallback()
{
if (_scrolledEventHandler != null || _twoFingerZoomedEventHandler != null
|| _clickedEventHandler != null || _doubleClickedEventHandler != null
|| _twoFingerClickedEventHandler != null || _twoFingerRotatedEventHandler != null
|| _longPressedEventHandler != null)
{
return;
}
handle.UnsetEventCb(Interop.ViewEventType.Gesture);
_gestureEventCallback = null;
Log.Info(string.Format("Gesture event callback is unset"));
}
private void SetObjectEventCallback()
{
if (_objectEventCallback == null)
{
_objectEventCallback = (type, eventData, userData) =>
{
if (type != Interop.ViewEventType.Object) return;
var eventArg = new Interop.ObjectEventDataHandle(eventData);
switch (eventArg.GestureType)
{
case Interop.ViewGesture.Click:
{
var mapObject = _handleToObjectTable[eventArg.ViewObject];
mapObject?.HandleClickedEvent();
break;
}
}
};
handle.SetEventCb(Interop.ViewEventType.Object, _objectEventCallback, IntPtr.Zero);
Log.Info(string.Format("Object event callback is set"));
}
}
private void SetViewReadyEventCallback()
{
if (_viewReadyEventCallback == null)
{
_viewReadyEventCallback = (type, eventData, userData) =>
{
_viewReadyEventHandler?.Invoke(this, EventArgs.Empty);
};
handle.SetEventCb(Interop.ViewEventType.Ready, _viewReadyEventCallback, IntPtr.Zero);
Log.Info(string.Format("ViewReady event callback is set"));
}
}
private void UnsetViewReadyEventCallback()
{
if (_viewReadyEventHandler == null)
{
handle.UnsetEventCb(Interop.ViewEventType.Ready);
_viewReadyEventCallback = null;
Log.Info(string.Format("ViewReady event callback is unset"));
}
}
#region IDisposable Support
private bool _disposedValue = false;
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
/// If true, managed and unmanaged resources can be disposed, otherwise only unmanaged resources can be disposed.
/// 3
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_handleToObjectTable?.Clear();
}
handle?.Dispose();
_service.handle.HasOwnership = true;
_disposedValue = true;
}
}
///
/// Releases all the resources used by this object.
///
/// 3
[Obsolete("Deprecated since API10. Might be removed in API12.")]
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}