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
22 [EditorBrowsable(EditorBrowsableState.Never)]
23 internal static class SystemInfo
25 [EditorBrowsable(EditorBrowsableState.Never)]
26 private static Interop.SystemInfo.SystemInfoType GetValueType(string key, out Interop.SystemInfo.SystemInfoValueType valueType)
28 InformationError err = Interop.SystemInfo.SystemInfoGetPlatformType(key, out valueType);
29 if (err == InformationError.None)
31 return Interop.SystemInfo.SystemInfoType.platform;
34 Log.Debug(InformationErrorFactory.LogTag, string.Format("Key {0} not in platform system info", key));
35 err = Interop.SystemInfo.SystemInfoGetCustomType(key, out valueType);
36 if (err == InformationError.None)
38 return Interop.SystemInfo.SystemInfoType.Custom;
41 Log.Debug(InformationErrorFactory.LogTag, string.Format("Key {0} not in custom system info", key));
42 return Interop.SystemInfo.SystemInfoType.None;
46 /// Checks if the type of value for the given feature is T.
48 /// <typeparam name="T">Type of value for the feature key.</typeparam>
49 /// <param name="key">The name of the feature.</param>
50 /// <returns>True if type of value for the given feature is T, otherwise false.</returns>
51 internal static bool Is<T>(string key)
53 Interop.SystemInfo.SystemInfoValueType valueType;
54 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
55 if (keyType == Interop.SystemInfo.SystemInfoType.None)
62 case Interop.SystemInfo.SystemInfoValueType.Bool:
63 return typeof(T) == typeof(bool);
64 case Interop.SystemInfo.SystemInfoValueType.Double:
65 return typeof(T) == typeof(double);
66 case Interop.SystemInfo.SystemInfoValueType.Int:
67 return typeof(T) == typeof(int);
68 case Interop.SystemInfo.SystemInfoValueType.String:
69 return typeof(T) == typeof(string);
75 /// Checks if the given key is a valid feature.
77 /// <param name="key">The name of the feature.</param>
78 /// <returns>True if the key is valid, otherwise false.</returns>
79 internal static bool IsValidKey(string key)
81 Interop.SystemInfo.SystemInfoValueType valueType;
82 return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
86 /// Gets the value of the feature.
88 /// <typeparam name="T">Type of key value.</typeparam>
89 /// <param name="key">The name of the feature.</param>
90 /// <param name="value">The value of the given feature.</param>
91 /// <returns>Returns true on success, otherwise false.</returns>
92 internal static bool TryGetValue<T>(string key, out T value)
95 if (typeof(T) == typeof(bool))
98 res = TryGetValue(key, out val);
99 value = (T)(object)val;
101 else if (typeof(T) == typeof(int))
104 res = TryGetValue(key, out val);
105 value = (T)(object)val;
107 else if (typeof(T) == typeof(double))
110 res = TryGetValue(key, out val);
111 value = (T)(object)val;
113 else if (typeof(T) == typeof(string))
116 res = TryGetValue(key, out val);
117 value = (T)(object)val;
127 /// Gets the bool value of the feature.
129 /// <param name="key">The name of the feature.</param>
130 /// <param name="value">The value of the given feature.</param>
131 /// <returns>Returns true on success, otherwise false.</returns>
132 internal static bool TryGetValue(string key, out bool value)
134 Interop.SystemInfo.SystemInfoValueType valueType;
135 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
137 InformationError err = InformationError.InvalidParameter;
138 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
140 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
142 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
144 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
150 if (err != InformationError.None)
152 Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
160 /// Gets the int value of the feature.
162 /// <param name="key">The name of the feature.</param>
163 /// <param name="value">The value of the given feature.</param>
164 /// <returns>Returns true on success, otherwise false.</returns>
165 internal static bool TryGetValue(string key, out int value)
167 Interop.SystemInfo.SystemInfoValueType valueType;
168 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
170 InformationError err = InformationError.InvalidParameter;
171 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
173 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
175 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
177 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
184 if (err != InformationError.None)
186 Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
194 /// Gets the double value of the feature.
196 /// <param name="key">The name of the feature.</param>
197 /// <param name="value">The value of the given feature.</param>
198 /// <returns>Returns true on success, otherwise false.</returns>
199 internal static bool TryGetValue(string key, out double value)
201 Interop.SystemInfo.SystemInfoValueType valueType;
202 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
204 InformationError err = InformationError.InvalidParameter;
205 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
207 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
209 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
211 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
218 if (err != InformationError.None)
220 Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
228 /// Gets the string value of the feature.
230 /// <param name="key">The name of the feature.</param>
231 /// <param name="value">The value of the given feature.</param>
232 /// <returns>Returns true on success, otherwise false.</returns>
233 internal static bool TryGetValue(string key, out string value)
235 Interop.SystemInfo.SystemInfoValueType valueType;
236 Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
238 InformationError err = InformationError.InvalidParameter;
239 if (keyType == Interop.SystemInfo.SystemInfoType.platform)
241 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
243 else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
245 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
249 value = string.Empty;
252 if (err != InformationError.None)
254 Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));