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.
18 using System.Collections.Generic;
19 namespace Tizen.System
22 /// The Vibrator class provides the properties and methods to control a vibrator.
25 /// The Vibrator API provides the way to access the vibrators in the device.
26 /// It allows the management of the device's vibrator parameters, such as the vibration count and level.
27 /// It provides the methods to vibrate and stop the vibration.
30 /// http://tizen.org/privilege/haptic
33 /// http://tizen.org/feature/feedback.vibration
35 /// <exception cref="ArgumentException"> When an invalid parameter value is set.</exception>
36 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
37 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
38 /// <exception creg="NotSupportedException">The required feature is not supported</exception>
41 /// Console.WriteLine("Total number of Vibrators are: {0}", Tizen.System.Vibrator.NumberOfVibrators);
44 /// <since_tizen> 3 </since_tizen>
45 public class Vibrator : IDisposable
47 private readonly int _vibratorId;
48 private IntPtr _hapticHandle = IntPtr.Zero;
49 private bool _disposedValue = false;
50 private static Lazy<IReadOnlyList<Vibrator>> _lazyVibrators;
51 private static int _maxcount = 0;
52 private Vibrator(int id)
55 DeviceError res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
56 if (res != DeviceError.None)
58 throw DeviceExceptionFactory.CreateException(res, "unable to create Vibrator for given Id");
60 res = (DeviceError)Interop.Device.DeviceHapticGetCount(out _maxcount);
61 if (res != DeviceError.None)
63 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
69 /// Finalizes an instance of the Vibrator class.
76 /// Gets the number of the available vibrators.
78 /// <since_tizen> 3 </since_tizen>
79 public static int NumberOfVibrators
84 DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
85 if (res != DeviceError.None)
87 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
93 /// Gets all the available vibrators.
95 /// <since_tizen> 3 </since_tizen>
96 public static IReadOnlyList<Vibrator> Vibrators
100 _lazyVibrators = new Lazy<IReadOnlyList<Vibrator>>(() => GetAllVibrators());
101 return _lazyVibrators.Value;
104 private static IReadOnlyList<Vibrator> GetAllVibrators()
107 List<Vibrator> vibrators = new List<Vibrator>();
108 DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
109 if (res != DeviceError.None)
111 throw DeviceExceptionFactory.CreateException(res, "unable to get Vibrators count.");
113 for (int i = 0; i < NumberOfVibrators; i++)
115 vibrators.Add(new Vibrator(i));
121 /// Vibrates during the specified time with a constant intensity.
122 /// This function can be used to start monotonous vibration for the specified time.
124 /// <since_tizen> 3 </since_tizen>
125 /// <param name="duration">The play duration in milliseconds.</param>
126 /// <param name="feedback">The amount of the intensity variation (0 ~ 100).</param>
128 /// http://tizen.org/feature/feedback.vibration
130 /// <exception cref="ArgumentException"> When an invalid parameter value is set.</exception>
131 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
132 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
133 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
136 /// Vibrator vibrator = Vibrator.Vibrators[0];
139 /// vibrator.Vibrate(2000, 70);
141 /// Catch(Exception e)
147 public void Vibrate(int duration, int feedback)
150 DeviceError res = DeviceError.None;
151 if (_hapticHandle == IntPtr.Zero)
153 res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
154 if (res != DeviceError.None)
156 throw DeviceExceptionFactory.CreateException(res, "unable to get vibrator.");
160 res = (DeviceError)Interop.Device.DeviceHapticVibrate(_hapticHandle, duration, feedback, out effect);
161 if (res != DeviceError.None)
163 throw DeviceExceptionFactory.CreateException(res, "unable to vibrate the current vibrator.");
167 /// Stops all the vibration effects which are being played.
168 /// This function can be used to stop all the effects started by Vibrate().
170 /// <since_tizen> 3 </since_tizen>
172 /// http://tizen.org/feature/feedback.vibration
174 /// <exception cref="ArgumentException"> In case an invalid vibrator instance is used.</exception>
175 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
176 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
177 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
180 /// Vibrator vibrator = Vibrator.Vibrators[0];
185 /// Catch(Exception e)
192 if (_hapticHandle != IntPtr.Zero)
194 DeviceError res = (DeviceError)Interop.Device.DeviceHapticStop(_hapticHandle, IntPtr.Zero);
195 if (res != DeviceError.None)
197 throw DeviceExceptionFactory.CreateException(res, "unable to stop the current vibrator.");
202 /// Dispose API for closing the internal resources.
203 /// This function can be used to stop all the effects started by Vibrate().
205 /// <since_tizen> 3 </since_tizen>
206 public void Dispose()
209 GC.SuppressFinalize(this);
213 /// Dispose API for closing the internal resources.
214 /// This function can be used to stop all the effects started by Vibrate().
216 /// <since_tizen> 3 </since_tizen>
217 protected virtual void Dispose(bool disposing)
221 if (_hapticHandle != IntPtr.Zero)
223 Interop.Device.DeviceHapticClose(_hapticHandle);
224 _hapticHandle = IntPtr.Zero;
226 _disposedValue = true;