2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 namespace Tizen.Location.Geofence
22 /// The Geofence Manager API provides service related to geofence (geo-fence).
23 /// Geofence is a virtual perimeter for a real-world geographic area.
24 /// This API provides functions to set geofence with a geopoint, MAC address of Wi-Fi, and Bluetooth address.
25 /// Also, notifications on events like changing in service status are provided.
26 /// There are two kinds of places and fences:
28 /// <item>Public places and fences are created by the MyPlace application that can be used by all applications.</item>
29 /// <item>Private places and fences are created by the specified application that can be used by the same application.</item>
31 /// Notifications can be received about the following events:
33 /// <item>Zone in when a device enters a specific area.</item>
34 /// <item>Zone out when a device exits a specific area.</item>
35 /// <item>Results and errors for each event requested to the geofence module.</item>
38 /// <since_tizen> 3 </since_tizen>
39 public class GeofenceManager : IDisposable
41 private bool _disposed = false;
43 internal IntPtr Handle
50 /// Creates a new Geofence manager.
52 /// <since_tizen> 3 </since_tizen>
53 /// <exception cref="OutOfMemoryException">In case of out of memory condition.</exception>
54 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
55 /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
56 public GeofenceManager()
59 GeofenceError ret = (GeofenceError) Interop.GeofenceManager.Create(out handle);
60 if(ret != GeofenceError.None)
62 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence Manager instance");
69 /// The destructor of the GeofenceManager class.
71 /// <since_tizen> 3 </since_tizen>
78 /// Checks whether the Geofence manager is available or not.
80 /// <since_tizen> 3 </since_tizen>
81 public static bool IsSupported
86 GeofenceError res= (GeofenceError)Interop.GeofenceManager.IsSupported(out ret);
87 if(res != GeofenceError.None)
89 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get IsSupported feature for Geofence manager");
97 /// Starts the geofencing service.
99 /// <since_tizen> 3 </since_tizen>
100 /// <param name="geofenceId">The specified geofence ID.</param>
101 /// <privilege>http://tizen.org/privilege/location</privilege>
103 /// When the location service is enabled, the StateChanged event is invoked and the service starts.
105 /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
106 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
107 /// <exception cref="UnauthorizedAccessException">In case privileges are not defined.</exception>
108 /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
109 public void Start(int geofenceId)
111 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.Start(Handle, geofenceId);
112 if (ret != GeofenceError.None)
114 throw GeofenceErrorFactory.CreateException(ret, "Failed to start service for " + geofenceId);
119 /// Stops the geofenceing service.
121 /// <since_tizen> 3 </since_tizen>
122 /// <param name="geofenceId">The specified geofence ID.</param>
123 /// <privilege>http://tizen.org/privilege/location</privilege>
125 /// This function initiates the process of stopping the service.
126 /// You can stop and start the Geofence manager as needed.
128 /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
129 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
130 /// <exception cref="UnauthorizedAccessException">In case privileges are not defined.</exception>
131 /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
132 public void Stop(int geofenceId)
134 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.Stop(Handle, geofenceId);
135 if (ret != GeofenceError.None)
137 throw GeofenceErrorFactory.CreateException(ret, "Failed to stop service for " + geofenceId);
141 private static readonly Interop.GeofenceManager.StateChangedCallback s_stateChangedCallback = (int fenceId, GeofenceState state, IntPtr data) =>
143 GeofenceStateEventArgs evenArgs = new GeofenceStateEventArgs(fenceId, state);
144 s_stateChanged?.Invoke(null, evenArgs);
148 private static event EventHandler<GeofenceStateEventArgs> s_stateChanged = null;
151 /// Invokes when a device enters or exits the given geofence if this event is registered.
153 /// <since_tizen> 3 </since_tizen>
155 /// Call to Start() will invoke this event.
157 /// <exception cref="NotSupportedException">In case the feature is not supported.</exception>
158 public event EventHandler<GeofenceStateEventArgs> StateChanged
162 if(s_stateChanged == null)
164 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.SetStateChangedCB(Handle, s_stateChangedCallback, IntPtr.Zero);
165 if (ret != GeofenceError.None)
167 throw GeofenceErrorFactory.CreateException(ret, "Failed to register state change callback");
170 s_stateChanged += value;
174 s_stateChanged -= value;
175 if (s_stateChanged == null)
177 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.UnsetStateChangedCB(Handle);
178 if (ret != GeofenceError.None)
180 throw GeofenceErrorFactory.CreateException(ret, "Failed to unregister state change callback");
186 private static readonly Interop.GeofenceManager.ProximityStateChangedCallback s_proximityChangedCallback = (int fenceId, ProximityState state, ProximityProvider provider, IntPtr data) =>
188 ProximityStateEventArgs evenArgs = new ProximityStateEventArgs(fenceId, state, provider);
189 s_proximityChanged?.Invoke(null, evenArgs);
193 private static event EventHandler<ProximityStateEventArgs> s_proximityChanged;
196 /// Called when the proximity state of a device is changed.
198 /// <since_tizen> 3 </since_tizen>
200 /// Call to Start() will invoke this event.
202 /// <exception cref="NotSupportedException">In case the feature is not supported.</exception>
203 public event EventHandler<ProximityStateEventArgs> ProximityChanged
207 if (s_proximityChanged == null)
209 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.SetProximityStateCB(Handle, s_proximityChangedCallback, IntPtr.Zero);
210 if (ret != GeofenceError.None)
212 throw GeofenceErrorFactory.CreateException(ret, "Failed to register proximity change callback");
214 s_proximityChanged += value;
219 s_proximityChanged -= value;
220 if (s_proximityChanged == null)
222 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.UnsetProximityStateCB(Handle);
223 if (ret != GeofenceError.None)
225 throw GeofenceErrorFactory.CreateException(ret, "Failed to un register proximity change callback");
231 private static readonly Interop.GeofenceManager.GeofenceEventCallback s_geofenceEventCallback = (int placeId, int fenceId, GeofenceError error, GeofenceEventType eventType, IntPtr data) =>
233 GeofenceResponseEventArgs evenArgs = new GeofenceResponseEventArgs(placeId, fenceId, error, eventType);
234 s_geofenceEventChanged?.Invoke(null, evenArgs);
238 private static event EventHandler<GeofenceResponseEventArgs> s_geofenceEventChanged;
241 /// Called when some event occurs in the geofence and the place, such as add, update, etc..
242 /// The events of public geofence is also received if there are public geofences.
244 /// <since_tizen> 3 </since_tizen>
246 /// Call to Start() will invoke this event.
247 /// The value of place_id or geofence_id is -1 when the place ID or geofence ID is not assigned.
249 /// <exception cref="NotSupportedException">In case the feature is not supported.</exception>
250 public event EventHandler<GeofenceResponseEventArgs> GeofenceEventChanged
254 if (s_geofenceEventChanged == null)
256 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.SetGeofenceEventCB(Handle, s_geofenceEventCallback, IntPtr.Zero);
257 if (ret != GeofenceError.None)
259 throw GeofenceErrorFactory.CreateException(ret, "Failed to register geofence event change callback");
261 s_geofenceEventChanged += value;
266 s_geofenceEventChanged -= value;
267 if (s_geofenceEventChanged == null)
269 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.UnsetGeofenceEventCB(Handle);
270 if (ret != GeofenceError.None)
272 throw GeofenceErrorFactory.CreateException(ret, "Failed to unregister geofence event change callback");
279 /// The overloaded Dispose API for destroying the GeofenceManager handle.
281 /// <since_tizen> 3 </since_tizen>
282 public void Dispose()
285 GC.SuppressFinalize(this);
291 /// <since_tizen> 3 </since_tizen>
292 protected virtual void Dispose(bool disposing)
297 if (Handle != IntPtr.Zero)
299 Interop.GeofenceManager.Destroy(Handle);
300 Handle = IntPtr.Zero;