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