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;
20 using System.Runtime.InteropServices;
22 namespace Tizen.Applications
25 /// The class for receiving widget events and sending data to the widget.
27 /// <since_tizen> 3 </since_tizen>
28 public class WidgetControl : IDisposable
30 private const string LogTag = "Tizen.Applications.WidgetControl";
31 private static Interop.WidgetService.LifecycleCallback _onLifecycleCallback;
33 /// Class for the widget instance.
35 /// <since_tizen> 3 </since_tizen>
38 private string _widgetId;
40 internal Instance(string widgetId)
48 /// <since_tizen> 3 </since_tizen>
49 public string Id { get; internal set; }
52 /// Gets the widget content.
54 /// <returns>The bundle containing widget content.</returns>
55 /// <since_tizen> 3 </since_tizen>
56 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
57 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
58 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
59 public Bundle GetContent()
63 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetContent(_widgetId, Id, out h);
67 case Interop.WidgetService.ErrorCode.InvalidParameter:
68 throw new InvalidOperationException("Invalid parameter at unmanaged code");
69 case Interop.WidgetService.ErrorCode.IoError:
70 throw new InvalidOperationException("Failed to access DB");
71 case Interop.WidgetService.ErrorCode.NotSupported:
72 throw new NotSupportedException("Not supported");
75 return new Bundle(new SafeBundleHandle(h, true));
79 /// Changes the content for the widget instance.
81 /// <since_tizen> 3 </since_tizen>
82 /// <param name="content">Content to be changed.</param>
83 /// <param name="force"> True if you want to update your widget even if the provider is paused, otherwise false.</param>
84 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
85 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
86 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
87 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
88 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
89 public void ChangeContent(Bundle content, bool force)
91 Interop.WidgetService.ErrorCode err = Interop.WidgetService.UpdateContent(_widgetId, Id, content.SafeBundleHandle, force ? 1 : 0);
95 case Interop.WidgetService.ErrorCode.InvalidParameter:
96 throw new ArgumentException("Invalid parameter");
98 case Interop.WidgetService.ErrorCode.Canceled:
99 throw new InvalidOperationException("Provider is paused, so this update request is canceld");
101 case Interop.WidgetService.ErrorCode.OutOfMemory:
102 throw new InvalidOperationException("Out-of-memory at unmanaged code");
104 case Interop.WidgetService.ErrorCode.Fault:
105 throw new InvalidOperationException("Failed to create a request packet");
107 case Interop.WidgetService.ErrorCode.PermissionDenied:
108 throw new UnauthorizedAccessException();
110 case Interop.WidgetService.ErrorCode.NotSupported:
111 throw new NotSupportedException("Not supported");
116 /// Changes the update period for the widget instance.
118 /// <since_tizen> 3 </since_tizen>
119 /// <param name="period">The period to be changed.</param>
120 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
121 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
122 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
123 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
124 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
125 public void ChangePeriod(double period)
127 Interop.WidgetService.ErrorCode err = Interop.WidgetService.ChangePeriod(_widgetId, Id, period);
131 case Interop.WidgetService.ErrorCode.InvalidParameter:
132 throw new ArgumentException("Invalid parameter");
134 case Interop.WidgetService.ErrorCode.OutOfMemory:
135 throw new InvalidOperationException("Out-of-memory at unmanaged code");
137 case Interop.WidgetService.ErrorCode.Fault:
138 throw new InvalidOperationException("Failed to create a request packet");
140 case Interop.WidgetService.ErrorCode.PermissionDenied:
141 throw new UnauthorizedAccessException();
143 case Interop.WidgetService.ErrorCode.NotSupported:
144 throw new NotSupportedException("Not supported");
150 /// The class for the widget size information.
152 /// <since_tizen> 3 </since_tizen>
161 /// Enumeration for the types of widget size.
163 /// <since_tizen> 3 </since_tizen>
164 public enum SizeType : int
167 /// 175x175 based on 720x1280 resolution.
172 /// 354x175 based on 720x1280 resolution.
177 /// 354x354 based on 720x1280 resolution.
182 /// 712x175 based on 720x1280 resolution.
187 /// 712x354 based on 720x1280 resolution.
192 /// 712x533 based on 720x1280 resolution.
197 /// 712x712 based on 720x1280 resolution.
202 /// 712x891 based on 720x1280 resolution.
207 /// 712x1070 based on 720x1280 resolution.
213 /// 224x215 based on 720x1280 resolution.
218 /// 680x215 based on 720x1280 resolution.
223 /// 680x653 based on 720x1280 resolution.
228 /// 720x1280 based on 720x1280 resolution.
236 /// <since_tizen> 3 </since_tizen>
237 public int Width { get; internal set; }
242 /// <since_tizen> 3 </since_tizen>
243 public int Height { get; internal set; }
246 /// The path for the widget preview image file.
248 /// <since_tizen> 3 </since_tizen>
249 public string PreviewImagePath { get; internal set; }
252 /// The size type of the widget.
254 /// <since_tizen> 3 </since_tizen>
255 public SizeType Type { get; internal set; }
258 private event EventHandler<WidgetLifecycleEventArgs> _created;
259 private event EventHandler<WidgetLifecycleEventArgs> _resumed;
260 private event EventHandler<WidgetLifecycleEventArgs> _paused;
261 private event EventHandler<WidgetLifecycleEventArgs> _destroyed;
262 private bool _disposedValue = false;
263 private static IDictionary<string, int> s_lifecycleEventRefCnt = new Dictionary<string, int>();
264 private static IList<WidgetControl> s_eventObjects = new List<WidgetControl>();
267 /// Factory method for the WidgetControl.
268 /// It will create all the objects of WidgetControl based on the package ID.
270 /// <since_tizen> 3 </since_tizen>
271 /// <param name="pkgId">Package ID.</param>
272 /// <returns>The widget control array.</returns>
273 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
274 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
275 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
276 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
277 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
278 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
279 public static WidgetControl[] CreateAll(string pkgId)
281 List<WidgetControl> list = new List<WidgetControl>();
283 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetWidgetListByPkgId(pkgId, (widgetId, isPrime, userData) =>
285 list.Add(new WidgetControl(widgetId));
290 case Interop.WidgetService.ErrorCode.InvalidParameter:
291 throw new ArgumentException("Invalid parameter");
293 case Interop.WidgetService.ErrorCode.IoError:
294 throw new InvalidOperationException("Failed to access DB");
296 case Interop.WidgetService.ErrorCode.PermissionDenied:
297 throw new UnauthorizedAccessException();
299 case Interop.WidgetService.ErrorCode.NotSupported:
300 throw new NotSupportedException("Not supported");
303 return list.ToArray();
307 /// Gets all the widget IDs by the package ID.
309 /// <since_tizen> 3 </since_tizen>
310 /// <param name="pkgId">Package ID.</param>
311 /// <returns>The widget id array.</returns>
312 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
313 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
314 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
315 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
316 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
317 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
318 public static string[] GetWidgetIds(string pkgId)
320 List<string> list = new List<string>();
322 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetWidgetListByPkgId(pkgId, (widgetId, isPrime, userData) =>
329 case Interop.WidgetService.ErrorCode.InvalidParameter:
330 throw new ArgumentException("Invalid parameter");
332 case Interop.WidgetService.ErrorCode.IoError:
333 throw new InvalidOperationException("Failed to access DB");
335 case Interop.WidgetService.ErrorCode.PermissionDenied:
336 throw new UnauthorizedAccessException();
338 case Interop.WidgetService.ErrorCode.NotSupported:
339 throw new NotSupportedException("Not supported");
342 return list.ToArray();
346 /// Gets main appid of the widget.
348 /// <since_tizen> 6 </since_tizen>
349 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
350 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
351 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
352 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
353 public string MainAppId
357 string str = Interop.WidgetService.GetWidgetMainAppId(Id);
358 Interop.WidgetService.ErrorCode err =
359 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
362 case Interop.WidgetService.ErrorCode.PermissionDenied:
363 throw new UnauthorizedAccessException();
365 case Interop.WidgetService.ErrorCode.NotSupported:
366 throw new NotSupportedException("Not supported");
374 /// Gets package ID of the widget.
376 /// <since_tizen> 6 </since_tizen>
377 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
378 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
379 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
380 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
381 public string PackageId
385 string str = Interop.WidgetService.GetWidgetPackageId(Id);
386 Interop.WidgetService.ErrorCode err =
387 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
390 case Interop.WidgetService.ErrorCode.PermissionDenied:
391 throw new UnauthorizedAccessException();
393 case Interop.WidgetService.ErrorCode.NotSupported:
394 throw new NotSupportedException("Not supported");
404 /// <since_tizen> 3 </since_tizen>
405 public string Id { get; internal set; }
408 /// The flag value for "nodisplay".
410 /// <since_tizen> 3 </since_tizen>
411 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
412 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
413 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
414 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
415 public bool IsNoDisplay
419 int ret = Interop.WidgetService.GetNoDisplay(Id);
420 Interop.WidgetService.ErrorCode err =
421 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
424 case Interop.WidgetService.ErrorCode.PermissionDenied:
425 throw new UnauthorizedAccessException();
427 case Interop.WidgetService.ErrorCode.NotSupported:
428 throw new NotSupportedException("Not supported");
439 /// Gets setup app ID of the widget.
441 /// <since_tizen> 8 </since_tizen>
442 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
443 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
444 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
445 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
446 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
447 public string SetupAppId
451 string str = Interop.WidgetService.GetSetupAppId(Id);
452 Interop.WidgetService.ErrorCode err =
453 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
456 case Interop.WidgetService.ErrorCode.PermissionDenied:
457 throw new UnauthorizedAccessException();
459 case Interop.WidgetService.ErrorCode.NotSupported:
460 throw new NotSupportedException("Not supported");
462 case Interop.WidgetService.ErrorCode.InvalidParameter:
463 throw new InvalidOperationException("Invalid parameter at unmanaged code");
465 case Interop.WidgetService.ErrorCode.IoError:
466 throw new InvalidOperationException("Failed to access DB");
468 case Interop.WidgetService.ErrorCode.Fault:
469 throw new InvalidOperationException("Failed to access DB");
471 case Interop.WidgetService.ErrorCode.NotExist:
472 throw new InvalidOperationException("Not exist in DB");
480 /// The event handler for a created widget instance.
482 /// <since_tizen> 3 </since_tizen>
483 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
484 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
485 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
486 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
487 public event EventHandler<WidgetLifecycleEventArgs> Created
491 RegisterLifecycleEvent();
497 UnregisterLifecycleEvent();
502 /// The event handler for a resumed widget instance.
504 /// <since_tizen> 3 </since_tizen>
505 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
506 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
507 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
508 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
509 public event EventHandler<WidgetLifecycleEventArgs> Resumed
513 RegisterLifecycleEvent();
519 UnregisterLifecycleEvent();
524 /// The event handler for a paused widget instance.
526 /// <since_tizen> 3 </since_tizen>
527 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
528 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
529 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
530 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
531 public event EventHandler<WidgetLifecycleEventArgs> Paused
535 RegisterLifecycleEvent();
541 UnregisterLifecycleEvent();
546 /// The event handler for a destroyed widget instance.
548 /// <since_tizen> 3 </since_tizen>
549 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
550 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
551 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
552 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
553 public event EventHandler<WidgetLifecycleEventArgs> Destroyed
557 RegisterLifecycleEvent();
563 UnregisterLifecycleEvent();
568 /// The constructor of the WidgetControl object.
570 /// <since_tizen> 3 </since_tizen>
571 /// <param name="widgetId">Widget ID.</param>
572 public WidgetControl(string widgetId)
578 /// Finalizer of the WidgetControl class.
586 /// Gets the objects for widget instance information.
588 /// <returns>The instances list.</returns>
589 /// <since_tizen> 3 </since_tizen>
590 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
591 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
592 /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
593 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
594 public IEnumerable<Instance> GetInstances()
596 IList<Instance> instances = new List<Instance>();
597 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetInstances(Id, (widgetId, instanceId, userData) =>
599 instances.Add(new Instance(widgetId) { Id = instanceId });
604 case Interop.WidgetService.ErrorCode.InvalidParameter:
605 throw new InvalidOperationException("Invalid parameter at unmanaged code");
607 case Interop.WidgetService.ErrorCode.NotSupported:
608 throw new NotSupportedException();
610 case Interop.WidgetService.ErrorCode.PermissionDenied:
611 throw new UnauthorizedAccessException();
618 /// Gets the objects for widget scale information.
620 /// <returns>The scales list.</returns>
621 /// <since_tizen> 3 </since_tizen>
622 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
623 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
624 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
625 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
626 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
627 public IEnumerable<Scale> GetScales()
637 IList<Scale> scales = new List<Scale>();
638 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetSupportedSizes(Id, ref cnt1, out wPtr, out hPtr);
642 Log.Error(LogTag, "No supported size :" + Id);
648 case Interop.WidgetService.ErrorCode.InvalidParameter:
649 throw new InvalidOperationException("Invalid parameter at unmanaged code");
651 case Interop.WidgetService.ErrorCode.IoError:
652 throw new InvalidOperationException("Failed to access DB");
654 case Interop.WidgetService.ErrorCode.PermissionDenied:
655 throw new UnauthorizedAccessException();
657 case Interop.WidgetService.ErrorCode.NotSupported:
658 throw new NotSupportedException();
661 Marshal.Copy(wPtr, w, 0, cnt1);
662 Interop.Libc.Free(wPtr);
665 Marshal.Copy(hPtr, h, 0, cnt1);
666 Interop.Libc.Free(hPtr);
668 err = Interop.WidgetService.GetSupportedSizeTypes(Id, ref cnt2, out typesPtr);
671 case Interop.WidgetService.ErrorCode.InvalidParameter:
672 throw new InvalidOperationException("Invalid parameter at unmanaged code");
674 case Interop.WidgetService.ErrorCode.IoError:
675 throw new InvalidOperationException("Failed to access DB");
677 case Interop.WidgetService.ErrorCode.PermissionDenied:
678 throw new UnauthorizedAccessException();
683 Log.Error(LogTag, "Count not match cnt1 :" + cnt1 + ", cnt2 :" + cnt2);
687 types = new int[cnt2];
688 Marshal.Copy(typesPtr, types, 0, cnt2);
689 Interop.Libc.Free(typesPtr);
691 for (int i = 0; i < cnt1; i++)
693 string prev = Interop.WidgetService.GetPreviewImagePath(Id, types[i]);
695 scales.Add(new Scale()
699 PreviewImagePath = prev,
700 Type = (Scale.SizeType)types[i]
707 /// Gets the widget name.
709 /// <since_tizen> 3 </since_tizen>
710 /// <param name="lang">Language.</param>
711 /// <returns>The widget name.</returns>
712 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
713 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
714 /// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
715 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
716 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
717 public string GetName(string lang)
720 throw new ArgumentNullException();
722 string str = Interop.WidgetService.GetName(Id, lang);
723 Interop.WidgetService.ErrorCode err =
724 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
728 case Interop.WidgetService.ErrorCode.PermissionDenied:
729 throw new UnauthorizedAccessException();
731 case Interop.WidgetService.ErrorCode.NotSupported:
732 throw new NotSupportedException();
739 /// Gets the widget icon path.
741 /// <since_tizen> 3 </since_tizen>
742 /// <param name="lang">Language.</param>
743 /// <returns>The widget icon path.</returns>
744 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
745 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
746 /// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
747 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
748 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
749 public string GetIconPath(string lang)
752 throw new ArgumentNullException();
754 string pkgId = Interop.WidgetService.GetPkgId(Id);
755 string str = Interop.WidgetService.GetIcon(pkgId, lang);
756 Interop.WidgetService.ErrorCode err =
757 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
761 case Interop.WidgetService.ErrorCode.PermissionDenied:
762 throw new UnauthorizedAccessException();
764 case Interop.WidgetService.ErrorCode.NotSupported:
765 throw new NotSupportedException();
772 /// Releases all the resources used by the WidgetControl class.
774 /// <since_tizen> 3 </since_tizen>
775 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
776 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
777 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
778 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
779 public void Dispose()
782 GC.SuppressFinalize(this);
785 private void Dispose(bool disposing)
797 UnregisterLifecycleEvent();
799 _disposedValue = true;
803 private void RegisterLifecycleEvent()
805 if (!s_lifecycleEventRefCnt.ContainsKey(Id))
806 s_lifecycleEventRefCnt[Id] = 0;
808 if (_created != null || _paused != null || _resumed != null || _destroyed != null)
811 if (s_lifecycleEventRefCnt[Id] == 0)
813 if (_onLifecycleCallback == null)
814 _onLifecycleCallback = new Interop.WidgetService.LifecycleCallback(OnLifecycleEvent);
816 Interop.WidgetService.ErrorCode err = Interop.WidgetService.SetLifecycleEvent(Id, _onLifecycleCallback, IntPtr.Zero);
819 case Interop.WidgetService.ErrorCode.InvalidParameter:
820 throw new InvalidOperationException("Invalid parameter at unmanaged code");
822 case Interop.WidgetService.ErrorCode.PermissionDenied:
823 throw new UnauthorizedAccessException();
825 case Interop.WidgetService.ErrorCode.NotSupported:
826 throw new NotSupportedException();
830 s_lifecycleEventRefCnt[Id]++;
831 s_eventObjects.Add(this);
832 Log.Debug(LogTag, "register lifecycle cb " + Id + " [" + s_lifecycleEventRefCnt[Id] + "]");
835 private void UnregisterLifecycleEvent()
837 if (!s_lifecycleEventRefCnt.ContainsKey(Id))
840 if (s_lifecycleEventRefCnt[Id] <= 0)
843 if (_created != null || _paused != null || _resumed != null || _destroyed != null)
846 if (s_lifecycleEventRefCnt[Id] == 1)
848 Interop.WidgetService.ErrorCode err = Interop.WidgetService.UnsetLifecycleEvent(Id, IntPtr.Zero);
852 case Interop.WidgetService.ErrorCode.InvalidParameter:
853 throw new InvalidOperationException("Invalid parameter at unmanaged code");
855 case Interop.WidgetService.ErrorCode.PermissionDenied:
856 throw new UnauthorizedAccessException();
858 case Interop.WidgetService.ErrorCode.NotExist:
859 throw new InvalidOperationException("Event handler is not exist");
861 case Interop.WidgetService.ErrorCode.NotSupported:
862 throw new NotSupportedException();
864 _onLifecycleCallback = null;
867 s_eventObjects.Remove(this);
868 s_lifecycleEventRefCnt[Id]--;
869 Log.Debug(LogTag, "unregister lifecycle cb " + Id + " [" + s_lifecycleEventRefCnt[Id] + "]");
872 private static int OnLifecycleEvent(string widgetId, Interop.WidgetService.LifecycleEvent e, string instanceId, IntPtr userData)
874 Log.Debug(LogTag, "Lifecycle event : " + instanceId + " [" + e + "]");
877 case Interop.WidgetService.LifecycleEvent.Created:
878 foreach (WidgetControl c in s_eventObjects)
880 if (c.Id.CompareTo(widgetId) == 0)
882 c._created?.Invoke(null, new WidgetLifecycleEventArgs()
885 InstanceId = instanceId,
886 Type = WidgetLifecycleEventArgs.EventType.Created
892 case Interop.WidgetService.LifecycleEvent.Resumed:
893 foreach (WidgetControl c in s_eventObjects)
895 if (c.Id.CompareTo(widgetId) == 0)
897 c._resumed?.Invoke(null, new WidgetLifecycleEventArgs()
900 InstanceId = instanceId,
901 Type = WidgetLifecycleEventArgs.EventType.Resumed
907 case Interop.WidgetService.LifecycleEvent.Paused:
908 foreach (WidgetControl c in s_eventObjects)
910 if (c.Id.CompareTo(widgetId) == 0)
912 c._paused?.Invoke(null, new WidgetLifecycleEventArgs()
915 InstanceId = instanceId,
916 Type = WidgetLifecycleEventArgs.EventType.Paused
922 case Interop.WidgetService.LifecycleEvent.Destroyed:
923 foreach (WidgetControl c in s_eventObjects)
925 if (c.Id.CompareTo(widgetId) == 0)
927 c._destroyed?.Invoke(null, new WidgetLifecycleEventArgs()
930 InstanceId = instanceId,
931 Type = WidgetLifecycleEventArgs.EventType.Destroyed