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.Sensor
22 /// WalkingActivityDetector Class. Used for registering callbacks for walking activity detector and getting the walking state
24 public sealed class WalkingActivityDetector : ActivityDetector
26 private static string ActivityDetectorKey = "http://tizen.org/feature/sensor.activity_recognition";
29 /// Gets the state of walking activity detector
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> Walking state </value>
33 public DetectorState Walking { get; private set; } = DetectorState.Unknown;
36 /// Returns true or false based on whether walking activity detector is supported by device.
38 /// <since_tizen> 3 </since_tizen>
39 /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
40 public static bool IsSupported
44 Log.Info(Globals.LogTag, "Checking if the walking activity detector is supported");
45 return CheckIfSupported(SensorType.WalkingActivityDetector, ActivityDetectorKey);
50 /// Returns the number of walking activity detectors available on the device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> The count of walking activity detectors </value>
54 public static int Count
58 Log.Info(Globals.LogTag, "Getting the count of walking activity detectors");
64 /// Initializes a new instance of the <see cref="Tizen.Sensor.walkingActivityDetector"/> class.
66 /// <since_tizen> 3 </since_tizen>
67 /// <feature>http://tizen.org/feature/sensor.activity_recognition</feature>
68 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
69 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
70 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
71 /// <param name='index'>
72 /// Index. Default value for this is 0. Index refers to a particular walking activity detector in case of multiple sensors.
74 public WalkingActivityDetector(uint index = 0) : base(index)
76 SetAttribute((SensorAttribute)ActivityAttribute, (int)ActivityType.Walking);
77 Log.Info(Globals.LogTag, "Creating walking activity gesture detector object");
80 private static int GetCount()
84 int error = Interop.SensorManager.GetSensorList(SensorType.WalkingActivityDetector, out list, out count);
85 if (error != (int)SensorError.None)
87 Log.Error(Globals.LogTag, "Error getting sensor list for walking activity detector");
91 Interop.Libc.Free(list);
96 /// Event Handler for storing the callback functions for event corresponding to change in walking activity gesture detector data.
98 /// <since_tizen> 3 </since_tizen>
99 public event EventHandler<WalkingActivityDetectorDataUpdatedEventArgs> DataUpdated;
101 internal static Interop.SensorListener.SensorEventCallback _callback;
103 internal override void EventListenStart()
105 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
106 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
108 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
109 Walking = (DetectorState)sensorData.values[0];
110 ActivityAccuracy = (SensorDataAccuracy) sensorData.accuracy;
112 DataUpdated?.Invoke(this, new WalkingActivityDetectorDataUpdatedEventArgs(sensorData.values[0]));
115 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
116 if (error != (int)SensorError.None)
118 Log.Error(Globals.LogTag, "Error setting event callback for walking activity detector");
119 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for walking activity detector");
123 internal override void EventListenStop()
125 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
126 if (error != (int)SensorError.None)
128 Log.Error(Globals.LogTag, "Error unsetting event callback for walking activity detector");
129 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for walking activity detector");
133 internal override SensorType GetSensorType()
135 return SensorType.WalkingActivityDetector;