1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
11 namespace Tizen.System.SystemInfo
14 /// System Information class. This class has methods which can be used to obtain device information
16 public static class SystemInfo
18 private const string LogTag = "Tizen.System";
20 private static Interop.SystemInfo.SystemInfoType GetValueType(string key, out Interop.SystemInfo.SystemInfoValueType valueType)
22 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.SystemInfoGetPlatformType(key, out valueType);
23 if (err == Interop.SystemInfo.ErrorCode.None)
25 return Interop.SystemInfo.SystemInfoType.platform;
28 Log.Debug(LogTag, string.Format("Key {0} not in platform system info", key));
29 err = Interop.SystemInfo.SystemInfoGetCustomType(key, out valueType);
30 if (err == Interop.SystemInfo.ErrorCode.None)
32 return Interop.SystemInfo.SystemInfoType.Custom;
35 Log.Debug(LogTag, string.Format("Key {0} not in custom system info", key));
36 return Interop.SystemInfo.SystemInfoType.None;
40 /// Checks if type of value for given feature is T
42 /// <typeparam name="T">Type of value for feature key</typeparam>
43 /// <param name="key">The name of the feature</param>
44 /// <returns>true if type of value for given feature is T, false otherwise</returns>
45 public static bool Is<T>(string key)
47 Interop.SystemInfo.SystemInfoValueType valueType;
48 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
49 if (keyType == Interop.SystemInfo.SystemInfoType.None)
56 case Interop.SystemInfo.SystemInfoValueType.Bool:
57 return typeof(T) == typeof(bool);
58 case Interop.SystemInfo.SystemInfoValueType.Double:
59 return typeof(T) == typeof(double);
60 case Interop.SystemInfo.SystemInfoValueType.Int:
61 return typeof(T) == typeof(int);
62 case Interop.SystemInfo.SystemInfoValueType.String:
63 return typeof(T) == typeof(string);
69 /// Checks if given key is valid feature
71 /// <param name="key">The name of the feature</param>
72 /// <returns>true of key is valid, false otherwise</returns>
73 public static bool IsValidKey(string key)
75 Interop.SystemInfo.SystemInfoValueType valueType;
76 return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
80 /// Gets the value of the feature.
82 /// <typeparam name="T">Type of key value</typeparam>
83 /// <param name="key">The name of the feature</param>
84 /// <param name="value">The value of the given feature</param>
85 /// <returns>return true on success otherwise false</returns>
86 public static bool TryGetValue<T>(string key, out T value)
89 if (typeof(T) == typeof(bool))
92 res = TryGetValue(key, out val);
93 value = (T)(object)val;
95 else if (typeof(T) == typeof(int))
98 res = TryGetValue(key, out val);
99 value = (T)(object)val;
101 else if (typeof(T) == typeof(double))
104 res = TryGetValue(key, out val);
105 value = (T)(object)val;
107 else if (typeof(T) == typeof(string))
110 res = TryGetValue(key, out val);
111 value = (T)(object)val;
121 /// Gets the bool value of the feature.
123 /// <param name="key">The name of the feature</param>
124 /// <param name="value">The value of the given feature</param>
125 /// <returns>return true on success otherwise false</returns>
126 public static bool TryGetValue(string key, out bool value)
128 Interop.SystemInfo.SystemInfoValueType valueType;
129 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
131 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
132 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
134 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
136 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
138 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
144 if (err != Interop.SystemInfo.ErrorCode.None)
146 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
154 /// Gets the int value of the feature.
156 /// <param name="key">The name of the feature</param>
157 /// <param name="value">The value of the given feature</param>
158 /// <returns>return true on success otherwise false</returns>
159 public static bool TryGetValue(string key, out int value)
161 Interop.SystemInfo.SystemInfoValueType valueType;
162 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
164 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
165 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
167 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
169 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
171 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
178 if (err != Interop.SystemInfo.ErrorCode.None)
180 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
188 /// Gets the double value of the feature.
190 /// <param name="key">The name of the feature</param>
191 /// <param name="value">The value of the given feature</param>
192 /// <returns>return true on success otherwise false</returns>
193 public static bool TryGetValue(string key, out double value)
195 Interop.SystemInfo.SystemInfoValueType valueType;
196 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
198 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
199 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
201 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
203 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
205 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
212 if (err != Interop.SystemInfo.ErrorCode.None)
214 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
222 /// Gets the string value of the feature.
224 /// <param name="key">The name of the feature</param>
225 /// <param name="value">The value of the given feature</param>
226 /// <returns>return true on success otherwise false</returns>
227 public static bool TryGetValue(string key, out string value)
229 Interop.SystemInfo.SystemInfoValueType valueType;
230 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
232 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
233 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
235 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
237 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
239 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
243 value = string.Empty;
246 if (err != Interop.SystemInfo.ErrorCode.None)
248 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));