2 * Copyright (c) 2019 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.
18 using System.ComponentModel;
20 namespace Tizen.Sensor
23 /// The AutoRotationSensor class is used for registering callbacks for the auto rotation sensor and getting the auto rotation data.
25 [EditorBrowsable(EditorBrowsableState.Never)]
26 public sealed class AutoRotationSensor : Sensor
28 private static string AccelerometerKey = "http://tizen.org/feature/sensor.accelerometer";
30 private event EventHandler<SensorAccuracyChangedEventArgs> _accuracyChanged;
33 /// Gets the value of the rotation state.
35 /// <since_tizen> 3 </since_tizen>
36 /// <value> The rotation state. </value>
37 public AutoRotationState Rotaion { get; private set; } = AutoRotationState.Degree_0;
41 /// Gets the accuracy of the auto rotation data.
43 /// <since_tizen> 3 </since_tizen>
44 /// <value> Accuracy </value>
45 public SensorDataAccuracy Accuracy { get; private set; } = SensorDataAccuracy.Undefined;
48 /// Returns true or false based on whether the auto rotation sensor is supported by the device.
50 /// <since_tizen> 3 </since_tizen>
51 /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
52 public static bool IsSupported
56 Log.Info(Globals.LogTag, "Checking if the AutoRotationSensor is supported");
57 return CheckIfSupported(SensorType.AutoRotation, AccelerometerKey);
62 /// Initializes a new instance of the <see cref="Tizen.Sensor.AutoRotationSensor"/> class.
64 /// <since_tizen> 3 </since_tizen>
65 /// <feature>http://tizen.org/feature/sensor.auto_rotation</feature>
66 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
67 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
68 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
69 /// <param name='index'>
70 /// Index. Default value for this is 0. Index refers to a particular auto rotation sensor in case of multiple sensors.
72 public AutoRotationSensor(uint index = 0) : base(index)
74 Log.Info(Globals.LogTag, "Creating AutoRotationSensor object");
77 internal override SensorType GetSensorType()
79 return SensorType.AutoRotation;
83 /// An event handler for storing the callback functions for the event corresponding to the change in the auto rotation sensor data.
85 /// <since_tizen> 3 </since_tizen>
87 public event EventHandler<AutoRotationSensorDataUpdatedEventArgs> DataUpdated;
90 /// An event handler for accuracy changed events.
92 /// <since_tizen> 3 </since_tizen>
93 public event EventHandler<SensorAccuracyChangedEventArgs> AccuracyChanged
97 if (_accuracyChanged == null)
99 AccuracyListenStart();
101 _accuracyChanged += value;
105 _accuracyChanged -= value;
106 if (_accuracyChanged == null)
108 AccuracyListenStop();
113 private static int GetCount()
117 int error = Interop.SensorManager.GetSensorList(SensorType.AutoRotation, out list, out count);
118 if (error != (int)SensorError.None)
120 Log.Error(Globals.LogTag, "Error getting sensor list for auto rotation");
124 Interop.Libc.Free(list);
128 private static Interop.SensorListener.SensorEventCallback _callback;
130 internal override void EventListenStart()
132 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
133 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
135 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
136 Rotaion = (AutoRotationState)sensorData.values[0];
137 Accuracy = sensorData.accuracy;
139 DataUpdated?.Invoke(this, new AutoRotationSensorDataUpdatedEventArgs(sensorData.values, sensorData.accuracy));
142 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
143 if (error != (int)SensorError.None)
145 Log.Error(Globals.LogTag, "Error setting event callback for auto rotation sensor");
146 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for auto rotation");
150 internal override void EventListenStop()
152 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
153 if (error != (int)SensorError.None)
155 Log.Error(Globals.LogTag, "Error unsetting event callback for auto rotation sensor");
156 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for auto rotation");
160 private static Interop.SensorListener.SensorAccuracyCallback _accuracyCallback;
162 private void AccuracyListenStart()
164 _accuracyCallback = (IntPtr sensorHandle, UInt64 timestamp, SensorDataAccuracy accuracy, IntPtr data) => {
165 TimeSpan = new TimeSpan((Int64)timestamp);
167 _accuracyChanged?.Invoke(this, new SensorAccuracyChangedEventArgs(new TimeSpan((Int64)timestamp), accuracy));
170 int error = Interop.SensorListener.SetAccuracyCallback(ListenerHandle, _accuracyCallback, IntPtr.Zero);
171 if (error != (int)SensorError.None)
173 Log.Error(Globals.LogTag, "Error setting accuracy event callback for auto rotation sensor");
174 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set accuracy event callback for auto rotation");
178 private void AccuracyListenStop()
180 int error = Interop.SensorListener.UnsetAccuracyCallback(ListenerHandle);
181 if (error != (int)SensorError.None)
183 Log.Error(Globals.LogTag, "Error unsetting accuracy event callback for auto rotation sensor");
184 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset accuracy event callback for auto rotation");