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