Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.System / Device / Haptic.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 using System.Collections.Generic;
19 namespace Tizen.System
20 {
21     /// <summary>
22     /// The Vibrator class provides the properties and methods to control a vibrator.
23     /// </summary>
24     /// <remarks>
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.
28     /// </remarks>
29     /// <privilege>
30     /// http://tizen.org/privilege/haptic
31     /// </privilege>
32     /// <code>
33     ///     Console.WriteLine("Total number of Vibrators are: {0}", Tizen.System.Vibrator.NumberOfVibrators);
34     /// </code>
35     public class Vibrator : IDisposable
36     {
37         private readonly int _vibratorId;
38         private IntPtr _hapticHandle = IntPtr.Zero;
39         private bool _disposedValue = false;
40         private static Lazy<IReadOnlyList<Vibrator>> _lazyVibrators;
41         private static int _maxcount = 0;
42         private Vibrator(int id)
43         {
44             _vibratorId = id;
45             DeviceError res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
46             if (res != DeviceError.None)
47             {
48                 throw DeviceExceptionFactory.CreateException(res, "unable to create Vibrator for given Id");
49             }
50             res = (DeviceError)Interop.Device.DeviceHapticGetCount(out _maxcount);
51             if (res != DeviceError.None)
52             {
53                 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
54             }
55
56         }
57
58         ~Vibrator()
59         {
60             Dispose(false);
61         }
62         /// <summary>
63         /// Gets the number of the available vibrators.
64         /// </summary>
65         /// <since_tizen> 3 </since_tizen>
66         public static int NumberOfVibrators
67         {
68             get
69             {
70                 int count = 0;
71                 DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
72                 if (res != DeviceError.None)
73                 {
74                     Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
75                 }
76                 return count;
77             }
78         }
79         /// <summary>
80         /// Gets all the available vibrators.
81         /// </summary>
82         /// <since_tizen> 3 </since_tizen>
83         public static IReadOnlyList<Vibrator> Vibrators
84         {
85             get
86             {
87                 _lazyVibrators = new Lazy<IReadOnlyList<Vibrator>>(() => GetAllVibrators());
88                 return _lazyVibrators.Value;
89             }
90         }
91         private static IReadOnlyList<Vibrator> GetAllVibrators()
92         {
93             int count = 0;
94             List<Vibrator> vibrators = new List<Vibrator>();
95             DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
96             if (res != DeviceError.None)
97             {
98                 throw DeviceExceptionFactory.CreateException(res, "unable to get Vibrators count.");
99             }
100             for (int i = 0; i < NumberOfVibrators; i++)
101             {
102                 vibrators.Add(new Vibrator(i));
103             }
104             return vibrators;
105
106         }
107         /// <summary>
108         /// Vibrates during the specified time with a constant intensity.
109         /// This function can be used to start monotonous vibration for the specified time.
110         /// </summary>
111         /// <since_tizen> 3 </since_tizen>
112         /// <param name="duration">The play duration in milliseconds.</param>
113         /// <param name="feedback">The amount of the intensity variation (0 ~ 100).</param>
114         /// <exception cref="ArgumentException"> When an invalid parameter value is set.</exception>
115         /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
116         /// <exception cref="InvalidOperationException">In case of any system error.</exception>
117         /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
118         /// <code>
119         ///     Vibrator vibrator = Vibrator.Vibrators[0];
120         ///     try
121         ///     {
122         ///         vibrator.Vibrate(2000, 70);
123         ///     }
124         ///     Catch(Exception e)
125         ///     {
126         ///     }
127         /// </code>
128
129         public void Vibrate(int duration, int feedback)
130         {
131             IntPtr effect;
132             DeviceError res = DeviceError.None;
133             if (_hapticHandle == IntPtr.Zero)
134             {
135                 res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
136                 if (res != DeviceError.None)
137                 {
138                     throw DeviceExceptionFactory.CreateException(res, "unable to get vibrator.");
139                 }
140             }
141
142             res = (DeviceError)Interop.Device.DeviceHapticVibrate(_hapticHandle, duration, feedback, out effect);
143             if (res != DeviceError.None)
144             {
145                 throw DeviceExceptionFactory.CreateException(res, "unable to vibrate the current vibrator.");
146             }
147         }
148         /// <summary>
149         /// Stops all the vibration effects which are being played.
150         /// This function can be used to stop all the effects started by Vibrate().
151         /// </summary>
152         /// <since_tizen> 3 </since_tizen>
153         /// <exception cref="ArgumentException"> In case an invalid vibrator instance is used.</exception>
154         /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
155         /// <exception cref="InvalidOperationException">In case of any system error.</exception>
156         /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
157         /// <code>
158         ///     Vibrator vibrator = Vibrator.Vibrators[0];
159         ///     try
160         ///     {
161         ///         vibrator.Stop();
162         ///     }
163         ///     Catch(Exception e)
164         ///     {
165         ///     }
166         /// </code>
167         public void Stop()
168         {
169             if (_hapticHandle != IntPtr.Zero)
170             {
171                 DeviceError res = (DeviceError)Interop.Device.DeviceHapticStop(_hapticHandle, IntPtr.Zero);
172                 if (res != DeviceError.None)
173                 {
174                     throw DeviceExceptionFactory.CreateException(res, "unable to stop the current vibrator.");
175                 }
176             }
177         }
178         /// <summary>
179         /// Dispose API for closing the internal resources.
180         /// This function can be used to stop all the effects started by Vibrate().
181         /// </summary>
182         /// <since_tizen> 3 </since_tizen>
183         public void Dispose()
184         {
185             Dispose(true);
186             GC.SuppressFinalize(this);
187         }
188
189         protected virtual void Dispose(bool disposing)
190         {
191             if (!_disposedValue)
192             {
193                 if (_hapticHandle != IntPtr.Zero)
194                 {
195                     Interop.Device.DeviceHapticClose(_hapticHandle);
196                     _hapticHandle = IntPtr.Zero;
197                 }
198                 _disposedValue = true;
199             }
200         }
201     }
202 }