Release 4.0.0-preview1-00201
[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     [EditorBrowsable(EditorBrowsableState.Never)]
23     internal static class SystemInfo
24     {
25         [EditorBrowsable(EditorBrowsableState.Never)]
26         private static Interop.SystemInfo.SystemInfoType GetValueType(string key, out Interop.SystemInfo.SystemInfoValueType valueType)
27         {
28             InformationError err = Interop.SystemInfo.SystemInfoGetPlatformType(key, out valueType);
29             if (err == InformationError.None)
30             {
31                 return Interop.SystemInfo.SystemInfoType.platform;
32             }
33
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)
37             {
38                 return Interop.SystemInfo.SystemInfoType.Custom;
39             }
40
41             Log.Debug(InformationErrorFactory.LogTag, string.Format("Key {0} not in custom system info", key));
42             return Interop.SystemInfo.SystemInfoType.None;
43         }
44
45         /// <summary>
46         /// Checks if the type of value for the given feature is T.
47         /// </summary>
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)
52         {
53             Interop.SystemInfo.SystemInfoValueType valueType;
54             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
55             if (keyType == Interop.SystemInfo.SystemInfoType.None)
56             {
57                 return false;
58             }
59
60             switch (valueType)
61             {
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);
70             }
71             return false;
72         }
73
74         /// <summary>
75         /// Checks if the given key is a valid feature.
76         /// </summary>
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)
80         {
81             Interop.SystemInfo.SystemInfoValueType valueType;
82             return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
83         }
84
85         /// <summary>
86         /// Gets the value of the feature.
87         /// </summary>
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)
93         {
94             bool res = false;
95             if (typeof(T) == typeof(bool))
96             {
97                 bool val;
98                 res = TryGetValue(key, out val);
99                 value = (T)(object)val;
100             }
101             else if (typeof(T) == typeof(int))
102             {
103                 int val;
104                 res = TryGetValue(key, out val);
105                 value = (T)(object)val;
106             }
107             else if (typeof(T) == typeof(double))
108             {
109                 double val;
110                 res = TryGetValue(key, out val);
111                 value = (T)(object)val;
112             }
113             else if (typeof(T) == typeof(string))
114             {
115                 string val;
116                 res = TryGetValue(key, out val);
117                 value = (T)(object)val;
118             }
119             else
120             {
121                 value = default(T);
122             }
123             return res;
124         }
125
126         /// <summary>
127         /// Gets the bool value of the feature.
128         /// </summary>
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)
133         {
134             Interop.SystemInfo.SystemInfoValueType valueType;
135             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
136
137             InformationError err = InformationError.InvalidParameter;
138             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
139             {
140                 err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
141             }
142             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
143             {
144                 err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
145             } else
146             {
147                 value = false;
148             }
149
150             if (err != InformationError.None)
151             {
152                 Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
153                 return false;
154             }
155
156             return true;
157         }
158
159         /// <summary>
160         /// Gets the int value of the feature.
161         /// </summary>
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)
166         {
167             Interop.SystemInfo.SystemInfoValueType valueType;
168             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
169
170             InformationError err = InformationError.InvalidParameter;
171             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
172             {
173                 err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
174             }
175             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
176             {
177                 err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
178             }
179             else
180             {
181                 value = 0;
182             }
183
184             if (err != InformationError.None)
185             {
186                 Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
187                 return false;
188             }
189
190             return true;
191         }
192
193         /// <summary>
194         /// Gets the double value of the feature.
195         /// </summary>
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)
200         {
201             Interop.SystemInfo.SystemInfoValueType valueType;
202             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
203
204             InformationError err = InformationError.InvalidParameter;
205             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
206             {
207                 err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
208             }
209             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
210             {
211                 err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
212             }
213             else
214             {
215                 value = 0;
216             }
217
218             if (err != InformationError.None)
219             {
220                 Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
221                 return false;
222             }
223
224             return true;
225         }
226
227         /// <summary>
228         /// Gets the string value of the feature.
229         /// </summary>
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)
234         {
235             Interop.SystemInfo.SystemInfoValueType valueType;
236             Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
237
238             InformationError err = InformationError.InvalidParameter;
239             if (keyType == Interop.SystemInfo.SystemInfoType.platform)
240             {
241                 err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
242             }
243             else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
244             {
245                 err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
246             }
247             else
248             {
249                 value = string.Empty;
250             }
251
252             if (err != InformationError.None)
253             {
254                 Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
255                 return false;
256             }
257
258             return true;
259         }
260     }
261 }