1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
10 using System.Collections.Generic;
11 using Tizen.Applications;
13 namespace Tizen.Applications
16 /// Class for receiving widget events and sending data to widget.
18 public class WidgetControl : IDisposable
21 /// Class for widget instance.
25 private string _widgetId;
30 /// <param name="widgetId">Widget ID</param>
31 public Instance(string widgetId)
39 public string Id { get; internal set; }
42 /// Gets widget content.
44 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
45 public Bundle GetContent()
49 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetContent(_widgetId, Id, out h);
53 case Interop.WidgetService.ErrorCode.InvalidParameter:
54 throw new InvalidOperationException("Invalid parameter at unmanaged code");
56 case Interop.WidgetService.ErrorCode.IoError:
57 throw new InvalidOperationException("Failed to access DB");
60 return new Bundle(new SafeBundleHandle(h, true));
64 /// Changes the content for the widget instance.
66 /// <param name="content">Content to be changed</param>
67 /// <param name="force"> True if you want to update your widget even if the provider is paused otherwise false.</param>
68 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
69 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
70 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
71 public void ChangeContent(Bundle content, bool force)
73 Interop.WidgetService.ErrorCode err = Interop.WidgetService.UpdateContent(_widgetId, Id, content.SafeBundleHandle, force ? 1 : 0);
77 case Interop.WidgetService.ErrorCode.InvalidParameter:
78 throw new ArgumentException("Invalid parameter");
80 case Interop.WidgetService.ErrorCode.Canceled:
81 throw new InvalidOperationException("Provider is paused, so this update request is canceld");
83 case Interop.WidgetService.ErrorCode.OutOfMemory:
84 throw new InvalidOperationException("Out-of-memory at unmanaged code");
86 case Interop.WidgetService.ErrorCode.Fault:
87 throw new InvalidOperationException("Failed to create a request packet");
89 case Interop.WidgetService.ErrorCode.PermissionDenied:
90 throw new UnauthorizedAccessException();
95 /// Changes the update period for the widget instance.
97 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
98 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
99 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
100 public void ChangePeriod(double period)
102 Interop.WidgetService.ErrorCode err = Interop.WidgetService.ChangePeriod(_widgetId, Id, period);
106 case Interop.WidgetService.ErrorCode.InvalidParameter:
107 throw new ArgumentException("Invalid parameter");
109 case Interop.WidgetService.ErrorCode.OutOfMemory:
110 throw new InvalidOperationException("Out-of-memory at unmanaged code");
112 case Interop.WidgetService.ErrorCode.Fault:
113 throw new InvalidOperationException("Failed to create a request packet");
115 case Interop.WidgetService.ErrorCode.PermissionDenied:
116 throw new UnauthorizedAccessException();
122 /// Class for widget size information.
127 /// Enumeration for types of widget size
129 public enum SizeType : int
132 /// 175x175 based on 720x1280 resolution.
137 /// 354x175 based on 720x1280 resolution.
142 /// 354x354 based on 720x1280 resolution.
147 /// 712x175 based on 720x1280 resolution.
152 /// 712x354 based on 720x1280 resolution.
157 /// 712x533 based on 720x1280 resolution.
162 /// 712x712 based on 720x1280 resolution.
167 /// 712x891 based on 720x1280 resolution.
172 /// 712x1070 based on 720x1280 resolution.
178 /// 224x215 based on 720x1280 resolution.
183 /// 680x215 based on 720x1280 resolution.
188 /// 680x653 based on 720x1280 resolution.
193 /// 720x1280 based on 720x1280 resolution.
201 public int Width { get; internal set; }
206 public int Height { get; internal set; }
209 /// The path for widget preview image file.
211 public string PreviewImagePath { get; internal set; }
214 /// The size type of the widget.
216 public SizeType Type { get; internal set; }
219 private event EventHandler<WidgetLifecycleEventArgs> _created;
220 private event EventHandler<WidgetLifecycleEventArgs> _resumed;
221 private event EventHandler<WidgetLifecycleEventArgs> _paused;
222 private event EventHandler<WidgetLifecycleEventArgs> _destroyed;
223 private bool _disposedValue = false;
224 private static IDictionary<string, int> s_lifecycleEventRefCnt = new Dictionary<string, int>();
225 private static IList<WidgetControl> s_eventObjects = new List<WidgetControl>();
228 /// Factory method for WidgetControl.
229 /// It will create all objects of WidgetControl base on package ID.
231 /// <param name="pkgId">Parkage ID</param>
232 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
233 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
234 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
235 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
236 public static WidgetControl[] CreateAll(string pkgId)
238 List<WidgetControl> list = new List<WidgetControl>();
240 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetWidgetListByPkgId(pkgId, (widgetId, isPrime, userData) =>
242 list.Add(new WidgetControl(widgetId));
247 case Interop.WidgetService.ErrorCode.InvalidParameter:
248 throw new ArgumentException("Invalid parameter");
250 case Interop.WidgetService.ErrorCode.IoError:
251 throw new InvalidOperationException("Failed to access DB");
253 case Interop.WidgetService.ErrorCode.PermissionDenied:
254 throw new UnauthorizedAccessException();
257 return list.ToArray();
261 /// Gets all widget IDs by package ID.
263 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
264 /// <exception cref="ArgumentException">Thrown when failed because of invalid argument</exception>
265 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
266 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
267 public static string[] GetWidgetIds(string pkgId)
269 List<string> list = new List<string>();
271 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetWidgetListByPkgId(pkgId, (widgetId, isPrime, userData) =>
278 case Interop.WidgetService.ErrorCode.InvalidParameter:
279 throw new ArgumentException("Invalid parameter");
281 case Interop.WidgetService.ErrorCode.IoError:
282 throw new InvalidOperationException("Failed to access DB");
284 case Interop.WidgetService.ErrorCode.PermissionDenied:
285 throw new UnauthorizedAccessException();
288 return list.ToArray();
294 public string Id { get; internal set; }
297 /// The flag value for "nodisplay"
299 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
300 public bool IsNoDisplay
304 if (Interop.WidgetService.GetNoDisplay(Id) != 0)
312 /// Event handler for created widget instance.
314 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
315 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
316 public event EventHandler<WidgetLifecycleEventArgs> Created
320 RegisterLifecycleEvent();
326 UnregisterLifecycleEvent();
331 /// Event handler for resumed widget instance.
333 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
334 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
335 public event EventHandler<WidgetLifecycleEventArgs> Resumed
339 RegisterLifecycleEvent();
345 UnregisterLifecycleEvent();
350 /// Event handler for paused widget instance.
352 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
353 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
354 public event EventHandler<WidgetLifecycleEventArgs> Paused
358 RegisterLifecycleEvent();
364 UnregisterLifecycleEvent();
369 /// Event handler for destroyed widget instance.
371 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
372 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
373 public event EventHandler<WidgetLifecycleEventArgs> Destroyed
377 RegisterLifecycleEvent();
383 UnregisterLifecycleEvent();
388 /// A constructor of WidgetControl object
390 /// <param name="widgetId">widget id.</param>
391 public WidgetControl(string widgetId)
397 /// Finalizer of the WidgetControl class.
405 /// Gets objects for widget instance information.
407 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
408 /// <exception cref="NotSupportedException">Thrown when API is not supported in this device</exception>
409 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
410 public IEnumerable<Instance> GetInstances()
412 IList<Instance> instances = new List<Instance>();
413 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetInstances(Id, (widgetId, instanceId, userData) =>
415 instances.Add(new Instance(widgetId) { Id = instanceId });
420 case Interop.WidgetService.ErrorCode.InvalidParameter:
421 throw new InvalidOperationException("Invalid parameter at unmanaged code");
423 case Interop.WidgetService.ErrorCode.NotSupported:
424 throw new NotSupportedException();
426 case Interop.WidgetService.ErrorCode.PermissionDenied:
427 throw new UnauthorizedAccessException();
434 /// Gets objects for widget scale information.
436 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
437 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions</exception>
438 /// <exception cref="UnauthorizedAccessException">Thrown when app does not have privilege to access this method</exception>
439 public IEnumerable<Scale> GetScales()
441 int[] w = new int[100];
442 int[] h = new int[100];
443 int[] types = new int[100];
446 IList<Scale> scales = new List<Scale>();
448 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetSupportedSizes(Id, ref cnt1, out w, out h);
452 case Interop.WidgetService.ErrorCode.InvalidParameter:
453 throw new InvalidOperationException("Invalid parameter at unmanaged code");
455 case Interop.WidgetService.ErrorCode.IoError:
456 throw new InvalidOperationException("Failed to access DB");
458 case Interop.WidgetService.ErrorCode.PermissionDenied:
459 throw new UnauthorizedAccessException();
462 err = Interop.WidgetService.GetSupportedSizeTypes(Id, ref cnt2, out types);
466 case Interop.WidgetService.ErrorCode.InvalidParameter:
467 throw new InvalidOperationException("Invalid parameter at unmanaged code");
469 case Interop.WidgetService.ErrorCode.IoError:
470 throw new InvalidOperationException("Failed to access DB");
472 case Interop.WidgetService.ErrorCode.PermissionDenied:
473 throw new UnauthorizedAccessException();
479 for (int i = 0; i < cnt1; i++)
481 string prev = Interop.WidgetService.GetPreviewImagePath(Id, types[i]);
483 scales.Add(new Scale()
487 PreviewImagePath = prev,
488 Type = (Scale.SizeType)types[i]
495 /// Gets widget name.
497 /// <param name="lang">Language</param>
498 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
499 /// <exception cref="ArgumentNullException">Thrown when argument is null</exception>
500 public string GetName(string lang)
503 throw new ArgumentNullException();
505 return Interop.WidgetService.GetName(Id, lang);
509 /// Gets widget icon path.
511 /// <param name="lang">Language</param>
512 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
513 /// <exception cref="ArgumentNullException">Thrown when argument is null</exception>
514 public string GetIconPath(string lang)
517 throw new ArgumentNullException();
519 string pkgId = Interop.WidgetService.GetPkgId(Id);
521 return Interop.WidgetService.GetIcon(pkgId, lang);
525 /// Releases all resources used by the WidgetControl class.
527 public void Dispose()
530 GC.SuppressFinalize(this);
533 private void Dispose(bool disposing)
545 UnregisterLifecycleEvent();
547 _disposedValue = true;
551 private void RegisterLifecycleEvent()
553 if (!s_lifecycleEventRefCnt.ContainsKey(Id))
554 s_lifecycleEventRefCnt[Id] = 0;
556 if (_created != null || _paused != null || _resumed != null || _destroyed != null)
559 if (s_lifecycleEventRefCnt[Id] == 0)
561 Interop.WidgetService.ErrorCode err = Interop.WidgetService.SetLifecycleEvent(Id, OnLifecycleEvent, IntPtr.Zero);
565 case Interop.WidgetService.ErrorCode.InvalidParameter:
566 throw new InvalidOperationException("Invalid parameter at unmanaged code");
568 case Interop.WidgetService.ErrorCode.PermissionDenied:
569 throw new UnauthorizedAccessException();
573 s_lifecycleEventRefCnt[Id]++;
574 s_eventObjects.Add(this);
577 private void UnregisterLifecycleEvent()
579 if (!s_lifecycleEventRefCnt.ContainsKey(Id))
582 if (s_lifecycleEventRefCnt[Id] <= 0)
585 if (_created != null || _paused != null || _resumed != null || _destroyed != null)
588 if (s_lifecycleEventRefCnt[Id] == 1)
590 Interop.WidgetService.ErrorCode err = Interop.WidgetService.UnsetLifecycleEvent(Id, IntPtr.Zero);
594 case Interop.WidgetService.ErrorCode.InvalidParameter:
595 throw new InvalidOperationException("Invalid parameter at unmanaged code");
597 case Interop.WidgetService.ErrorCode.PermissionDenied:
598 throw new UnauthorizedAccessException();
600 case Interop.WidgetService.ErrorCode.NotExist:
601 throw new InvalidOperationException("Event handler is not exist");
605 s_eventObjects.Remove(this);
606 s_lifecycleEventRefCnt[Id]--;
609 private static void OnLifecycleEvent(string widgetId, Interop.WidgetService.LifecycleEvent e, string instanceId, IntPtr userData)
613 case Interop.WidgetService.LifecycleEvent.Created:
614 foreach (WidgetControl c in s_eventObjects)
616 if (c.Id.CompareTo(widgetId) == 0)
618 c._created?.Invoke(null, new WidgetLifecycleEventArgs()
621 InstanceId = instanceId,
622 Type = WidgetLifecycleEventArgs.EventType.Created
628 case Interop.WidgetService.LifecycleEvent.Resumed:
629 foreach (WidgetControl c in s_eventObjects)
631 if (c.Id.CompareTo(widgetId) == 0)
633 c._resumed?.Invoke(null, new WidgetLifecycleEventArgs()
636 InstanceId = instanceId,
637 Type = WidgetLifecycleEventArgs.EventType.Resumed
643 case Interop.WidgetService.LifecycleEvent.Paused:
644 foreach (WidgetControl c in s_eventObjects)
646 if (c.Id.CompareTo(widgetId) == 0)
648 c._paused?.Invoke(null, new WidgetLifecycleEventArgs()
651 InstanceId = instanceId,
652 Type = WidgetLifecycleEventArgs.EventType.Paused
658 case Interop.WidgetService.LifecycleEvent.Destroyed:
659 foreach (WidgetControl c in s_eventObjects)
661 if (c.Id.CompareTo(widgetId) == 0)
663 c._destroyed?.Invoke(null, new WidgetLifecycleEventArgs()
666 InstanceId = instanceId,
667 Type = WidgetLifecycleEventArgs.EventType.Destroyed