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;
21 using System.ComponentModel;
23 namespace Tizen.Applications
26 /// The class for receiving widget events and sending data to the widget.
28 /// <since_tizen> 3 </since_tizen>
29 public class WidgetControl : IDisposable
31 private const string LogTag = "Tizen.Applications.WidgetControl";
32 private static Interop.WidgetService.LifecycleCallback _onLifecycleCallback;
34 /// Class for the widget instance.
36 /// <since_tizen> 3 </since_tizen>
39 private string _widgetId;
41 internal Instance(string widgetId)
49 /// <since_tizen> 3 </since_tizen>
50 public string Id { get; internal set; }
53 /// Gets the widget content.
55 /// <returns>The bundle containing widget content.</returns>
56 /// <since_tizen> 3 </since_tizen>
57 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
58 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
59 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
60 public Bundle GetContent()
64 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetContent(_widgetId, Id, out h);
68 case Interop.WidgetService.ErrorCode.InvalidParameter:
69 throw new InvalidOperationException("Invalid parameter at unmanaged code");
70 case Interop.WidgetService.ErrorCode.IoError:
71 throw new InvalidOperationException("Failed to access DB");
72 case Interop.WidgetService.ErrorCode.NotSupported:
73 throw new NotSupportedException("Not supported");
76 return new Bundle(new SafeBundleHandle(h, true));
80 /// Changes the content for the widget instance.
82 /// <since_tizen> 3 </since_tizen>
83 /// <param name="content">Content to be changed.</param>
84 /// <param name="force"> True if you want to update your widget even if the provider is paused, otherwise false.</param>
85 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
86 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
87 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
88 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
89 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
90 public void ChangeContent(Bundle content, bool force)
92 Interop.WidgetService.ErrorCode err = Interop.WidgetService.UpdateContent(_widgetId, Id, content.SafeBundleHandle, force ? 1 : 0);
96 case Interop.WidgetService.ErrorCode.InvalidParameter:
97 throw new ArgumentException("Invalid parameter");
99 case Interop.WidgetService.ErrorCode.Canceled:
100 throw new InvalidOperationException("Provider is paused, so this update request is canceld");
102 case Interop.WidgetService.ErrorCode.OutOfMemory:
103 throw new InvalidOperationException("Out-of-memory at unmanaged code");
105 case Interop.WidgetService.ErrorCode.Fault:
106 throw new InvalidOperationException("Failed to create a request packet");
108 case Interop.WidgetService.ErrorCode.PermissionDenied:
109 throw new UnauthorizedAccessException();
111 case Interop.WidgetService.ErrorCode.NotSupported:
112 throw new NotSupportedException("Not supported");
117 /// Changes the update period for the widget instance.
119 /// <since_tizen> 3 </since_tizen>
120 /// <param name="period">The period to be changed.</param>
121 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
122 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
123 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
124 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
125 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
126 public void ChangePeriod(double period)
128 Interop.WidgetService.ErrorCode err = Interop.WidgetService.ChangePeriod(_widgetId, Id, period);
132 case Interop.WidgetService.ErrorCode.InvalidParameter:
133 throw new ArgumentException("Invalid parameter");
135 case Interop.WidgetService.ErrorCode.OutOfMemory:
136 throw new InvalidOperationException("Out-of-memory at unmanaged code");
138 case Interop.WidgetService.ErrorCode.Fault:
139 throw new InvalidOperationException("Failed to create a request packet");
141 case Interop.WidgetService.ErrorCode.PermissionDenied:
142 throw new UnauthorizedAccessException();
144 case Interop.WidgetService.ErrorCode.NotSupported:
145 throw new NotSupportedException("Not supported");
151 /// The class for the widget size information.
153 /// <since_tizen> 3 </since_tizen>
162 /// Enumeration for the types of widget size.
164 /// <since_tizen> 3 </since_tizen>
165 public enum SizeType : int
168 /// 175x175 based on 720x1280 resolution.
173 /// 354x175 based on 720x1280 resolution.
178 /// 354x354 based on 720x1280 resolution.
183 /// 712x175 based on 720x1280 resolution.
188 /// 712x354 based on 720x1280 resolution.
193 /// 712x533 based on 720x1280 resolution.
198 /// 712x712 based on 720x1280 resolution.
203 /// 712x891 based on 720x1280 resolution.
208 /// 712x1070 based on 720x1280 resolution.
214 /// 224x215 based on 720x1280 resolution.
219 /// 680x215 based on 720x1280 resolution.
224 /// 680x653 based on 720x1280 resolution.
229 /// 720x1280 based on 720x1280 resolution.
237 /// <since_tizen> 3 </since_tizen>
238 public int Width { get; internal set; }
243 /// <since_tizen> 3 </since_tizen>
244 public int Height { get; internal set; }
247 /// The path for the widget preview image file.
249 /// <since_tizen> 3 </since_tizen>
250 public string PreviewImagePath { get; internal set; }
253 /// The size type of the widget.
255 /// <since_tizen> 3 </since_tizen>
256 public SizeType Type { get; internal set; }
259 private event EventHandler<WidgetLifecycleEventArgs> _created;
260 private event EventHandler<WidgetLifecycleEventArgs> _resumed;
261 private event EventHandler<WidgetLifecycleEventArgs> _paused;
262 private event EventHandler<WidgetLifecycleEventArgs> _destroyed;
263 private bool _disposedValue = false;
264 private static IDictionary<string, int> s_lifecycleEventRefCnt = new Dictionary<string, int>();
265 private static IList<WidgetControl> s_eventObjects = new List<WidgetControl>();
268 /// Factory method for the WidgetControl.
269 /// It will create all the objects of WidgetControl based on the package ID.
271 /// <since_tizen> 3 </since_tizen>
272 /// <param name="pkgId">Package ID.</param>
273 /// <returns>The widget control array.</returns>
274 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
275 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
276 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
277 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
278 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
279 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
280 public static WidgetControl[] CreateAll(string pkgId)
282 List<WidgetControl> list = new List<WidgetControl>();
284 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetWidgetListByPkgId(pkgId, (widgetId, isPrime, userData) =>
286 list.Add(new WidgetControl(widgetId));
288 return (int)Interop.WidgetService.ErrorCode.None;
293 case Interop.WidgetService.ErrorCode.InvalidParameter:
294 throw new ArgumentException("Invalid parameter");
296 case Interop.WidgetService.ErrorCode.IoError:
297 throw new InvalidOperationException("Failed to access DB");
299 case Interop.WidgetService.ErrorCode.PermissionDenied:
300 throw new UnauthorizedAccessException();
302 case Interop.WidgetService.ErrorCode.NotSupported:
303 throw new NotSupportedException("Not supported");
306 return list.ToArray();
310 /// Gets all the widget IDs by the package ID.
312 /// <since_tizen> 3 </since_tizen>
313 /// <param name="pkgId">Package ID.</param>
314 /// <returns>The widget id array.</returns>
315 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
316 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
317 /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
318 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
319 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
320 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
321 public static string[] GetWidgetIds(string pkgId)
323 List<string> list = new List<string>();
325 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetWidgetListByPkgId(pkgId, (widgetId, isPrime, userData) =>
329 return (int)Interop.WidgetService.ErrorCode.None;
334 case Interop.WidgetService.ErrorCode.InvalidParameter:
335 throw new ArgumentException("Invalid parameter");
337 case Interop.WidgetService.ErrorCode.IoError:
338 throw new InvalidOperationException("Failed to access DB");
340 case Interop.WidgetService.ErrorCode.PermissionDenied:
341 throw new UnauthorizedAccessException();
343 case Interop.WidgetService.ErrorCode.NotSupported:
344 throw new NotSupportedException("Not supported");
347 return list.ToArray();
351 /// Gets main appid of the widget.
353 /// <since_tizen> 6 </since_tizen>
354 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
355 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
356 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
357 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
358 public string MainAppId
362 string str = Interop.WidgetService.GetWidgetMainAppId(Id);
363 Interop.WidgetService.ErrorCode err =
364 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
367 case Interop.WidgetService.ErrorCode.PermissionDenied:
368 throw new UnauthorizedAccessException();
370 case Interop.WidgetService.ErrorCode.NotSupported:
371 throw new NotSupportedException("Not supported");
379 /// Gets package ID of the widget.
381 /// <since_tizen> 6 </since_tizen>
382 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
383 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
384 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
385 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
386 public string PackageId
390 string str = Interop.WidgetService.GetWidgetPackageId(Id);
391 Interop.WidgetService.ErrorCode err =
392 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
395 case Interop.WidgetService.ErrorCode.PermissionDenied:
396 throw new UnauthorizedAccessException();
398 case Interop.WidgetService.ErrorCode.NotSupported:
399 throw new NotSupportedException("Not supported");
409 /// <since_tizen> 3 </since_tizen>
410 public string Id { get; internal set; }
413 /// The flag value for "nodisplay".
415 /// <since_tizen> 3 </since_tizen>
416 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
417 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
418 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
419 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
420 public bool IsNoDisplay
424 int ret = Interop.WidgetService.GetNoDisplay(Id);
425 Interop.WidgetService.ErrorCode err =
426 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
429 case Interop.WidgetService.ErrorCode.PermissionDenied:
430 throw new UnauthorizedAccessException();
432 case Interop.WidgetService.ErrorCode.NotSupported:
433 throw new NotSupportedException("Not supported");
444 /// Gets setup app ID of the widget.
446 /// <since_tizen> 8 </since_tizen>
447 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
448 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
449 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
450 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
451 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
452 public string SetupAppId
456 string str = Interop.WidgetService.GetSetupAppId(Id);
457 Interop.WidgetService.ErrorCode err =
458 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
461 case Interop.WidgetService.ErrorCode.PermissionDenied:
462 throw new UnauthorizedAccessException();
464 case Interop.WidgetService.ErrorCode.NotSupported:
465 throw new NotSupportedException("Not supported");
467 case Interop.WidgetService.ErrorCode.InvalidParameter:
468 throw new InvalidOperationException("Invalid parameter at unmanaged code");
470 case Interop.WidgetService.ErrorCode.IoError:
471 throw new InvalidOperationException("Failed to access DB");
473 case Interop.WidgetService.ErrorCode.Fault:
474 throw new InvalidOperationException("Failed to access DB");
476 case Interop.WidgetService.ErrorCode.NotExist:
477 throw new InvalidOperationException("Not exist in DB");
485 /// Gets widget max count
487 /// <since_tizen> 8 </since_tizen>
488 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
489 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
490 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
491 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
492 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
493 [EditorBrowsable(EditorBrowsableState.Never)]
498 int ret = Interop.WidgetService.GetWidgetMaxCount(Id);
500 switch ((Interop.WidgetService.ErrorCode)ret)
502 case Interop.WidgetService.ErrorCode.NotSupported:
503 throw new NotSupportedException();
505 case Interop.WidgetService.ErrorCode.PermissionDenied:
506 throw new UnauthorizedAccessException();
508 case Interop.WidgetService.ErrorCode.InvalidParameter:
509 throw new InvalidOperationException("Invalid parameter");
511 case Interop.WidgetService.ErrorCode.IoError:
512 throw new InvalidOperationException("Failed to access DB");
514 case Interop.WidgetService.ErrorCode.Fault:
515 throw new InvalidOperationException("Failed to get information from DB");
517 case Interop.WidgetService.ErrorCode.NotExist:
518 throw new InvalidOperationException("The data does not exist");
526 /// The event handler for a created widget instance.
528 /// <since_tizen> 3 </since_tizen>
529 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
530 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
531 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
532 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
533 public event EventHandler<WidgetLifecycleEventArgs> Created
537 RegisterLifecycleEvent();
543 UnregisterLifecycleEvent();
548 /// The event handler for a resumed widget instance.
550 /// <since_tizen> 3 </since_tizen>
551 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
552 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
553 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
554 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
555 public event EventHandler<WidgetLifecycleEventArgs> Resumed
559 RegisterLifecycleEvent();
565 UnregisterLifecycleEvent();
570 /// The event handler for a paused widget instance.
572 /// <since_tizen> 3 </since_tizen>
573 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
574 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
575 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
576 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
577 public event EventHandler<WidgetLifecycleEventArgs> Paused
581 RegisterLifecycleEvent();
587 UnregisterLifecycleEvent();
592 /// The event handler for a destroyed widget instance.
594 /// <since_tizen> 3 </since_tizen>
595 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
596 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
597 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
598 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
599 public event EventHandler<WidgetLifecycleEventArgs> Destroyed
603 RegisterLifecycleEvent();
609 UnregisterLifecycleEvent();
614 /// The constructor of the WidgetControl object.
616 /// <since_tizen> 3 </since_tizen>
617 /// <param name="widgetId">Widget ID.</param>
618 public WidgetControl(string widgetId)
624 /// Finalizer of the WidgetControl class.
632 /// Gets the objects for widget instance information.
634 /// <returns>The instances list.</returns>
635 /// <since_tizen> 3 </since_tizen>
636 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
637 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
638 /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
639 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
640 public IEnumerable<Instance> GetInstances()
642 IList<Instance> instances = new List<Instance>();
643 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetInstances(Id, (widgetId, instanceId, userData) =>
645 instances.Add(new Instance(widgetId) { Id = instanceId });
647 return (int)Interop.WidgetService.ErrorCode.None;
652 case Interop.WidgetService.ErrorCode.InvalidParameter:
653 throw new InvalidOperationException("Invalid parameter at unmanaged code");
655 case Interop.WidgetService.ErrorCode.NotSupported:
656 throw new NotSupportedException();
658 case Interop.WidgetService.ErrorCode.PermissionDenied:
659 throw new UnauthorizedAccessException();
666 /// Gets the objects for widget scale information.
668 /// <returns>The scales list.</returns>
669 /// <since_tizen> 3 </since_tizen>
670 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
671 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
672 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
673 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
674 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
675 public IEnumerable<Scale> GetScales()
685 IList<Scale> scales = new List<Scale>();
686 Interop.WidgetService.ErrorCode err = Interop.WidgetService.GetSupportedSizes(Id, ref cnt1, out wPtr, out hPtr);
690 Log.Error(LogTag, "No supported size :" + Id);
696 case Interop.WidgetService.ErrorCode.InvalidParameter:
697 throw new InvalidOperationException("Invalid parameter at unmanaged code");
699 case Interop.WidgetService.ErrorCode.IoError:
700 throw new InvalidOperationException("Failed to access DB");
702 case Interop.WidgetService.ErrorCode.PermissionDenied:
703 throw new UnauthorizedAccessException();
705 case Interop.WidgetService.ErrorCode.NotSupported:
706 throw new NotSupportedException();
709 Marshal.Copy(wPtr, w, 0, cnt1);
710 Interop.Libc.Free(wPtr);
713 Marshal.Copy(hPtr, h, 0, cnt1);
714 Interop.Libc.Free(hPtr);
716 err = Interop.WidgetService.GetSupportedSizeTypes(Id, ref cnt2, out typesPtr);
719 case Interop.WidgetService.ErrorCode.InvalidParameter:
720 throw new InvalidOperationException("Invalid parameter at unmanaged code");
722 case Interop.WidgetService.ErrorCode.IoError:
723 throw new InvalidOperationException("Failed to access DB");
725 case Interop.WidgetService.ErrorCode.PermissionDenied:
726 throw new UnauthorizedAccessException();
731 Log.Error(LogTag, "Count not match cnt1 :" + cnt1 + ", cnt2 :" + cnt2);
735 types = new int[cnt2];
736 Marshal.Copy(typesPtr, types, 0, cnt2);
737 Interop.Libc.Free(typesPtr);
739 for (int i = 0; i < cnt1; i++)
741 string prev = Interop.WidgetService.GetPreviewImagePath(Id, types[i]);
743 scales.Add(new Scale()
747 PreviewImagePath = prev,
748 Type = (Scale.SizeType)types[i]
755 /// Gets the widget name.
757 /// <since_tizen> 3 </since_tizen>
758 /// <param name="lang">Language.</param>
759 /// <returns>The widget name.</returns>
760 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
761 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
762 /// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
763 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
764 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
765 public string GetName(string lang)
768 throw new ArgumentNullException();
770 string str = Interop.WidgetService.GetName(Id, lang);
771 Interop.WidgetService.ErrorCode err =
772 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
776 case Interop.WidgetService.ErrorCode.PermissionDenied:
777 throw new UnauthorizedAccessException();
779 case Interop.WidgetService.ErrorCode.NotSupported:
780 throw new NotSupportedException();
787 /// Gets the widget icon path.
789 /// <since_tizen> 3 </since_tizen>
790 /// <param name="lang">Language.</param>
791 /// <returns>The widget icon path.</returns>
792 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
793 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
794 /// <exception cref="ArgumentNullException">Thrown when the argument is null.</exception>
795 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
796 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
797 public string GetIconPath(string lang)
800 throw new ArgumentNullException();
802 string pkgId = Interop.WidgetService.GetPkgId(Id);
803 string str = Interop.WidgetService.GetIcon(pkgId, lang);
804 Interop.WidgetService.ErrorCode err =
805 (Interop.WidgetService.ErrorCode)Internals.Errors.ErrorFacts.GetLastResult();
809 case Interop.WidgetService.ErrorCode.PermissionDenied:
810 throw new UnauthorizedAccessException();
812 case Interop.WidgetService.ErrorCode.NotSupported:
813 throw new NotSupportedException();
820 /// Gets widget instance count
822 /// <since_tizen> 8 </since_tizen>
823 /// <returns>The widget instance count.</returns>
824 /// <privilege>http://tizen.org/privilege/widget.viewer</privilege>
825 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
826 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
827 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
828 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
829 [EditorBrowsable(EditorBrowsableState.Never)]
830 public int GetInstanceCount()
832 int ret = Interop.WidgetService.GetWidgetInstanceCount(Id, null, null);
834 switch ((Interop.WidgetService.ErrorCode)ret)
836 case Interop.WidgetService.ErrorCode.NotSupported:
837 throw new NotSupportedException();
839 case Interop.WidgetService.ErrorCode.InvalidParameter:
840 throw new InvalidOperationException("Invalid parameter");
842 case Interop.WidgetService.ErrorCode.PermissionDenied:
843 throw new UnauthorizedAccessException();
850 /// Releases all the resources used by the WidgetControl class.
852 /// <since_tizen> 3 </since_tizen>
853 /// <feature>http://tizen.org/feature/shell.appwidget</feature>
854 /// <exception cref="InvalidOperationException">Thrown in case of failed conditions.</exception>
855 /// <exception cref="UnauthorizedAccessException">Thrown when an application does not have the required privileges to access this method.</exception>
856 /// <exception cref="NotSupportedException">Thrown when the required features are not supported.</exception>
857 public void Dispose()
860 GC.SuppressFinalize(this);
863 private void Dispose(bool disposing)
875 UnregisterLifecycleEvent();
877 _disposedValue = true;
881 private void RegisterLifecycleEvent()
883 if (!s_lifecycleEventRefCnt.ContainsKey(Id))
884 s_lifecycleEventRefCnt[Id] = 0;
886 if (_created != null || _paused != null || _resumed != null || _destroyed != null)
889 if (s_lifecycleEventRefCnt[Id] == 0)
891 if (_onLifecycleCallback == null)
892 _onLifecycleCallback = new Interop.WidgetService.LifecycleCallback(OnLifecycleEvent);
894 Interop.WidgetService.ErrorCode err = Interop.WidgetService.SetLifecycleEvent(Id, _onLifecycleCallback, IntPtr.Zero);
897 case Interop.WidgetService.ErrorCode.InvalidParameter:
898 throw new InvalidOperationException("Invalid parameter at unmanaged code");
900 case Interop.WidgetService.ErrorCode.PermissionDenied:
901 throw new UnauthorizedAccessException();
903 case Interop.WidgetService.ErrorCode.NotSupported:
904 throw new NotSupportedException();
908 s_lifecycleEventRefCnt[Id]++;
909 s_eventObjects.Add(this);
910 Log.Debug(LogTag, "register lifecycle cb " + Id + " [" + s_lifecycleEventRefCnt[Id] + "]");
913 private void UnregisterLifecycleEvent()
915 if (!s_lifecycleEventRefCnt.ContainsKey(Id))
918 if (s_lifecycleEventRefCnt[Id] <= 0)
921 if (_created != null || _paused != null || _resumed != null || _destroyed != null)
924 if (s_lifecycleEventRefCnt[Id] == 1)
926 Interop.WidgetService.ErrorCode err = Interop.WidgetService.UnsetLifecycleEvent(Id, IntPtr.Zero);
930 case Interop.WidgetService.ErrorCode.InvalidParameter:
931 throw new InvalidOperationException("Invalid parameter at unmanaged code");
933 case Interop.WidgetService.ErrorCode.PermissionDenied:
934 throw new UnauthorizedAccessException();
936 case Interop.WidgetService.ErrorCode.NotExist:
937 throw new InvalidOperationException("Event handler is not exist");
939 case Interop.WidgetService.ErrorCode.NotSupported:
940 throw new NotSupportedException();
942 _onLifecycleCallback = null;
945 s_eventObjects.Remove(this);
946 s_lifecycleEventRefCnt[Id]--;
947 Log.Debug(LogTag, "unregister lifecycle cb " + Id + " [" + s_lifecycleEventRefCnt[Id] + "]");
950 private static int OnLifecycleEvent(string widgetId, Interop.WidgetService.LifecycleEvent e, string instanceId, IntPtr userData)
952 Log.Debug(LogTag, "Lifecycle event : " + instanceId + " [" + e + "]");
955 case Interop.WidgetService.LifecycleEvent.Created:
956 foreach (WidgetControl c in s_eventObjects)
958 if (c.Id.CompareTo(widgetId) == 0)
960 c._created?.Invoke(null, new WidgetLifecycleEventArgs()
963 InstanceId = instanceId,
964 Type = WidgetLifecycleEventArgs.EventType.Created
970 case Interop.WidgetService.LifecycleEvent.Resumed:
971 foreach (WidgetControl c in s_eventObjects)
973 if (c.Id.CompareTo(widgetId) == 0)
975 c._resumed?.Invoke(null, new WidgetLifecycleEventArgs()
978 InstanceId = instanceId,
979 Type = WidgetLifecycleEventArgs.EventType.Resumed
985 case Interop.WidgetService.LifecycleEvent.Paused:
986 foreach (WidgetControl c in s_eventObjects)
988 if (c.Id.CompareTo(widgetId) == 0)
990 c._paused?.Invoke(null, new WidgetLifecycleEventArgs()
993 InstanceId = instanceId,
994 Type = WidgetLifecycleEventArgs.EventType.Paused
1000 case Interop.WidgetService.LifecycleEvent.Destroyed:
1001 foreach (WidgetControl c in s_eventObjects)
1003 if (c.Id.CompareTo(widgetId) == 0)
1005 c._destroyed?.Invoke(null, new WidgetLifecycleEventArgs()
1007 WidgetId = widgetId,
1008 InstanceId = instanceId,
1009 Type = WidgetLifecycleEventArgs.EventType.Destroyed