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