2 * Copyright (c) 2016 - 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 using System.ComponentModel;
22 namespace Tizen.System
24 [EditorBrowsable(EditorBrowsableState.Never)]
25 internal static class RuntimeInfo
27 private static RuntimeInfoEventHandler BluetoothEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.Bluetooth);
28 private static RuntimeInfoEventHandler WifiHotspotEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.WifiHotspot);
29 private static RuntimeInfoEventHandler BluetoothTetheringEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.BluetoothTethering);
30 private static RuntimeInfoEventHandler UsbTetheringEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.UsbTethering);
31 private static RuntimeInfoEventHandler PacketDataEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.PacketData);
32 private static RuntimeInfoEventHandler DataRoamingEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.DataRoaming);
33 private static RuntimeInfoEventHandler VibrationEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.Vibration);
34 private static RuntimeInfoEventHandler AudioJackConnected = new RuntimeInfoEventHandler(RuntimeInfoKey.AudioJack);
35 private static RuntimeInfoEventHandler GpsStatusChanged = new RuntimeInfoEventHandler(RuntimeInfoKey.Gps);
36 private static RuntimeInfoEventHandler BatteryIsCharging = new RuntimeInfoEventHandler(RuntimeInfoKey.BatteryIsCharging);
37 private static RuntimeInfoEventHandler TvOutConnected = new RuntimeInfoEventHandler(RuntimeInfoKey.TvOut);
38 private static RuntimeInfoEventHandler AudioJackConnectorChanged = new RuntimeInfoEventHandler(RuntimeInfoKey.AudioJackConnector);
39 private static RuntimeInfoEventHandler ChargerConnected = new RuntimeInfoEventHandler(RuntimeInfoKey.Charger);
40 private static RuntimeInfoEventHandler AutoRotationEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.AutoRotation);
42 internal static readonly Dictionary<RuntimeInfoKey, Type> s_keyDataTypeMapping = new Dictionary<RuntimeInfoKey, Type>
44 [RuntimeInfoKey.Bluetooth] = typeof(bool),
45 [RuntimeInfoKey.WifiHotspot] = typeof(bool),
46 [RuntimeInfoKey.BluetoothTethering] = typeof(bool),
47 [RuntimeInfoKey.UsbTethering] = typeof(bool),
48 [RuntimeInfoKey.PacketData] = typeof(bool),
49 [RuntimeInfoKey.DataRoaming] = typeof(bool),
50 [RuntimeInfoKey.Vibration] = typeof(bool),
51 [RuntimeInfoKey.AudioJack] = typeof(bool),
52 [RuntimeInfoKey.BatteryIsCharging] = typeof(bool),
53 [RuntimeInfoKey.TvOut] = typeof(bool),
54 [RuntimeInfoKey.Charger] = typeof(bool),
55 [RuntimeInfoKey.AutoRotation] = typeof(bool),
56 [RuntimeInfoKey.Gps] = typeof(int),
57 [RuntimeInfoKey.AudioJackConnector] = typeof(int)
61 /// Validates the data type of the status represented by the runtime key.
62 /// Note that this is a generic method.
64 /// <typeparam name="T">The generic type to validate.</typeparam>
65 /// <param name="key">The runtime information key for which the status type is validated.</param>
66 /// <returns>True if the data type matches.</returns>
67 /// <exception cref="ArgumentException">Thrown when the <paramref name="key"/> is invalid.</exception>
68 internal static bool Is<T>(RuntimeInfoKey key)
70 if (!s_keyDataTypeMapping.ContainsKey(key))
72 Log.Error(InformationErrorFactory.LogTag, "Invalid data type");
73 throw new ArgumentException("Invalid parameter");
76 return s_keyDataTypeMapping[key] == typeof(T);
80 /// Gets the status of runtime key.
81 /// Note that this is a generic method.
83 /// <typeparam name="T">The generic type to return.</typeparam>
84 /// <param name="key">The runtime information key for which the current should be read.</param>
85 /// <param name="value">The value of the given feature.</param>
86 /// <returns>Returns true on success, otherwise false.</returns>
87 internal static bool TryGetValue<T>(RuntimeInfoKey key, out T value)
92 if (!s_keyDataTypeMapping.TryGetValue(key, out type))
94 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
98 if (type == typeof(bool))
100 InformationError ret = Interop.RuntimeInfo.GetValue(TvProductHelper.ConvertKeyIfTvProduct(key), out bool val);
102 if (ret != InformationError.None)
104 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get value for key {0}", key.ToString());
108 value = (T)(object)val;
110 else if(type == typeof(int))
112 InformationError ret = Interop.RuntimeInfo.GetValue(TvProductHelper.ConvertKeyIfTvProduct(key), out int val);
114 if (ret != InformationError.None)
116 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get value for key {0}", key.ToString());
120 value = (T)(object)val;
126 private static void FindEventHandler(RuntimeInfoKey key, ref RuntimeInfoEventHandler handler)
130 case RuntimeInfoKey.Bluetooth:
131 handler = BluetoothEnabled;
133 case RuntimeInfoKey.WifiHotspot:
134 handler = WifiHotspotEnabled;
136 case RuntimeInfoKey.BluetoothTethering:
137 handler = BluetoothTetheringEnabled;
139 case RuntimeInfoKey.UsbTethering:
140 handler = UsbTetheringEnabled;
142 case RuntimeInfoKey.PacketData:
143 handler = PacketDataEnabled;
145 case RuntimeInfoKey.DataRoaming:
146 handler = DataRoamingEnabled;
148 case RuntimeInfoKey.Vibration:
149 handler = VibrationEnabled;
151 case RuntimeInfoKey.AudioJack:
152 handler = AudioJackConnected;
154 case RuntimeInfoKey.Gps:
155 handler = GpsStatusChanged;
157 case RuntimeInfoKey.BatteryIsCharging:
158 handler = BatteryIsCharging;
160 case RuntimeInfoKey.TvOut:
161 handler = TvOutConnected;
163 case RuntimeInfoKey.AudioJackConnector:
164 handler = AudioJackConnectorChanged;
166 case RuntimeInfoKey.Charger:
167 handler = ChargerConnected;
169 case RuntimeInfoKey.AutoRotation:
170 handler = AutoRotationEnabled;
179 /// Registers a change event callback for given key.
181 /// <param name="key">The runtime information key which wants to register callback.</param>
182 /// <param name="callback">The callback function to subscribe.</param>
183 /// <exception cref="ArgumentException">Thrown when the <paramref name="key"/> is invalid.</exception>
184 /// <exception cref="NotSupportedException">Thrown when the feature related <paramref name="key"/> is not supported.</exception>
185 internal static void SetCallback(RuntimeInfoKey key, EventHandler<RuntimeFeatureStatusChangedEventArgs> callback)
187 RuntimeInfoEventHandler handler = null;
189 FindEventHandler(key, ref handler);
192 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
193 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
196 handler.EventHandler += callback;
200 /// Unregisters a change event callback for given key.
202 /// <param name="key">The runtime information key which wants to unregister callback.</param>
203 /// <param name="callback">The callback function to unsubscribe.</param>
204 /// <exception cref="ArgumentException">Thrown when the <paramref name="key"/> is invalid.</exception>
205 internal static void UnsetCallback(RuntimeInfoKey key, EventHandler<RuntimeFeatureStatusChangedEventArgs> callback)
207 RuntimeInfoEventHandler handler = null;
209 FindEventHandler(key, ref handler);
212 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
213 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
216 handler.EventHandler -= callback;