From: Kichan Kwon Date: Tue, 22 Aug 2017 10:22:07 +0000 (+0900) Subject: [Information] Runtime-info event handler refactoring X-Git-Tag: preview1-00199~4^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b6be7cc61c72b0883f515b99c4db7d9f2b74f8aa;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [Information] Runtime-info event handler refactoring - Separate event handling class - To (un)set callback, use function instead of event - For consistency; using key to use Information class Change-Id: I4ba4429cd5c7e82f777f8d102844635d133229d7 Signed-off-by: Kichan Kwon --- diff --git a/src/Tizen.System.Information/RuntimeInfo/RuntimeInfoEventHandler.cs b/src/Tizen.System.Information/RuntimeInfo/RuntimeInfoEventHandler.cs new file mode 100755 index 000000000..445ceca8e --- /dev/null +++ b/src/Tizen.System.Information/RuntimeInfo/RuntimeInfoEventHandler.cs @@ -0,0 +1,74 @@ +/* +* Copyright (c) 2016 - 2017 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.ComponentModel; + +namespace Tizen.System +{ + [EditorBrowsable(EditorBrowsableState.Never)] + internal class RuntimeInfoEventHandler + { + private RuntimeInformationKey Key; + private event EventHandler Handler; + + internal RuntimeInfoEventHandler(RuntimeInformationKey key) + { + Key = key; + Handler = null; + } + + internal event EventHandler EventHandler + { + add + { + if (Handler == null) + { + InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(Key), RuntimeInformationChangedCallback, IntPtr.Zero); + if (ret != InformationError.None) + { + Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); + InformationErrorFactory.ThrowException(ret); + } + } + Handler += value; + } + remove + { + Handler -= value; + if (Handler == null) + { + InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(Key)); + if (ret != InformationError.None) + { + Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); + InformationErrorFactory.ThrowException(ret); + } + } + } + } + + private void RuntimeInformationChangedCallback(RuntimeInformationKey key, IntPtr userData) + { + RuntimeKeyStatusChangedEventArgs eventArgs = new RuntimeKeyStatusChangedEventArgs() + { + Key = key + }; + + Handler?.Invoke(null, eventArgs); + } + } +} diff --git a/src/Tizen.System.Information/RuntimeInfo/RuntimeInformation.cs b/src/Tizen.System.Information/RuntimeInfo/RuntimeInformation.cs index 213e3749c..12601cff3 100755 --- a/src/Tizen.System.Information/RuntimeInfo/RuntimeInformation.cs +++ b/src/Tizen.System.Information/RuntimeInfo/RuntimeInformation.cs @@ -30,103 +30,20 @@ namespace Tizen.System /// public static class RuntimeInformation { - private static event EventHandler s_bluetoothEnabled; - private static event EventHandler s_wifiHotspotEnabled; - private static event EventHandler s_bluetoothTetheringEnabled; - private static event EventHandler s_usbTetheringEnabled; - private static event EventHandler s_packetDataEnabled; - private static event EventHandler s_dataRoamingEnabled; - private static event EventHandler s_vibrationEnabled; - private static event EventHandler s_audioJackConnected; - private static event EventHandler s_gpsStatusChanged; - private static event EventHandler s_batteryIsCharging; - private static event EventHandler s_tvOutConnected; - private static event EventHandler s_audioJackConnectorChanged; - private static event EventHandler s_chargerConnected; - private static event EventHandler s_autoRotationEnabled; - - private static readonly Interop.RuntimeInfo.RuntimeInformationChangedCallback s_runtimeInfoChangedCallback = (RuntimeInformationKey key, IntPtr userData) => - { - RuntimeKeyStatusChangedEventArgs eventArgs = new RuntimeKeyStatusChangedEventArgs() - { - Key = key - }; - switch (key) - { - case RuntimeInformationKey.Bluetooth: - { - s_bluetoothEnabled?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.WifiHotspot: - { - s_wifiHotspotEnabled?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.BluetoothTethering: - { - s_bluetoothTetheringEnabled?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.UsbTethering: - { - s_usbTetheringEnabled?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.PacketData: - { - s_packetDataEnabled?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.DataRoaming: - { - s_dataRoamingEnabled?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.Vibration: - { - s_vibrationEnabled?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.AudioJack: - { - s_audioJackConnected?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.Gps: - { - s_gpsStatusChanged?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.BatteryIsCharging: - { - s_batteryIsCharging?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.TvOut: - { - s_tvOutConnected?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.AudioJackConnector: - { - s_audioJackConnectorChanged?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.Charger: - { - s_chargerConnected?.Invoke(null, eventArgs); - break; - }; - case RuntimeInformationKey.AutoRotation: - { - s_autoRotationEnabled?.Invoke(null, eventArgs); - break; - }; - default: - break; - }; - }; + private static RuntimeInfoEventHandler BluetoothEnabled = new RuntimeInfoEventHandler(RuntimeInformationKey.Bluetooth); + private static RuntimeInfoEventHandler WifiHotspotEnabled = new RuntimeInfoEventHandler(RuntimeInformationKey.WifiHotspot); + private static RuntimeInfoEventHandler BluetoothTetheringEnabled = new RuntimeInfoEventHandler(RuntimeInformationKey.BluetoothTethering); + private static RuntimeInfoEventHandler UsbTetheringEnabled = new RuntimeInfoEventHandler(RuntimeInformationKey.UsbTethering); + private static RuntimeInfoEventHandler PacketDataEnabled = new RuntimeInfoEventHandler(RuntimeInformationKey.PacketData); + private static RuntimeInfoEventHandler DataRoamingEnabled = new RuntimeInfoEventHandler(RuntimeInformationKey.DataRoaming); + private static RuntimeInfoEventHandler VibrationEnabled = new RuntimeInfoEventHandler(RuntimeInformationKey.Vibration); + private static RuntimeInfoEventHandler AudioJackConnected = new RuntimeInfoEventHandler(RuntimeInformationKey.AudioJack); + private static RuntimeInfoEventHandler GpsStatusChanged = new RuntimeInfoEventHandler(RuntimeInformationKey.Gps); + private static RuntimeInfoEventHandler BatteryIsCharging = new RuntimeInfoEventHandler(RuntimeInformationKey.BatteryIsCharging); + private static RuntimeInfoEventHandler TvOutConnected = new RuntimeInfoEventHandler(RuntimeInformationKey.TvOut); + private static RuntimeInfoEventHandler AudioJackConnectorChanged = new RuntimeInfoEventHandler(RuntimeInformationKey.AudioJackConnector); + private static RuntimeInfoEventHandler ChargerConnected = new RuntimeInfoEventHandler(RuntimeInformationKey.Charger); + private static RuntimeInfoEventHandler AutoRotationEnabled = new RuntimeInfoEventHandler(RuntimeInformationKey.AutoRotation); internal static readonly Dictionary s_keyDataTypeMapping = new Dictionary { @@ -217,453 +134,100 @@ namespace Tizen.System return (T)GetStatus(key); } - /// - /// (event) BluetoothEnabled is raised when the system preference for Bluetooth is changed. - /// - public static event EventHandler BluetoothEnabled - { - add - { - if (s_bluetoothEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.Bluetooth), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_bluetoothEnabled += value; - } - remove - { - s_bluetoothEnabled -= value; - if (s_bluetoothEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.Bluetooth)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) WifiHotspotEnabled is raised when the system preference for Wi-Fi is changed. - /// - public static event EventHandler WifiHotspotEnabled - { - add - { - if (s_wifiHotspotEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.WifiHotspot), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_wifiHotspotEnabled += value; - } - remove - { - s_wifiHotspotEnabled -= value; - if (s_wifiHotspotEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.WifiHotspot)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) BluetoothTetheringEnabled is raised when the system preference for bluetooth tethering is changed. - /// - public static event EventHandler BluetoothTetheringEnabled - { - add - { - if (s_bluetoothTetheringEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.BluetoothTethering), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_bluetoothTetheringEnabled += value; - } - remove - { - s_bluetoothTetheringEnabled -= value; - if (s_bluetoothTetheringEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.BluetoothTethering)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) UsbTetheringEnabled is raised when the system preference for USB tethering is changed. - /// - public static event EventHandler UsbTetheringEnabled - { - add - { - if (s_usbTetheringEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.UsbTethering), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_usbTetheringEnabled += value; - } - remove - { - s_usbTetheringEnabled -= value; - if (s_usbTetheringEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.UsbTethering)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) PacketDataEnabled is raised when the system preference for package data through 3G network is changed. - /// - public static event EventHandler PacketDataEnabled - { - add - { - if (s_packetDataEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.PacketData), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_packetDataEnabled += value; - } - remove - { - s_packetDataEnabled -= value; - if (s_packetDataEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.PacketData)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) DataRoamingEnabled is raised when the system preference for data roaming is changed. - /// - public static event EventHandler DataRoamingEnabled - { - add - { - if (s_dataRoamingEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.DataRoaming), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_dataRoamingEnabled += value; - } - remove - { - s_dataRoamingEnabled -= value; - if (s_dataRoamingEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.DataRoaming)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) VibrationEnabled is raised when the system preference for vibration is changed. - /// - public static event EventHandler VibrationEnabled - { - add - { - if (s_vibrationEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.Vibration), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_vibrationEnabled += value; - } - remove - { - s_vibrationEnabled -= value; - if (s_vibrationEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.Vibration)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) AudioJackConnected is raised when the audio jack is connected/disconnected. - /// - public static event EventHandler AudioJackConnected - { - add - { - if (s_audioJackConnected == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.AudioJack), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_audioJackConnected += value; - } - remove - { - s_audioJackConnected -= value; - if (s_audioJackConnected == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.AudioJack)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) GpsStatusChanged is raised when the status of GPS is changed. - /// - public static event EventHandler GpsStatusChanged - { - add - { - if (s_gpsStatusChanged == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.Gps), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_gpsStatusChanged += value; - } - remove - { - s_gpsStatusChanged -= value; - if (s_gpsStatusChanged == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.Gps)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) BatteryIsCharging is raised when the battery is currently charging. - /// - public static event EventHandler BatteryIsCharging - { - add - { - if (s_batteryIsCharging == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.BatteryIsCharging), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_batteryIsCharging += value; - } - remove - { - s_batteryIsCharging -= value; - if (s_batteryIsCharging == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.BatteryIsCharging)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) TvOutConnected is raised when TV out is connected/disconnected. - /// - public static event EventHandler TvOutConnected - { - add - { - if (s_tvOutConnected == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.TvOut), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_tvOutConnected += value; - } - remove - { - s_tvOutConnected -= value; - if (s_tvOutConnected == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.TvOut)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - } - } - /// - /// (event) AudioJackConnectorChanged is raised when the audio jack connection changes. - /// - public static event EventHandler AudioJackConnectorChanged + [EditorBrowsable(EditorBrowsableState.Never)] + private static void FindEventHandler(RuntimeInformationKey key, ref RuntimeInfoEventHandler handler) { - add - { - if (s_audioJackConnectorChanged == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.AudioJackConnector), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_audioJackConnectorChanged += value; - } - remove + switch (key) { - s_audioJackConnectorChanged -= value; - if (s_audioJackConnectorChanged == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.AudioJackConnector)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } + case RuntimeInformationKey.Bluetooth: + handler = BluetoothEnabled; + break; + case RuntimeInformationKey.WifiHotspot: + handler = WifiHotspotEnabled; + break; + case RuntimeInformationKey.BluetoothTethering: + handler = BluetoothTetheringEnabled; + break; + case RuntimeInformationKey.UsbTethering: + handler = UsbTetheringEnabled; + break; + case RuntimeInformationKey.PacketData: + handler = PacketDataEnabled; + break; + case RuntimeInformationKey.DataRoaming: + handler = DataRoamingEnabled; + break; + case RuntimeInformationKey.Vibration: + handler = VibrationEnabled; + break; + case RuntimeInformationKey.AudioJack: + handler = AudioJackConnected; + break; + case RuntimeInformationKey.Gps: + handler = GpsStatusChanged; + break; + case RuntimeInformationKey.BatteryIsCharging: + handler = BatteryIsCharging; + break; + case RuntimeInformationKey.TvOut: + handler = TvOutConnected; + break; + case RuntimeInformationKey.AudioJackConnector: + handler = AudioJackConnectorChanged; + break; + case RuntimeInformationKey.Charger: + handler = ChargerConnected; + break; + case RuntimeInformationKey.AutoRotation: + handler = AutoRotationEnabled; + break; + default: + handler = null; + break; } } + /// - /// (event) ChargerConnected is raised when the charger is connected/disconnected. + /// Registers a change event callback for given key. /// - public static event EventHandler ChargerConnected + /// 4 + /// The runtime information key which wants to register callback. + /// The callback function to subscribe. + /// Thrown when the is invalid. + /// Thrown when the feature related is not supported. + public static void SetCallback(RuntimeInformationKey key, EventHandler callback) { - add - { - if (s_chargerConnected == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.Charger), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_chargerConnected += value; - } - remove + RuntimeInfoEventHandler handler = null; + + FindEventHandler(key, ref handler); + if (handler == null) { - s_chargerConnected -= value; - if (s_chargerConnected == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.Charger)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } + Log.Error(InformationErrorFactory.LogTag, "Invalid key"); + InformationErrorFactory.ThrowException(InformationError.InvalidParameter); } + + handler.EventHandler += callback; } + /// - /// (event) AutoRotationEnabled is raised when the system preference for auto rotation is changed. + /// Unegisters a change event callback for given key. /// - public static event EventHandler AutoRotationEnabled + /// 4 + /// The runtime information key which wants to unregister callback. + /// The callback function to unsubscribe. + /// Thrown when the is invalid. + public static void UnsetCallback(RuntimeInformationKey key, EventHandler callback) { - add - { - if (s_autoRotationEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.AutoRotation), s_runtimeInfoChangedCallback, IntPtr.Zero); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } - s_autoRotationEnabled += value; - } - remove + RuntimeInfoEventHandler handler = null; + + FindEventHandler(key, ref handler); + if (handler == null) { - s_autoRotationEnabled -= value; - if (s_autoRotationEnabled == null) - { - InformationError ret = Interop.RuntimeInfo.UnsetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(RuntimeInformationKey.AutoRotation)); - if (ret != InformationError.None) - { - Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler"); - InformationErrorFactory.ThrowException(ret); - } - } + Log.Error(InformationErrorFactory.LogTag, "Invalid key"); + InformationErrorFactory.ThrowException(InformationError.InvalidParameter); } + + handler.EventHandler -= callback; } } }