Release 4.0.0-preview1-00201
[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 using System.IO;
20 using System.ComponentModel;
21
22 namespace Tizen.System
23 {
24     [EditorBrowsable(EditorBrowsableState.Never)]
25     internal static class RuntimeInfo
26     {
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);
41
42         internal static readonly Dictionary<RuntimeInfoKey, Type> s_keyDataTypeMapping = new Dictionary<RuntimeInfoKey, Type>
43         {
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)
58         };
59
60         /// <summary>
61         /// Validates the data type of the status represented by the runtime key.
62         /// Note that this is a generic method.
63         /// </summary>
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)
69         {
70             if (!s_keyDataTypeMapping.ContainsKey(key))
71             {
72                 Log.Error(InformationErrorFactory.LogTag, "Invalid data type");
73                 throw new ArgumentException("Invalid parameter");
74             }
75
76             return s_keyDataTypeMapping[key] == typeof(T);
77         }
78
79         /// <summary>
80         /// Gets the status of runtime key.
81         /// Note that this is a generic method.
82         /// </summary>
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)
88         {
89             Type type;
90             value = default(T);
91
92             if (!s_keyDataTypeMapping.TryGetValue(key, out type))
93             {
94                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
95                 return false;
96             }
97
98             if (type == typeof(bool))
99             {
100                 InformationError ret = Interop.RuntimeInfo.GetValue(TvProductHelper.ConvertKeyIfTvProduct(key), out bool val);
101
102                 if (ret != InformationError.None)
103                 {
104                     Log.Error(InformationErrorFactory.LogTag, "Interop failed to get value for key {0}", key.ToString());
105                     return false;
106                 }
107
108                 value = (T)(object)val;
109             }
110             else if(type == typeof(int))
111             {
112                 InformationError ret = Interop.RuntimeInfo.GetValue(TvProductHelper.ConvertKeyIfTvProduct(key), out int val);
113
114                 if (ret != InformationError.None)
115                 {
116                     Log.Error(InformationErrorFactory.LogTag, "Interop failed to get value for key {0}", key.ToString());
117                     return false;
118                 }
119
120                 value = (T)(object)val;
121             }
122
123             return true;
124         }
125
126         private static void FindEventHandler(RuntimeInfoKey key, ref RuntimeInfoEventHandler handler)
127         {
128             switch (key)
129             {
130                 case RuntimeInfoKey.Bluetooth:
131                     handler = BluetoothEnabled;
132                     break;
133                 case RuntimeInfoKey.WifiHotspot:
134                     handler = WifiHotspotEnabled;
135                     break;
136                 case RuntimeInfoKey.BluetoothTethering:
137                     handler = BluetoothTetheringEnabled;
138                     break;
139                 case RuntimeInfoKey.UsbTethering:
140                     handler = UsbTetheringEnabled;
141                     break;
142                 case RuntimeInfoKey.PacketData:
143                     handler = PacketDataEnabled;
144                     break;
145                 case RuntimeInfoKey.DataRoaming:
146                     handler = DataRoamingEnabled;
147                     break;
148                 case RuntimeInfoKey.Vibration:
149                     handler = VibrationEnabled;
150                     break;
151                 case RuntimeInfoKey.AudioJack:
152                     handler = AudioJackConnected;
153                     break;
154                 case RuntimeInfoKey.Gps:
155                     handler = GpsStatusChanged;
156                     break;
157                 case RuntimeInfoKey.BatteryIsCharging:
158                     handler = BatteryIsCharging;
159                     break;
160                 case RuntimeInfoKey.TvOut:
161                     handler = TvOutConnected;
162                     break;
163                 case RuntimeInfoKey.AudioJackConnector:
164                     handler = AudioJackConnectorChanged;
165                     break;
166                 case RuntimeInfoKey.Charger:
167                     handler = ChargerConnected;
168                     break;
169                 case RuntimeInfoKey.AutoRotation:
170                     handler = AutoRotationEnabled;
171                     break;
172                 default:
173                     handler = null;
174                     break;
175             }
176         }
177
178         /// <summary>
179         /// Registers a change event callback for given key.
180         /// </summary>
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)
186         {
187             RuntimeInfoEventHandler handler = null;
188
189             FindEventHandler(key, ref handler);
190             if (handler == null)
191             {
192                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
193                 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
194             }
195
196             handler.EventHandler += callback;
197         }
198
199         /// <summary>
200         /// Unregisters a change event callback for given key.
201         /// </summary>
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)
206         {
207             RuntimeInfoEventHandler handler = null;
208
209             FindEventHandler(key, ref handler);
210             if (handler == null)
211             {
212                 Log.Error(InformationErrorFactory.LogTag, "Invalid key");
213                 InformationErrorFactory.ThrowException(InformationError.InvalidParameter);
214             }
215
216             handler.EventHandler -= callback;
217         }
218     }
219 }