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;
23 /// The callback function that is invoked when add request occurred
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);
30 /// The callback function that is invoked when delete request occurred
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);
37 /// This class provides a way to register callback function for shortcut add, delete events.
39 public static class ShortcutEventManager
41 private static Interop.Shortcut.AddCallback shortcutAddCallback;
43 private static Interop.Shortcut.DeleteCallback shortcutDeleteCallback;
45 private static IList<ShortcutTemplate> shortcutTemplates = new List<ShortcutTemplate>();
47 private static ShortcutAdded shortcutAdded = null;
49 private static ShortcutDeleted shortcutDeleted = null;
52 /// Registers a callback function to listen requests from applications.
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>
59 /// Previous registered delegate function should be unregister.
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)
67 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 throw ShortcutErrorFactory.GetException(err, "unable to register callback");
82 /// Registers a callback function to listen requests from applications.
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>
89 /// Previous registered delegate function should be unregister.
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)
97 shortcutDeleted = deletedEvent;
99 if (shortcutDeleteCallback == null)
101 shortcutDeleteCallback = new Interop.Shortcut.DeleteCallback(DeleteCallback);
103 Interop.Shortcut.ErrorCode err = Interop.Shortcut.SetShortcutDeleteCallback(shortcutDeleteCallback, IntPtr.Zero);
104 if (err != Interop.Shortcut.ErrorCode.None)
106 throw ShortcutErrorFactory.GetException(err, "unable to register callback");
112 /// Unregisters a callback for the shortcut request.
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)
124 if (shortcutAdded.Equals(addedEvent))
126 shortcutAdded = null;
128 if (shortcutAddCallback != null)
130 Interop.Shortcut.UnsetShortcutAddCallback();
131 shortcutAddCallback = null;
136 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
141 /// Unregisters a callback for the shortcut request.
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)
153 if (shortcutDeleted.Equals(deletedEvent))
155 shortcutDeleted = null;
157 if (shortcutDeleteCallback != null)
159 Interop.Shortcut.UnsetShortcutDeleteCallback();
160 shortcutDeleteCallback = null;
165 throw ShortcutErrorFactory.GetException(Interop.Shortcut.ErrorCode.InvalidParameter, null);
170 /// Gets the preset list of shortcut template from the installed package.
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)
183 shortcutTemplates.Clear();
185 Interop.Shortcut.ListCallback callback = (appName, iconPath, shortcutName, extrakey, extraData, user_data) =>
187 ShortcutTemplate template = new ShortcutTemplate
190 ShortcutName = shortcutName,
193 ExtraData = extraData,
196 shortcutTemplates.Add(template);
201 Interop.Shortcut.GetList(appId, callback, IntPtr.Zero);
203 return shortcutTemplates;
206 private static int AddCallback(string appId, string shortcutName, int type, string contentInfo, string iconPath, int processId, double period, bool isAllowDuplicate, IntPtr data)
210 if (type == (int)ShortcutType.LaunchByApp || type == (int)ShortcutType.LaunchByUri)
212 HomeShortcutAddedInfo shortcutInfo = new HomeShortcutAddedInfo
214 ShortcutName = shortcutName,
216 IsAllowDuplicate = isAllowDuplicate,
220 if (contentInfo != null && contentInfo != String.Empty)
222 shortcutInfo.Uri = contentInfo;
225 if (shortcutAdded != null)
227 err = shortcutAdded(shortcutInfo);
231 err = ShortcutError.IoError;
236 WidgetShortcutAddedInfo shortcutInfo = new WidgetShortcutAddedInfo
238 ShortcutName = shortcutName,
240 IsAllowDuplicate = isAllowDuplicate,
242 WidgetSize = (ShortcutWidgetSize)type,
246 if (shortcutAdded != null)
248 err = shortcutAdded(shortcutInfo);
252 err = ShortcutError.IoError;
259 private static int DeleteCallback(string appId, string shortcutName, int processId, IntPtr data)
261 ShortcutError err = ShortcutError.None;
263 ShortcutDeletedInfo deletedInfo = new ShortcutDeletedInfo
266 ShortcutName = shortcutName,
269 if (shortcutDeleted != null)
271 err = shortcutDeleted(deletedInfo);
275 err = ShortcutError.IoError;