2 * Copyright (c) 2023 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;
21 using Tizen.Applications;
22 using System.ComponentModel;
23 using System.Runtime.InteropServices;
24 using System.Reflection;
29 /// This class has the methods and events of the NUIGadgetManager.
31 /// <since_tizen> 10 </since_tizen>
32 [EditorBrowsable(EditorBrowsableState.Never)]
33 public static class NUIGadgetManager
35 private static readonly Dictionary<string, NUIGadgetInfo> _gadgetInfos = new Dictionary<string, NUIGadgetInfo>();
36 private static readonly List<NUIGadget> _gadgets = new List<NUIGadget>();
38 static NUIGadgetManager()
40 IntPtr resPkgIds = Interop.Libc.GetEnviornmentVariable("RES_PKGIDS");
41 if (resPkgIds != IntPtr.Zero)
43 string packages = Marshal.PtrToStringAnsi(resPkgIds);
44 if (string.IsNullOrEmpty(packages))
46 Log.Warn("There is no resource packages");
50 foreach (string packageId in packages.Split(':').ToList())
52 NUIGadgetInfo info = NUIGadgetInfo.CreateNUIGadgetInfo(packageId);
55 _gadgetInfos.Add(info.ResourceType, info);
62 Log.Warn("Failed to get environment variable");
65 var context = (CoreApplication)CoreApplication.Current;
66 context.AppControlReceived += OnAppControlReceived;
67 context.LowMemory += OnLowMemory;
68 context.LowBattery += OnLowBattery;
69 context.LocaleChanged += OnLocaleChanged;
70 context.RegionFormatChanged += OnRegionFormatChanged;
71 context.DeviceOrientationChanged += OnDeviceOrientationChanged;
74 private static void OnAppControlReceived(object sender, AppControlReceivedEventArgs args)
76 HandleAppControl(args);
79 private static void OnLowMemory(object sender, LowMemoryEventArgs args)
81 HandleEvents(NUIGadgetEventType.LowMemory, args);
84 private static void OnLowBattery(object sender, LowBatteryEventArgs args)
86 HandleEvents(NUIGadgetEventType.LowBattery, args);
89 private static void OnLocaleChanged(object sender, LocaleChangedEventArgs args)
91 HandleEvents(NUIGadgetEventType.LocaleChanged, args);
94 private static void OnRegionFormatChanged(object sender, RegionFormatChangedEventArgs args)
96 HandleEvents(NUIGadgetEventType.RegionFormatChanged, args);
99 private static void OnDeviceOrientationChanged(object sender, DeviceOrientationEventArgs args)
101 HandleEvents(NUIGadgetEventType.DeviceORientationChanged, args);
105 /// Occurs when the lifecycle of the NUIGadget is changed.
107 /// <since_tizen> 10 </since_tizen>
108 public static event EventHandler<NUIGadgetLifecycleChangedEventArgs> NUIGadgetLifecycleChanged;
110 private static void OnNUIGadgetLifecycleChanged(object sender, NUIGadgetLifecycleChangedEventArgs args)
112 NUIGadgetLifecycleChanged?.Invoke(sender, args);
114 if (args.State == NUIGadgetLifecycleState.Destroyed)
116 args.Gadget.LifecycleChanged -= OnNUIGadgetLifecycleChanged;
117 _gadgets.Remove(args.Gadget);
122 /// Adds a NUIGadget to a NUIGadgetManager.
124 /// <param name="resourceType">The resource type of the NUIGadget package.</param>
125 /// <param name="className">The class name of the NUIGadget.</param>
126 /// <returns>The NUIGadget object.</returns>
127 /// <exception cref="ArgumentException">Thrown when failed because of a invalid argument.</exception>
128 /// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
129 /// <since_tizen> 10 </since_tizen>
130 public static NUIGadget Add(string resourceType, string className)
132 if (string.IsNullOrEmpty(resourceType) || string.IsNullOrEmpty(className))
134 throw new ArgumentException("Invalid argument");
137 if (!_gadgetInfos.TryGetValue(resourceType, out NUIGadgetInfo info))
139 throw new ArgumentException("Failed to find NUIGadgetInfo. resource type: " + resourceType);
142 Assembly assembly = null;
145 Log.Warn("NUIGadgetAssembly.Load(): " + info.ResourcePath + info.ExecutableFile + " ++");
146 assembly = Assembly.Load(File.ReadAllBytes(info.ResourcePath + info.ExecutableFile));
147 Log.Warn("NUIGadgetAssembly.Load(): " + info.ResourcePath + info.ExecutableFile + " --");
149 catch (FileLoadException e)
151 throw new InvalidOperationException(e.Message);
154 NUIGadget gadget = assembly.CreateInstance(className, true) as NUIGadget;
157 throw new InvalidOperationException("Failed to create instance. className: " + className);
160 gadget.NUIGadgetInfo = info;
161 gadget.ClassName = className;
162 gadget.NUIGadgetResourceManager = new NUIGadgetResourceManager(info);
163 gadget.LifecycleChanged += OnNUIGadgetLifecycleChanged;
164 if (!gadget.Create())
166 throw new InvalidOperationException("The View MUST be created");
169 _gadgets.Add(gadget);
174 /// Gets the instance of the running NUIGadgets.
176 /// <returns>The NUIGadget list.</returns>
177 /// <since_tizen> 10 </since_tizen>
178 public static IEnumerable<NUIGadget> GetGadgets()
184 /// Gets the information of the available NUIGadgets.
187 /// This method only returns the available gadget informations, not all installed gadget informations.
188 /// The resource package of the NUIGadget can set the allowed packages using "allowed-package".
189 /// When executing an application, the platform mounts the resource package into the resource path of the application.
191 /// <returns>The NUIGadgetInfo list.</returns>
192 /// <since_tizen> 10 </since_tizen>
193 public static IEnumerable<NUIGadgetInfo> GetGadgetInfos()
195 return _gadgetInfos.Values.ToList();
199 /// Removes the NUIGadget from a NUIGadgetManager.
201 /// <param name="gadget">The NUIGadget object.</param>
202 /// <since_tizen> 10 </since_tizen>
203 public static void Remove(NUIGadget gadget)
205 if (gadget == null || !_gadgets.Contains(gadget))
210 if (gadget.State == NUIGadgetLifecycleState.Destroyed)
215 _gadgets.Remove(gadget);
216 CoreApplication.Post(() => {
217 Log.Warn("ResourceType: " + gadget.NUIGadgetInfo.ResourceType + ", State: " + gadget.State);
223 /// Removes all NUIGadget from a NUIGadgetManager.
225 /// <since_tizen> 10 </since_tizen>
226 public static void RemoveAll()
228 for (int i = _gadgets.Count - 1; i >= 0; i--)
235 /// Resumes the running NUIGadget.
237 /// <param name="gadget">The NUIGadget object.</param>
238 /// <since_tizen> 10 </since_tizen>
239 public static void Resume(NUIGadget gadget)
241 if (!_gadgets.Contains(gadget))
246 CoreApplication.Post(() =>
248 Log.Warn("ResourceType: " + gadget.NUIGadgetInfo.ResourceType + ", State: " + gadget.State);
254 /// Pauses the running NUIGadget.
256 /// <param name="gadget">The NUIGadget object.</param>
257 /// <since_tizen> 10 </since_tizen>
258 public static void Pause(NUIGadget gadget)
260 if (!_gadgets.Contains(gadget))
265 CoreApplication.Post(() =>
267 Log.Warn("ResourceType: " + gadget.NUIGadgetInfo.ResourceType + ", State: " + gadget.State);
273 /// Sends the appcontrol to the running NUIGadget.
275 /// <param name="gadget">The NUIGadget object.</param>
276 /// <param name="appControl">The appcontrol object.</param>
277 /// <exception cref="ArgumentException">Thrown when failed because of a invalid argument.</exception>
278 /// <exception cref="ArgumentNullException">Thrown when failed because the argument is null.</exception>
279 /// <since_tizen> 10 </since_tizen>
280 public static void SendAppControl(NUIGadget gadget, AppControl appControl)
284 throw new ArgumentNullException(nameof(gadget));
287 if (!_gadgets.Contains(gadget))
289 throw new ArgumentException("Invalid argument");
292 if (appControl == null)
294 throw new ArgumentNullException(nameof(appControl));
297 gadget.HandleAppControlReceivedEvent(new AppControlReceivedEventArgs(new ReceivedAppControl(appControl.SafeAppControlHandle)));
300 internal static bool HandleAppControl(AppControlReceivedEventArgs args)
302 if (!args.ReceivedAppControl.ExtraData.TryGet("__K_GADGET_RES_TYPE", out string resourceType) ||
303 !args.ReceivedAppControl.ExtraData.TryGet("__K_GADGET_CLASS_NAME", out string className))
308 foreach (NUIGadget gadget in _gadgets)
310 if (gadget.NUIGadgetInfo.ResourceType == resourceType && gadget.ClassName == className)
312 gadget.HandleAppControlReceivedEvent(args);
320 internal static void HandleEvents(NUIGadgetEventType eventType, EventArgs args)
322 foreach (NUIGadget gadget in _gadgets)
324 gadget.HandleEvents(eventType, args);