Release 4.0.0-preview1-00201
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Information / Common / Information.cs
1 /*
2 * Copyright (c) 2017 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.Collections.Generic;
19 using System.ComponentModel;
20 using System.IO;
21
22 namespace Tizen.System
23 {
24     /// <summary>
25     /// The Information class provides functions to obtain various system information.
26     /// </summary>
27     public static class Information
28     {
29         internal const string HttpPrefix = "http://";
30         internal const string RuntimeInfoStringKeyPrefix = "tizen.org/runtimefeature/";
31
32         internal const string RuntimeInfoStringKeyBluetooth = "bluetooth";
33         internal const string RuntimeInfoStringKeyTetheringWiFi = "tethering.wifi";
34         internal const string RuntimeInfoStringKeyTetheringBluetooth = "tethering.bluetooth";
35         internal const string RuntimeInfoStringKeyTetheringUsb = "tethering.usb";
36         internal const string RuntimeInfoStringKeyPacketData = "packetdata";
37         internal const string RuntimeInfoStringKeyDataRoaming = "dataroaming";
38         internal const string RuntimeInfoStringKeyVibration = "vibration";
39         internal const string RuntimeInfoStringKeyAudioJackConnected = "audiojack.connected";
40         internal const string RuntimeInfoStringKeyBatteryCharging = "battery.charging";
41         internal const string RuntimeInfoStringKeyTvOut = "tvout";
42         internal const string RuntimeInfoStringKeyCharger = "charger";
43         internal const string RuntimeInfoStringKeyAutoRotation = "autorotation";
44         internal const string RuntimeInfoStringKeyGps = "gps";
45         internal const string RuntimeInfoStringKeyAudioJackType = "audiojack.type";
46
47
48         private static readonly Dictionary<string, RuntimeInfoKey> StringEnumMapping = new Dictionary<string, RuntimeInfoKey>
49         {
50             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyBluetooth] = RuntimeInfoKey.Bluetooth,
51             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyTetheringWiFi] = RuntimeInfoKey.WifiHotspot,
52             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyTetheringBluetooth] = RuntimeInfoKey.BluetoothTethering,
53             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyTetheringUsb] = RuntimeInfoKey.UsbTethering,
54             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyPacketData] = RuntimeInfoKey.PacketData,
55             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyDataRoaming] = RuntimeInfoKey.DataRoaming,
56             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyVibration] = RuntimeInfoKey.Vibration,
57             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyAudioJackConnected] = RuntimeInfoKey.AudioJack,
58             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyBatteryCharging] = RuntimeInfoKey.BatteryIsCharging,
59             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyTvOut] = RuntimeInfoKey.TvOut,
60             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyCharger] = RuntimeInfoKey.Charger,
61             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyAutoRotation] = RuntimeInfoKey.AutoRotation,
62             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyGps] = RuntimeInfoKey.Gps,
63             [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyAudioJackType] = RuntimeInfoKey.AudioJackConnector
64         };
65
66         internal static readonly Dictionary<RuntimeInfoKey, string> EnumStringMapping = new Dictionary<RuntimeInfoKey, string>
67         {
68             [RuntimeInfoKey.Bluetooth] = RuntimeInfoStringKeyBluetooth,
69             [RuntimeInfoKey.WifiHotspot] = RuntimeInfoStringKeyTetheringWiFi,
70             [RuntimeInfoKey.BluetoothTethering] = RuntimeInfoStringKeyTetheringBluetooth,
71             [RuntimeInfoKey.UsbTethering] = RuntimeInfoStringKeyTetheringUsb,
72             [RuntimeInfoKey.PacketData] = RuntimeInfoStringKeyPacketData,
73             [RuntimeInfoKey.DataRoaming] = RuntimeInfoStringKeyDataRoaming,
74             [RuntimeInfoKey.Vibration] = RuntimeInfoStringKeyVibration,
75             [RuntimeInfoKey.AudioJack] = RuntimeInfoStringKeyAudioJackConnected,
76             [RuntimeInfoKey.BatteryIsCharging] = RuntimeInfoStringKeyBatteryCharging,
77             [RuntimeInfoKey.TvOut] = RuntimeInfoStringKeyTvOut,
78             [RuntimeInfoKey.Charger] = RuntimeInfoStringKeyCharger,
79             [RuntimeInfoKey.AutoRotation] = RuntimeInfoStringKeyAutoRotation,
80             [RuntimeInfoKey.Gps] = RuntimeInfoStringKeyGps,
81             [RuntimeInfoKey.AudioJackConnector] = RuntimeInfoStringKeyAudioJackType
82         };
83
84         [EditorBrowsable(EditorBrowsableState.Never)]
85         private static bool ConvertStringToRuntimeInfoKey(string key, out RuntimeInfoKey feature)
86         {
87             string filteredKey = key.StartsWith(HttpPrefix) ? key.Substring(HttpPrefix.Length) : key;
88             feature = default(RuntimeInfoKey);
89
90             return StringEnumMapping.TryGetValue(filteredKey, out feature);
91         }
92
93         [EditorBrowsable(EditorBrowsableState.Never)]
94         private static bool TryGetRuntimeInfoValue<T>(RuntimeInfoKey key, out T value)
95         {
96             value = default(T);
97
98             if (!RuntimeInfo.Is<T>(key))
99             {
100                 Log.Error(InformationErrorFactory.LogTag, "Invalid return type");
101                 return false;
102             }
103
104             return RuntimeInfo.TryGetValue<T>(key, out value);
105         }
106
107         [EditorBrowsable(EditorBrowsableState.Never)]
108         private static bool TryGetSystemInfoValue<T>(string key, out T value)
109         {
110             value = default(T);
111
112             if (!SystemInfo.IsValidKey(key))
113             {
114                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
115                 return false;
116             }
117
118             if (!SystemInfo.Is<T>(key))
119             {
120                 Log.Error(InformationErrorFactory.LogTag, "Invalid return type");
121                 return false;
122             }
123
124             return SystemInfo.TryGetValue<T>(key, out value);
125         }
126
127         /// <summary>
128         /// Gets the value of the feature.
129         /// </summary>
130         /// <since_tizen> 4 </since_tizen>
131         /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
132         /// <param name="key">The name of the feature.</param>
133         /// <param name="value">The value of the given feature.</param>
134         /// <returns>Returns true on success, otherwise false.</returns>
135         public static bool TryGetValue<T>(string key, out T value)
136         {
137             RuntimeInfoKey runtimeFeature;
138
139             if (ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
140             {
141                 return TryGetRuntimeInfoValue<T>(runtimeFeature, out value);
142             }
143             else
144             {
145                 return TryGetSystemInfoValue<T>(key, out value);
146             }
147         }
148
149         /// <summary>
150         /// Registers a change event callback for given runtime feature key.
151         /// </summary>
152         /// <remarks>
153         /// This function is only for runtime feature.
154         /// </remarks>
155         /// <since_tizen> 4 </since_tizen>
156         /// <param name="key">The name of runtime feature which wants to register callback.</param>
157         /// <param name="callback">The callback function to subscribe.</param>
158         /// <exception cref="ArgumentException">Thrown when the <paramref name="key"/> is invalid.</exception>
159         /// <exception cref="NotSupportedException">Thrown when the feature related <paramref name="key"/> is not supported.</exception>
160         public static void SetCallback(string key, EventHandler<RuntimeFeatureStatusChangedEventArgs> callback)
161         {
162             RuntimeInfoKey runtimeFeature;
163
164             if (!ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
165             {
166                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
167                 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
168             }
169
170             RuntimeInfo.SetCallback(runtimeFeature, callback);
171         }
172
173         /// <summary>
174         /// Unregisters a change event callback for given runtime feature key.
175         /// </summary>
176         /// <remarks>
177         /// This function is only for runtime feature.
178         /// </remarks>
179         /// <since_tizen> 4 </since_tizen>
180         /// <param name="key">The name of runtime feature which wants to unregister callback.</param>
181         /// <param name="callback">The callback function to unsubscribe.</param>
182         /// <exception cref="ArgumentException">Thrown when the <paramref name="key"/> is invalid.</exception>
183         /// <exception cref="NotSupportedException">Thrown when the feature related <paramref name="key"/> is not supported.</exception>
184         public static void UnsetCallback(string key, EventHandler<RuntimeFeatureStatusChangedEventArgs> callback)
185         {
186             RuntimeInfoKey runtimeFeature;
187
188             if (!ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
189             {
190                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
191                 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
192             }
193
194             RuntimeInfo.UnsetCallback(runtimeFeature, callback);
195         }
196     }
197 }