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 type of value for given feature is T
52 /// <typeparam name="T">Type of value for feature key</typeparam>
53 /// <param name="key">The name of the feature</param>
54 /// <returns>true if type of value for given feature is T, false otherwise</returns>
55 public static bool Is<T>(string key)
57 Interop.SystemInfo.SystemInfoValueType valueType;
58 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
59 if (keyType == Interop.SystemInfo.SystemInfoType.None)
66 case Interop.SystemInfo.SystemInfoValueType.Bool:
67 return typeof(T) == typeof(bool);
68 case Interop.SystemInfo.SystemInfoValueType.Double:
69 return typeof(T) == typeof(double);
70 case Interop.SystemInfo.SystemInfoValueType.Int:
71 return typeof(T) == typeof(int);
72 case Interop.SystemInfo.SystemInfoValueType.String:
73 return typeof(T) == typeof(string);
79 /// Checks if given key is valid feature
81 /// <param name="key">The name of the feature</param>
82 /// <returns>true of key is valid, false otherwise</returns>
83 public static bool IsValidKey(string key)
85 Interop.SystemInfo.SystemInfoValueType valueType;
86 return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
90 /// Gets the value of the feature.
92 /// <typeparam name="T">Type of key value</typeparam>
93 /// <param name="key">The name of the feature</param>
94 /// <param name="value">The value of the given feature</param>
95 /// <returns>return true on success otherwise false</returns>
96 public static bool TryGetValue<T>(string key, out T value)
99 if (typeof(T) == typeof(bool))
102 res = TryGetValue(key, out val);
103 value = (T)(object)val;
105 else if (typeof(T) == typeof(int))
108 res = TryGetValue(key, out val);
109 value = (T)(object)val;
111 else if (typeof(T) == typeof(double))
114 res = TryGetValue(key, out val);
115 value = (T)(object)val;
117 else if (typeof(T) == typeof(string))
120 res = TryGetValue(key, out val);
121 value = (T)(object)val;
131 /// Gets the bool value of the feature.
133 /// <param name="key">The name of the feature</param>
134 /// <param name="value">The value of the given feature</param>
135 /// <returns>return true on success otherwise false</returns>
136 public static bool TryGetValue(string key, out bool value)
138 Interop.SystemInfo.SystemInfoValueType valueType;
139 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
141 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
142 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
144 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
146 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
148 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
154 if (err != Interop.SystemInfo.ErrorCode.None)
156 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
164 /// Gets the int value of the feature.
166 /// <param name="key">The name of the feature</param>
167 /// <param name="value">The value of the given feature</param>
168 /// <returns>return true on success otherwise false</returns>
169 public static bool TryGetValue(string key, out int value)
171 Interop.SystemInfo.SystemInfoValueType valueType;
172 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
174 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
175 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
177 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
179 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
181 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
188 if (err != Interop.SystemInfo.ErrorCode.None)
190 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
198 /// Gets the double value of the feature.
200 /// <param name="key">The name of the feature</param>
201 /// <param name="value">The value of the given feature</param>
202 /// <returns>return true on success otherwise false</returns>
203 public static bool TryGetValue(string key, out double value)
205 Interop.SystemInfo.SystemInfoValueType valueType;
206 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
208 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
209 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
211 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
213 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
215 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
222 if (err != Interop.SystemInfo.ErrorCode.None)
224 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
232 /// Gets the string value of the feature.
234 /// <param name="key">The name of the feature</param>
235 /// <param name="value">The value of the given feature</param>
236 /// <returns>return true on success otherwise false</returns>
237 public static bool TryGetValue(string key, out string value)
239 Interop.SystemInfo.SystemInfoValueType valueType;
240 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
242 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
243 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
245 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
247 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
249 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
253 value = string.Empty;
256 if (err != Interop.SystemInfo.ErrorCode.None)
258 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));