[NUI] Delete stackTrace and log
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Timer.cs
1 /*
2  * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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.Runtime.InteropServices;
19 using System.ComponentModel;
20 using System.Threading;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// Mechanism to issue simple periodic or one-shot events.<br />
26     /// Timer is provided for application developers to be able to issue
27     /// simple periodic or one-shot events. Please note that the timer
28     /// callback functions should return as soon as possible because they
29     /// block the next SignalTick. Please note that timer signals are not
30     /// in sync with DALi's render timer.<br />
31     /// This class is a handle class so it can be stack allocated and used
32     /// as a member.<br />
33     /// </summary>
34     /// <since_tizen> 3 </since_tizen>
35     public class Timer : BaseHandle
36     {
37         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
38
39         private bool played = false;
40
41         internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
42         {
43             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
44         }
45
46         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
47         {
48             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
49         }
50
51         /// <summary>
52         /// Dispose.
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         protected override void Dispose(DisposeTypes type)
56         {
57             if (disposed)
58             {
59                 return;
60             }
61
62             if (type == DisposeTypes.Explicit)
63             {
64                 //Called by User
65                 //Release your own managed resources here.
66                 //You should release all of your own disposable objects here.
67             }
68
69             //Release your own unmanaged resources here.
70             //You should not access any managed member here except static instance.
71             //because the execution order of Finalizes is non-deterministic.
72
73             if (_timerTickCallbackDelegate != null)
74             {
75                 TickSignal().Disconnect(_timerTickCallbackDelegate);
76             }
77
78             if (swigCPtr.Handle != global::System.IntPtr.Zero)
79             {
80                 if (swigCMemOwn)
81                 {
82                     swigCMemOwn = false;
83                     NDalicPINVOKE.delete_Timer(swigCPtr);
84                 }
85                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
86             }
87
88             played = false;
89             base.Dispose(type);
90         }
91
92
93         /// <summary>
94         /// Event arguments that passed via the tick event.
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         public class TickEventArgs : EventArgs
98         {
99         }
100
101         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
102         private delegate bool TickCallbackDelegate(IntPtr data);
103         private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
104         private TickCallbackDelegate _timerTickCallbackDelegate;
105
106         /// <summary>
107         /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
108         /// provided by the user. The ticked signal is emitted after specified time interval.<br />
109         /// </summary>
110         /// <since_tizen> 3 </since_tizen>
111         public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
112         {
113             add
114             {
115                 if (_timerTickEventHandler == null)
116                 {
117                     _timerTickCallbackDelegate = new TickCallbackDelegate(OnTick);
118                     TickSignal().Connect(_timerTickCallbackDelegate);
119                 }
120                 _timerTickEventHandler += value;
121             }
122             remove
123             {
124                 _timerTickEventHandler -= value;
125                 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
126                 {
127                     TickSignal().Disconnect(_timerTickCallbackDelegate);
128                 }
129             }
130         }
131
132         private bool OnTick(IntPtr data)
133         {
134             TickEventArgs e = new TickEventArgs();
135
136             if (played == false)
137             {
138                 Tizen.Log.Fatal("NUI", $"({swigCPtr.Handle}) OnTick() is called even played is false!");
139             }
140
141             if (_timerTickEventHandler != null && played == true)
142             {
143                 //here we send all data to user event handlers
144                 return _timerTickEventHandler(this, e);
145             }
146             return false;
147         }
148
149         /// <summary>
150         /// Creates a tick timer that emits periodic signal.
151         /// </summary>
152         /// <param name="milliSec">Interval in milliseconds.</param>
153         /// <returns>A new timer.</returns>
154         /// <since_tizen> 3 </since_tizen>
155         public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
156         {
157             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
158
159             Tizen.Log.Error("NUI", $"({swigCPtr.Handle}) Timer({milliSec}) Constructor!");
160         }
161         internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
162         {
163             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
164         }
165
166         /// <summary>
167         /// Downcasts a handle to Timer handle.
168         /// </summary>
169         /// <since_tizen> 3 </since_tizen>
170         /// Please do not use! this will be deprecated!
171         /// Instead please use as keyword.
172         [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead!")]
173         [EditorBrowsable(EditorBrowsableState.Never)]
174         public static Timer DownCast(BaseHandle handle)
175         {
176             Timer ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
177             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
178             return ret;
179         }
180
181         /// <summary>
182         /// Starts the timer.<br />
183         /// In case a timer is already running, its time is reset and the timer is restarted.<br />
184         /// </summary>
185         /// <since_tizen> 3 </since_tizen>
186         public void Start()
187         {
188             played = true;
189             NDalicPINVOKE.Timer_Start(swigCPtr);
190
191             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
192         }
193
194         /// <summary>
195         /// Stops the timer.
196         /// </summary>
197         /// <since_tizen> 3 </since_tizen>
198         public void Stop()
199         {
200             played = false;
201             NDalicPINVOKE.Timer_Stop(swigCPtr);
202
203             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
204         }
205
206         /// <summary>
207         /// Gets/Sets the interval of the timer.
208         /// </summary>
209         /// <since_tizen> 4 </since_tizen>
210         public uint Interval
211         {
212             get
213             {
214                 return GetInterval();
215             }
216             set
217             {
218                 SetInterval(value);
219             }
220         }
221
222         /// <summary>
223         /// Sets a new interval on the timer and starts the timer.<br />
224         /// Cancels the previous timer.<br />
225         /// </summary>
226         /// <param name="milliSec">MilliSec interval in milliseconds.</param>
227         internal void SetInterval(uint milliSec)
228         {
229
230             Tizen.Log.Error("NUI", $"({swigCPtr.Handle}) SetInterval({milliSec})");
231             played = true;
232
233             NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
234             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
235         }
236
237         internal uint GetInterval()
238         {
239             uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
240             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
241             Tizen.Log.Error("NUI", $"({swigCPtr.Handle})GetInterval({ret})");
242             return ret;
243         }
244
245         /// <summary>
246         /// Tells whether the timer is running.
247         /// </summary>
248         /// <returns>Whether the timer is started or not.</returns>
249         /// <since_tizen> 3 </since_tizen>
250         public bool IsRunning()
251         {
252             bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
253             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
254             return ret;
255         }
256
257         internal TimerSignalType TickSignal()
258         {
259             TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
260             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
261             return ret;
262         }
263
264     }
265
266 }
267