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.
19 namespace Tizen.System
22 /// System Information class. This class has methods which can be used to obtain device information
24 public static class SystemInfo
26 private const string LogTag = "Tizen.System";
28 private static Interop.SystemInfo.SystemInfoType GetValueType(string key, out Interop.SystemInfo.SystemInfoValueType valueType)
30 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.SystemInfoGetPlatformType(key, out valueType);
31 if (err == Interop.SystemInfo.ErrorCode.None)
33 return Interop.SystemInfo.SystemInfoType.platform;
36 Log.Debug(LogTag, string.Format("Key {0} not in platform system info", key));
37 err = Interop.SystemInfo.SystemInfoGetCustomType(key, out valueType);
38 if (err == Interop.SystemInfo.ErrorCode.None)
40 return Interop.SystemInfo.SystemInfoType.Custom;
43 Log.Debug(LogTag, string.Format("Key {0} not in custom system info", key));
44 return Interop.SystemInfo.SystemInfoType.None;
48 /// Checks if type of value for given feature is T
50 /// <typeparam name="T">Type of value for feature key</typeparam>
51 /// <param name="key">The name of the feature</param>
52 /// <returns>true if type of value for given feature is T, false otherwise</returns>
53 public static bool Is<T>(string key)
55 Interop.SystemInfo.SystemInfoValueType valueType;
56 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
57 if (keyType == Interop.SystemInfo.SystemInfoType.None)
64 case Interop.SystemInfo.SystemInfoValueType.Bool:
65 return typeof(T) == typeof(bool);
66 case Interop.SystemInfo.SystemInfoValueType.Double:
67 return typeof(T) == typeof(double);
68 case Interop.SystemInfo.SystemInfoValueType.Int:
69 return typeof(T) == typeof(int);
70 case Interop.SystemInfo.SystemInfoValueType.String:
71 return typeof(T) == typeof(string);
77 /// Checks if given key is valid feature
79 /// <param name="key">The name of the feature</param>
80 /// <returns>true of key is valid, false otherwise</returns>
81 public static bool IsValidKey(string key)
83 Interop.SystemInfo.SystemInfoValueType valueType;
84 return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
88 /// Gets the value of the feature.
90 /// <typeparam name="T">Type of key value</typeparam>
91 /// <param name="key">The name of the feature</param>
92 /// <param name="value">The value of the given feature</param>
93 /// <returns>return true on success otherwise false</returns>
94 public static bool TryGetValue<T>(string key, out T value)
97 if (typeof(T) == typeof(bool))
100 res = TryGetValue(key, out val);
101 value = (T)(object)val;
103 else if (typeof(T) == typeof(int))
106 res = TryGetValue(key, out val);
107 value = (T)(object)val;
109 else if (typeof(T) == typeof(double))
112 res = TryGetValue(key, out val);
113 value = (T)(object)val;
115 else if (typeof(T) == typeof(string))
118 res = TryGetValue(key, out val);
119 value = (T)(object)val;
129 /// Gets the bool value of the feature.
131 /// <param name="key">The name of the feature</param>
132 /// <param name="value">The value of the given feature</param>
133 /// <returns>return true on success otherwise false</returns>
134 public static bool TryGetValue(string key, out bool value)
136 Interop.SystemInfo.SystemInfoValueType valueType;
137 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
139 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
140 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
142 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
144 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
146 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
152 if (err != Interop.SystemInfo.ErrorCode.None)
154 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
162 /// Gets the int value of the feature.
164 /// <param name="key">The name of the feature</param>
165 /// <param name="value">The value of the given feature</param>
166 /// <returns>return true on success otherwise false</returns>
167 public static bool TryGetValue(string key, out int value)
169 Interop.SystemInfo.SystemInfoValueType valueType;
170 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
172 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
173 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
175 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
177 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
179 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
186 if (err != Interop.SystemInfo.ErrorCode.None)
188 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
196 /// Gets the double value of the feature.
198 /// <param name="key">The name of the feature</param>
199 /// <param name="value">The value of the given feature</param>
200 /// <returns>return true on success otherwise false</returns>
201 public static bool TryGetValue(string key, out double value)
203 Interop.SystemInfo.SystemInfoValueType valueType;
204 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
206 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
207 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
209 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
211 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
213 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
220 if (err != Interop.SystemInfo.ErrorCode.None)
222 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
230 /// Gets the string value of the feature.
232 /// <param name="key">The name of the feature</param>
233 /// <param name="value">The value of the given feature</param>
234 /// <returns>return true on success otherwise false</returns>
235 public static bool TryGetValue(string key, out string value)
237 Interop.SystemInfo.SystemInfoValueType valueType;
238 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
240 Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
241 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
243 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
245 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
247 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
251 value = string.Empty;
254 if (err != Interop.SystemInfo.ErrorCode.None)
256 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));