2 * Copyright (c) 2022 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.
19 using System.Collections.Generic;
20 using System.Threading.Tasks;
21 using static Interop.StickerData;
22 using static Interop.StickerProvider;
24 namespace Tizen.Uix.Sticker
27 /// The StickerProvider provides the methods to create/update/delete the sticker data.
29 /// <since_tizen> 10 </since_tizen>
30 public static class StickerProvider
32 private static IntPtr _handle = IntPtr.Zero;
33 private static StickerProviderInsertFinishedCallback _insertDelegate;
36 /// Gets a flag indicating whether the StickerProvider is initialized
38 public static bool Initialized { get; private set; }
41 /// Initialize sticker provider.
43 /// <since_tizen> 10 </since_tizen>
44 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
45 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
46 /// <exception cref="InvalidOperationException">
47 /// This can occur due to the following reasons:
48 /// 1) This exception can be due to out of memory.
49 /// 2) This exception can be due to operation failed.
51 public static void Initialize()
56 ErrorCode error = StickerProviderCreate(out handle);
57 if (error != ErrorCode.None)
59 Log.Error(LogTag, "Create Failed with error " + error);
60 throw ExceptionFactory.CreateException(error);
67 Log.Debug(LogTag, "Already initialized");
72 /// Deinitialize the sticker provider.
74 /// <since_tizen> 10 </since_tizen>
75 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
76 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
77 /// <exception cref="InvalidOperationException">This exception can be due to operation failed.</exception>
79 /// StickerProvider must be initialized.
81 public static void Deinitialize()
83 ErrorCode error = StickerProviderDestroy(_handle);
84 if (error != ErrorCode.None)
86 Log.Error(LogTag, "Destroy Failed with error " + error);
87 if (error == ErrorCode.InvalidParameter)
88 throw ExceptionFactory.CreateException(ErrorCode.OperationFailed);
90 throw ExceptionFactory.CreateException(error);
95 /// Inserts a sticker data to the sticker database.
97 /// <since_tizen> 10 </since_tizen>
98 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
99 /// <param name="data">The sticker data to be saved.</param>
100 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
101 /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
102 /// <exception cref="InvalidOperationException">
103 /// This can occur due to the following reasons:
104 /// 1) This exception can be due to operation failed.
105 /// 2) This exception can be due to file exists.
106 /// 3) This exception can be due to no such file.
108 public static void InsertData(StickerData data)
110 ErrorCode error = StickerProviderInsertData(_handle, data.SafeStickerDataHandle);
111 if (error != ErrorCode.None)
113 Log.Error(LogTag, "InsertData Failed with error " + error);
114 throw ExceptionFactory.CreateException(error);
119 /// Inserts a sticker data using JSON file.
121 /// <since_tizen> 10 </since_tizen>
122 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
124 /// All data except thumbnail, description, and display_type must be set in the JSON file to insert the sticker data.
125 /// <paramref name="jsonPath"/> must have a non-null value and must be an existing file.
126 /// If the URI type is lacal path, the sticker file is copied to a sticker directory.
127 /// It is recommended to delete your sticker files after inserting a sticker data.
129 /// <param name="jsonPath">The path of JSON file containing sticker data to be saved</param>
130 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
131 /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
132 /// <exception cref="InvalidOperationException">This exception can be due to operation failed.</exception>
133 public static Task InsertData(string jsonPath)
135 var task = new TaskCompletionSource<bool>();
136 _insertDelegate = (ErrorCode errorCode, IntPtr userData) =>
138 if (errorCode != ErrorCode.None)
140 Log.Error(LogTag, "Exception occurred while inserting");
141 throw ExceptionFactory.CreateException(errorCode);
145 task.SetResult(true);
148 ErrorCode error = StickerProviderInsertDataByJsonFile(_handle, jsonPath, _insertDelegate, IntPtr.Zero);
149 if (error != ErrorCode.None)
151 Log.Error(LogTag, "InsertData Failed with error " + error);
152 task.SetException(ExceptionFactory.CreateException(error));
158 /// Updates a sticker data in the sticker database.
160 /// <since_tizen> 10 </since_tizen>
161 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
162 /// <param name="data">The sticker data to be updated.</param>
163 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
164 /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
165 /// <exception cref="InvalidOperationException">
166 /// This can occur due to the following reasons:
167 /// 1) This exception can be due to operation failed.
168 /// 2) This exception can be due to file exists.
169 /// 3) This exception can be due to no such file.
171 public static void UpdateData(StickerData data)
173 ErrorCode error = StickerProviderUpdateData(_handle, data.SafeStickerDataHandle);
174 if (error != ErrorCode.None)
176 Log.Error(LogTag, "UpdateData Failed with error " + error);
177 throw ExceptionFactory.CreateException(error);
182 /// Deletes a sticker data in the sticker database.
184 /// <since_tizen> 10 </since_tizen>
185 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
186 /// <param name="data">The sticker data to be deleted.</param>
187 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
188 /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
189 /// <exception cref="InvalidOperationException">This exception can be due to operation failed.</exception>
190 public static void DeleteData(StickerData data)
192 ErrorCode error = StickerProviderDeleteData(_handle, data.SafeStickerDataHandle);
193 if (error != ErrorCode.None)
195 Log.Error(LogTag, "DeleteData Failed with error " + error);
196 throw ExceptionFactory.CreateException(error);
201 /// Deletes a sticker data in the sticker database.
203 /// <since_tizen> 10 </since_tizen>
204 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
205 /// <param name="uri">The URI of the sticker data to be deleted.</param>
206 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
207 /// <exception cref="InvalidOperationException">
208 /// This can occur due to the following reasons:
209 /// 1) This exception can be due to operation failed.
210 /// 2) This exception can be due to no such file.
212 public static void DeleteData(string uri)
214 ErrorCode error = StickerProviderDeleteDataByUri(_handle, uri);
215 if (error != ErrorCode.None)
217 Log.Error(LogTag, "DeleteData Failed with error " + error);
218 throw ExceptionFactory.CreateException(error);
223 /// Gets the count of stickers stored by the provider application.
225 /// <since_tizen> 10 </since_tizen>
226 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
227 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
228 /// <exception cref="InvalidOperationException">This exception can be due to operation failed.</exception>
229 public static int StickerCount
234 ErrorCode error = StickerProviderGetStickerCount(_handle, out _count);
235 if (error != ErrorCode.None)
237 Log.Error(LogTag, "StickerCount Failed with error " + error);
238 throw ExceptionFactory.CreateException(error);
246 /// Retrieves all sticker data in the sticker database.
248 /// <since_tizen> 10 </since_tizen>
249 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
250 /// <param name="offset">The start position (Starting from zero).</param>
251 /// <param name="count">The number of stickers to be retrieved with respect to the offset.</param>
252 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
253 /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
254 /// <exception cref="InvalidOperationException">
255 /// This can occur due to the following reasons:
256 /// 1) This exception can be due to out of memory.
257 /// 2) This exception can be due to operation failed.
259 public static IEnumerable<StickerData> GetAllStickers(int offset, int count)
261 var stickers = new List<StickerData>();
262 StickerProviderDataForeachCallback _dataForeachDelegate = (IntPtr stickerData, IntPtr userData) =>
264 StickerData data = new StickerData(stickerData);
267 ErrorCode error = StickerProviderDataForeachAll(_handle, offset, count, out var result, _dataForeachDelegate, IntPtr.Zero);
268 if (error != ErrorCode.None)
270 Log.Error(LogTag, "GetAllStickers Failed with error " + error);
271 throw ExceptionFactory.CreateException(error);
278 /// Sets the image of the sticker group.
279 /// URI must be a relative path like '/res/smile.png' when URI type is local path.
281 /// <since_tizen> 10 </since_tizen>
282 /// <feature>http://tizen.org/feature/ui_service.sticker</feature>
283 /// <param name="groupImage">The group image to be set.</param>
284 /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
285 /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
286 /// <exception cref="InvalidOperationException">
287 /// This can occur due to the following reasons:
288 /// 1) This exception can be due to operation failed.
289 /// 2) This exception can be due to no such file.
291 public static void SetGroupImage(GroupImage groupImage)
293 ErrorCode error = StickerProviderSetGroupImage(_handle, groupImage.GroupName, groupImage.UriType, groupImage.Uri);
294 if (error != ErrorCode.None)
296 Log.Error(LogTag, "SetGroupImage Failed with error " + error);
297 throw ExceptionFactory.CreateException(error);