2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
22 namespace Tizen.System
25 /// The Information class provides functions to obtain various system information.
27 public static class Information
29 internal const string HttpPrefix = "http://";
30 internal const string RuntimeInfoStringKeyPrefix = "tizen.org/runtimefeature/";
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";
48 private static readonly Dictionary<string, RuntimeInfoKey> StringEnumMapping = new Dictionary<string, RuntimeInfoKey>
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
66 internal static readonly Dictionary<RuntimeInfoKey, string> EnumStringMapping = new Dictionary<RuntimeInfoKey, string>
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
84 [EditorBrowsable(EditorBrowsableState.Never)]
85 private static bool ConvertStringToRuntimeInfoKey(string key, out RuntimeInfoKey feature)
87 string filteredKey = key.StartsWith(HttpPrefix) ? key.Substring(HttpPrefix.Length) : key;
88 feature = default(RuntimeInfoKey);
90 return StringEnumMapping.TryGetValue(filteredKey, out feature);
93 [EditorBrowsable(EditorBrowsableState.Never)]
94 private static bool TryGetRuntimeInfoValue<T>(RuntimeInfoKey key, out T value)
98 if (!RuntimeInfo.Is<T>(key))
100 Log.Error(InformationErrorFactory.LogTag, "Invalid return type");
104 return RuntimeInfo.TryGetValue<T>(key, out value);
107 [EditorBrowsable(EditorBrowsableState.Never)]
108 private static bool TryGetSystemInfoValue<T>(string key, out T value)
112 if (!SystemInfo.IsValidKey(key))
114 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
118 if (!SystemInfo.Is<T>(key))
120 Log.Error(InformationErrorFactory.LogTag, "Invalid return type");
124 return SystemInfo.TryGetValue<T>(key, out value);
128 /// Gets the value of the feature.
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)
137 RuntimeInfoKey runtimeFeature;
139 if (ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
141 return TryGetRuntimeInfoValue<T>(runtimeFeature, out value);
145 return TryGetSystemInfoValue<T>(key, out value);
150 /// Registers a change event callback for given runtime feature key.
153 /// This function is only for runtime feature.
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)
162 RuntimeInfoKey runtimeFeature;
164 if (!ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
166 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
167 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
170 RuntimeInfo.SetCallback(runtimeFeature, callback);
174 /// Unregisters a change event callback for given runtime feature key.
177 /// This function is only for runtime feature.
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)
186 RuntimeInfoKey runtimeFeature;
188 if (!ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
190 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
191 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
194 RuntimeInfo.UnsetCallback(runtimeFeature, callback);