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 /// <since_tizen> 4 </since_tizen>
29 public delegate ShortcutError ShortcutAdded(ShortcutAddedInfo args);
32 /// The callback function that is invoked when delete request occurred
34 /// <param name="args">Object that contain shortcut info to delete.</param>
35 /// <returns>The result of handling a shortcut delete request</returns>
36 /// <since_tizen> 4 </since_tizen>
37 public delegate ShortcutError ShortcutDeleted(ShortcutDeletedInfo args);
40 /// This class provides a way to register callback function for shortcut add, delete events.
42 /// <since_tizen> 4 </since_tizen>
43 public static class ShortcutEventManager
45 private static Interop.Shortcut.AddCallback shortcutAddCallback;
47 private static Interop.Shortcut.DeleteCallback shortcutDeleteCallback;
49 private static IList<ShortcutTemplate> shortcutTemplates = new List<ShortcutTemplate>();
51 private static ShortcutAdded shortcutAdded = null;
53 private static ShortcutDeleted shortcutDeleted = null;
56 /// Registers a callback function to listen requests from applications.
58 /// <since_tizen> 4 </since_tizen>
59 /// <param name="addedEvent">The callback function pointer that is invoked when Add() is requested</param>
60 /// <feature>http://tizen.org/feature/shortcut</feature>
61 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
63 /// Previous registered delegate function should be unregister.
65 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
66 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
67 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
68 /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
69 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
70 public static void RegisterEventHandler(ShortcutAdded addedEvent)
72 if (shortcutAddCallback == null)
74 shortcutAddCallback = new Interop.Shortcut.AddCallback(AddCallback);
76 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutAddCallback(shortcutAddCallback, IntPtr.Zero);
77 if (err != Interop.Shortcut.ErrorCode.None)
79 shortcutAddCallback = null;
80 throw ShortcutErrorFactory.GetException(err, "unable to register callback");
83 shortcutAdded = addedEvent;
87 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
92 /// Registers a callback function to listen requests from applications.
94 /// <since_tizen> 4 </since_tizen>
95 /// <param name="deletedEvent">The callback function pointer that is invoked when Delete() is requested</param>
96 /// <feature>http://tizen.org/feature/shortcut</feature>
97 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
99 /// Previous registered delegate function should be unregister.
101 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
102 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
103 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
104 /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
105 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
106 public static void RegisterEventHandler(ShortcutDeleted deletedEvent)
108 if (shortcutDeleteCallback == null)
110 shortcutDeleteCallback = new Interop.Shortcut.DeleteCallback(DeleteCallback);
112 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutDeleteCallback(shortcutDeleteCallback, IntPtr.Zero);
113 if (err != Interop.Shortcut.ErrorCode.None)
115 shortcutDeleteCallback = null;
116 throw ShortcutErrorFactory.GetException(err, "unable to register callback");
119 shortcutDeleted = deletedEvent;
123 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
128 /// Unregisters a callback for the shortcut request.
130 /// <since_tizen> 4 </since_tizen>
131 /// <param name="addedEvent">The callback function pointer that used for RegisterCallback</param>
132 /// <feature>http://tizen.org/feature/shortcut</feature>
133 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
134 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
135 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
136 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
137 public static void UnregisterEventHandler(ShortcutAdded addedEvent)
139 if (shortcutAdded != null && shortcutAdded.Equals(addedEvent))
141 shortcutAdded = null;
143 if (shortcutAddCallback != null)
145 Interop.Shortcut.UnsetShortcutAddCallback();
146 shortcutAddCallback = null;
148 int err = ErrorFacts.GetLastResult();
149 if (err != (int)Interop.Shortcut.ErrorCode.None)
151 throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode)err, "unable to unregister callback");
157 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
162 /// Unregisters a callback for the shortcut request.
164 /// <since_tizen> 4 </since_tizen>
165 /// <param name="deletedEvent">The callback function pointer that used for RegisterCallback</param>
166 /// <feature>http://tizen.org/feature/shortcut</feature>
167 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
168 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
169 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
170 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
171 public static void UnregisterEventHandler(ShortcutDeleted deletedEvent)
173 if (shortcutDeleted != null && shortcutDeleted.Equals(deletedEvent))
175 shortcutDeleted = null;
177 if (shortcutDeleteCallback != null)
179 Interop.Shortcut.UnsetShortcutDeleteCallback();
180 shortcutDeleteCallback = null;
182 int err = ErrorFacts.GetLastResult();
183 if (err != (int)Interop.Shortcut.ErrorCode.None)
185 throw ShortcutErrorFactory.GetException((Interop.Shortcut.ErrorCode) err, "unable to unregister callback");
191 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
196 /// Gets the preset list of shortcut template from the installed package.
198 /// <since_tizen> 4 </since_tizen>
199 /// <param name="appId">Application ID.</param>
200 /// <returns>The List of ShortcutTemplate.</returns>
201 /// <feature>http://tizen.org/feature/shortcut</feature>
202 /// <privilege>http://tizen.org/privilege/shortcut</privilege>
203 /// <exception cref="ArgumentException">Thrown when argument is invalid.</exception>
204 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
205 /// <exception cref="NotSupportedException">Thrown when Shortcut is not supported.</exception>
206 /// <exception cref="OutOfMemoryException">Thrown in case of out of memory.</exception>
207 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
208 public static IEnumerable<ShortcutTemplate> GetTemplateList(string appId)
210 shortcutTemplates.Clear();
212 if (string.IsNullOrEmpty(appId))
214 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
217 Interop.Shortcut.ListCallback callback = (appName, iconPath, shortcutName, extrakey, extraData, user_data) =>
219 ShortcutTemplate template = new ShortcutTemplate
222 ShortcutName = shortcutName,
225 ExtraData = extraData,
228 shortcutTemplates.Add(template);
233 Interop.Shortcut.ErrorCode err = Interop.Shortcut.GetList(appId, callback, IntPtr.Zero);
234 if (err < Interop.Shortcut.ErrorCode.None)
236 throw ShortcutErrorFactory.GetException(err, "unable to get ShortcutTemplate Lists");
239 return shortcutTemplates;
242 private static int AddCallback(string appId, string shortcutName, int type, string contentInfo, string iconPath, int processId, double period, bool isAllowDuplicate, IntPtr data)
246 if (type == (int)ShortcutType.LaunchByApp || type == (int)ShortcutType.LaunchByUri)
248 HomeShortcutAddedInfo shortcutInfo = new HomeShortcutAddedInfo
250 ShortcutName = shortcutName,
252 IsAllowDuplicate = isAllowDuplicate,
256 if (contentInfo != null && contentInfo != String.Empty)
258 shortcutInfo.Uri = contentInfo;
261 if (shortcutAdded != null)
263 err = shortcutAdded(shortcutInfo);
267 err = ShortcutError.IoError;
272 WidgetShortcutAddedInfo shortcutInfo = new WidgetShortcutAddedInfo
274 ShortcutName = shortcutName,
276 IsAllowDuplicate = isAllowDuplicate,
278 WidgetSize = (ShortcutWidgetSize)type,
282 if (shortcutAdded != null)
284 err = shortcutAdded(shortcutInfo);
288 err = ShortcutError.IoError;
295 private static int DeleteCallback(string appId, string shortcutName, int processId, IntPtr data)
297 ShortcutError err = ShortcutError.None;
299 ShortcutDeletedInfo deletedInfo = new ShortcutDeletedInfo
302 ShortcutName = shortcutName,
305 if (shortcutDeleted != null)
307 err = shortcutDeleted(deletedInfo);
311 err = ShortcutError.IoError;