2 * Copyright (c) 2016 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.
18 using System.Collections.Generic;
19 using Tizen.Applications;
21 namespace Tizen.Applications
24 /// The class for receiving widget events and sending data to the widget.
26 public class WidgetControl : IDisposable
29 /// Class for the widget instance.
33 private string _widgetId;
35 internal Instance(string widgetId)
43 /// <since_tizen> 3 </since_tizen>
44 public string Id { get; internal set; }
47 /// Gets the widget content.
49 /// <since_tizen> 3 </since_tizen>
50 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
51 public Bundle GetContent()
55 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetContent(_widgetId, Id, out h);
59 case Interop.WidgetService.ErrorCode.InvalidParameter:
60 throw new InvalidOperationException("Invalid parameter at unmanaged code");
62 case Interop.WidgetService.ErrorCode.IoError:
63 throw new InvalidOperationException("Failed to access DB");
66 return new Bundle(new SafeBundleHandle(h, true));
70 /// Changes the content for the widget instance.
72 /// <since_tizen> 3 </since_tizen>
73 /// <param name="content">Content to be changed.</param>
74 /// <param name="force"> True if you want to update your widget even if the provider is paused, otherwise false.</param>
75 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
76 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
77 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
78 public void ChangeContent(Bundle content, bool force)
80 Interop.WidgetService.ErrorCode err = Interop.WidgetService.UpdateContent(_widgetId, Id, content.SafeBundleHandle, force ? 1 : 0);
84 case Interop.WidgetService.ErrorCode.InvalidParameter:
85 throw new ArgumentException("Invalid parameter");
87 case Interop.WidgetService.ErrorCode.Canceled:
88 throw new InvalidOperationException("Provider is paused, so this update request is canceld");
90 case Interop.WidgetService.ErrorCode.OutOfMemory:
91 throw new InvalidOperationException("Out-of-memory at unmanaged code");
93 case Interop.WidgetService.ErrorCode.Fault:
94 throw new InvalidOperationException("Failed to create a request packet");
96 case Interop.WidgetService.ErrorCode.PermissionDenied:
97 throw new UnauthorizedAccessException();
102 /// Changes the update period for the widget instance.
104 /// <since_tizen> 3 </since_tizen>
105 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
106 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
107 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
108 public void ChangePeriod(double period)
110 Interop.WidgetService.ErrorCode err = Interop.WidgetService.ChangePeriod(_widgetId, Id, period);
114 case Interop.WidgetService.ErrorCode.InvalidParameter:
115 throw new ArgumentException("Invalid parameter");
117 case Interop.WidgetService.ErrorCode.OutOfMemory:
118 throw new InvalidOperationException("Out-of-memory at unmanaged code");
120 case Interop.WidgetService.ErrorCode.Fault:
121 throw new InvalidOperationException("Failed to create a request packet");
123 case Interop.WidgetService.ErrorCode.PermissionDenied:
124 throw new UnauthorizedAccessException();
130 /// The class for the widget size information.
140 /// Enumeration for the types of widget size.
142 /// <since_tizen> 3 </since_tizen>
143 public enum SizeType : int
146 /// 175x175 based on 720x1280 resolution.
151 /// 354x175 based on 720x1280 resolution.
156 /// 354x354 based on 720x1280 resolution.
161 /// 712x175 based on 720x1280 resolution.
166 /// 712x354 based on 720x1280 resolution.
171 /// 712x533 based on 720x1280 resolution.
176 /// 712x712 based on 720x1280 resolution.
181 /// 712x891 based on 720x1280 resolution.
186 /// 712x1070 based on 720x1280 resolution.
192 /// 224x215 based on 720x1280 resolution.
197 /// 680x215 based on 720x1280 resolution.
202 /// 680x653 based on 720x1280 resolution.
207 /// 720x1280 based on 720x1280 resolution.
215 /// <since_tizen> 3 </since_tizen>
216 public int Width { get; internal set; }
221 /// <since_tizen> 3 </since_tizen>
222 public int Height { get; internal set; }
225 /// The path for the widget preview image file.
227 /// <since_tizen> 3 </since_tizen>
228 public string PreviewImagePath { get; internal set; }
231 /// The size type of the widget.
233 /// <since_tizen> 3 </since_tizen>
234 public SizeType Type { get; internal set; }
237 private event EventHandler<WidgetLifecycleEventArgs> _created;
238 private event EventHandler<WidgetLifecycleEventArgs> _resumed;
239 private event EventHandler<WidgetLifecycleEventArgs> _paused;
240 private event EventHandler<WidgetLifecycleEventArgs> _destroyed;
241 private bool _disposedValue = false;
242 private static IDictionary<string, int> s_lifecycleEventRefCnt = new Dictionary<string, int>();
243 private static IList<WidgetControl> s_eventObjects = new List<WidgetControl>();
246 /// Factory method for the WidgetControl.
247 /// It will create all the objects of WidgetControl based on the package ID.
249 /// <since_tizen> 3 </since_tizen>
250 /// <param name="pkgId">Package ID.</param>
251 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
252 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
253 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
254 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
255 public static WidgetControl[] CreateAll(string pkgId)
257 List<WidgetControl> list = new List<WidgetControl>();
259 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetWidgetListByPkgId(pkgId, (widgetId, isPrime, userData) =>
261 list.Add(new WidgetControl(widgetId));
266 case Interop.WidgetService.ErrorCode.InvalidParameter:
267 throw new ArgumentException("Invalid parameter");
269 case Interop.WidgetService.ErrorCode.IoError:
270 throw new InvalidOperationException("Failed to access DB");
272 case Interop.WidgetService.ErrorCode.PermissionDenied:
273 throw new UnauthorizedAccessException();
276 return list.ToArray();
280 /// Gets all the widget IDs by the package ID.
282 /// <since_tizen> 3 </since_tizen>
283 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
284 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
285 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
286 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
287 public static string[] GetWidgetIds(string pkgId)
289 List<string> list = new List<string>();
291 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetWidgetListByPkgId(pkgId, (widgetId, isPrime, userData) =>
298 case Interop.WidgetService.ErrorCode.InvalidParameter:
299 throw new ArgumentException("Invalid parameter");
301 case Interop.WidgetService.ErrorCode.IoError:
302 throw new InvalidOperationException("Failed to access DB");
304 case Interop.WidgetService.ErrorCode.PermissionDenied:
305 throw new UnauthorizedAccessException();
308 return list.ToArray();
314 /// <since_tizen> 3 </since_tizen>
315 public string Id { get; internal set; }
318 /// The flag value for "nodisplay".
320 /// <since_tizen> 3 </since_tizen>
321 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
322 public bool IsNoDisplay
326 if (Interop.WidgetService.GetNoDisplay(Id) != 0)
334 /// The event handler for a created widget instance.
336 /// <since_tizen> 3 </since_tizen>
337 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
338 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
339 public event EventHandler<WidgetLifecycleEventArgs> Created
343 RegisterLifecycleEvent();
349 UnregisterLifecycleEvent();
354 /// The event handler for a resumed widget instance.
356 /// <since_tizen> 3 </since_tizen>
357 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
358 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
359 public event EventHandler<WidgetLifecycleEventArgs> Resumed
363 RegisterLifecycleEvent();
369 UnregisterLifecycleEvent();
374 /// The event handler for a paused widget instance.
376 /// <since_tizen> 3 </since_tizen>
377 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
378 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
379 public event EventHandler<WidgetLifecycleEventArgs> Paused
383 RegisterLifecycleEvent();
389 UnregisterLifecycleEvent();
394 /// The event handler for a destroyed widget instance.
396 /// <since_tizen> 3 </since_tizen>
397 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
398 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
399 public event EventHandler<WidgetLifecycleEventArgs> Destroyed
403 RegisterLifecycleEvent();
409 UnregisterLifecycleEvent();
414 /// The constructor of the WidgetControl object.
416 /// <since_tizen> 3 </since_tizen>
417 /// <param name="widgetId">Widget ID.</param>
418 public WidgetControl(string widgetId)
424 /// Finalizer of the WidgetControl class.
432 /// Gets the objects for widget instance information.
434 /// <since_tizen> 3 </since_tizen>
435 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
436 /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
437 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
438 public IEnumerable<Instance> GetInstances()
440 IList<Instance> instances = new List<Instance>();
441 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetInstances(Id, (widgetId, instanceId, userData) =>
443 instances.Add(new Instance(widgetId) { Id = instanceId });
448 case Interop.WidgetService.ErrorCode.InvalidParameter:
449 throw new InvalidOperationException("Invalid parameter at unmanaged code");
451 case Interop.WidgetService.ErrorCode.NotSupported:
452 throw new NotSupportedException();
454 case Interop.WidgetService.ErrorCode.PermissionDenied:
455 throw new UnauthorizedAccessException();
462 /// Gets the objects for widget scale information.
464 /// <since_tizen> 3 </since_tizen>
465 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
466 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
467 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the privilege to access this method.</exception>
468 public IEnumerable<Scale> GetScales()
470 int[] w = new int[100];
471 int[] h = new int[100];
472 int[] types = new int[100];
475 IList<Scale> scales = new List<Scale>();
477 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetSupportedSizes(Id, ref cnt1, out w, out h);
481 case Interop.WidgetService.ErrorCode.InvalidParameter:
482 throw new InvalidOperationException("Invalid parameter at unmanaged code");
484 case Interop.WidgetService.ErrorCode.IoError:
485 throw new InvalidOperationException("Failed to access DB");
487 case Interop.WidgetService.ErrorCode.PermissionDenied:
488 throw new UnauthorizedAccessException();
491 err = Interop.WidgetService.GetSupportedSizeTypes(Id, ref cnt2, out types);
495 case Interop.WidgetService.ErrorCode.InvalidParameter:
496 throw new InvalidOperationException("Invalid parameter at unmanaged code");
498 case Interop.WidgetService.ErrorCode.IoError:
499 throw new InvalidOperationException("Failed to access DB");
501 case Interop.WidgetService.ErrorCode.PermissionDenied:
502 throw new UnauthorizedAccessException();
508 for (int i = 0; i < cnt1; i++)
510 string prev = Interop.WidgetService.GetPreviewImagePath(Id, types[i]);
512 scales.Add(new Scale()
516 PreviewImagePath = prev,
517 Type = (Scale.SizeType)types[i]
524 /// Gets the widget name.
526 /// <since_tizen> 3 </since_tizen>
527 /// <param name="lang">Language.</param>
528 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
529 /// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
530 public string GetName(string lang)
533 throw new ArgumentNullException();
535 return Interop.WidgetService.GetName(Id, lang);
539 /// Gets the widget icon path.
541 /// <since_tizen> 3 </since_tizen>
542 /// <param name="lang">Language.</param>
543 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
544 /// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
545 public string GetIconPath(string lang)
548 throw new ArgumentNullException();
550 string pkgId = Interop.WidgetService.GetPkgId(Id);
552 return Interop.WidgetService.GetIcon(pkgId, lang);
556 /// Releases all the resources used by the WidgetControl class.
558 /// <since_tizen> 3 </since_tizen>
559 public void Dispose()
562 GC.SuppressFinalize(this);
565 private void Dispose(bool disposing)
577 UnregisterLifecycleEvent();
579 _disposedValue = true;
583 private void RegisterLifecycleEvent()
585 if (!s_lifecycleEventRefCnt.ContainsKey(Id))
586 s_lifecycleEventRefCnt[Id] = 0;
588 if (_created != null || _paused != null || _resumed != null || _destroyed != null)
591 if (s_lifecycleEventRefCnt[Id] == 0)
593 Interop.WidgetService.ErrorCode err = Interop.WidgetService.SetLifecycleEvent(Id, OnLifecycleEvent, IntPtr.Zero);
597 case Interop.WidgetService.ErrorCode.InvalidParameter:
598 throw new InvalidOperationException("Invalid parameter at unmanaged code");
600 case Interop.WidgetService.ErrorCode.PermissionDenied:
601 throw new UnauthorizedAccessException();
605 s_lifecycleEventRefCnt[Id]++;
606 s_eventObjects.Add(this);
609 private void UnregisterLifecycleEvent()
611 if (!s_lifecycleEventRefCnt.ContainsKey(Id))
614 if (s_lifecycleEventRefCnt[Id] <= 0)
617 if (_created != null || _paused != null || _resumed != null || _destroyed != null)
620 if (s_lifecycleEventRefCnt[Id] == 1)
622 Interop.WidgetService.ErrorCode err = Interop.WidgetService.UnsetLifecycleEvent(Id, IntPtr.Zero);
626 case Interop.WidgetService.ErrorCode.InvalidParameter:
627 throw new InvalidOperationException("Invalid parameter at unmanaged code");
629 case Interop.WidgetService.ErrorCode.PermissionDenied:
630 throw new UnauthorizedAccessException();
632 case Interop.WidgetService.ErrorCode.NotExist:
633 throw new InvalidOperationException("Event handler is not exist");
637 s_eventObjects.Remove(this);
638 s_lifecycleEventRefCnt[Id]--;
641 private static void OnLifecycleEvent(string widgetId, Interop.WidgetService.LifecycleEvent e, string instanceId, IntPtr userData)
645 case Interop.WidgetService.LifecycleEvent.Created:
646 foreach (WidgetControl c in s_eventObjects)
648 if (c.Id.CompareTo(widgetId) == 0)
650 c._created?.Invoke(null, new WidgetLifecycleEventArgs()
653 InstanceId = instanceId,
654 Type = WidgetLifecycleEventArgs.EventType.Created
660 case Interop.WidgetService.LifecycleEvent.Resumed:
661 foreach (WidgetControl c in s_eventObjects)
663 if (c.Id.CompareTo(widgetId) == 0)
665 c._resumed?.Invoke(null, new WidgetLifecycleEventArgs()
668 InstanceId = instanceId,
669 Type = WidgetLifecycleEventArgs.EventType.Resumed
675 case Interop.WidgetService.LifecycleEvent.Paused:
676 foreach (WidgetControl c in s_eventObjects)
678 if (c.Id.CompareTo(widgetId) == 0)
680 c._paused?.Invoke(null, new WidgetLifecycleEventArgs()
683 InstanceId = instanceId,
684 Type = WidgetLifecycleEventArgs.EventType.Paused
690 case Interop.WidgetService.LifecycleEvent.Destroyed:
691 foreach (WidgetControl c in s_eventObjects)
693 if (c.Id.CompareTo(widgetId) == 0)
695 c._destroyed?.Invoke(null, new WidgetLifecycleEventArgs()
698 InstanceId = instanceId,
699 Type = WidgetLifecycleEventArgs.EventType.Destroyed