Ejemplo n.º 1
0
        private static void AllowChangeNotifications(string key)
        {
            int ret = Interop.Preference.SetChangedCb(key, s_preferenceChangedCallback, IntPtr.Zero);

            if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
            {
                Log.Error(LogTag, "Failed to set key notification");
                throw PreferenceErrorFactory.GetException(ret);
            }
        }
Ejemplo n.º 2
0
        private static void DisallowChangeNotifications(string key)
        {
            int ret = Interop.Preference.UnsetChangedCb(key);

            if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
            {
                Log.Error(LogTag, "Failed to remove key notification");
                throw PreferenceErrorFactory.GetException(ret);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Removes all the key-value pairs from the preference.
        /// </summary>
        /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
        /// <example>
        /// <code>
        ///     Preference.Set("Option_enabled", true);
        ///     Preference.Set("active_user", "Joe");
        ///     Preference.Set("default_volume", 10);
        ///     Preference.Set("brightness", "0.6");
        ///     Preference.RemoveAll();
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public static void RemoveAll()
        {
            int ret = Interop.Preference.RemoveAll();

            if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
            {
                Log.Error(LogTag, "Failed to remove all keys");
                throw PreferenceErrorFactory.GetException(ret);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Checks whether the given key exists in the preference.
        /// </summary>
        /// <param name="key">The name of the key to check.</param>
        /// <returns>True if the key exists in the preference, otherwise false.</returns>
        /// <exception cref="ArgumentException">Thrown if the key is an invalid parameter.</exception>
        /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
        /// <example>
        /// <code>
        ///     Preference.Set("active_user", "Joe");
        ///     bool exists = Preference.Contains("active_user");
        ///     if (exists)
        ///     {
        ///         string value = Preference.Get&lt;istring&gt;("active_user");
        ///         Console.WriteLine("user {0}", value);
        ///     }
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public static bool Contains(string key)
        {
            bool contains;
            int  ret = Interop.Preference.IsExisting(key, out contains);

            if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
            {
                Log.Error(LogTag, "Failed to find key");
                throw PreferenceErrorFactory.GetException(ret);
            }

            return(contains);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the event context for the given key.
        /// </summary>
        /// <seealso cref="EventContext"/>
        /// <param name="key">The preference key.</param>
        /// <returns>The event context of respective key.</returns>
        /// <exception cref="KeyNotFoundException">Thrown if the key is not found.</exception>
        /// <exception cref="ArgumentException">Thrown if the key is invalid parameter.</exception>
        /// <example>
        /// <code>
        ///     private static void Preference_PreferenceChanged(object sender, PreferenceChangedEventArgs e)
        ///     {
        ///         Console.WriteLine("key {0}", e.Key);
        ///     }
        ///
        ///     Preference.EventContext context = null;
        ///     Preference.GetEventContext("active_user").TryGetTarget(out context);
        ///     if(context != null)
        ///     {
        ///         context.Changed += Preference_PreferenceChanged;
        ///     }
        ///
        ///     Preference.Set("active_user", "Poe");
        ///
        ///     Preference.GetEventContext("active_user").TryGetTarget(out context);
        ///     if (context != null)
        ///     {
        ///         context.Changed -= Preference_PreferenceChanged;
        ///     }
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public static WeakReference <EventContext> GetEventContext(string key)
        {
            if (!s_eventMap.ContainsKey(key))
            {
                if (Contains(key))
                {
                    s_eventMap[key] = new EventContext(key);
                }
                else
                {
                    throw PreferenceErrorFactory.GetException((int)PreferenceErrorFactory.PreferenceError.KeyNotAvailable);
                }
            }

            return(new WeakReference <EventContext>(s_eventMap[key]));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Sets a key-value pair representing the preference.
        /// </summary>
        /// <remarks>
        /// If the key already exists in the preference, the old value will be overwritten with a new value.
        /// Data types for supported values are: integer, double, string, and bool.
        /// </remarks>
        /// <param name="key">The name of the key to create/modify.</param>
        /// <param name="value">The value corresponding to the key.</param>
        /// <exception cref="ArgumentException">Thrown if the key is an invalid parameter.</exception>
        /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
        /// <example>
        /// <code>
        ///     Preference.Set("Option_enabled", true);
        ///     Preference.Set("active_user", "Joe");
        ///     Preference.Set("default_volume", 10);
        ///     Preference.Set("brightness", "0.6");
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public static void Set(string key, object value)
        {
            int ret = 0;

            if (value is int)
            {
                ret = Interop.Preference.SetInt(key, (int)value);
                if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
                {
                    Log.Error(LogTag, "Failed to find key");
                    throw PreferenceErrorFactory.GetException(ret);
                }
            }
            else if (value is double)
            {
                ret = Interop.Preference.SetDouble(key, (double)value);
                if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
                {
                    Log.Error(LogTag, "Failed to find key");
                    throw PreferenceErrorFactory.GetException(ret);
                }
            }
            else if (value is string)
            {
                ret = Interop.Preference.SetString(key, (string)value);
                if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
                {
                    Log.Error(LogTag, "Failed to find key");
                    throw PreferenceErrorFactory.GetException(ret);
                }
            }
            else if (value is bool)
            {
                ret = Interop.Preference.SetBoolean(key, (bool)value);
                if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
                {
                    Log.Error(LogTag, "Failed to find key");
                    throw PreferenceErrorFactory.GetException(ret);
                }
            }
            else
            {
                Log.Error(LogTag, "Failed to Set");
                throw new ArgumentException("Invalid parameter");
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the value of a preference item with the specified key.
        /// Note that this is a generic method.
        /// </summary>
        /// <typeparam name="T">The generic type to return.</typeparam>
        /// <param name="key">The key of the preference.</param>
        /// <returns>The value of the preference item if it is of the specified generic type.</returns>
        /// <exception cref="KeyNotFoundException">Thrown if the key is not found.</exception>
        /// <exception cref="ArgumentException">Thrown if the key is an invalid parameter.</exception>
        /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
        /// <example>
        /// <code>
        ///     bool exists = Preference.Contains("active_user");
        ///     if (exists)
        ///     {
        ///         string value = Preference.Get&lt;string&gt;("active_user");
        ///         Console.WriteLine("user {0}", value);
        ///     }
        /// </code>
        /// </example>
        /// <since_tizen> 3 </since_tizen>
        public static T Get <T>(string key)
        {
            object result = null;
            int    ret    = (int)PreferenceErrorFactory.PreferenceError.None;

            if (typeof(T) == typeof(bool))
            {
                bool val;
                ret    = Interop.Preference.GetBoolean(key, out val);
                result = val;
            }
            else if (typeof(T) == typeof(int))
            {
                int val;
                ret    = Interop.Preference.GetInt(key, out val);
                result = val;
            }
            else if (typeof(T) == typeof(string))
            {
                string val;
                ret    = Interop.Preference.GetString(key, out val);
                result = val;
            }
            else if (typeof(T) == typeof(double))
            {
                double val;
                ret    = Interop.Preference.GetDouble(key, out val);
                result = val;
            }
            else
            {
                Log.Error(LogTag, "Failed to remove key");
                throw new ArgumentException("Invalid parameter");
            }

            if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
            {
                Log.Error(LogTag, "Failed to remove key");
                throw PreferenceErrorFactory.GetException(ret);
            }

            return((result != null) ? (T)result : default(T));
        }