2 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 namespace Tizen.Applications.Shortcut
20 using System.Collections.Generic;
21 using Tizen.Internals.Errors;
24 /// The callback function that is invoked when add request occurred
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);
31 /// The callback function that is invoked when delete request occurred
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);
38 /// This class provides a way to register callback function for shortcut add, delete events.
40 public static class ShortcutEventManager
42 private static Interop.Shortcut.AddCallback shortcutAddCallback;
44 private static Interop.Shortcut.DeleteCallback shortcutDeleteCallback;
46 private static IList<ShortcutTemplate> shortcutTemplates = new List<ShortcutTemplate>();
48 private static ShortcutAdded shortcutAdded = null;
50 private static ShortcutDeleted shortcutDeleted = null;
53 /// Registers a callback function to listen requests from applications.
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>
60 /// Previous registered delegate function should be unregister.
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)
69 if (shortcutAddCallback == null)
71 shortcutAddCallback = new Interop.Shortcut.AddCallback(AddCallback);
73 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutAddCallback(shortcutAddCallback, IntPtr.Zero);
74 if (err != Interop.Shortcut.ErrorCode.None)
76 shortcutAddCallback = null;
77 throw ShortcutErrorFactory.GetException(err, "unable to register callback");
80 shortcutAdded = addedEvent;
84 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
89 /// Registers a callback function to listen requests from applications.
91 /// <since_tizen> 3 </since_tizen>
92 /// <param name="deletedEvent">The callback function pointer that is invoked when Delete() is requested</param>
93 /// <feature>http://tizen.org/feature/shortcut</feature>
94 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
96 /// Previous registered delegate function should be unregister.
98 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
99 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
100 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
101 /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
102 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
103 public static void RegisterEventHandler(ShortcutDeleted deletedEvent)
105 if (shortcutDeleteCallback == null)
107 shortcutDeleteCallback = new Interop.Shortcut.DeleteCallback(DeleteCallback);
109 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutDeleteCallback(shortcutDeleteCallback, IntPtr.Zero);
110 if (err != Interop.Shortcut.ErrorCode.None)
112 shortcutDeleteCallback = null;
113 throw ShortcutErrorFactory.GetException(err, "unable to register callback");
116 shortcutDeleted = deletedEvent;
120 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
125 /// Unregisters a callback for the shortcut request.
127 /// <since_tizen> 3 </since_tizen>
128 /// <param name="addedEvent">The callback function pointer that used for RegisterCallback</param>
129 /// <feature>http://tizen.org/feature/shortcut</feature>
130 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
131 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
132 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
133 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
134 public static void UnregisterEventHandler(ShortcutAdded addedEvent)
136 if (shortcutAdded != null && shortcutAdded.Equals(addedEvent))
138 shortcutAdded = null;
140 if (shortcutAddCallback != null)
142 Interop.Shortcut.UnsetShortcutAddCallback();
143 shortcutAddCallback = null;
145 int err = ErrorFacts.GetLastResult();
146 if (err != (int)Interop.Shortcut.ErrorCode.None)
148 throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode)err, "unable to unregister callback");
154 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
159 /// Unregisters a callback for the shortcut request.
161 /// <since_tizen> 3 </since_tizen>
162 /// <param name="deletedEvent">The callback function pointer that used for RegisterCallback</param>
163 /// <feature>http://tizen.org/feature/shortcut</feature>
164 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
165 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
166 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
167 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
168 public static void UnregisterEventHandler(ShortcutDeleted deletedEvent)
170 if (shortcutDeleted != null && shortcutDeleted.Equals(deletedEvent))
172 shortcutDeleted = null;
174 if (shortcutDeleteCallback != null)
176 Interop.Shortcut.UnsetShortcutDeleteCallback();
177 shortcutDeleteCallback = null;
179 int err = ErrorFacts.GetLastResult();
180 if (err != (int)Interop.Shortcut.ErrorCode.None)
182 throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode) err, "unable to unregister callback");
188 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
193 /// Gets the preset list of shortcut template from the installed package.
195 /// <since_tizen> 3 </since_tizen>
196 /// <param name="appId">Application ID.</param>
197 /// <returns>The List of ShortcutTemplate.</returns>
198 /// <feature>http://tizen.org/feature/shortcut</feature>
199 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
200 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
201 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
202 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
203 /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
204 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
205 public static IEnumerable<ShortcutTemplate> GetTemplateList(string appId)
207 shortcutTemplates.Clear();
209 if (string.IsNullOrEmpty(appId))
211 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
214 Interop.Shortcut.ListCallback callback = (appName, iconPath, shortcutName, extrakey, extraData, user_data) =>
216 ShortcutTemplate template = new ShortcutTemplate
219 ShortcutName = shortcutName,
222 ExtraData = extraData,
225 shortcutTemplates.Add(template);
230 Interop.Shortcut.ErrorCode err = Interop.Shortcut.GetList(appId, callback, IntPtr.Zero);
231 if (err < Interop.Shortcut.ErrorCode.None)
233 throw ShortcutErrorFactory.GetException(err, "unable to get ShortcutTemplate Lists");
236 return shortcutTemplates;
239 private static int AddCallback(string appId, string shortcutName, int type, string contentInfo, string iconPath, int processId, double period, bool isAllowDuplicate, IntPtr data)
243 if (type == (int)ShortcutType.LaunchByApp || type == (int)ShortcutType.LaunchByUri)
245 HomeShortcutAddedInfo shortcutInfo = new HomeShortcutAddedInfo
247 ShortcutName = shortcutName,
249 IsAllowDuplicate = isAllowDuplicate,
253 if (contentInfo != null && contentInfo != String.Empty)
255 shortcutInfo.Uri = contentInfo;
258 if (shortcutAdded != null)
260 err = shortcutAdded(shortcutInfo);
264 err = ShortcutError.IoError;
269 WidgetShortcutAddedInfo shortcutInfo = new WidgetShortcutAddedInfo
271 ShortcutName = shortcutName,
273 IsAllowDuplicate = isAllowDuplicate,
275 WidgetSize = (ShortcutWidgetSize)type,
279 if (shortcutAdded != null)
281 err = shortcutAdded(shortcutInfo);
285 err = ShortcutError.IoError;
292 private static int DeleteCallback(string appId, string shortcutName, int processId, IntPtr data)
294 ShortcutError err = ShortcutError.None;
296 ShortcutDeletedInfo deletedInfo = new ShortcutDeletedInfo
299 ShortcutName = shortcutName,
302 if (shortcutDeleted != null)
304 err = shortcutDeleted(deletedInfo);
308 err = ShortcutError.IoError;