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