[Information] No more convert for TV product (#766)
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Information / RuntimeInfo / RuntimeInfo.cs
1 /*
2 * Copyright (c) 2016 - 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     internal static class RuntimeInfo
23     {
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);
38
39         internal static readonly Dictionary<RuntimeInfoKey, Type> s_keyDataTypeMapping = new Dictionary<RuntimeInfoKey, Type>
40         {
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)
55         };
56
57         /// <summary>
58         /// Validates the data type of the status represented by the runtime key.
59         /// Note that this is a generic method.
60         /// </summary>
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)
66         {
67             if (!s_keyDataTypeMapping.ContainsKey(key))
68             {
69                 Log.Error(InformationErrorFactory.LogTag, "Invalid data type");
70                 throw new ArgumentException("Invalid parameter");
71             }
72
73             return s_keyDataTypeMapping[key] == typeof(T);
74         }
75
76         /// <summary>
77         /// Gets the status of runtime key.
78         /// Note that this is a generic method.
79         /// </summary>
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)
85         {
86             Type type;
87             value = default(T);
88
89             if (!s_keyDataTypeMapping.TryGetValue(key, out type))
90             {
91                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
92                 return false;
93             }
94
95             if (type == typeof(bool))
96             {
97                 InformationError ret = Interop.RuntimeInfo.GetValue(key, out bool val);
98
99                 if (ret != InformationError.None)
100                 {
101                     Log.Error(InformationErrorFactory.LogTag, "Interop failed to get value for key {0}", key.ToString());
102                     return false;
103                 }
104
105                 value = (T)(object)val;
106             }
107             else if(type == typeof(int))
108             {
109                 InformationError ret = Interop.RuntimeInfo.GetValue(key, out int val);
110
111                 if (ret != InformationError.None)
112                 {
113                     Log.Error(InformationErrorFactory.LogTag, "Interop failed to get value for key {0}", key.ToString());
114                     return false;
115                 }
116
117                 value = (T)(object)val;
118             }
119
120             return true;
121         }
122
123         private static void FindEventHandler(RuntimeInfoKey key, ref RuntimeInfoEventHandler handler)
124         {
125             switch (key)
126             {
127                 case RuntimeInfoKey.Bluetooth:
128                     handler = BluetoothEnabled;
129                     break;
130                 case RuntimeInfoKey.WifiHotspot:
131                     handler = WifiHotspotEnabled;
132                     break;
133                 case RuntimeInfoKey.BluetoothTethering:
134                     handler = BluetoothTetheringEnabled;
135                     break;
136                 case RuntimeInfoKey.UsbTethering:
137                     handler = UsbTetheringEnabled;
138                     break;
139                 case RuntimeInfoKey.PacketData:
140                     handler = PacketDataEnabled;
141                     break;
142                 case RuntimeInfoKey.DataRoaming:
143                     handler = DataRoamingEnabled;
144                     break;
145                 case RuntimeInfoKey.Vibration:
146                     handler = VibrationEnabled;
147                     break;
148                 case RuntimeInfoKey.AudioJack:
149                     handler = AudioJackConnected;
150                     break;
151                 case RuntimeInfoKey.Gps:
152                     handler = GpsStatusChanged;
153                     break;
154                 case RuntimeInfoKey.BatteryIsCharging:
155                     handler = BatteryIsCharging;
156                     break;
157                 case RuntimeInfoKey.TvOut:
158                     handler = TvOutConnected;
159                     break;
160                 case RuntimeInfoKey.AudioJackConnector:
161                     handler = AudioJackConnectorChanged;
162                     break;
163                 case RuntimeInfoKey.Charger:
164                     handler = ChargerConnected;
165                     break;
166                 case RuntimeInfoKey.AutoRotation:
167                     handler = AutoRotationEnabled;
168                     break;
169                 default:
170                     handler = null;
171                     break;
172             }
173         }
174
175         /// <summary>
176         /// Registers a change event callback for given key.
177         /// </summary>
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)
183         {
184             RuntimeInfoEventHandler handler = null;
185
186             FindEventHandler(key, ref handler);
187             if (handler == null)
188             {
189                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
190                 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
191             }
192
193             handler.EventHandler += callback;
194         }
195
196         /// <summary>
197         /// Unregisters a change event callback for given key.
198         /// </summary>
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)
203         {
204             RuntimeInfoEventHandler handler = null;
205
206             FindEventHandler(key, ref handler);
207             if (handler == null)
208             {
209                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
210                 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
211             }
212
213             handler.EventHandler -= callback;
214         }
215     }
216 }