Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcManagerEvent.cs
1 /*
2  * Copyright (c) 2016 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.Threading.Tasks;
19
20 namespace Tizen.Network.Nfc
21 {
22     internal static class EventHandlerExtension
23     {
24         internal static void SafeInvoke(this EventHandler evt, object sender, EventArgs e)
25         {
26             var handler = evt;
27             if (handler != null)
28             {
29                 handler(sender, e);
30             }
31         }
32         internal static void SafeInvoke<T>(this EventHandler<T> evt, object sender, T e) where T : EventArgs
33         {
34             var handler = evt;
35             if (handler != null)
36             {
37                 handler(sender, e);
38             }
39         }
40     }
41
42     internal partial class NfcManagerImpl
43     {
44         private event EventHandler<ActivationChangedEventArgs> _activationChanged;
45         private event EventHandler<NdefMessageDiscoveredEventArgs> _ndefMessageDiscovered;
46
47         private Interop.Nfc.ActivationChangedCallback _activationChangedCallback;
48         private Interop.Nfc.NdefMessageDiscoveredCallback _ndefMessageDiscoveredCallback;
49
50         internal event EventHandler<ActivationChangedEventArgs> ActivationChanged
51         {
52             add
53             {
54                 if (_activationChanged == null)
55                 {
56                     RegisterActivationChangedEvent();
57                 }
58                 _activationChanged += value;
59             }
60             remove
61             {
62                 _activationChanged -= value;
63                 if (_activationChanged == null)
64                 {
65                     UnregisterActivationChangedEvent();
66                 }
67             }
68         }
69
70         internal event EventHandler<NdefMessageDiscoveredEventArgs> NdefMessageDiscovered
71         {
72             add
73             {
74                 if (_ndefMessageDiscovered == null)
75                 {
76                     RegisterNdefMessageDiscoveredEvent();
77                 }
78                 _ndefMessageDiscovered += value;
79             }
80             remove
81             {
82                 _ndefMessageDiscovered -= value;
83                 if (_ndefMessageDiscovered == null)
84                 {
85                     UnregisterNdefMessageDiscoveredEvent();
86                 }
87             }
88         }
89
90         internal void RemoveAllRegisteredEvent()
91         {
92             //unregister all remaining events when this object is released.
93             if (_activationChanged != null)
94             {
95                 UnregisterActivationChangedEvent();
96             }
97         }
98
99         private void RegisterActivationChangedEvent()
100         {
101             _activationChangedCallback = (bool activated, IntPtr userData) =>
102             {
103                 bool isActivated = activated;
104                 ActivationChangedEventArgs e = new ActivationChangedEventArgs(isActivated);
105                 _activationChanged.SafeInvoke(null, e);
106             };
107             int ret = Interop.Nfc.SetActivationChangedCallback(_activationChangedCallback, IntPtr.Zero);
108             if (ret != (int)NfcError.None)
109             {
110                 Log.Error(Globals.LogTag, "Failed to set activation changed callback, Error - " + (NfcError)ret);
111             }
112         }
113
114         private void UnregisterActivationChangedEvent()
115         {
116             Interop.Nfc.UnsetActivationChangedCallback();
117         }
118
119         private void RegisterNdefMessageDiscoveredEvent()
120         {
121             _ndefMessageDiscoveredCallback = (IntPtr ndefMessageHandle, IntPtr userData) =>
122             {
123                 NdefMessageDiscoveredEventArgs e = new NdefMessageDiscoveredEventArgs(ndefMessageHandle);
124                 _ndefMessageDiscovered.SafeInvoke(null, e);
125             };
126
127             int ret = Interop.Nfc.SetNdefDiscoveredCallback(_ndefMessageDiscoveredCallback, IntPtr.Zero);
128             if (ret != (int)NfcError.None)
129             {
130                 Log.Error(Globals.LogTag, "Failed to set ndef message discovered callback, Error - " + (NfcError)ret);
131             }
132         }
133
134         private void UnregisterNdefMessageDiscoveredEvent()
135         {
136             Interop.Nfc.UnsetNdefDiscoveredCallback();
137         }
138     }
139 }