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.ComponentModel;
20 namespace Tizen.System
23 /// System Information class. This class has methods which can be used to obtain device information.
25 public static class SystemInfo
27 private const string LogTag = "Tizen.System";
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 private static Interop.SystemInfo.SystemInfoType GetValueType(string key, out Interop.SystemInfo.SystemInfoValueType valueType)
32 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.SystemInfoGetPlatformType(key, out valueType);
33 if (err == Interop.SystemInfo.ErrorCode.None)
35 return Interop.SystemInfo.SystemInfoType.platform;
38 Log.Debug(LogTag, string.Format("Key {0} not in platform system info", key));
39 err = Interop.SystemInfo.SystemInfoGetCustomType(key, out valueType);
40 if (err == Interop.SystemInfo.ErrorCode.None)
42 return Interop.SystemInfo.SystemInfoType.Custom;
45 Log.Debug(LogTag, string.Format("Key {0} not in custom system info", key));
46 return Interop.SystemInfo.SystemInfoType.None;
50 /// Checks if the type of value for the given feature is T.
52 /// <since_tizen> 3 </since_tizen>
53 /// <typeparam name="T">Type of value for the feature key.</typeparam>
54 /// <param name="key">The name of the feature.</param>
55 /// <returns>True if type of value for the given feature is T, otherwise false.</returns>
56 public static bool Is<T>(string key)
58 Interop.SystemInfo.SystemInfoValueType valueType;
59 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
60 if (keyType == Interop.SystemInfo.SystemInfoType.None)
67 case Interop.SystemInfo.SystemInfoValueType.Bool:
68 return typeof(T) == typeof(bool);
69 case Interop.SystemInfo.SystemInfoValueType.Double:
70 return typeof(T) == typeof(double);
71 case Interop.SystemInfo.SystemInfoValueType.Int:
72 return typeof(T) == typeof(int);
73 case Interop.SystemInfo.SystemInfoValueType.String:
74 return typeof(T) == typeof(string);
80 /// Checks if the given key is a valid feature.
82 /// <since_tizen> 3 </since_tizen>
83 /// <param name="key">The name of the feature.</param>
84 /// <returns>True if the key is valid, otherwise false.</returns>
85 public static bool IsValidKey(string key)
87 Interop.SystemInfo.SystemInfoValueType valueType;
88 return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
92 /// Gets the value of the feature.
94 /// <since_tizen> 3 </since_tizen>
95 /// <typeparam name="T">Type of key value.</typeparam>
96 /// <param name="key">The name of the feature.</param>
97 /// <param name="value">The value of the given feature.</param>
98 /// <returns>Returns true on success, otherwise false.</returns>
99 public static bool TryGetValue<T>(string key, out T value)
102 if (typeof(T) == typeof(bool))
105 res = TryGetValue(key, out val);
106 value = (T)(object)val;
108 else if (typeof(T) == typeof(int))
111 res = TryGetValue(key, out val);
112 value = (T)(object)val;
114 else if (typeof(T) == typeof(double))
117 res = TryGetValue(key, out val);
118 value = (T)(object)val;
120 else if (typeof(T) == typeof(string))
123 res = TryGetValue(key, out val);
124 value = (T)(object)val;
134 /// Gets the bool value of the feature.
136 /// <since_tizen> 3 </since_tizen>
137 /// <param name="key">The name of the feature.</param>
138 /// <param name="value">The value of the given feature.</param>
139 /// <returns>Returns true on success, otherwise false.</returns>
140 public static bool TryGetValue(string key, out bool value)
142 Interop.SystemInfo.SystemInfoValueType valueType;
143 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
145 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
146 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
148 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
150 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
152 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
158 if (err != Interop.SystemInfo.ErrorCode.None)
160 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
168 /// Gets the int value of the feature.
170 /// <since_tizen> 3 </since_tizen>
171 /// <param name="key">The name of the feature.</param>
172 /// <param name="value">The value of the given feature.</param>
173 /// <returns>Returns true on success, otherwise false.</returns>
174 public static bool TryGetValue(string key, out int value)
176 Interop.SystemInfo.SystemInfoValueType valueType;
177 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
179 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
180 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
182 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
184 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
186 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
193 if (err != Interop.SystemInfo.ErrorCode.None)
195 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
203 /// Gets the double value of the feature.
205 /// <since_tizen> 3 </since_tizen>
206 /// <param name="key">The name of the feature.</param>
207 /// <param name="value">The value of the given feature.</param>
208 /// <returns>Returns true on success, otherwise false.</returns>
209 public static bool TryGetValue(string key, out double value)
211 Interop.SystemInfo.SystemInfoValueType valueType;
212 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
214 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
215 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
217 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
219 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
221 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
228 if (err != Interop.SystemInfo.ErrorCode.None)
230 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
238 /// Gets the string value of the feature.
240 /// <since_tizen> 3 </since_tizen>
241 /// <param name="key">The name of the feature.</param>
242 /// <param name="value">The value of the given feature.</param>
243 /// <returns>Returns true on success, otherwise false.</returns>
244 public static bool TryGetValue(string key, out string value)
246 Interop.SystemInfo.SystemInfoValueType valueType;
247 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
249 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
250 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
252 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
254 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
256 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
260 value = string.Empty;
263 if (err != Interop.SystemInfo.ErrorCode.None)
265 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));