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