[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Feedback / Feedback / Feedback.cs
1 /*
2 * Copyright (c) 2018 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
20
21 namespace Tizen.System
22 {
23     /// <summary>
24     /// The class for constants.
25     /// </summary>
26     internal static class Constants
27     {
28         internal const int NumberOfPattern = 39;
29     }
30
31     /// <summary>
32     /// The Feedback API provides functions to control haptic and sound.
33     /// The Feedback API provides the way to play and stop feedback, and get the information whether a specific pattern is supported.
34     /// Below is the supported pattern string:
35     /// Tap
36     /// SoftInputPanel
37     /// Key0
38     /// Key1
39     /// Key2
40     /// Key3
41     /// Key4
42     /// Key5
43     /// Key6
44     /// Key7
45     /// Key8
46     /// Key9
47     /// KeyStar
48     /// KeySharp
49     /// KeyBack
50     /// Hold
51     /// HardwareKeyPressed
52     /// HardwareKeyHold
53     /// Message
54     /// Email
55     /// WakeUp
56     /// Schedule
57     /// Timer
58     /// General
59     /// PowerOn
60     /// PowerOff
61     /// ChargerConnected
62     /// ChargingError
63     /// FullyCharged
64     /// LowBattery
65     /// Lock
66     /// UnLock
67     /// VibrationModeAbled
68     /// SilentModeDisabled
69     /// BluetoothDeviceConnected
70     /// BluetoothDeviceDisconnected
71     /// ListReorder
72     /// ListSlider
73     /// VolumeKeyPressed
74     /// </summary>
75     /// <privilege>
76     /// For controlling the haptic device:
77     /// http://tizen.org/privilege/haptic
78     /// For controlling the sound, privilege is not needed.
79     /// </privilege>
80     /// <example>
81     /// <code>
82     /// Feedback feedback = new Feedback();
83     /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
84     /// </code>
85     /// </example>
86     /// <since_tizen> 3 </since_tizen>
87     public class Feedback
88     {
89         private const string LogTag = "Tizen.System.Feedback";
90
91         private readonly FeedbackPattern[] Pattern = new FeedbackPattern[39];
92
93         /// <summary>
94         /// Constructor of Feedback class
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         /// <feature>
98         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
99         /// </feature>
100         /// <exception cref="NotSupportedException">Thrown when failed because the devices (vibration and sound) are not supported.</exception>
101         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
102         /// <privilege>http://tizen.org/privilege/haptic</privilege>
103         /// <example>
104         /// <code>
105         /// Feedback feedback = new Feedback();
106         /// </code>
107         /// </example>
108         public Feedback()
109         {
110             Pattern[0].PatternNumber = 0;
111             Pattern[0].PatternString = "Tap";
112             Pattern[1].PatternNumber = 1;
113             Pattern[1].PatternString = "SoftInputPanel";
114             Pattern[2].PatternNumber = 6;
115             Pattern[2].PatternString = "Key0";
116             Pattern[3].PatternNumber = 7;
117             Pattern[3].PatternString = "Key1";
118             Pattern[4].PatternNumber = 8;
119             Pattern[4].PatternString = "Key2";
120             Pattern[5].PatternNumber = 9;
121             Pattern[5].PatternString = "Key3";
122             Pattern[6].PatternNumber = 10;
123             Pattern[6].PatternString = "Key4";
124             Pattern[7].PatternNumber = 11;
125             Pattern[7].PatternString = "Key5";
126             Pattern[8].PatternNumber = 12;
127             Pattern[8].PatternString = "Key6";
128             Pattern[9].PatternNumber = 13;
129             Pattern[9].PatternString = "Key7";
130             Pattern[10].PatternNumber = 14;
131             Pattern[10].PatternString = "Key8";
132             Pattern[11].PatternNumber = 15;
133             Pattern[11].PatternString = "Key9";
134             Pattern[12].PatternNumber = 16;
135             Pattern[12].PatternString = "KeyStar";
136             Pattern[13].PatternNumber = 17;
137             Pattern[13].PatternString = "KeySharp";
138             Pattern[14].PatternNumber = 18;
139             Pattern[14].PatternString = "KeyBack";
140             Pattern[15].PatternNumber = 19;
141             Pattern[15].PatternString = "Hold";
142             Pattern[16].PatternNumber = 21;
143             Pattern[16].PatternString = "HardwareKeyPressed";
144             Pattern[17].PatternNumber = 22;
145             Pattern[17].PatternString = "HardwareKeyHold";
146             Pattern[18].PatternNumber = 23;
147             Pattern[18].PatternString = "Message";
148             Pattern[19].PatternNumber = 25;
149             Pattern[19].PatternString = "Email";
150             Pattern[20].PatternNumber = 27;
151             Pattern[20].PatternString = "WakeUp";
152             Pattern[21].PatternNumber = 29;
153             Pattern[21].PatternString = "Schedule";
154             Pattern[22].PatternNumber = 31;
155             Pattern[22].PatternString = "Timer";
156             Pattern[23].PatternNumber = 33;
157             Pattern[23].PatternString = "General";
158             Pattern[24].PatternNumber = 36;
159             Pattern[24].PatternString = "PowerOn";
160             Pattern[25].PatternNumber = 37;
161             Pattern[25].PatternString = "PowerOff";
162             Pattern[26].PatternNumber = 38;
163             Pattern[26].PatternString = "ChargerConnected";
164             Pattern[27].PatternNumber = 40;
165             Pattern[27].PatternString = "ChargingError";
166             Pattern[28].PatternNumber = 42;
167             Pattern[28].PatternString = "FullyCharged";
168             Pattern[29].PatternNumber = 44;
169             Pattern[29].PatternString = "LowBattery";
170             Pattern[30].PatternNumber = 46;
171             Pattern[30].PatternString = "Lock";
172             Pattern[31].PatternNumber = 47;
173             Pattern[31].PatternString = "UnLock";
174             Pattern[32].PatternNumber = 55;
175             Pattern[32].PatternString = "VibrationModeAbled";
176             Pattern[33].PatternNumber = 56;
177             Pattern[33].PatternString = "SilentModeDisabled";
178             Pattern[34].PatternNumber = 57;
179             Pattern[34].PatternString = "BluetoothDeviceConnected";
180             Pattern[35].PatternNumber = 58;
181             Pattern[35].PatternString = "BluetoothDeviceDisconnected";
182             Pattern[36].PatternNumber = 62;
183             Pattern[36].PatternString = "ListReorder";
184             Pattern[37].PatternNumber = 63;
185             Pattern[37].PatternString = "ListSlider";
186             Pattern[38].PatternNumber = 64;
187             Pattern[38].PatternString = "VolumeKeyPressed";
188
189             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Initialize();
190             if (res != Interop.Feedback.FeedbackError.None)
191             {
192                 Log.Warn(LogTag, string.Format("Failed to initialize feedback. err = {0}", res));
193                 switch (res)
194                 {
195                     case Interop.Feedback.FeedbackError.NotSupported:
196                         throw new NotSupportedException("Device is not supported");
197                     default:
198                         throw new InvalidOperationException("Failed to initialize");
199                 }
200             }
201         }
202
203         /// <summary>
204         /// Finalizes an instance of the Feedback class.
205         /// </summary>
206         ~Feedback()
207         {
208             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Deinitialize();
209             if (res != Interop.Feedback.FeedbackError.None)
210             {
211                 Log.Warn(LogTag, string.Format("Failed to deinitialize feedback. err = {0}", res));
212                 switch (res)
213                 {
214                     case Interop.Feedback.FeedbackError.NotInitialized:
215                         throw new Exception("Not initialized");
216                     default:
217                         throw new InvalidOperationException("Failed to initialize");
218                 }
219             }
220         }
221
222         /// <summary>
223         /// Gets the supported information about a specific type and pattern.
224         /// </summary>
225         /// <remarks>
226         /// Now, IsSupportedPattern is not working for FeedbackType.All.
227         /// This API is working for FeedbackType.Sound and FeedbackType.Vibration only.
228         /// If you use FeedbackType.All for type parameter, this API will throw ArgumentException.
229         /// To get the supported information for Vibration type, the application should have http://tizen.org/privilege/haptic privilege.
230         /// </remarks>
231         /// <since_tizen> 3 </since_tizen>
232         /// <param name="type">The feedback type.</param>
233         /// <param name="pattern">The feedback pattern string.</param>
234         /// <feature>
235         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
236         /// </feature>
237         /// <returns>Information whether a pattern is supported.</returns>
238         /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
239         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
240         /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
241         /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted (No privilege).</exception>
242         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
243         /// <privilege>http://tizen.org/privilege/haptic</privilege>
244         /// <example>
245         /// <code>
246             /// Feedback feedback = new Feedback();
247         /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
248         /// </code>
249         /// </example>
250         public bool IsSupportedPattern(FeedbackType type, String pattern)
251         {
252             bool supported = false;
253             bool found = false;
254             int i = 0;
255             Interop.Feedback.FeedbackError res;
256
257             for (i = 0; i < Constants.NumberOfPattern; i++)
258             {
259                 if (String.Compare(pattern, Pattern[i].PatternString) == 0)
260                 {
261                     found = true;
262                     break;
263                 }
264             }
265
266             if (!found)
267                 throw new ArgumentException("Invalid Arguments");
268
269             res = (Interop.Feedback.FeedbackError)Interop.Feedback.IsSupportedPattern((Interop.Feedback.FeedbackType)type, Pattern[i].PatternNumber, out supported);
270
271
272             if (res != Interop.Feedback.FeedbackError.None)
273             {
274                 Log.Warn(LogTag, string.Format("Failed to get supported information. err = {0}", res));
275                 switch (res)
276                 {
277                     case Interop.Feedback.FeedbackError.NotInitialized:
278                         throw new Exception("Not initialized");
279                     case Interop.Feedback.FeedbackError.InvalidParameter:
280                         throw new ArgumentException("Invalid Arguments");
281                     case Interop.Feedback.FeedbackError.NotSupported:
282                         throw new NotSupportedException("Device is not supported");
283                     case Interop.Feedback.FeedbackError.PermissionDenied:
284                         throw new UnauthorizedAccessException("Access is not granted");
285                     case Interop.Feedback.FeedbackError.OperationFailed:
286                     default:
287                         throw new InvalidOperationException("Failed to get supported information");
288                 }
289             }
290             return supported;
291         }
292
293         /// <summary>
294         /// Plays a specific feedback pattern.
295         /// </summary>
296         /// <remarks>
297         /// To play Vibration type, app should have http://tizen.org/privilege/haptic privilege.
298         /// </remarks>
299         /// <since_tizen> 3 </since_tizen>
300         /// <param name="type">The feedback type.</param>
301         /// <param name="pattern">The feedback pattern string.</param>
302         /// <feature>
303         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
304         /// </feature>
305         /// <exception cref="Exception">Thrown when failed because feedback is not initialized.</exception>
306         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
307         /// <exception cref="NotSupportedException">Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.</exception>
308         /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted(No privilege)</exception>
309         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
310         /// <privilege>http://tizen.org/privilege/haptic</privilege>
311         /// <example>
312         /// <code>
313         /// Feedback feedback = new Feedback();
314         /// feedback.Play(FeedbackType.All, "Tap");
315         /// </code>
316         /// </example>
317         public void Play(FeedbackType type, String pattern)
318         {
319             bool found = false;
320             int i = 0;
321             Interop.Feedback.FeedbackError res;
322
323             for (i = 0; i < Constants.NumberOfPattern; i++)
324             {
325                 if (String.Compare(pattern, Pattern[i].PatternString) == 0)
326                 {
327                     found = true;
328                     break;
329                 }
330             }
331
332             if (!found)
333                 throw new ArgumentException("Invalid Arguments");
334
335             if (type == FeedbackType.All)
336                 res = (Interop.Feedback.FeedbackError)Interop.Feedback.Play(Pattern[i].PatternNumber);
337             else
338                 res = (Interop.Feedback.FeedbackError)Interop.Feedback.PlayType((Interop.Feedback.FeedbackType)type, Pattern[i].PatternNumber);
339
340             if (res != Interop.Feedback.FeedbackError.None)
341             {
342                 Log.Warn(LogTag, string.Format("Failed to play feedback. err = {0}", res));
343                 switch (res)
344                 {
345                     case Interop.Feedback.FeedbackError.NotInitialized:
346                         throw new Exception("Not initialized");
347                     case Interop.Feedback.FeedbackError.InvalidParameter:
348                         throw new ArgumentException("Invalid Arguments");
349                     case Interop.Feedback.FeedbackError.NotSupported:
350                         throw new NotSupportedException("Not supported");
351                     case Interop.Feedback.FeedbackError.PermissionDenied:
352                         throw new UnauthorizedAccessException("Access is not granted");
353                     case Interop.Feedback.FeedbackError.OperationFailed:
354                     default:
355                         throw new InvalidOperationException("Failed to play pattern");
356                 }
357             }
358         }
359
360         /// <summary>
361         /// Stops to play the feedback.
362         /// </summary>
363         /// <remarks>
364         /// To stop vibration, the application should have http://tizen.org/privilege/haptic privilege.
365         /// </remarks>
366         /// <since_tizen> 3 </since_tizen>
367         /// <feature>
368         /// http://tizen.org/feature/feedback.vibration
369         /// </feature>
370         /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
371         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament</exception>
372         /// <exception cref="NotSupportedException">Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.</exception>
373         /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted (No privilege).</exception>
374         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
375         /// <privilege>http://tizen.org/privilege/haptic</privilege>
376         /// <example>
377         /// <code>
378         /// Feedback Feedback1 = new Feedback();
379         /// Feedback1.Stop();
380         /// </code>
381         /// </example>
382         public void Stop()
383         {
384             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Stop();
385
386             if (res != Interop.Feedback.FeedbackError.None)
387             {
388                 Log.Warn(LogTag, string.Format("Failed to stop feedback. err = {0}", res));
389                 switch (res)
390                 {
391                     case Interop.Feedback.FeedbackError.NotInitialized:
392                         throw new Exception("Not initialized");
393                     case Interop.Feedback.FeedbackError.InvalidParameter:
394                         throw new ArgumentException("Invalid Arguments");
395                     case Interop.Feedback.FeedbackError.NotSupported:
396                         throw new NotSupportedException("Not supported");
397                     case Interop.Feedback.FeedbackError.PermissionDenied:
398                         throw new UnauthorizedAccessException("Access is not granted");
399                     case Interop.Feedback.FeedbackError.OperationFailed:
400                     default:
401                         throw new InvalidOperationException("Failed to stop pattern");
402                 }
403             }
404         }
405     }
406 }