391f3ae2a865ef64a3f6e062b388023a554549b0
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / WalkingActivityDetector.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     /// WalkingActivityDetector Class. Used for registering callbacks for walking activity detector and getting the walking state
23     /// </summary>
24     public sealed class WalkingActivityDetector : ActivityDetector
25     {
26         private static string ActivityDetectorKey = "http://tizen.org/feature/sensor.activity_recognition";
27
28         /// <summary>
29         /// Gets the state of walking activity detector
30         /// </summary>
31         public DetectorState Walking { get; private set; } = DetectorState.Unknown;
32
33         /// <summary>
34         /// Returns true or false based on whether walking activity detector is supported by device.
35         /// </summary>
36         public static bool IsSupported
37         {
38             get
39             {
40                 Log.Info(Globals.LogTag, "Checking if the walking activity detector is supported");
41                 return CheckIfSupported(SensorType.WalkingActivityDetector, ActivityDetectorKey);
42             }
43         }
44
45         /// <summary>
46         /// Returns the number of walking activity detector available on the device.
47         /// </summary>
48         public static int Count
49         {
50             get
51             {
52                 Log.Info(Globals.LogTag, "Getting the count of walking activity detectors");
53                 return GetCount();
54             }
55         }
56
57         /// <summary>
58         /// Initializes a new instance of the <see cref="Tizen.Sensor.walkingActivityDetector"/> class.
59         /// </summary>
60         /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
61         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
62         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
63         /// <param name='index'>
64         /// Index. Default value for this is 0. Index refers to a particular walking activity detector in case of multiple sensors.
65         /// </param>
66         public WalkingActivityDetector(uint index = 0) : base(index)
67         {
68             SetAttribute((SensorAttribute)ActivityAttribute, (int)ActivityType.Walking);
69             Log.Info(Globals.LogTag, "Creating walking activity gesture detector object");
70         }
71
72         private static int GetCount()
73         {
74             IntPtr list;
75             int count;
76             int error = Interop.SensorManager.GetSensorList(SensorType.WalkingActivityDetector, out list, out count);
77             if (error != (int)SensorError.None)
78             {
79                 Log.Error(Globals.LogTag, "Error getting sensor list for walking activity detector");
80                 count = 0;
81             }
82             else
83                 Interop.Libc.Free(list);
84             return count;
85         }
86
87         /// <summary>
88         /// Event Handler for storing the callback functions for event corresponding to change in walking activity gesture detector data.
89         /// </summary>
90         public event EventHandler<WalkingActivityDetectorDataUpdatedEventArgs> DataUpdated;
91
92         internal static Interop.SensorListener.SensorEventCallback _callback;
93
94         internal override void EventListenStart()
95         {
96             _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
97                 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
98
99                 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
100                 Walking = (DetectorState)sensorData.values[0];
101                 ActivityAccuracy = (SensorDataAccuracy) sensorData.accuracy;
102
103                 DataUpdated?.Invoke(this, new WalkingActivityDetectorDataUpdatedEventArgs(sensorData.values[0]));
104             };
105
106             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
107             if (error != (int)SensorError.None)
108             {
109                 Log.Error(Globals.LogTag, "Error setting event callback for walking activity detector");
110                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for walking activity detector");
111             }
112         }
113
114         internal override void EventListenStop()
115         {
116             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
117             if (error != (int)SensorError.None)
118             {
119                 Log.Error(Globals.LogTag, "Error unsetting event callback for walking activity detector");
120                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for walking activity detector");
121             }
122         }
123
124         internal override SensorType GetSensorType()
125         {
126             return SensorType.WalkingActivityDetector;
127         }
128     }
129 }