*/
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
-using System.Linq;
using System.IO;
-using Tizen.Applications;
-using System.ComponentModel;
+using System.Linq;
using System.Runtime.InteropServices;
-using System.Runtime.Loader;
using System.Reflection;
-using System.Threading.Tasks;
-using System.Security.AccessControl;
+using Tizen.Applications;
+using System.ComponentModel;
using SystemIO = System.IO;
[EditorBrowsable(EditorBrowsableState.Never)]
public static class NUIGadgetManager
{
- private static readonly Dictionary<string, NUIGadgetInfo> _gadgetInfos = new Dictionary<string, NUIGadgetInfo>();
- private static readonly List<NUIGadget> _gadgets = new List<NUIGadget>();
+ private static readonly ConcurrentDictionary<string, NUIGadgetInfo> _gadgetInfos = new ConcurrentDictionary<string, NUIGadgetInfo>(StringComparer.Ordinal);
+ private static readonly ConcurrentDictionary<NUIGadget, byte> _gadgets = new ConcurrentDictionary<NUIGadget, byte>();
static NUIGadgetManager()
{
- IntPtr gadgetPkgIds = Interop.Libc.GetEnviornmentVariable("GADGET_PKGIDS");
- if (gadgetPkgIds != IntPtr.Zero)
+ var ptr = Interop.Libc.GetEnvironmentVariable("GADGET_PKGIDS");
+ if (ptr != IntPtr.Zero)
{
- string packages = Marshal.PtrToStringAnsi(gadgetPkgIds);
- if (string.IsNullOrEmpty(packages))
- {
- Log.Warn("There is no resource packages");
- }
- else
+ var packages = Marshal.PtrToStringAnsi(ptr);
+ if (!string.IsNullOrWhiteSpace(packages))
{
- foreach (string packageId in packages.Split(':').ToList())
+ foreach (var pkg in packages.Split(':'))
{
- NUIGadgetInfo info = NUIGadgetInfo.CreateNUIGadgetInfo(packageId);
+ var info = NUIGadgetInfo.CreateNUIGadgetInfo(pkg);
if (info != null)
{
- _gadgetInfos.Add(info.ResourceType, info);
+ try
+ {
+ _gadgetInfos.TryAdd(info.ResourceType, info);
+ }
+ catch (Exception e) when (e is ArgumentNullException || e is OverflowException)
+ {
+ Log.Error("Exception occurs. " + e.Message);
+ }
}
}
}
+ else
+ {
+ Log.Warn("No gadget packages found.");
+ }
}
else
{
- Log.Warn("Failed to get environment variable");
+ Log.Warn("GADGET_PKGIDS not set.");
}
- var context = (CoreApplication)CoreApplication.Current;
- context.AppControlReceived += OnAppControlReceived;
- context.LowMemory += OnLowMemory;
- context.LowBattery += OnLowBattery;
- context.LocaleChanged += OnLocaleChanged;
- context.RegionFormatChanged += OnRegionFormatChanged;
- context.DeviceOrientationChanged += OnDeviceOrientationChanged;
- }
-
- private static void OnAppControlReceived(object sender, AppControlReceivedEventArgs args)
- {
- HandleAppControl(args);
- }
-
- private static void OnLowMemory(object sender, LowMemoryEventArgs args)
- {
- HandleEvents(NUIGadgetEventType.LowMemory, args);
- }
-
- private static void OnLowBattery(object sender, LowBatteryEventArgs args)
- {
- HandleEvents(NUIGadgetEventType.LowBattery, args);
- }
-
- private static void OnLocaleChanged(object sender, LocaleChangedEventArgs args)
- {
- HandleEvents(NUIGadgetEventType.LocaleChanged, args);
- }
-
- private static void OnRegionFormatChanged(object sender, RegionFormatChangedEventArgs args)
- {
- HandleEvents(NUIGadgetEventType.RegionFormatChanged, args);
- }
-
- private static void OnDeviceOrientationChanged(object sender, DeviceOrientationEventArgs args)
- {
- HandleEvents(NUIGadgetEventType.DeviceORientationChanged, args);
+ var app = (CoreApplication)CoreApplication.Current;
+ app.AppControlReceived += (s, e) => HandleAppControl(e);
+ app.LowMemory += (s, e) => HandleEvents(NUIGadgetEventType.LowMemory, e);
+ app.LowBattery += (s, e) => HandleEvents(NUIGadgetEventType.LowBattery, e);
+ app.LocaleChanged += (s, e) => HandleEvents(NUIGadgetEventType.LocaleChanged, e);
+ app.RegionFormatChanged += (s, e) => HandleEvents(NUIGadgetEventType.RegionFormatChanged, e);
}
/// <summary>
if (args.State == NUIGadgetLifecycleState.Destroyed)
{
args.Gadget.LifecycleChanged -= OnNUIGadgetLifecycleChanged;
- _gadgets.Remove(args.Gadget);
+ _gadgets.TryRemove(args.Gadget, out _);
}
}
private static NUIGadgetInfo Find(string resourceType)
{
- if (!_gadgetInfos.TryGetValue(resourceType, out NUIGadgetInfo info))
+ if (!_gadgetInfos.TryGetValue(resourceType, out var info))
{
throw new ArgumentException("Failed to find NUIGadgetInfo. resource type: " + resourceType);
}
/// <exception cref="ArgumentException">Thrown when failed because of a invalid argument.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <since_tizen> 10 </since_tizen>
- public static void Load(string resourceType)
- {
- Load(resourceType, true);
- }
+ public static void Load(string resourceType) => Load(resourceType, true);
/// <summary>
/// Loads an assembly of the NUIGadget.
}
NUIGadgetInfo info = Find(resourceType);
- Load(info, useDefaultContext);
- }
-
- /// <summary>
- /// Unloads the specified NUIGadget assembly from memory.
- /// </summary>
- /// <remarks>
- /// To use this method properly, the assembly of the gadget must be loaded using Load() with the custom context.
- /// </remarks>
- /// <param name="resourceType">The resource type of the NUIGadget package to unload.</param>
- /// <exception cref="ArgumentException">Thrown when the argument passed is not valid.</exception>
- /// <example>
- /// <code>
- /// /// Load an assembly of the NUIGadget.
- /// NUIGadgetManager.Load("org.tizen.appfw.gadget.NetworkSetting", false);
- /// /// NUIGadgetManager.Add("org.tizen.appfw.gadget.NetworkSetting", "NetworkSettingGadget", false);
- ///
- /// /// Unload the loaded assembly
- /// NUIGadgetManager.Unload("org.tizen.appfw.gadget.NetworkSetting");
- /// </code>
- /// </example>
- /// <since_tizen> 10 </since_tizen>
- public static void Unload(string resourceType)
- {
- if (string.IsNullOrEmpty(resourceType))
- {
- throw new ArgumentException("Invalid argument");
- }
-
- NUIGadgetInfo info = Find(resourceType);
- Unload(info);
+ LoadInternal(info, useDefaultContext);
}
- private static void Unload(NUIGadgetInfo info)
+ private static void LoadInternal(NUIGadgetInfo info, bool useDefaultContext)
{
if (info == null)
{
- throw new ArgumentException("Invalid argument");
- }
-
- lock (info)
- {
- if (info.NUIGadgetAssembly != null && info.NUIGadgetAssembly.IsLoaded)
- {
- info.NUIGadgetAssembly.Unload();
- }
- }
- }
-
- private static void Load(NUIGadgetInfo info, bool useDefaultContext)
- {
- if (info == null)
- {
- throw new ArgumentException("Invalid argument");
+ throw new ArgumentException("Invalid argument", nameof(info));
}
try
}
}
}
- catch (FileLoadException e)
+ catch (Exception e) when (e is FileLoadException || e is BadImageFormatException)
{
- throw new InvalidOperationException(e.Message);
+ throw new InvalidOperationException(e.Message, e);
}
- catch (BadImageFormatException e)
+ }
+
+ /// <summary>
+ /// Unloads the specified NUIGadget assembly from memory.
+ /// </summary>
+ /// <remarks>
+ /// To use this method properly, the assembly of the gadget must be loaded using Load() with the custom context.
+ /// </remarks>
+ /// <param name="resourceType">The resource type of the NUIGadget package to unload.</param>
+ /// <exception cref="ArgumentException">Thrown when the argument passed is not valid.</exception>
+ /// <example>
+ /// <code>
+ /// /// Load an assembly of the NUIGadget.
+ /// NUIGadgetManager.Load("org.tizen.appfw.gadget.NetworkSetting", false);
+ /// /// NUIGadgetManager.Add("org.tizen.appfw.gadget.NetworkSetting", "NetworkSettingGadget", false);
+ ///
+ /// /// Unload the loaded assembly
+ /// NUIGadgetManager.Unload("org.tizen.appfw.gadget.NetworkSetting");
+ /// </code>
+ /// </example>
+ /// <since_tizen> 10 </since_tizen>
+ public static void Unload(string resourceType)
+ {
+ if (string.IsNullOrWhiteSpace(resourceType))
+ {
+ throw new ArgumentException("Invalid argument", nameof(resourceType));
+ }
+
+ NUIGadgetInfo info = Find(resourceType);
+ if (info == null)
+ {
+ throw new ArgumentException("Invalid argument", nameof(resourceType));
+ }
+
+ lock (info)
{
- throw new InvalidOperationException(e.Message);
+ if (info.NUIGadgetAssembly?.IsLoaded == true)
+ {
+ info.NUIGadgetAssembly.Unload();
+ }
}
}
/// <exception cref="ArgumentException">Thrown when failed because of a invalid argument.</exception>
/// <exception cref="InvalidOperationException">Thrown when failed because of an invalid operation.</exception>
/// <since_tizen> 10 </since_tizen>
- public static NUIGadget Add(string resourceType, string className)
- {
- return Add(resourceType, className, true);
- }
+ public static NUIGadget Add(string resourceType, string className) => Add(resourceType, className, true);
/// <summary>
/// Adds a NUIGadget to the NUIGadgetManager.
/// <since_tizen> 10 </since_tizen>
public static NUIGadget Add(string resourceType, string className, bool useDefaultContext)
{
- if (string.IsNullOrEmpty(resourceType) || string.IsNullOrEmpty(className))
+ if (string.IsNullOrWhiteSpace(resourceType) || string.IsNullOrWhiteSpace(className))
{
throw new ArgumentException("Invalid argument");
}
NUIGadgetInfo info = Find(resourceType);
- Load(info, useDefaultContext);
+ LoadInternal(info, useDefaultContext);
NUIGadget gadget = useDefaultContext ? info.Assembly.CreateInstance(className, true) as NUIGadget : info.NUIGadgetAssembly.CreateInstance(className);
if (gadget == null)
throw new InvalidOperationException("The View MUST be created");
}
- _gadgets.Add(gadget);
+ _gadgets.TryAdd(gadget, 0);
return gadget;
}
/// </summary>
/// <returns>An enumerable list containing all the active NUIGadgets.</returns>
/// <since_tizen> 10 </since_tizen>
- public static IEnumerable<NUIGadget> GetGadgets()
- {
- return _gadgets;
- }
+ public static IEnumerable<NUIGadget> GetGadgets() => _gadgets.Keys;
/// <summary>
/// Retrieves information about available NUIGadgets.
/// </remarks>
/// <returns>An enumerable list of NUIGadgetInfo objects.</returns>
/// <since_tizen> 10 </since_tizen>
- public static IEnumerable<NUIGadgetInfo> GetGadgetInfos()
- {
- return _gadgetInfos.Values.ToList();
- }
+ public static IEnumerable<NUIGadgetInfo> GetGadgetInfos() => _gadgetInfos.Values;
/// <summary>
/// Removes the specified NUIGadget from the NUIGadgetManager.
/// <since_tizen> 10 </since_tizen>
public static void Remove(NUIGadget gadget)
{
- if (gadget == null || !_gadgets.Contains(gadget))
+ if (gadget == null || !_gadgets.ContainsKey(gadget) || gadget.State == NUIGadgetLifecycleState.Destroyed)
{
return;
}
- if (gadget.State == NUIGadgetLifecycleState.Destroyed)
+ _gadgets.TryRemove(gadget, out _);
+ CoreApplication.Post(() =>
{
- return;
- }
-
- _gadgets.Remove(gadget);
- CoreApplication.Post(() => {
Log.Warn("ResourceType: " + gadget.NUIGadgetInfo.ResourceType + ", State: " + gadget.State);
gadget.Finish();
});
/// <since_tizen> 10 </since_tizen>
public static void RemoveAll()
{
- for (int i = _gadgets.Count - 1; i >= 0; i--)
+ foreach (var gadget in _gadgets.Keys.ToList())
{
- Remove(_gadgets[i]);
+ Remove(gadget);
}
}
throw new ArgumentNullException(nameof(gadget));
}
- if (!_gadgets.Contains(gadget))
+ if (!_gadgets.ContainsKey(gadget))
{
return;
}
throw new ArgumentNullException(nameof(gadget));
}
- if (!_gadgets.Contains(gadget))
+ if (!_gadgets.ContainsKey(gadget))
{
return;
}
throw new ArgumentNullException(nameof(gadget));
}
- if (!_gadgets.Contains(gadget))
+ if (!_gadgets.ContainsKey(gadget))
{
- throw new ArgumentException("Invalid argument");
+ throw new ArgumentException("Invalid argument", nameof(gadget));
}
if (appControl == null)
internal static bool HandleAppControl(AppControlReceivedEventArgs args)
{
var extraData = args.ReceivedAppControl?.ExtraData;
- if (extraData == null||!extraData.TryGet("__K_GADGET_RES_TYPE", out string resourceType) ||
+ if (extraData == null || !extraData.TryGet("__K_GADGET_RES_TYPE", out string resourceType) ||
!extraData.TryGet("__K_GADGET_CLASS_NAME", out string className))
{
return false;
}
- foreach (NUIGadget gadget in _gadgets)
+ foreach (var gadget in _gadgets.Keys)
{
if (gadget.NUIGadgetInfo.ResourceType == resourceType && gadget.ClassName == className)
{
internal static void HandleEvents(NUIGadgetEventType eventType, EventArgs args)
{
- foreach (NUIGadget gadget in _gadgets)
+ foreach (NUIGadget gadget in _gadgets.Keys)
{
gadget.HandleEvents(eventType, args);
}