summaryrefslogtreecommitdiff
path: root/src/Tizen.System/Device/Haptic.cs
blob: b451f33a3c139ce9fb59eaff376b253591573258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
namespace Tizen.System
{
    /// <summary>
    /// The Vibrator class provides properties and methods to control a vibrator.
    /// </summary>
    /// <remarks>
    /// The Vibrator API provides the way to access the Vibrators in the device.
    /// It allows the management of the device's vibrator parameters, such as the vibration count and level.
    /// It provides methods to Vibrate and Stop the vibration.
    /// </remarks>
    /// <privilege>
    /// http://tizen.org/privilege/haptic
    /// </privilege>
    /// <code>
    ///     Console.WriteLine("Total number of Vibrators are: {0}", Tizen.System.Vibrator.NumberOfVibrators);
    /// </code>
    public class Vibrator : IDisposable
    {
        private readonly int _vibratorId;
        private IntPtr _hapticHandle = IntPtr.Zero;
        private bool _disposedValue = false;
        private static Lazy<IReadOnlyList<Vibrator>> _lazyVibrators;
        private static int _maxcount = 0;
        private Vibrator(int id)
        {
            _vibratorId = id;
            DeviceError res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
            if (res != DeviceError.None)
            {
                throw DeviceExceptionFactory.CreateException(res, "unable to create Vibrator for given Id");
            }
            res = (DeviceError)Interop.Device.DeviceHapticGetCount(out _maxcount);
            if (res != DeviceError.None)
            {
                Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
            }

        }

        ~Vibrator()
        {
            Dispose(false);
        }
        /// <summary>
        /// Get the number of avaialble vibrators.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public static int NumberOfVibrators
        {
            get
            {
                int count = 0;
                DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
                if (res != DeviceError.None)
                {
                    Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
                }
                return count;
            }
        }
        /// <summary>
        /// Get all the avaialble vibrators.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public static IReadOnlyList<Vibrator> Vibrators
        {
            get
            {
                _lazyVibrators = new Lazy<IReadOnlyList<Vibrator>>(() => GetAllVibrators());
                return _lazyVibrators.Value;
            }
        }
        private static IReadOnlyList<Vibrator> GetAllVibrators()
        {
            int count = 0;
            List<Vibrator> vibrators = new List<Vibrator>();
            DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
            if (res != DeviceError.None)
            {
                throw DeviceExceptionFactory.CreateException(res, "unable to get Vibrators count.");
            }
            for (int i = 0; i < NumberOfVibrators; i++)
            {
                vibrators.Add(new Vibrator(i));
            }
            return vibrators;

        }
        /// <summary>
        /// Vibrates during the specified time with a constant intensity.
        /// This function can be used to start monotonous vibration for the specified time.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <param name="duration">The play duration in milliseconds </param>
        /// <param name="feedback">The amount of the intensity variation (0 ~ 100) </param>
        /// <exception cref="ArgumentException"> When the invalid parameter value is set.</exception>
        /// <exception cref = "UnauthorizedAccessException"> If the privilege is not set.</exception>
        /// <exception cref = "InvalidOperationException"> In case of any system error.</exception>
        /// <exception cref = "NotSupportedException"> In case of device does not support this behavior.</exception>
        /// <code>
        ///     Vibrator vibrator = Vibrator.Vibrators[0];
        ///     try
        ///     {
        ///         vibrator.Vibrate(2000, 70);
        ///     }
        ///     Catch(Exception e)
        ///     {
        ///     }
        /// </code>

        public void Vibrate(int duration, int feedback)
        {
            IntPtr effect;
            DeviceError res = DeviceError.None;
            if (_hapticHandle == IntPtr.Zero)
            {
                res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
                if (res != DeviceError.None)
                {
                    throw DeviceExceptionFactory.CreateException(res, "unable to get vibrator.");
                }
            }

            res = (DeviceError)Interop.Device.DeviceHapticVibrate(_hapticHandle, duration, feedback, out effect);
            if (res != DeviceError.None)
            {
                throw DeviceExceptionFactory.CreateException(res, "unable to vibrate the current vibrator.");
            }
        }
        /// <summary>
        /// Stops all vibration effects which are being played.
        /// This function can be used to stop all effects started by Vibrate().
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <exception cref="ArgumentException"> In case of invalid vibrator instance is used.</exception>
        /// <exception cref = "UnauthorizedAccessException"> If the privilege is not set.</exception>
        /// <exception cref = "InvalidOperationException"> In case of any system error.</exception>
        /// <exception cref = "NotSupportedException"> In case of device does not support this behavior.</exception>
        /// <code>
        ///     Vibrator vibrator = Vibrator.Vibrators[0];
        ///     try
        ///     {
        ///         vibrator.Stop();
        ///     }
        ///     Catch(Exception e)
        ///     {
        ///     }
        /// </code>
        public void Stop()
        {
            if (_hapticHandle != IntPtr.Zero)
            {
                DeviceError res = (DeviceError)Interop.Device.DeviceHapticStop(_hapticHandle, IntPtr.Zero);
                if (res != DeviceError.None)
                {
                    throw DeviceExceptionFactory.CreateException(res, "unable to stop the current vibrator.");
                }
            }
        }
        /// <summary>
        /// Dispose API for closing the internal resources.
        /// This function can be used to stop all effects started by Vibrate().
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (_hapticHandle != IntPtr.Zero)
                {
                    Interop.Device.DeviceHapticClose(_hapticHandle);
                    _hapticHandle = IntPtr.Zero;
                }
                _disposedValue = true;
            }
        }
    }
}