From 0bae94ee66e108e321a52ac1bea85390d778be49 Mon Sep 17 00:00:00 2001 From: jomui Date: Mon, 16 Jan 2017 16:58:31 +0900 Subject: [PATCH] 1.fix Dispose 2.move proximity to GeofenceManager 3.seperate FenceStatus class Signed-off-by: jomui Change-Id: Icb6aad7d90b5e10327c02a702e16add8c4d2dc70 --- packaging/csapi-geofence.spec | 2 +- .../Interop/Interop.Libraries.cs | 4 +- .../Interop/Interop.Location.cs | 120 ++++++++++---------- .../Tizen.Location.Geofence.Net45.csproj | 1 + .../Tizen.Location.Geofence.csproj | 1 + .../Tizen.Location.Geofence/Fence.cs | 55 ++------- .../Tizen.Location.Geofence/FenceStatus.cs | 124 +++++++++++++++++++++ .../Tizen.Location.Geofence/GeofenceEnum.cs | 8 +- .../Tizen.Location.Geofence/GeofenceEventArgs.cs | 4 +- .../Tizen.Location.Geofence/GeofenceManager.cs | 110 ++++++++++++++++-- .../Tizen.Location.Geofence/VirtualPerimeter.cs | 90 --------------- 11 files changed, 311 insertions(+), 208 deletions(-) create mode 100644 src/Tizen.Location.Geofence/Tizen.Location.Geofence/FenceStatus.cs diff --git a/packaging/csapi-geofence.spec b/packaging/csapi-geofence.spec index dfa1790..602b5bb 100755 --- a/packaging/csapi-geofence.spec +++ b/packaging/csapi-geofence.spec @@ -8,7 +8,7 @@ Name: csapi-geofence Summary: Tizen location geofence API for C# -Version: 1.0.0 +Version: 1.0.1 Release: 1 Group: Development/Libraries License: Apache-2.0 diff --git a/src/Tizen.Location.Geofence/Interop/Interop.Libraries.cs b/src/Tizen.Location.Geofence/Interop/Interop.Libraries.cs index 9993ad4..40b8453 100755 --- a/src/Tizen.Location.Geofence/Interop/Interop.Libraries.cs +++ b/src/Tizen.Location.Geofence/Interop/Interop.Libraries.cs @@ -18,7 +18,7 @@ internal static partial class Interop { internal static partial class Libraries { - public const string Geofence = "libcapi-geofence-manager.so.0"; - public const string Libc = "libc.so.6"; + internal const string Geofence = "libcapi-geofence-manager.so.0"; + internal const string Libc = "libc.so.6"; } } diff --git a/src/Tizen.Location.Geofence/Interop/Interop.Location.cs b/src/Tizen.Location.Geofence/Interop/Interop.Location.cs index 026da11..cb94777 100755 --- a/src/Tizen.Location.Geofence/Interop/Interop.Location.cs +++ b/src/Tizen.Location.Geofence/Interop/Interop.Location.cs @@ -23,129 +23,135 @@ internal static partial class Interop internal static partial class Geofence { [DllImport(Libraries.Geofence, EntryPoint = "geofence_create_geopoint")] - public static extern int CreateGPSFence(int placeId, double latitude, double longitude, int radius, string address, out IntPtr handle); + internal static extern int CreateGPSFence(int placeId, double latitude, double longitude, int radius, string address, out IntPtr handle); [DllImport(Libraries.Geofence, EntryPoint = "geofence_create_bluetooth")] - public static extern int CreateBTFence(int placeId, string bssid, string ssid, out IntPtr handle); + internal static extern int CreateBTFence(int placeId, string bssid, string ssid, out IntPtr handle); [DllImport(Libraries.Geofence, EntryPoint = "geofence_create_wifi")] - public static extern int CreateWiFiFence(int placeId, string bssid, string ssid, out IntPtr handle); + internal static extern int CreateWiFiFence(int placeId, string bssid, string ssid, out IntPtr handle); [DllImport(Libraries.Geofence, EntryPoint = "geofence_destroy")] - public static extern int Destroy(IntPtr handle); - - [DllImport(Libraries.Geofence, EntryPoint = "geofence_status_create")] - public static extern int CreateFenceStatus(int placeId, out IntPtr statusHandle); - - [DllImport(Libraries.Geofence, EntryPoint = "geofence_status_destroy")] - public static extern int ReleaseFenceStatus(IntPtr statusHandle); + internal static extern int Destroy(IntPtr handle); [DllImport(Libraries.Geofence, EntryPoint = "geofence_get_type")] - public static extern int FenceType(IntPtr handle, out FenceType type); + internal static extern int FenceType(IntPtr handle, out FenceType type); [DllImport(Libraries.Geofence, EntryPoint = "geofence_get_place_id")] - public static extern int FencePlaceID(IntPtr handle, out int placeId); + internal static extern int FencePlaceID(IntPtr handle, out int placeId); [DllImport(Libraries.Geofence, EntryPoint = "geofence_get_latitude")] - public static extern int FenceLatitude(IntPtr handle, out double latitude); + internal static extern int FenceLatitude(IntPtr handle, out double latitude); [DllImport(Libraries.Geofence, EntryPoint = "geofence_get_longitude")] - public static extern int FenceLongitude(IntPtr handle, out double longitude); + internal static extern int FenceLongitude(IntPtr handle, out double longitude); [DllImport(Libraries.Geofence, EntryPoint = "geofence_get_radius")] - public static extern int FenceRadius(IntPtr handle, out int radius); + internal static extern int FenceRadius(IntPtr handle, out int radius); [DllImport(Libraries.Geofence, EntryPoint = "geofence_get_address")] - public static extern int FenceAddress(IntPtr handle, out string address); + internal static extern int FenceAddress(IntPtr handle, out string address); [DllImport(Libraries.Geofence, EntryPoint = "geofence_get_bssid")] - public static extern int FenceBSSID(IntPtr handle, out string bssid); + internal static extern int FenceBSSID(IntPtr handle, out string bssid); [DllImport(Libraries.Geofence, EntryPoint = "geofence_get_ssid")] - public static extern int FenceSSID(IntPtr handle, out string ssid); + internal static extern int FenceSSID(IntPtr handle, out string ssid); + } + + internal static partial class GeofenceStatus + { + [DllImport(Libraries.Geofence, EntryPoint = "geofence_status_create")] + internal static extern int Create(int fenceId, out IntPtr statusHandle); + + [DllImport(Libraries.Geofence, EntryPoint = "geofence_status_destroy")] + internal static extern int Destroy(IntPtr statusHandle); [DllImport(Libraries.Geofence, EntryPoint = "geofence_status_get_state")] - public static extern int FenceState(IntPtr statusHandle, out GeofenceState state); + internal static extern int State(IntPtr statusHandle, out GeofenceState state); [DllImport(Libraries.Geofence, EntryPoint = "geofence_status_get_duration")] - public static extern int FenceDuration(IntPtr statusHandle, out int seconds); - + internal static extern int Duration(IntPtr statusHandle, out int seconds); } internal static partial class GeofenceManager { - public delegate bool StateChangedCallback(int fenceId,GeofenceState state,IntPtr userData); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool StateChangedCallback(int fenceId, GeofenceState state, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool ProximityStateChangedCallback(int fenceId, ProximityState state, ProximityProvider provider, IntPtr userData); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool GeofenceEventCallback(int placeId, int fenceId, GeofenceError error, GeoFenceEventType eventType, IntPtr userData); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_is_supported")] - public static extern int IsSupported(out bool supported); + internal static extern int IsSupported(out bool supported); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_create")] - public static extern int Create(out IntPtr handle); + internal static extern int Create(out IntPtr handle); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_destroy")] - public static extern int Destroy(IntPtr handle); + internal static extern int Destroy(IntPtr handle); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_start")] - public static extern int Start(IntPtr handle, int fenceId); + internal static extern int Start(IntPtr handle, int fenceId); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_stop")] - public static extern int Stop(IntPtr handle, int fenceId); + internal static extern int Stop(IntPtr handle, int fenceId); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_set_geofence_state_changed_cb")] - public static extern int SetStateChangedCB(IntPtr handle, StateChangedCallback callback, IntPtr userData); + internal static extern int SetStateChangedCB(IntPtr handle, StateChangedCallback callback, IntPtr userData); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_unset_geofence_state_changed_cb")] - public static extern int UnsetStateChangedCB(IntPtr handle); + internal static extern int UnsetStateChangedCB(IntPtr handle); + + [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_set_geofence_proximity_state_changed_cb")] + internal static extern int SetProximityStateCB(IntPtr handle, ProximityStateChangedCallback callback, IntPtr userData); + + [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_unset_geofence_proximity_state_changed_cb")] + internal static extern int UnsetProximityStateCB(IntPtr handle); + + [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_set_geofence_event_cb")] + internal static extern int SetGeofenceEventCB(IntPtr handle, GeofenceEventCallback callback, IntPtr userData); + + [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_unset_geofence_event_cb")] + internal static extern int UnsetGeofenceEventCB(IntPtr handle); } internal static partial class VirtualPerimeter { - public delegate bool ForEachPlaceListCallback(int placeId, string placeName, int placeIndex, int placeCount, IntPtr userData); - - public delegate bool ForEachFenceListCallback(int fenceId, IntPtr fenceHandle, int placeIndex, int placeCount, IntPtr userData); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool ForEachPlaceListCallback(int placeId, string placeName, int placeIndex, int placeCount, IntPtr userData); - public delegate bool ProximityStateChangedCallback(int fenceId, ProximityState state, ProximityProvider provider, IntPtr userData); - - public delegate bool GeofenceEventCallback(int placeId,int fenceId, GeofenceError error, GeoFenceEventType eventType, IntPtr userData); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate bool ForEachFenceListCallback(int fenceId, IntPtr fenceHandle, int placeIndex, int placeCount, IntPtr userData); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_add_place")] - public static extern int AddPlace(IntPtr handle, string placeName, out int placeId); + internal static extern int AddPlace(IntPtr handle, string placeName, out int placeId); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_update_place")] - public static extern int UpdatePlace(IntPtr handle, int placeId, string placeName); + internal static extern int UpdatePlace(IntPtr handle, int placeId, string placeName); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_remove_place")] - public static extern int RemovePlace(IntPtr handle, int placeId); + internal static extern int RemovePlace(IntPtr handle, int placeId); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_add_fence")] - public static extern int AddFence(IntPtr handle, IntPtr fenceHandle, out int fenceId); + internal static extern int AddFence(IntPtr handle, IntPtr fenceHandle, out int fenceId); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_remove_fence")] - public static extern int RemoveFence(IntPtr handle, int fenceId); - - [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_set_geofence_event_cb")] - public static extern int SetGeofenceEventCB(IntPtr handle, GeofenceEventCallback callback, IntPtr userData); - - [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_unset_geofence_event_cb")] - public static extern int UnsetGeofenceEventCB(IntPtr handle); - - [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_set_geofence_proximity_state_changed_cb")] - public static extern int SetProximityStateCB(IntPtr handle, ProximityStateChangedCallback callback, IntPtr userData); - - [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_unset_geofence_proximity_state_changed_cb")] - public static extern int UnsetProximityStateCB(IntPtr handle); + internal static extern int RemoveFence(IntPtr handle, int fenceId); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_foreach_geofence_list")] - public static extern int GetForEachFenceList(IntPtr handle, ForEachFenceListCallback callback, IntPtr userData); + internal static extern int GetForEachFenceList(IntPtr handle, ForEachFenceListCallback callback, IntPtr userData); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_foreach_place_geofence_list")] - public static extern int GetForEachPlaceFenceList(IntPtr handle,int placeId, ForEachFenceListCallback callback, IntPtr userData); + internal static extern int GetForEachPlaceFenceList(IntPtr handle, int placeId, ForEachFenceListCallback callback, IntPtr userData); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_foreach_place_list")] - public static extern int GetForEachPlaceList(IntPtr handle, ForEachPlaceListCallback callback, IntPtr userData); + internal static extern int GetForEachPlaceList(IntPtr handle, ForEachPlaceListCallback callback, IntPtr userData); [DllImport(Libraries.Geofence, EntryPoint = "geofence_manager_get_place_name")] - public static extern int GetPlaceName(IntPtr handle, int placeId, out string placeName); - + internal static extern int GetPlaceName(IntPtr handle, int placeId, out string placeName); } } diff --git a/src/Tizen.Location.Geofence/Tizen.Location.Geofence.Net45.csproj b/src/Tizen.Location.Geofence/Tizen.Location.Geofence.Net45.csproj index 32ac5af..65b59c1 100644 --- a/src/Tizen.Location.Geofence/Tizen.Location.Geofence.Net45.csproj +++ b/src/Tizen.Location.Geofence/Tizen.Location.Geofence.Net45.csproj @@ -51,6 +51,7 @@ + diff --git a/src/Tizen.Location.Geofence/Tizen.Location.Geofence.csproj b/src/Tizen.Location.Geofence/Tizen.Location.Geofence.csproj index 8cb67cb..c402d8a 100644 --- a/src/Tizen.Location.Geofence/Tizen.Location.Geofence.csproj +++ b/src/Tizen.Location.Geofence/Tizen.Location.Geofence.csproj @@ -56,6 +56,7 @@ + diff --git a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/Fence.cs b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/Fence.cs index d2e1ed6..bbbe0c6 100755 --- a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/Fence.cs +++ b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/Fence.cs @@ -32,13 +32,14 @@ namespace Tizen.Location.Geofence /// public class Fence : IDisposable { + private bool _disposed = false; + internal IntPtr Handle { get; set; } - internal Fence(IntPtr handle) { Handle = handle; @@ -140,42 +141,6 @@ namespace Tizen.Location.Geofence } /// - /// Gets the state of geofence. - /// - public GeofenceState FenceState - { - get - { - GeofenceState state; - GeofenceError ret = (GeofenceError)Interop.Geofence.FenceState(Handle, out state); - if (ret != GeofenceError.None) - { - Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get GeofenceState"); - } - - return state; - } - } - - /// - /// Gets the amount of seconds geofence is in the current state. - /// - public int Duration - { - get - { - int result = -1; - GeofenceError ret = (GeofenceError)Interop.Geofence.FenceDuration(Handle, out result); - if (ret != GeofenceError.None) - { - Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Duration"); - } - - return result; - } - } - - /// /// Gets the address of geofence. /// public string Address @@ -245,7 +210,7 @@ namespace Tizen.Location.Geofence public static Fence CreateGPSFence(int placeId, int latitude, int longitude, int radius, string address) { IntPtr handle = IntPtr.Zero; - GeofenceError ret = (GeofenceError)Interop.Geofence.CreateGPSFence(placeId,latitude, longitude, radius,address,out handle); + GeofenceError ret = (GeofenceError)Interop.Geofence.CreateGPSFence(placeId, latitude, longitude, radius,address, out handle); if (ret != GeofenceError.None) { throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from GPS Data for " + placeId); @@ -311,14 +276,16 @@ namespace Tizen.Location.Geofence private void Dispose(bool disposing) { - if (!disposing) + if (_disposed) + return; + + if (Handle != IntPtr.Zero) { - if (Handle != IntPtr.Zero) - { - Interop.Geofence.Destroy(Handle); - Handle = IntPtr.Zero; - } + Interop.Geofence.Destroy(Handle); + Handle = IntPtr.Zero; } + + _disposed = true; } } } diff --git a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/FenceStatus.cs b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/FenceStatus.cs new file mode 100644 index 0000000..9c84c25 --- /dev/null +++ b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/FenceStatus.cs @@ -0,0 +1,124 @@ +/* + * 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; + +namespace Tizen.Location.Geofence +{ + /// + /// The geofence status describes the current state and duration of a geofence. + /// + /// State: State is specified by current state of fence + /// Duration: Geofence is specified by duration of current state + /// + /// + public class FenceStatus : IDisposable + { + private bool _disposed = false; + + internal IntPtr Handle + { + get; + set; + } + + /// + /// Creates a new geofence status. + /// + /// Incase of Invalid parameter + /// Incase of Pvivileges are not defined + /// Incase of Geofence is not supported + public FenceStatus(int fenceId) + { + IntPtr handle; + GeofenceError ret = (GeofenceError)Interop.GeofenceStatus.Create(fenceId, out handle); + if (ret != GeofenceError.None) + { + throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence Status instance"); + } + + Handle = handle; + } + + ~FenceStatus() + { + Dispose(false); + } + + /// + /// Gets the state of geofence. + /// + /// Incase of Invalid parameter + /// Incase of Geofence is not supported + public GeofenceState State + { + get + { + GeofenceState state; + GeofenceError ret = (GeofenceError)Interop.GeofenceStatus.State(Handle, out state); + if (ret != GeofenceError.None) + { + Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get FenceState"); + } + + return state; + } + } + + /// + /// Gets the amount of seconds geofence is in the current state. + /// + /// Incase of Invalid parameter + /// Incase of Geofence is not supported + public int Duration + { + get + { + int result = -1; + GeofenceError ret = (GeofenceError)Interop.GeofenceStatus.Duration(Handle, out result); + if (ret != GeofenceError.None) + { + Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get FenceDuration"); + } + + return result; + } + } + + /// + /// Overloaded Dispose API for destroying the fence Handle. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (_disposed) + return; + + if (Handle != IntPtr.Zero) + { + Interop.GeofenceStatus.Destroy(Handle); + Handle = IntPtr.Zero; + } + + _disposed = true; + } + } +} diff --git a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceEnum.cs b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceEnum.cs index 7e7c250..3b3e0ed 100755 --- a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceEnum.cs +++ b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceEnum.cs @@ -38,7 +38,7 @@ namespace Tizen.Location.Geofence }; /// - /// Enumerations for the state of geofence manager. + /// Enumerations for the state of geofence. /// public enum GeofenceState { @@ -117,12 +117,12 @@ namespace Tizen.Location.Geofence /// /// Proximity is specified by geospatial coordinate /// - Location, + Location = 0, /// /// Proximity is specified by Wi-Fi access point /// - WiFi, + Wifi, /// /// Proximity is specified by Bluetooth device @@ -148,7 +148,7 @@ namespace Tizen.Location.Geofence /// /// Uncertain state of proximity /// - Uncertain, + Uncertain = 0, /// /// Far state of proximity diff --git a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceEventArgs.cs b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceEventArgs.cs index bffb98b..4072c73 100755 --- a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceEventArgs.cs +++ b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceEventArgs.cs @@ -21,7 +21,7 @@ namespace Tizen.Location.Geofence /// /// Event arguments passed when Event is triggered to notify proximity state change. /// - public class ProximityStateEventArgs: EventArgs + public class ProximityStateEventArgs : EventArgs { /// /// Internal constructor @@ -64,7 +64,7 @@ namespace Tizen.Location.Geofence /// /// Event arguments passed when Event is triggered to notify Geofence state change. /// - public class GeofenceStateEventArgs :EventArgs + public class GeofenceStateEventArgs : EventArgs { /// /// Internal constructor diff --git a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceManager.cs b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceManager.cs index 1c5ff19..b2bb3ff 100755 --- a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceManager.cs +++ b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/GeofenceManager.cs @@ -23,11 +23,11 @@ namespace Tizen.Location.Geofence /// A geofence is a virtual perimeter for a real-world geographic area. /// This API provides functions to set geofence with geopoint, MAC address of Wi-Fi and Bluetooth address. /// And, notifications on events like changing in service status are provided. - /// + /// There are two kinds of places and fences: /// Public places and fences that are created by MyPlace app can be used by all apps. /// Private places and fences that are created by specified app can be used by the same app. /// - /// Notifications can be received about the following events: + /// Notifications can be received about the following events: /// Zone in when a device enters a specific area /// Zone out when a device exits a specific area /// Results and errors for each event requested to geofence module @@ -35,6 +35,8 @@ namespace Tizen.Location.Geofence /// public class GeofenceManager : IDisposable { + private bool _disposed = false; + internal IntPtr Handle { get; @@ -170,6 +172,96 @@ namespace Tizen.Location.Geofence } } + private static readonly Interop.GeofenceManager.ProximityStateChangedCallback s_proximityChangedCallback = (int fenceId, ProximityState state, ProximityProvider provider, IntPtr data) => + { + ProximityStateEventArgs evenArgs = new ProximityStateEventArgs(fenceId, state, provider); + s_proximityChanged?.Invoke(null, evenArgs); + return true; + }; + + private static event EventHandler s_proximityChanged; + + /// + /// Called when a proximity state of device is changed. + /// + /// + /// Call to Start() will invoke this event. + /// + /// Incase of feature Not supported + public event EventHandler ProximityChanged + { + add + { + if (s_proximityChanged == null) + { + GeofenceError ret = (GeofenceError)Interop.GeofenceManager.SetProximityStateCB(Handle, s_proximityChangedCallback, IntPtr.Zero); + if (ret != GeofenceError.None) + { + throw GeofenceErrorFactory.CreateException(ret, "Failed to register proximity change callback"); + } + s_proximityChanged += value; + } + } + remove + { + s_proximityChanged -= value; + if (s_proximityChanged == null) + { + GeofenceError ret = (GeofenceError)Interop.GeofenceManager.UnsetProximityStateCB(Handle); + if (ret != GeofenceError.None) + { + throw GeofenceErrorFactory.CreateException(ret, "Failed to un register proximity change callback"); + } + } + } + } + + private static readonly Interop.GeofenceManager.GeofenceEventCallback s_geofenceEventCallback = (int placeId, int fenceId, GeofenceError error, GeoFenceEventType eventType, IntPtr data) => + { + GeofenceResponseEventArgs evenArgs = new GeofenceResponseEventArgs(placeId, fenceId, error, eventType); + s_geofenceEventChanged?.Invoke(null, evenArgs); + return true; + }; + + private static event EventHandler s_geofenceEventChanged; + + /// + /// Called when the some event occurs in geofence and place such as add, update, etc.. + /// The events of public geofence is also received if there are public geofences. + /// + /// + /// Call to Start() will invoke this event. + /// The value of place_id or geofence_id is -1 when the place id or geofence id is not assigned. + /// + /// Incase of feature Not supported + public event EventHandler GeoFenceEventChanged + { + add + { + if (s_geofenceEventChanged == null) + { + GeofenceError ret = (GeofenceError)Interop.GeofenceManager.SetGeofenceEventCB(Handle, s_geofenceEventCallback, IntPtr.Zero); + if (ret != GeofenceError.None) + { + throw GeofenceErrorFactory.CreateException(ret, "Failed to register geofence event change callback"); + } + s_geofenceEventChanged += value; + } + } + remove + { + s_geofenceEventChanged -= value; + if (s_geofenceEventChanged == null) + { + GeofenceError ret = (GeofenceError)Interop.GeofenceManager.UnsetGeofenceEventCB(Handle); + if (ret != GeofenceError.None) + { + throw GeofenceErrorFactory.CreateException(ret, "Failed to unregister geofence event change callback"); + } + } + } + } + /// /// Overloaded Dispose API for destroying the GeofenceManager Handle. /// @@ -181,14 +273,16 @@ namespace Tizen.Location.Geofence private void Dispose(bool disposing) { - if (!disposing) + if (_disposed) + return; + + if (Handle != IntPtr.Zero) { - if (Handle != IntPtr.Zero) - { - Interop.GeofenceManager.Destroy(Handle); - Handle = IntPtr.Zero; - } + Interop.GeofenceManager.Destroy(Handle); + Handle = IntPtr.Zero; } + + _disposed = true; } } } diff --git a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/VirtualPerimeter.cs b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/VirtualPerimeter.cs index 9cf128d..283a583 100755 --- a/src/Tizen.Location.Geofence/Tizen.Location.Geofence/VirtualPerimeter.cs +++ b/src/Tizen.Location.Geofence/Tizen.Location.Geofence/VirtualPerimeter.cs @@ -254,95 +254,5 @@ namespace Tizen.Location.Geofence return fences; } - - private static readonly Interop.VirtualPerimeter.ProximityStateChangedCallback s_proximityChangedCallback = (int fenceId, ProximityState state, ProximityProvider provider, IntPtr data) => - { - ProximityStateEventArgs evenArgs = new ProximityStateEventArgs(fenceId, state, provider); - s_proximityChanged?.Invoke(null, evenArgs); - return true; - }; - - private static event EventHandler s_proximityChanged; - - /// - /// Called when a proximity state of device is changed. - /// - /// - /// Call to Start() will invoke this event. - /// - /// Incase of feature Not supported - public event EventHandler ProximityChanged - { - add - { - if (s_proximityChanged == null) - { - GeofenceError ret = (GeofenceError)Interop.VirtualPerimeter.SetProximityStateCB(Handle, s_proximityChangedCallback, IntPtr.Zero); - if (ret != GeofenceError.None) - { - throw GeofenceErrorFactory.CreateException(ret, "Failed to register proximity change callback"); - } - s_proximityChanged += value; - } - } - remove - { - s_proximityChanged -= value; - if (s_proximityChanged == null) - { - GeofenceError ret = (GeofenceError)Interop.VirtualPerimeter.UnsetProximityStateCB(Handle); - if (ret != GeofenceError.None) - { - throw GeofenceErrorFactory.CreateException(ret, "Failed to un register proximity change callback"); - } - } - } - } - - private static readonly Interop.VirtualPerimeter.GeofenceEventCallback s_geofenceEventCallback = (int placeId, int fenceId, GeofenceError error, GeoFenceEventType eventType, IntPtr data) => - { - GeofenceResponseEventArgs evenArgs = new GeofenceResponseEventArgs(placeId, fenceId, error, eventType); - s_geofenceEventChanged?.Invoke(null, evenArgs); - return true; - }; - - private static event EventHandler s_geofenceEventChanged; - - /// - /// Called when the some event occurs in geofence and place such as add, update, etc.. - /// The events of public geofence is also received if there are public geofences. - /// - /// - /// Call to Start() will invoke this event. - /// The value of place_id or geofence_id is -1 when the place id or geofence id is not assigned. - /// - /// Incase of feature Not supported - public event EventHandler GeoFenceEventChanged - { - add - { - if (s_geofenceEventChanged == null) - { - GeofenceError ret = (GeofenceError)Interop.VirtualPerimeter.SetGeofenceEventCB(Handle, s_geofenceEventCallback, IntPtr.Zero); - if (ret != GeofenceError.None) - { - throw GeofenceErrorFactory.CreateException(ret, "Failed to register geofence event change callback"); - } - s_geofenceEventChanged += value; - } - } - remove - { - s_geofenceEventChanged -= value; - if (s_geofenceEventChanged == null) - { - GeofenceError ret = (GeofenceError)Interop.VirtualPerimeter.UnsetGeofenceEventCB(Handle); - if (ret != GeofenceError.None) - { - throw GeofenceErrorFactory.CreateException(ret, "Failed to un register geofence event change callback"); - } - } - } - } } } -- 2.7.4