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