Added Vconf service.
authorAndrzej Krawczyk <a.krawczyk@samsung.com>
Tue, 31 Aug 2021 12:44:43 +0000 (14:44 +0200)
committerPiotr Czaja <p.czaja@samsung.com>
Tue, 14 Sep 2021 11:01:36 +0000 (13:01 +0200)
Fitness/Services/Interop/Interop.VConf.cs [new file with mode: 0644]
Fitness/Services/Vconf.cs [new file with mode: 0644]
Fitness/Utils/ExceptionFactory.cs [new file with mode: 0644]
Fitness/tizen-manifest.xml

diff --git a/Fitness/Services/Interop/Interop.VConf.cs b/Fitness/Services/Interop/Interop.VConf.cs
new file mode 100644 (file)
index 0000000..c0c71a0
--- /dev/null
@@ -0,0 +1,91 @@
+using System;
+using System.Runtime.InteropServices;
+
+using static Fitness.Services.Vconf;
+
+namespace Fitness.Services.Interoperability
+{
+    /// <summary>
+    /// Interop.
+    /// </summary>
+    internal static partial class Interop
+    {
+        /// <summary>
+        /// Vconf.
+        /// </summary>
+        internal static partial class Vconf
+        {
+            private const string LIBRARYVCONF = "libvconf.so.0";
+
+            /// <summary>
+            /// VconfGetBool.
+            /// </summary>
+            /// <param name="key">key.</param>
+            /// <param name="val">value.</param>
+            /// <returns>int.</returns>
+            [DllImport(LIBRARYVCONF, EntryPoint = "vconf_get_bool")]
+            internal static extern int VconfGetBool(string key, out bool val);
+
+            /// <summary>
+            /// VconfSetBool.
+            /// </summary>
+            /// <param name="key">key.</param>
+            /// <param name="intval">value.</param>
+            /// <returns>int.</returns>
+            [DllImport(LIBRARYVCONF, EntryPoint = "vconf_set_bool")]
+            internal static extern int VconfSetBool(string key, bool intval);
+
+            /// <summary>
+            /// VconfGetInt.
+            /// </summary>
+            /// <param name="key">key.</param>
+            /// <param name="val">value.</param>
+            /// <returns>int.</returns>
+            [DllImport(LIBRARYVCONF, EntryPoint = "vconf_get_int")]
+            internal static extern int VconfGetInt(string key, out int val);
+
+            /// <summary>
+            /// VconfSetInt.
+            /// </summary>
+            /// <param name="key">key.</param>
+            /// <param name="intval">value.</param>
+            /// <returns>int.</returns>
+            [DllImport(LIBRARYVCONF, EntryPoint = "vconf_set_int")]
+            internal static extern int VconfSetInt(string key, int intval);
+
+            /// <summary>
+            /// VconfGetStr.
+            /// </summary>
+            /// <param name="key">key.</param>
+            /// <returns>string.</returns>
+            [DllImport(LIBRARYVCONF, EntryPoint = "vconf_get_str")]
+            internal static extern string VconfGetStr(string key);
+
+            /// <summary>
+            /// VconfSetStr.
+            /// </summary>
+            /// <param name="key">key.</param>
+            /// <param name="value">value.</param>
+            /// <returns>int.</returns>
+            [DllImport(LIBRARYVCONF, EntryPoint = "vconf_set_str")]
+            internal static extern int VconfSetStr(string key, string value);
+
+            /// <summary>
+            /// VconfNotifyKeyChanged.
+            /// </summary>
+            /// <param name="key">key.</param>
+            /// <param name="callback">callback.</param>
+            /// <param name="userData">userData.</param>
+            [DllImport(LIBRARYVCONF, EntryPoint = "vconf_notify_key_changed")]
+            internal static extern void VconfNotifyKeyChanged(string key, NotificationCallback callback, IntPtr userData);
+
+            /// <summary>
+            /// VconfIgnoreKeyChanged.
+            /// </summary>
+            /// <param name="key">key.</param>
+            /// <param name="callback">callback.</param>
+            [DllImport(LIBRARYVCONF, EntryPoint = "vconf_ignore_key_changed")]
+            internal static extern void VconfIgnoreKeyChanged(string key, NotificationCallback callback);
+        }
+    }
+}
diff --git a/Fitness/Services/Vconf.cs b/Fitness/Services/Vconf.cs
new file mode 100644 (file)
index 0000000..473e96f
--- /dev/null
@@ -0,0 +1,133 @@
+using System;
+using System.Collections.Generic;
+using System.Runtime.InteropServices;
+using Fitness.Utils;
+
+using static Fitness.Services.Interoperability.Interop.Vconf;
+
+namespace Fitness.Services
+{
+    /// <summary>
+    /// This class provides the API to use Vconf methods.
+    /// Vconf is a global configuration registry for the device.
+    /// </summary>
+    public static class Vconf
+    {
+        private static readonly List<NotificationCallback> Callbacks = new List<NotificationCallback>();
+
+        /// <summary>
+        /// Delegate for notification callbacks.
+        /// </summary>
+        /// <param name="node">Pointer to event node.</param>
+        /// <param name="userData">Pointer to event user data.</param>
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        public delegate void NotificationCallback(IntPtr node, IntPtr userData);
+
+        /// <summary>
+        /// Gets a boolean value from Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <returns>A value assigned to the specified key.</returns>
+        public static bool GetBool(string key)
+        {
+            int errorCode = VconfGetBool(key, out bool value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+
+            return value;
+        }
+
+        /// <summary>
+        /// Sets a boolean value in Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <param name="value">The value to be set.</param>
+        public static void SetBool(string key, bool value)
+        {
+            int errorCode = VconfSetBool(key, value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+        }
+
+        /// <summary>
+        /// Gets an integer value from Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <returns>A value assigned to the specified key.</returns>
+        public static int GetInt(string key)
+        {
+            int errorCode = VconfGetInt(key, out int value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+
+            return value;
+        }
+
+        /// <summary>
+        /// Sets an integer value in Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <param name="value">The value to be set.</param>
+        public static void SetInt(string key, int value)
+        {
+            int errorCode = VconfSetInt(key, value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+        }
+
+        /// <summary>
+        /// Gets a string value from Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <returns>A value assigned to the specified key.</returns>
+        public static string GetString(string key)
+        {
+            return VconfGetStr(key);
+        }
+
+        /// <summary>
+        /// Sets a string value in Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <param name="value">The value to be set.</param>
+        public static void SetString(string key, string value)
+        {
+            int errorCode = VconfSetStr(key, value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+        }
+
+        /// <summary>
+        /// Registers a callback to a KeyChanged event.
+        /// </summary>
+        /// <param name="key">The key to be observed for changes.</param>
+        /// <param name="callback">The callback to be registered.</param>
+        /// <param name="userData">Additional data.</param>
+        public static void NotifyKeyChanged(string key, NotificationCallback callback, IntPtr? userData = null)
+        {
+            Callbacks.Add(callback);
+            VconfNotifyKeyChanged(key, callback, userData ?? IntPtr.Zero);
+        }
+
+        /// <summary>
+        /// Unregisters a callback from a KeyChanged event.
+        /// </summary>
+        /// <param name="key">The key that was observed for changes.</param>
+        /// <param name="callback">The callback to be unregistered.</param>
+        public static void IgnoreKeyChanged(string key, NotificationCallback callback)
+        {
+            VconfIgnoreKeyChanged(key, callback);
+            Callbacks.Remove(callback);
+        }
+    }
+}
diff --git a/Fitness/Utils/ExceptionFactory.cs b/Fitness/Utils/ExceptionFactory.cs
new file mode 100644 (file)
index 0000000..5ab2b1a
--- /dev/null
@@ -0,0 +1,43 @@
+using System;
+using Tizen.Internals.Errors;
+
+namespace Fitness.Utils
+{
+    /// <summary>
+    /// This class provides the API to translate Tizen Error Codes to .NET exceptions.
+    /// </summary>
+    public static class ExceptionFactory
+    {
+        /// <summary>
+        /// Gets the exception that best corresponds to the given error code.
+        /// </summary>
+        /// <param name="errorCode">The Tizen Error Code to be translated.</param>
+        /// <returns>An exception object.</returns>
+        public static Exception GetException(int errorCode)
+        {
+            var msg = ErrorFacts.GetErrorMessage(errorCode);
+            var c = (ErrorCode)errorCode;
+
+            switch (c)
+            {
+                case ErrorCode.NotSupported:
+                    return new NotSupportedException(msg);
+
+                case ErrorCode.OutOfMemory:
+                    return new OutOfMemoryException(msg);
+
+                case ErrorCode.InvalidParameter:
+                    return new ArgumentException(msg);
+
+                case ErrorCode.InvalidOperation:
+                    return new InvalidOperationException(msg);
+
+                case ErrorCode.PermissionDenied:
+                    return new UnauthorizedAccessException(msg);
+
+                default:
+                    return new Exception(msg);
+            }
+        }
+    }
+}
index f61790666574963b5a58e667cb6e662f5c7f8d00..d3ed772329aaeaa57d83819f79cf114d205dffc7 100644 (file)
@@ -13,6 +13,7 @@
     <metadata key="http://tizen.org/metadata/prefer_dotnet_aot" value="true" />
   </ui-application>
   <privileges>
+    <privilege>http://tizen.org/privilege/systemsettings.admin</privilege>
     <privilege>http://tizen.org/privilege/camera</privilege>
     <privilege>http://tizen.org/privilege/window.priority.set</privilege>
   </privileges>