00ad3effb413fe1e50a8affc6368b3ceb2d76317
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / StationaryActivityDetector.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.Sensor
20 {
21     /// <summary>
22     /// The StationaryActivityDetector class is used for registering callbacks for the stationary activity detector and getting the stationary state.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public sealed class StationaryActivityDetector : ActivityDetector
26     {
27         private static string ActivityDetectorKey = "http://tizen.org/feature/sensor.activity_recognition";
28
29         /// <summary>
30         /// Gets the state of the stationary activity detector.
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         /// <value> The stationary state. </value>
34         public DetectorState Stationary { get; private set; } = DetectorState.Unknown;
35
36         /// <summary>
37         /// Returns true or false based on whether the stationary activity detector is supported by the device.
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
41         public static bool IsSupported
42         {
43             get
44             {
45                 Log.Info(Globals.LogTag, "Checking if the stationary activity detector is supported");
46                 return CheckIfSupported(SensorType.StationaryActivityDetector, ActivityDetectorKey);
47             }
48         }
49
50         /// <summary>
51         /// Returns the number of stationary activity detectors available on the device.
52         /// </summary>
53         /// <since_tizen> 3 </since_tizen>
54         /// <value> The count of stationary activity detectors. </value>
55         public static int Count
56         {
57             get
58             {
59                 Log.Info(Globals.LogTag, "Getting the count of stationary activity detectors");
60                 return GetCount();
61             }
62         }
63
64         /// <summary>
65         /// Initializes a new instance of the <see cref="Tizen.Sensor.StationaryActivityDetector"/> class.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         /// <feature>http://tizen.org/feature/sensor.activity_recognition</feature>
69         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
70         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
71         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
72         /// <param name='index'>
73         /// Index. Default value for this is 0. Index refers to a particular stationary activity detector in case of multiple sensors.
74         /// </param>
75         public StationaryActivityDetector(uint index = 0) : base(index)
76         {
77             SetAttribute((SensorAttribute)ActivityAttribute, (int)ActivityType.Stationary);
78             Log.Info(Globals.LogTag, "Creating stationary activity detector object");
79         }
80
81         private static int GetCount()
82         {
83             IntPtr list;
84             int count;
85             int error = Interop.SensorManager.GetSensorList(SensorType.StationaryActivityDetector, out list, out count);
86             if (error != (int)SensorError.None)
87             {
88                 Log.Error(Globals.LogTag, "Error getting sensor list for stationary activity detector");
89                 count = 0;
90             }
91             else
92                 Interop.Libc.Free(list);
93             return count;
94         }
95
96         /// <summary>
97         /// Read stationary activity detector data synchronously.
98         /// </summary>
99         internal override void ReadData()
100         {
101             Interop.SensorEventStruct sensorData;
102             int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
103             if (error != (int)SensorError.None)
104             {
105                 Log.Error(Globals.LogTag, "Error reading stationary activity detector data");
106                 throw SensorErrorFactory.CheckAndThrowException(error, "Reading stationary activity detector data failed");
107             }
108
109             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
110             Stationary = (DetectorState)sensorData.values[0];
111             ActivityAccuracy = (SensorDataAccuracy)sensorData.accuracy;
112         }
113
114         /// <summary>
115         /// An event handler for storing the callback functions for the event corresponding to the change in the stationary activity detector data.
116         /// </summary>
117         /// <since_tizen> 3 </since_tizen>
118         public event EventHandler<StationaryActivityDetectorDataUpdatedEventArgs> DataUpdated;
119
120         internal static Interop.SensorListener.SensorEventsCallback _callback;
121
122         internal override void EventListenStart()
123         {
124             _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
125                 updateBatchEvents(eventPtr, events_count);
126                 Interop.SensorEventStruct sensorData = latestEvent();
127
128                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
129                 Stationary = (DetectorState)sensorData.values[0];
130                 ActivityAccuracy = (SensorDataAccuracy) sensorData.accuracy;
131
132                 DataUpdated?.Invoke(this, new StationaryActivityDetectorDataUpdatedEventArgs(sensorData.values[0]));
133             };
134
135             int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
136             if (error != (int)SensorError.None)
137             {
138                 Log.Error(Globals.LogTag, "Error setting event callback for stationary activity detector");
139                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for stationary activity detector");
140             }
141         }
142
143         internal override void EventListenStop()
144         {
145             int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
146             if (error != (int)SensorError.None)
147             {
148                 Log.Error(Globals.LogTag, "Error unsetting event callback for stationary activity detector");
149                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for stationary activity detector");
150             }
151         }
152
153         internal override SensorType GetSensorType()
154         {
155             return SensorType.StationaryActivityDetector;
156         }
157     }
158 }