Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Location.Geofence / Tizen.Location.Geofence / FenceStatus.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace Tizen.Location.Geofence
20 {
21     /// <summary>
22     /// The geofence status describes the current state and duration of a geofence.
23     /// <list>
24     /// <item>State: State is specified by current state of fence</item>
25     /// <item>Duration: Geofence is specified by duration of current state</item>
26     /// </list>
27     /// </summary>
28     /// <since_tizen>3</since_tizen>
29     public class FenceStatus : IDisposable
30     {
31         private bool _disposed = false;
32
33         internal IntPtr Handle
34         {
35             get;
36             set;
37         }
38
39         /// <summary>
40         /// Creates a new geofence status.
41         /// </summary>
42         /// <since_tizen>3</since_tizen>
43         /// <exception cref="ArgumentException">Incase of Invalid parameter.</exception>
44         /// <exception cref="NotSupportedException">Incase of Geofence is not supported.</exception>
45         public FenceStatus(int fenceId)
46         {
47             IntPtr handle;
48             GeofenceError ret = (GeofenceError)Interop.GeofenceStatus.Create(fenceId, out handle);
49             if (ret != GeofenceError.None)
50             {
51                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence Status instance");
52             }
53
54             Handle = handle;
55         }
56
57         ~FenceStatus()
58         {
59             Dispose(false);
60         }
61
62         /// <summary>
63         /// Gets the state of geofence.
64         /// </summary>
65         /// <since_tizen>3</since_tizen>
66         /// <exception cref="NotSupportedException">Incase of Geofence is not supported.</exception>
67         public GeofenceState State
68         {
69             get
70             {
71                 GeofenceState state;
72                 GeofenceError ret = (GeofenceError)Interop.GeofenceStatus.State(Handle, out state);
73                 if (ret != GeofenceError.None)
74                 {
75                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get FenceState");
76                 }
77
78                 return state;
79             }
80         }
81
82         /// <summary>
83         /// Gets the amount of seconds geofence is in the current state.
84         /// </summary>
85         /// <since_tizen>3</since_tizen>
86         /// <exception cref="NotSupportedException">Incase of Geofence is not supported.</exception>
87         public int Duration
88         {
89             get
90             {
91                 int result = -1;
92                 GeofenceError ret = (GeofenceError)Interop.GeofenceStatus.Duration(Handle, out result);
93                 if (ret != GeofenceError.None)
94                 {
95                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get FenceDuration");
96                 }
97
98                 return result;
99             }
100         }
101
102         /// <summary>
103         /// Overloaded Dispose API for destroying the fence Handle.
104         /// </summary>
105         /// <since_tizen>3</since_tizen>
106         public void Dispose()
107         {
108             Dispose(true);
109             GC.SuppressFinalize(this);
110         }
111
112         private void Dispose(bool disposing)
113         {
114             if (_disposed)
115                 return;
116
117             if (Handle != IntPtr.Zero)
118             {
119                 Interop.GeofenceStatus.Destroy(Handle);
120                 Handle = IntPtr.Zero;
121             }
122
123             _disposed = true;
124         }
125     }
126 }