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.Internals.Errors;
21 namespace Tizen.Applications
24 /// The preference class provides APIs to store and retrieve an application specific data/preference. A preference is saved in the form of a key-value pair.
25 /// Keys are always text strings and the value can be any one of the four types: integer, double, string, and boolean.
27 /// <since_tizen> 3 </since_tizen>
28 public static class Preference
30 private const string LogTag = "Tizen.Applications";
31 private static Interop.Preference.ChangedCallback s_preferenceChangedCallback;
32 private static IDictionary<string, EventContext> s_eventMap = new Dictionary<string, EventContext>();
36 s_preferenceChangedCallback = (string key, IntPtr userData) =>
40 s_eventMap[key]?.FireEvent();
44 Log.Warn(LogTag, e.Message);
50 /// Retrieves all keys of the application preferences.
57 /// Preference.Set("Option_enabled", true);
58 /// Preference.Set("active_user", "Joe");
59 /// Preference.Set("default_volume", 10);
60 /// Preference.Set("brightness", "0.6");
61 /// foreach(string key in Preference.Keys)
63 /// Console.WriteLine("key {0}", key);
67 /// <since_tizen> 3 </since_tizen>
68 public static IEnumerable<string> Keys
72 var collection = new List<string>();
73 Interop.Preference.ItemCallback itemsCallback = (string key, IntPtr userData) =>
78 Interop.Preference.ForeachItem(itemsCallback, IntPtr.Zero);
84 /// Gets the event context for the given key.
86 /// <seealso cref="EventContext"/>
87 /// <param name="key">The preference key.</param>
88 /// <returns>The event context of respective key.</returns>
89 /// <exception cref="KeyNotFoundException">Thrown if the key is not found.</exception>
90 /// <exception cref="ArgumentException">Thrown if the key is invalid parameter.</exception>
93 /// private static void Preference_PreferenceChanged(object sender, PreferenceChangedEventArgs e)
95 /// Console.WriteLine("key {0}", e.Key);
98 /// Preference.EventContext context = null;
99 /// Preference.GetEventContext("active_user").TryGetTarget(out context);
100 /// if(context != null)
102 /// context.Changed += Preference_PreferenceChanged;
105 /// Preference.Set("active_user", "Poe");
107 /// Preference.GetEventContext("active_user").TryGetTarget(out context);
108 /// if (context != null)
110 /// context.Changed -= Preference_PreferenceChanged;
114 /// <since_tizen> 3 </since_tizen>
115 public static WeakReference<EventContext> GetEventContext(string key)
117 if (!s_eventMap.ContainsKey(key))
121 s_eventMap[key] = new EventContext(key);
125 throw PreferenceErrorFactory.GetException((int)PreferenceErrorFactory.PreferenceError.KeyNotAvailable);
129 return new WeakReference<EventContext>(s_eventMap[key]);
133 /// Checks whether the given key exists in the preference.
135 /// <param name="key">The name of the key to check.</param>
136 /// <returns>True if the key exists in the preference, otherwise false.</returns>
137 /// <exception cref="ArgumentException">Thrown if the key is an invalid parameter.</exception>
138 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
141 /// Preference.Set("active_user", "Joe");
142 /// bool exists = Preference.Contains("active_user");
145 /// string value = Preference.Get<istring>("active_user");
146 /// Console.WriteLine("user {0}", value);
150 /// <since_tizen> 3 </since_tizen>
151 public static bool Contains(string key)
154 int ret = Interop.Preference.IsExisting(key, out contains);
155 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
157 Log.Error(LogTag, "Failed to find key");
158 throw PreferenceErrorFactory.GetException(ret);
165 /// Sets a key-value pair representing the preference.
168 /// If the key already exists in the preference, the old value will be overwritten with a new value.
169 /// Data types for supported values are: integer, double, string, and bool.
171 /// <param name="key">The name of the key to create/modify.</param>
172 /// <param name="value">The value corresponding to the key.</param>
173 /// <exception cref="ArgumentException">Thrown if the key is an invalid parameter.</exception>
174 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
177 /// Preference.Set("Option_enabled", true);
178 /// Preference.Set("active_user", "Joe");
179 /// Preference.Set("default_volume", 10);
180 /// Preference.Set("brightness", "0.6");
183 /// <since_tizen> 3 </since_tizen>
184 public static void Set(string key, object value)
189 ret = Interop.Preference.SetInt(key, (int)value);
190 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
192 Log.Error(LogTag, "Failed to find key");
193 throw PreferenceErrorFactory.GetException(ret);
196 else if (value is double)
198 ret = Interop.Preference.SetDouble(key, (double)value);
199 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
201 Log.Error(LogTag, "Failed to find key");
202 throw PreferenceErrorFactory.GetException(ret);
205 else if (value is string)
207 ret = Interop.Preference.SetString(key, (string)value);
208 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
210 Log.Error(LogTag, "Failed to find key");
211 throw PreferenceErrorFactory.GetException(ret);
214 else if (value is bool)
216 ret = Interop.Preference.SetBoolean(key, (bool)value);
217 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
219 Log.Error(LogTag, "Failed to find key");
220 throw PreferenceErrorFactory.GetException(ret);
225 Log.Error(LogTag, "Failed to Set");
226 throw new ArgumentException("Invalid parameter");
231 /// Gets the value of a preference item with the specified key.
232 /// Note that this is a generic method.
234 /// <typeparam name="T">The generic type to return.</typeparam>
235 /// <param name="key">The key of the preference.</param>
236 /// <returns>The value of the preference item if it is of the specified generic type.</returns>
237 /// <exception cref="KeyNotFoundException">Thrown if the key is not found.</exception>
238 /// <exception cref="ArgumentException">Thrown if the key is an invalid parameter.</exception>
239 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
242 /// bool exists = Preference.Contains("active_user");
245 /// string value = Preference.Get<string>("active_user");
246 /// Console.WriteLine("user {0}", value);
250 /// <since_tizen> 3 </since_tizen>
251 public static T Get<T>(string key)
253 object result = null;
254 int ret = (int)PreferenceErrorFactory.PreferenceError.None;
255 if (typeof(T) == typeof(bool))
258 ret = Interop.Preference.GetBoolean(key, out val);
261 else if (typeof(T) == typeof(int))
264 ret = Interop.Preference.GetInt(key, out val);
267 else if (typeof(T) == typeof(string))
270 ret = Interop.Preference.GetString(key, out val);
273 else if (typeof(T) == typeof(double))
276 ret = Interop.Preference.GetDouble(key, out val);
281 Log.Error(LogTag, "Failed to remove key");
282 throw new ArgumentException("Invalid parameter");
285 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
287 Log.Error(LogTag, "Failed to remove key");
288 throw PreferenceErrorFactory.GetException(ret);
291 return (result != null) ? (T)result : default(T);
295 /// Removes any preference value with the given key.
297 /// <param name="key">The key to remove.</param>
298 /// <exception cref="KeyNotFoundException">Thrown if the key is not found.</exception>
299 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
302 /// bool exists = Preference.Contains("active_user");
305 /// string value = Preference.Remove("active_user");
309 /// <since_tizen> 3 </since_tizen>
310 public static void Remove(string key)
312 int ret = Interop.Preference.Remove(key);
313 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
315 Log.Error(LogTag, "Failed to remove key");
316 throw PreferenceErrorFactory.GetException(ret);
321 /// Removes all the key-value pairs from the preference.
323 /// <exception cref="System.IO.IOException">Thrown when the method failed due to an internal I/O error.</exception>
326 /// Preference.Set("Option_enabled", true);
327 /// Preference.Set("active_user", "Joe");
328 /// Preference.Set("default_volume", 10);
329 /// Preference.Set("brightness", "0.6");
330 /// Preference.RemoveAll();
333 /// <since_tizen> 3 </since_tizen>
334 public static void RemoveAll()
336 int ret = Interop.Preference.RemoveAll();
337 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
339 Log.Error(LogTag, "Failed to remove all keys");
340 throw PreferenceErrorFactory.GetException(ret);
344 private static void AllowChangeNotifications(string key)
346 int ret = Interop.Preference.SetChangedCb(key, s_preferenceChangedCallback, IntPtr.Zero);
347 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
349 Log.Error(LogTag, "Failed to set key notification");
350 throw PreferenceErrorFactory.GetException(ret);
354 private static void DisallowChangeNotifications(string key)
356 int ret = Interop.Preference.UnsetChangedCb(key);
357 if (ret != (int)PreferenceErrorFactory.PreferenceError.None)
359 Log.Error(LogTag, "Failed to remove key notification");
360 throw PreferenceErrorFactory.GetException(ret);
365 /// The class manages event handlers of the preference keys. The class enables having event handlers for individual preference keys.
367 /// <since_tizen> 3 </since_tizen>
368 public class EventContext
372 internal EventContext(string key)
378 /// Occurs whenever there is a change in the value of a preference key.
380 /// <exception cref="System.ArgumentException">Thrown when the key does not exist or when there is an invalid parameter.</exception>
381 /// <exception cref="System.InvalidOperationException">Thrown when the bundle instance has been disposed.</exception>
384 /// private static void Preference_PreferenceChanged(object sender, PreferenceChangedEventArgs e)
386 /// Console.WriteLine("key {0}", e.Key);
388 /// Preference.EventContext context = null;
389 /// Preference.GetEventContext("active_user").TryGetTarget(out context);
390 /// if(context != null)
392 /// context.Changed += Preference_PreferenceChanged;
395 /// Preference.Set("active_user", "Poe");
397 /// Preference.GetEventContext("active_user").TryGetTarget(out context);
398 /// if (context != null)
400 /// context.Changed -= Preference_PreferenceChanged;
404 /// <since_tizen> 3 </since_tizen>
405 public event EventHandler<PreferenceChangedEventArgs> Changed
409 if (_changed == null)
411 AllowChangeNotifications(_key);
420 if (_changed == null)
422 DisallowChangeNotifications(_key);
423 s_eventMap.Remove(_key);
428 private event EventHandler<PreferenceChangedEventArgs> _changed;
430 internal void FireEvent()
432 _changed?.Invoke(null, new PreferenceChangedEventArgs() { Key = _key });
438 internal static class PreferenceErrorFactory
440 internal enum PreferenceError
442 None = ErrorCode.None,
443 OutOfMemory = ErrorCode.OutOfMemory,
444 InvalidParameter = ErrorCode.InvalidParameter,
445 KeyNotAvailable = -0x01100000 | 0x30,
446 IoError = ErrorCode.IoError
449 static internal Exception GetException(int error)
451 if ((PreferenceError)error == PreferenceError.OutOfMemory)
453 return new OutOfMemoryException("Out of memory");
455 else if ((PreferenceError)error == PreferenceError.InvalidParameter)
457 return new ArgumentException("Invalid parameter");
459 else if ((PreferenceError)error == PreferenceError.KeyNotAvailable)
461 return new KeyNotFoundException("Key does not exist in the bundle");
463 else if ((PreferenceError)error == PreferenceError.IoError)
465 return new System.IO.IOException("I/O Error");
469 return new ArgumentException("Unknown error");