Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Information / SystemInfo / SystemInfo.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18 using System.ComponentModel;
19
20 namespace Tizen.System
21 {
22     /// <summary>
23     /// System Information class. This class has methods which can be used to obtain device information.
24     /// </summary>
25     public static class SystemInfo
26     {
27         private const string LogTag = "Tizen.System";
28
29         [EditorBrowsable(EditorBrowsableState.Never)]
30         private static Interop.SystemInfo.SystemInfoType GetValueType(string key, out Interop.SystemInfo.SystemInfoValueType valueType)
31         {
32             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.SystemInfoGetPlatformType(key, out valueType);
33             if (err == Interop.SystemInfo.ErrorCode.None)
34             {
35                 return Interop.SystemInfo.SystemInfoType.platform;
36             }
37
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)
41             {
42                 return Interop.SystemInfo.SystemInfoType.Custom;
43             }
44
45             Log.Debug(LogTag, string.Format("Key {0} not in custom system info", key));
46             return Interop.SystemInfo.SystemInfoType.None;
47         }
48
49         /// <summary>
50         /// Checks if the type of value for the given feature is T.
51         /// </summary>
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)
57         {
58             Interop.SystemInfo.SystemInfoValueType valueType;
59             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
60             if (keyType == Interop.SystemInfo.SystemInfoType.None)
61             {
62                 return false;
63             }
64
65             switch (valueType)
66             {
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);
75             }
76             return false;
77         }
78
79         /// <summary>
80         /// Checks if the given key is a valid feature.
81         /// </summary>
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)
86         {
87             Interop.SystemInfo.SystemInfoValueType valueType;
88             return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
89         }
90
91         /// <summary>
92         /// Gets the value of the feature.
93         /// </summary>
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)
100         {
101             bool res = false;
102             if (typeof(T) == typeof(bool))
103             {
104                 bool val;
105                 res = TryGetValue(key, out val);
106                 value = (T)(object)val;
107             }
108             else if (typeof(T) == typeof(int))
109             {
110                 int val;
111                 res = TryGetValue(key, out val);
112                 value = (T)(object)val;
113             }
114             else if (typeof(T) == typeof(double))
115             {
116                 double val;
117                 res = TryGetValue(key, out val);
118                 value = (T)(object)val;
119             }
120             else if (typeof(T) == typeof(string))
121             {
122                 string val;
123                 res = TryGetValue(key, out val);
124                 value = (T)(object)val;
125             }
126             else
127             {
128                 value = default(T);
129             }
130             return res;
131         }
132
133         /// <summary>
134         /// Gets the bool value of the feature.
135         /// </summary>
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)
141         {
142             Interop.SystemInfo.SystemInfoValueType valueType;
143             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
144
145             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
146             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
147             {
148                 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
149             }
150             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
151             {
152                 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
153             } else
154             {
155                 value = false;
156             }
157
158             if (err != Interop.SystemInfo.ErrorCode.None)
159             {
160                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
161                 return false;
162             }
163
164             return true;
165         }
166
167         /// <summary>
168         /// Gets the int value of the feature.
169         /// </summary>
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)
175         {
176             Interop.SystemInfo.SystemInfoValueType valueType;
177             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
178
179             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
180             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
181             {
182                 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
183             }
184             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
185             {
186                 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
187             }
188             else
189             {
190                 value = 0;
191             }
192
193             if (err != Interop.SystemInfo.ErrorCode.None)
194             {
195                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
196                 return false;
197             }
198
199             return true;
200         }
201
202         /// <summary>
203         /// Gets the double value of the feature.
204         /// </summary>
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)
210         {
211             Interop.SystemInfo.SystemInfoValueType valueType;
212             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
213
214             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
215             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
216             {
217                 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
218             }
219             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
220             {
221                 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
222             }
223             else
224             {
225                 value = 0;
226             }
227
228             if (err != Interop.SystemInfo.ErrorCode.None)
229             {
230                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
231                 return false;
232             }
233
234             return true;
235         }
236
237         /// <summary>
238         /// Gets the string value of the feature.
239         /// </summary>
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)
245         {
246             Interop.SystemInfo.SystemInfoValueType valueType;
247             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
248
249             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
250             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
251             {
252                 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
253             }
254             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
255             {
256                 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
257             }
258             else
259             {
260                 value = string.Empty;
261             }
262
263             if (err != Interop.SystemInfo.ErrorCode.None)
264             {
265                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
266                 return false;
267             }
268
269             return true;
270         }
271     }
272 }