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 /// This Geofence Manager API provides service related to geofence(geo-fence).
23 /// A geofence is a virtual perimeter for a real-world geographic area.
24 /// This API provides functions to set geofence with geopoint, MAC address of Wi-Fi and Bluetooth address.
25 /// And, notifications on events like changing in service status are provided.
26 /// <list type="ul">There are two kinds of places and fences:
27 /// <item>Public places and fences that are created by MyPlace app can be used by all apps.</item>
28 /// <item>Private places and fences that are created by specified app can be used by the same app.</item>
30 /// <list>Notifications can be received about the following events:
31 /// <item>Zone in when a device enters a specific area</item>
32 /// <item>Zone out when a device exits a specific area</item>
33 /// <item>Results and errors for each event requested to geofence module</item>
36 public class GeofenceManager : IDisposable
38 private bool _disposed = false;
40 internal IntPtr Handle
47 /// Creates a new geofence manager.
49 /// <exception cref="OutOfMemoryException">Incase of OutOfMemory condition.</exception>
50 /// <exception cref="InvalidOperationException">Incase of any System error.</exception>
51 /// <exception cref="NotSupportedException">Incase of Geofence is not supported.</exception>
52 public GeofenceManager()
55 GeofenceError ret = (GeofenceError) Interop.GeofenceManager.Create(out handle);
56 if(ret != GeofenceError.None)
58 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence Manager instance");
70 /// Checks whether the geofence manager is available or not.
72 public static bool IsSupported
77 GeofenceError res= (GeofenceError)Interop.GeofenceManager.IsSupported(out ret);
78 if(res != GeofenceError.None)
80 Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get IsSupported feature for Geofence manager");
88 /// Starts the geofencing service.
90 /// <param name="geofenceId">The specified geofence id.</param>
91 /// <privilege>http://tizen.org/privilege/location</privilege>
93 /// When the location service is enabled, the StateChanged event is invoked and the service starts.
95 /// <exception cref="ArgumentException">Incase of Invalid parameter.</exception>
96 /// <exception cref="InvalidOperationException">Incase of any System error.</exception>
97 /// <exception cref="UnauthorizedAccessException">Incase of Privileges are not defined.</exception>
98 /// <exception cref="NotSupportedException">Incase of Geofence is not supported.</exception>
99 public void Start(int geofenceId)
101 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.Start(Handle, geofenceId);
102 if (ret != GeofenceError.None)
104 throw GeofenceErrorFactory.CreateException(ret, "Failed to start service for " + geofenceId);
109 /// Stops the geofenceing service.
111 /// <param name="geofenceId">The specified geofence id.</param>
112 /// <privilege>http://tizen.org/privilege/location</privilege>
114 /// This function initiates the process of stopping the service.
115 /// You can stop and start the geofence manager as needed.
117 /// <exception cref="ArgumentException">Incase of Invalid parameter.</exception>
118 /// <exception cref="InvalidOperationException">Incase of any System error.</exception>
119 /// <exception cref="UnauthorizedAccessException">Incase of Privileges are not defined.</exception>
120 /// <exception cref="NotSupportedException">Incase of Geofence is not supported.</exception>
121 public void Stop(int geofenceId)
123 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.Stop(Handle, geofenceId);
124 if (ret != GeofenceError.None)
126 throw GeofenceErrorFactory.CreateException(ret, "Failed to stop service for " + geofenceId);
130 private static readonly Interop.GeofenceManager.StateChangedCallback s_stateChangedCallback = (int fenceId, GeofenceState state, IntPtr data) =>
132 GeofenceStateEventArgs evenArgs = new GeofenceStateEventArgs(fenceId, state);
133 s_stateChanged?.Invoke(null, evenArgs);
137 private static event EventHandler<GeofenceStateEventArgs> s_stateChanged = null;
140 /// Invokes when a device enters or exits the given geofence, If this event is registered.
143 /// Call to Start() will invoke this event.
145 /// <exception cref="NotSupportedException">Incase of feature Not supported.</exception>
146 public event EventHandler<GeofenceStateEventArgs> StateChanged
150 if(s_stateChanged == null)
152 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.SetStateChangedCB(Handle, s_stateChangedCallback, IntPtr.Zero);
153 if (ret != GeofenceError.None)
155 throw GeofenceErrorFactory.CreateException(ret, "Failed to register state change callback");
158 s_stateChanged += value;
162 s_stateChanged -= value;
163 if (s_stateChanged == null)
165 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.UnsetStateChangedCB(Handle);
166 if (ret != GeofenceError.None)
168 throw GeofenceErrorFactory.CreateException(ret, "Failed to unregister state change callback");
174 private static readonly Interop.GeofenceManager.ProximityStateChangedCallback s_proximityChangedCallback = (int fenceId, ProximityState state, ProximityProvider provider, IntPtr data) =>
176 ProximityStateEventArgs evenArgs = new ProximityStateEventArgs(fenceId, state, provider);
177 s_proximityChanged?.Invoke(null, evenArgs);
181 private static event EventHandler<ProximityStateEventArgs> s_proximityChanged;
184 /// Called when a proximity state of device is changed.
187 /// Call to Start() will invoke this event.
189 /// <exception cref="NotSupportedException">Incase of feature Not supported.</exception>
190 public event EventHandler<ProximityStateEventArgs> ProximityChanged
194 if (s_proximityChanged == null)
196 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.SetProximityStateCB(Handle, s_proximityChangedCallback, IntPtr.Zero);
197 if (ret != GeofenceError.None)
199 throw GeofenceErrorFactory.CreateException(ret, "Failed to register proximity change callback");
201 s_proximityChanged += value;
206 s_proximityChanged -= value;
207 if (s_proximityChanged == null)
209 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.UnsetProximityStateCB(Handle);
210 if (ret != GeofenceError.None)
212 throw GeofenceErrorFactory.CreateException(ret, "Failed to un register proximity change callback");
218 private static readonly Interop.GeofenceManager.GeofenceEventCallback s_geofenceEventCallback = (int placeId, int fenceId, GeofenceError error, GeoFenceEventType eventType, IntPtr data) =>
220 GeofenceResponseEventArgs evenArgs = new GeofenceResponseEventArgs(placeId, fenceId, error, eventType);
221 s_geofenceEventChanged?.Invoke(null, evenArgs);
225 private static event EventHandler<GeofenceResponseEventArgs> s_geofenceEventChanged;
228 /// Called when the some event occurs in geofence and place such as add, update, etc..
229 /// The events of public geofence is also received if there are public geofences.
232 /// Call to Start() will invoke this event.
233 /// The value of place_id or geofence_id is -1 when the place id or geofence id is not assigned.
235 /// <exception cref="NotSupportedException">Incase of feature Not supported.</exception>
236 public event EventHandler<GeofenceResponseEventArgs> GeoFenceEventChanged
240 if (s_geofenceEventChanged == null)
242 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.SetGeofenceEventCB(Handle, s_geofenceEventCallback, IntPtr.Zero);
243 if (ret != GeofenceError.None)
245 throw GeofenceErrorFactory.CreateException(ret, "Failed to register geofence event change callback");
247 s_geofenceEventChanged += value;
252 s_geofenceEventChanged -= value;
253 if (s_geofenceEventChanged == null)
255 GeofenceError ret = (GeofenceError)Interop.GeofenceManager.UnsetGeofenceEventCB(Handle);
256 if (ret != GeofenceError.None)
258 throw GeofenceErrorFactory.CreateException(ret, "Failed to unregister geofence event change callback");
265 /// Overloaded Dispose API for destroying the GeofenceManager Handle.
267 public void Dispose()
270 GC.SuppressFinalize(this);
273 private void Dispose(bool disposing)
278 if (Handle != IntPtr.Zero)
280 Interop.GeofenceManager.Destroy(Handle);
281 Handle = IntPtr.Zero;