Add EditorBrowsable attribute for internal components
[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 type of value for given feature is T
51         /// </summary>
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)
56         {
57             Interop.SystemInfo.SystemInfoValueType valueType;
58             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
59             if (keyType == Interop.SystemInfo.SystemInfoType.None)
60             {
61                 return false;
62             }
63
64             switch (valueType)
65             {
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);
74             }
75             return false;
76         }
77
78         /// <summary>
79         /// Checks if given key is valid feature
80         /// </summary>
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)
84         {
85             Interop.SystemInfo.SystemInfoValueType valueType;
86             return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
87         }
88
89         /// <summary>
90         /// Gets the value of the feature.
91         /// </summary>
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)
97         {
98             bool res = false;
99             if (typeof(T) == typeof(bool))
100             {
101                 bool val;
102                 res = TryGetValue(key, out val);
103                 value = (T)(object)val;
104             }
105             else if (typeof(T) == typeof(int))
106             {
107                 int val;
108                 res = TryGetValue(key, out val);
109                 value = (T)(object)val;
110             }
111             else if (typeof(T) == typeof(double))
112             {
113                 double val;
114                 res = TryGetValue(key, out val);
115                 value = (T)(object)val;
116             }
117             else if (typeof(T) == typeof(string))
118             {
119                 string val;
120                 res = TryGetValue(key, out val);
121                 value = (T)(object)val;
122             }
123             else
124             {
125                 value = default(T);
126             }
127             return res;
128         }
129
130         /// <summary>
131         /// Gets the bool value of the feature.
132         /// </summary>
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)
137         {
138             Interop.SystemInfo.SystemInfoValueType valueType;
139             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
140
141             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
142             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
143             {
144                 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
145             }
146             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
147             {
148                 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
149             } else
150             {
151                 value = false;
152             }
153
154             if (err != Interop.SystemInfo.ErrorCode.None)
155             {
156                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
157                 return false;
158             }
159
160             return true;
161         }
162
163         /// <summary>
164         /// Gets the int value of the feature.
165         /// </summary>
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)
170         {
171             Interop.SystemInfo.SystemInfoValueType valueType;
172             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
173
174             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
175             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
176             {
177                 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
178             }
179             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
180             {
181                 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
182             }
183             else
184             {
185                 value = 0;
186             }
187
188             if (err != Interop.SystemInfo.ErrorCode.None)
189             {
190                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
191                 return false;
192             }
193
194             return true;
195         }
196
197         /// <summary>
198         /// Gets the double value of the feature.
199         /// </summary>
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)
204         {
205             Interop.SystemInfo.SystemInfoValueType valueType;
206             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
207
208             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
209             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
210             {
211                 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
212             }
213             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
214             {
215                 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
216             }
217             else
218             {
219                 value = 0;
220             }
221
222             if (err != Interop.SystemInfo.ErrorCode.None)
223             {
224                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
225                 return false;
226             }
227
228             return true;
229         }
230
231         /// <summary>
232         /// Gets the string value of the feature.
233         /// </summary>
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)
238         {
239             Interop.SystemInfo.SystemInfoValueType valueType;
240             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
241
242             Interop.SystemInfo.ErrorCode err = Interop.SystemInfo.ErrorCode.InvalidParameter;
243             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
244             {
245                 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
246             }
247             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
248             {
249                 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
250             }
251             else
252             {
253                 value = string.Empty;
254             }
255
256             if (err != Interop.SystemInfo.ErrorCode.None)
257             {
258                 Log.Warn(LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
259                 return false;
260             }
261
262             return true;
263         }
264     }
265 }