Change names not to use the same name for a namespace and a type in that namespace
[platform/core/csapi/tizenfx.git] / src / Tizen.System / SystemInfo / SystemInfo.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10
11 namespace Tizen.System
12 {
13     /// <summary>
14     /// System Information class. This class has methods which can be used to obtain device information
15     /// </summary>
16     public static class SystemInfo
17     {
18         private const string LogTag = "Tizen.System";
19
20         private static Interop.SystemInfo.SystemInfoType GetValueType(string key, out Interop.SystemInfo.SystemInfoValueType valueType)
21         {
22             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.SystemInfoGetPlatformType(key, out valueType);
23             if (err == Interop.SystemInfo.ErrorCode.None)
24             {
25                 return Interop.SystemInfo.SystemInfoType.platform;
26             }
27
28             Log.Debug(LogTag, string.Format("Key {0} not in platform system info", key));
29             err = Interop.SystemInfo.SystemInfoGetCustomType(key, out valueType);
30             if (err == Interop.SystemInfo.ErrorCode.None)
31             {
32                 return Interop.SystemInfo.SystemInfoType.Custom;
33             }
34
35             Log.Debug(LogTag, string.Format("Key {0} not in custom system info", key));
36             return Interop.SystemInfo.SystemInfoType.None;
37         }
38
39         /// <summary>
40         /// Checks if type of value for given feature is T
41         /// </summary>
42         /// <typeparam name="T">Type of value for feature key</typeparam>
43         /// <param name="key">The name of the feature</param>
44         /// <returns>true if type of value for given feature is T, false otherwise</returns>
45         public static bool Is<T>(string key)
46         {
47             Interop.SystemInfo.SystemInfoValueType valueType;
48             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
49             if (keyType == Interop.SystemInfo.SystemInfoType.None)
50             {
51                 return false;
52             }
53
54             switch (valueType)
55             {
56                 case Interop.SystemInfo.SystemInfoValueType.Bool:
57                     return typeof(T) == typeof(bool);
58                 case Interop.SystemInfo.SystemInfoValueType.Double:
59                     return typeof(T) == typeof(double);
60                 case Interop.SystemInfo.SystemInfoValueType.Int:
61                     return typeof(T) == typeof(int);
62                 case Interop.SystemInfo.SystemInfoValueType.String:
63                     return typeof(T) == typeof(string);
64             }
65             return false;
66         }
67
68         /// <summary>
69         /// Checks if given key is valid feature
70         /// </summary>
71         /// <param name="key">The name of the feature</param>
72         /// <returns>true of key is valid, false otherwise</returns>
73         public static bool IsValidKey(string key)
74         {
75             Interop.SystemInfo.SystemInfoValueType valueType;
76             return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
77         }
78
79         /// <summary>
80         /// Gets the value of the feature.
81         /// </summary>
82         /// <typeparam name="T">Type of key value</typeparam>
83         /// <param name="key">The name of the feature</param>
84         /// <param name="value">The value of the given feature</param>
85         /// <returns>return true on success otherwise false</returns>
86         public static bool TryGetValue<T>(string key, out T value)
87         {
88             bool res = false;
89             if (typeof(T) == typeof(bool))
90             {
91                 bool val;
92                 res = TryGetValue(key, out val);
93                 value = (T)(object)val;
94             }
95             else if (typeof(T) == typeof(int))
96             {
97                 int val;
98                 res = TryGetValue(key, out val);
99                 value = (T)(object)val;
100             }
101             else if (typeof(T) == typeof(double))
102             {
103                 double val;
104                 res = TryGetValue(key, out val);
105                 value = (T)(object)val;
106             }
107             else if (typeof(T) == typeof(string))
108             {
109                 string val;
110                 res = TryGetValue(key, out val);
111                 value = (T)(object)val;
112             }
113             else
114             {
115                 value = default(T);
116             }
117             return res;
118         }
119
120         /// <summary>
121         /// Gets the bool value of the feature.
122         /// </summary>
123         /// <param name="key">The name of the feature</param>
124         /// <param name="value">The value of the given feature</param>
125         /// <returns>return true on success otherwise false</returns>
126         public static bool TryGetValue(string key, out bool value)
127         {
128             Interop.SystemInfo.SystemInfoValueType valueType;
129             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
130
131             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
132             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
133             {
134                 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
135             }
136             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
137             {
138                 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
139             } else
140             {
141                 value = false;
142             }
143
144             if (err != Interop.SystemInfo.ErrorCode.None)
145             {
146                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
147                 return false;
148             }
149
150             return true;
151         }
152
153         /// <summary>
154         /// Gets the int value of the feature.
155         /// </summary>
156         /// <param name="key">The name of the feature</param>
157         /// <param name="value">The value of the given feature</param>
158         /// <returns>return true on success otherwise false</returns>
159         public static bool TryGetValue(string key, out int value)
160         {
161             Interop.SystemInfo.SystemInfoValueType valueType;
162             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
163
164             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
165             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
166             {
167                 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
168             }
169             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
170             {
171                 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
172             }
173             else
174             {
175                 value = 0;
176             }
177
178             if (err != Interop.SystemInfo.ErrorCode.None)
179             {
180                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
181                 return false;
182             }
183
184             return true;
185         }
186
187         /// <summary>
188         /// Gets the double value of the feature.
189         /// </summary>
190         /// <param name="key">The name of the feature</param>
191         /// <param name="value">The value of the given feature</param>
192         /// <returns>return true on success otherwise false</returns>
193         public static bool TryGetValue(string key, out double value)
194         {
195             Interop.SystemInfo.SystemInfoValueType valueType;
196             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
197
198             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
199             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
200             {
201                 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
202             }
203             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
204             {
205                 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
206             }
207             else
208             {
209                 value = 0;
210             }
211
212             if (err != Interop.SystemInfo.ErrorCode.None)
213             {
214                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
215                 return false;
216             }
217
218             return true;
219         }
220
221         /// <summary>
222         /// Gets the string value of the feature.
223         /// </summary>
224         /// <param name="key">The name of the feature</param>
225         /// <param name="value">The value of the given feature</param>
226         /// <returns>return true on success otherwise false</returns>
227         public static bool TryGetValue(string key, out string value)
228         {
229             Interop.SystemInfo.SystemInfoValueType valueType;
230             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
231
232             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
233             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
234             {
235                 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
236             }
237             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
238             {
239                 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
240             }
241             else
242             {
243                 value = string.Empty;
244             }
245
246             if (err != Interop.SystemInfo.ErrorCode.None)
247             {
248                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
249                 return false;
250             }
251
252             return true;
253         }
254     }
255 }