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 namespace Tizen.System
22 internal static class RuntimeInfo
24 private static RuntimeInfoEventHandler BluetoothEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.Bluetooth);
25 private static RuntimeInfoEventHandler WifiHotspotEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.WifiHotspot);
26 private static RuntimeInfoEventHandler BluetoothTetheringEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.BluetoothTethering);
27 private static RuntimeInfoEventHandler UsbTetheringEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.UsbTethering);
28 private static RuntimeInfoEventHandler PacketDataEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.PacketData);
29 private static RuntimeInfoEventHandler DataRoamingEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.DataRoaming);
30 private static RuntimeInfoEventHandler VibrationEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.Vibration);
31 private static RuntimeInfoEventHandler AudioJackConnected = new RuntimeInfoEventHandler(RuntimeInfoKey.AudioJack);
32 private static RuntimeInfoEventHandler GpsStatusChanged = new RuntimeInfoEventHandler(RuntimeInfoKey.Gps);
33 private static RuntimeInfoEventHandler BatteryIsCharging = new RuntimeInfoEventHandler(RuntimeInfoKey.BatteryIsCharging);
34 private static RuntimeInfoEventHandler TvOutConnected = new RuntimeInfoEventHandler(RuntimeInfoKey.TvOut);
35 private static RuntimeInfoEventHandler AudioJackConnectorChanged = new RuntimeInfoEventHandler(RuntimeInfoKey.AudioJackConnector);
36 private static RuntimeInfoEventHandler ChargerConnected = new RuntimeInfoEventHandler(RuntimeInfoKey.Charger);
37 private static RuntimeInfoEventHandler AutoRotationEnabled = new RuntimeInfoEventHandler(RuntimeInfoKey.AutoRotation);
39 internal static readonly Dictionary<RuntimeInfoKey, Type> s_keyDataTypeMapping = new Dictionary<RuntimeInfoKey, Type>
41 [RuntimeInfoKey.Bluetooth] = typeof(bool),
42 [RuntimeInfoKey.WifiHotspot] = typeof(bool),
43 [RuntimeInfoKey.BluetoothTethering] = typeof(bool),
44 [RuntimeInfoKey.UsbTethering] = typeof(bool),
45 [RuntimeInfoKey.PacketData] = typeof(bool),
46 [RuntimeInfoKey.DataRoaming] = typeof(bool),
47 [RuntimeInfoKey.Vibration] = typeof(bool),
48 [RuntimeInfoKey.AudioJack] = typeof(bool),
49 [RuntimeInfoKey.BatteryIsCharging] = typeof(bool),
50 [RuntimeInfoKey.TvOut] = typeof(bool),
51 [RuntimeInfoKey.Charger] = typeof(bool),
52 [RuntimeInfoKey.AutoRotation] = typeof(bool),
53 [RuntimeInfoKey.Gps] = typeof(int),
54 [RuntimeInfoKey.AudioJackConnector] = typeof(int)
58 /// Validates the data type of the status represented by the runtime key.
59 /// Note that this is a generic method.
61 /// <typeparam name="T">The generic type to validate.</typeparam>
62 /// <param name="key">The runtime information key for which the status type is validated.</param>
63 /// <returns>True if the data type matches.</returns>
64 /// <exception cref="ArgumentException">Thrown when the <paramref name="key"/> is invalid.</exception>
65 internal static bool Is<T>(RuntimeInfoKey key)
67 if (!s_keyDataTypeMapping.ContainsKey(key))
69 Log.Error(InformationErrorFactory.LogTag, "Invalid data type");
70 throw new ArgumentException("Invalid parameter");
73 return s_keyDataTypeMapping[key] == typeof(T);
77 /// Gets the status of runtime key.
78 /// Note that this is a generic method.
80 /// <typeparam name="T">The generic type to return.</typeparam>
81 /// <param name="key">The runtime information key for which the current should be read.</param>
82 /// <param name="value">The value of the given feature.</param>
83 /// <returns>Returns true on success, otherwise false.</returns>
84 internal static bool TryGetValue<T>(RuntimeInfoKey key, out T value)
89 if (!s_keyDataTypeMapping.TryGetValue(key, out type))
91 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
95 if (type == typeof(bool))
97 InformationError ret = Interop.RuntimeInfo.GetValue(key, out bool val);
99 if (ret != InformationError.None)
101 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get value for key {0}", key.ToString());
105 value = (T)(object)val;
107 else if(type == typeof(int))
109 InformationError ret = Interop.RuntimeInfo.GetValue(key, out int val);
111 if (ret != InformationError.None)
113 Log.Error(InformationErrorFactory.LogTag, "Interop failed to get value for key {0}", key.ToString());
117 value = (T)(object)val;
123 private static void FindEventHandler(RuntimeInfoKey key, ref RuntimeInfoEventHandler handler)
127 case RuntimeInfoKey.Bluetooth:
128 handler = BluetoothEnabled;
130 case RuntimeInfoKey.WifiHotspot:
131 handler = WifiHotspotEnabled;
133 case RuntimeInfoKey.BluetoothTethering:
134 handler = BluetoothTetheringEnabled;
136 case RuntimeInfoKey.UsbTethering:
137 handler = UsbTetheringEnabled;
139 case RuntimeInfoKey.PacketData:
140 handler = PacketDataEnabled;
142 case RuntimeInfoKey.DataRoaming:
143 handler = DataRoamingEnabled;
145 case RuntimeInfoKey.Vibration:
146 handler = VibrationEnabled;
148 case RuntimeInfoKey.AudioJack:
149 handler = AudioJackConnected;
151 case RuntimeInfoKey.Gps:
152 handler = GpsStatusChanged;
154 case RuntimeInfoKey.BatteryIsCharging:
155 handler = BatteryIsCharging;
157 case RuntimeInfoKey.TvOut:
158 handler = TvOutConnected;
160 case RuntimeInfoKey.AudioJackConnector:
161 handler = AudioJackConnectorChanged;
163 case RuntimeInfoKey.Charger:
164 handler = ChargerConnected;
166 case RuntimeInfoKey.AutoRotation:
167 handler = AutoRotationEnabled;
176 /// Registers a change event callback for given key.
178 /// <param name="key">The runtime information key which wants to register callback.</param>
179 /// <param name="callback">The callback function to subscribe.</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 internal static void SetCallback(RuntimeInfoKey key, EventHandler<RuntimeFeatureStatusChangedEventArgs> callback)
184 RuntimeInfoEventHandler handler = null;
186 FindEventHandler(key, ref handler);
189 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
190 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
193 handler.EventHandler += callback;
197 /// Unregisters a change event callback for given key.
199 /// <param name="key">The runtime information key which wants to unregister callback.</param>
200 /// <param name="callback">The callback function to unsubscribe.</param>
201 /// <exception cref="ArgumentException">Thrown when the <paramref name="key"/> is invalid.</exception>
202 internal static void UnsetCallback(RuntimeInfoKey key, EventHandler<RuntimeFeatureStatusChangedEventArgs> callback)
204 RuntimeInfoEventHandler handler = null;
206 FindEventHandler(key, ref handler);
209 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
210 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
213 handler.EventHandler -= callback;