Release 4.0.0-preview1-00170
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Shortcut / Tizen.Applications.ShortcutEvent / ShortcutEventManager.cs
1 /*
2  * Copyright (c) 2017 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 namespace Tizen.Applications.Shortcut
18 {
19     using System;
20     using System.Collections.Generic;
21
22     /// <summary>
23     /// The callback function that is invoked when add request occurred
24     /// </summary>
25     /// <param name="args">Object that contain shortcut info to add.</param>
26     /// <returns>The result of handling a shortcut add request</returns>
27     public delegate ShortcutError ShortcutAdded(ShortcutAddedInfo args);
28
29     /// <summary>
30     /// The callback function that is invoked when delete request occurred
31     /// </summary>
32     /// <param name="args">Object that contain shortcut info to delete.</param>
33     /// <returns>The result of handling a shortcut delete request</returns>
34     public delegate ShortcutError ShortcutDeleted(ShortcutDeletedInfo args);
35
36     /// <summary>
37     /// This class provides a way to register callback function for shortcut add, delete events.
38     /// </summary>
39     public static class ShortcutEventManager
40     {
41         private static Interop.Shortcut.AddCallback shortcutAddCallback;
42
43         private static Interop.Shortcut.DeleteCallback shortcutDeleteCallback;
44
45         private static IList<ShortcutTemplate> shortcutTemplates = new List<ShortcutTemplate>();
46
47         private static ShortcutAdded shortcutAdded = null;
48
49         private static ShortcutDeleted shortcutDeleted = null;
50
51         /// <summary>
52         /// Registers a callback function to listen requests from applications.
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         /// <param name="addedEvent">The callback function pointer that is invoked when Add() is requested</param>
56         /// <feature>http://tizen.org/feature/shortcut </feature>
57         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
58         /// <remarks>
59         /// Previous registered delegate function should be unregister.
60         /// </remarks>
61         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
62         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
63         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
64         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
65         public static void RegisterEventHandler(ShortcutAdded addedEvent)
66         {
67             shortcutAdded = addedEvent;
68
69             if (shortcutAddCallback == null)
70             {
71                 shortcutAddCallback = new Interop.Shortcut.AddCallback(AddCallback);
72
73                 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutAddCallback(shortcutAddCallback, IntPtr.Zero);
74                 if (err != Interop.Shortcut.ErrorCode.None)
75                 {
76                     throw ShortcutErrorFactory.GetException(err, "unable to register callback");
77                 }
78             }
79         }
80
81         /// <summary>
82         /// Registers a callback function to listen requests from applications.
83         /// </summary>
84         /// <since_tizen> 3 </since_tizen>
85         /// <param name="deletedEvent">The callback function pointer that is invoked when Delete() is requested</param>
86         /// <feature>http://tizen.org/feature/shortcut </feature>
87         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
88         /// <remarks>
89         /// Previous registered delegate function should be unregister.
90         /// </remarks>
91         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
92         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
93         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
94         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
95         public static void RegisterEventHandler(ShortcutDeleted deletedEvent)
96         {
97             shortcutDeleted = deletedEvent;
98
99             if (shortcutDeleteCallback == null)
100             {
101                 shortcutDeleteCallback = new Interop.Shortcut.DeleteCallback(DeleteCallback);
102
103                 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutDeleteCallback(shortcutDeleteCallback, IntPtr.Zero);
104                 if (err != Interop.Shortcut.ErrorCode.None)
105                 {
106                     throw ShortcutErrorFactory.GetException(err, "unable to register callback");
107                 }
108             }
109         }
110
111         /// <summary>
112         /// Unregisters a callback for the shortcut request.
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         /// <param name="addedEvent">The callback function pointer that used for RegisterCallback</param>
116         /// <feature>http://tizen.org/feature/shortcut </feature>
117         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
118         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
119         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
120         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
121         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
122         public static void UnregisterEventHandler(ShortcutAdded addedEvent)
123         {
124             if (shortcutAdded.Equals(addedEvent))
125             {
126                 shortcutAdded = null;
127
128                 if (shortcutAddCallback != null)
129                 {
130                     Interop.Shortcut.UnsetShortcutAddCallback();
131                     shortcutAddCallback = null;
132                 }
133             }
134             else
135             {
136                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
137             }
138         }
139
140         /// <summary>
141         /// Unregisters a callback for the shortcut request.
142         /// </summary>
143         /// <since_tizen> 3 </since_tizen>
144         /// <param name="deletedEvent">The callback function pointer that used for RegisterCallback</param>
145         /// <feature>http://tizen.org/feature/shortcut </feature>
146         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
147         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
148         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
149         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
150         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
151         public static void UnregisterEventHandler(ShortcutDeleted deletedEvent)
152         {
153             if (shortcutDeleted.Equals(deletedEvent))
154             {
155                 shortcutDeleted = null;
156
157                 if (shortcutDeleteCallback != null)
158                 {
159                     Interop.Shortcut.UnsetShortcutDeleteCallback();
160                     shortcutDeleteCallback = null;
161                 }
162             }
163             else
164             {
165                 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
166             }
167         }
168
169         /// <summary>
170         /// Gets the preset list of shortcut template from the installed package.
171         /// </summary>
172         /// <since_tizen> 3 </since_tizen>
173         /// <param name="appId">Application ID.</param>
174         /// <returns>The List of ShortcutTemplate.</returns>
175         /// <feature>http://tizen.org/feature/shortcut </feature>
176         /// <privilege>http://tizen.org/privilege/shortcut</privilege>
177         /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
178         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
179         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
180         /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
181         public static IEnumerable<ShortcutTemplate> GetTemplateList(string appId)
182         {
183             shortcutTemplates.Clear();
184
185             Interop.Shortcut.ListCallback callback = (appName, iconPath, shortcutName, extrakey, extraData, user_data) =>
186             {
187                 ShortcutTemplate template = new ShortcutTemplate
188                 {
189                     AppId = appName,
190                     ShortcutName = shortcutName,
191                     IconPath = iconPath,
192                     ExtraKey = extrakey,
193                     ExtraData = extraData,
194                 };
195
196                 shortcutTemplates.Add(template);
197
198                 return 0;
199             };
200
201             Interop.Shortcut.GetList(appId, callback, IntPtr.Zero);
202
203             return shortcutTemplates;
204         }
205
206         private static int AddCallback(string appId, string shortcutName, int type, string contentInfo, string iconPath, int processId, double period, bool isAllowDuplicate, IntPtr data)
207         {
208             ShortcutError err;
209
210             if (type == (int)ShortcutType.LaunchByApp || type == (int)ShortcutType.LaunchByUri)
211             {
212                 HomeShortcutAddedInfo shortcutInfo = new HomeShortcutAddedInfo
213                 {
214                     ShortcutName = shortcutName,
215                     IconPath = iconPath,
216                     IsAllowDuplicate = isAllowDuplicate,
217                     AppId = appId,
218                 };
219
220                 if (contentInfo != null && contentInfo != String.Empty)
221                 {
222                     shortcutInfo.Uri = contentInfo;
223                 }
224
225                 if (shortcutAdded != null)
226                 {
227                     err = shortcutAdded(shortcutInfo);
228                 }
229                 else
230                 {
231                     err = ShortcutError.IoError;
232                 }
233             }
234             else
235             {
236                 WidgetShortcutAddedInfo shortcutInfo = new WidgetShortcutAddedInfo
237                 {
238                     ShortcutName = shortcutName,
239                     IconPath = iconPath,
240                     IsAllowDuplicate = isAllowDuplicate,
241                     WidgetId = appId,
242                     WidgetSize = (ShortcutWidgetSize)type,
243                     Period = period,
244                 };
245
246                 if (shortcutAdded != null)
247                 {
248                     err = shortcutAdded(shortcutInfo);
249                 }
250                 else
251                 {
252                     err = ShortcutError.IoError;
253                 }
254             }
255
256             return (int)err;
257         }
258
259         private static int DeleteCallback(string appId, string shortcutName, int processId, IntPtr data)
260         {
261             ShortcutError err = ShortcutError.None;
262
263             ShortcutDeletedInfo deletedInfo = new ShortcutDeletedInfo
264             {
265                 AppId = appId,
266                 ShortcutName = shortcutName,
267             };
268
269             if (shortcutDeleted != null)
270             {
271                 err = shortcutDeleted(deletedInfo);
272             }
273             else
274             {
275                 err = ShortcutError.IoError;
276             }
277
278             return (int)err;
279         }
280     }
281 }