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;
20 namespace Tizen.System
23 /// The Information class provides functions to obtain various system information.
25 /// <since_tizen> 4 </since_tizen>
26 public static class Information
28 internal const string HttpPrefix = "http://";
29 internal const string RuntimeInfoStringKeyPrefix = "tizen.org/runtimefeature/";
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";
47 private static readonly Dictionary<string, RuntimeInfoKey> StringEnumMapping = new Dictionary<string, RuntimeInfoKey>
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
65 internal static readonly Dictionary<RuntimeInfoKey, string> EnumStringMapping = new Dictionary<RuntimeInfoKey, string>
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
83 private static bool ConvertStringToRuntimeInfoKey(string key, out RuntimeInfoKey feature)
85 string filteredKey = key.StartsWith(HttpPrefix) ? key.Substring(HttpPrefix.Length) : key;
86 feature = default(RuntimeInfoKey);
88 return StringEnumMapping.TryGetValue(filteredKey, out feature);
91 private static bool TryGetRuntimeInfoValue<T>(RuntimeInfoKey key, out T value)
95 if (!RuntimeInfo.Is<T>(key))
97 Log.Error(InformationErrorFactory.LogTag, "Invalid return type");
101 return RuntimeInfo.TryGetValue<T>(key, out value);
104 #pragma warning disable CS0618 // Type or member is obsolete
105 private static bool TryGetSystemInfoValue<T>(string key, out T value)
109 if (!SystemInfo.IsValidKey(key))
111 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
115 if (!SystemInfo.Is<T>(key))
117 Log.Error(InformationErrorFactory.LogTag, "Invalid return type");
121 return SystemInfo.TryGetValue<T>(key, out value);
123 #pragma warning restore CS0618 // Type or member is obsolete
126 /// Gets the value of the feature.
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)
135 RuntimeInfoKey runtimeFeature;
137 if (ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
139 return TryGetRuntimeInfoValue<T>(runtimeFeature, out value);
143 return TryGetSystemInfoValue<T>(key, out value);
148 /// Registers a change event callback for given runtime feature key.
151 /// This function is only for runtime feature.
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)
160 RuntimeInfoKey runtimeFeature;
162 if (!ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
164 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
165 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
168 RuntimeInfo.SetCallback(runtimeFeature, callback);
172 /// Unregisters a change event callback for given runtime feature key.
175 /// This function is only for runtime feature.
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)
184 RuntimeInfoKey runtimeFeature;
186 if (!ConvertStringToRuntimeInfoKey(key, out runtimeFeature))
188 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
189 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
192 RuntimeInfo.UnsetCallback(runtimeFeature, callback);