[NUI] Fix build warning CA2213
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / NUICoreBackend.cs
1 /*
2  * Copyright (c) 2018 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
18 using System;
19 using System.Collections.Generic;
20 using Tizen.Applications.CoreBackend;
21 using Tizen.Applications;
22
23 namespace Tizen.NUI
24 {
25     class NUICoreBackend : ICoreBackend
26     {
27         /// <summary>
28         /// The Application instance to connect event.
29         /// </summary>
30         protected Application _application;
31         private string _stylesheet = "";
32         private NUIApplication.WindowMode _windowMode = NUIApplication.WindowMode.Opaque;
33         private Size2D _windowSize = null;
34         private Position2D _windowPosition = null;
35
36         /// <summary>
37         /// The Dictionary to contain each type of event callback.
38         /// </summary>
39         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
40
41         /// <summary>
42         /// The default Constructor.
43         /// </summary>
44         public NUICoreBackend()
45         {
46         }
47
48         /// <summary>
49         /// The constructor with stylesheet.
50         /// </summary>
51         public NUICoreBackend(string stylesheet)
52         {
53             _stylesheet = stylesheet;
54         }
55
56         /// <summary>
57         /// The constructor with stylesheet and window mode.
58         /// </summary>
59         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
60         {
61             _stylesheet = stylesheet;
62             _windowMode = windowMode;
63         }
64
65         /// <summary>
66         /// The constructor with stylesheet, window mode, window size and window position.
67         /// </summary>
68         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
69         {
70             _stylesheet = stylesheet;
71             _windowMode = windowMode;
72             _windowSize = windowSize;
73             _windowPosition = windowPosition;
74         }
75
76         /// <summary>
77         /// Adds NUIApplication event to Application.
78         /// Puts each type of event callback in Dictionary.
79         /// </summary>
80         /// <param name="evType">The type of event.</param>
81         /// <param name="handler">The event callback.</param>
82         public void AddEventHandler(EventType evType, Action handler)
83         {
84             Handlers.Add(evType, handler);
85         }
86
87         /// <summary>
88         /// Adds NUIApplication event to Application.
89         /// Puts each type of event callback in Dictionary.
90         /// </summary>
91         /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
92         /// <param name="evType">The type of event.</param>
93         /// <param name="handler">The event callback.</param>
94         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
95         {
96             Handlers.Add(evType, handler);
97         }
98
99         /// <summary>
100         /// The Dispose function.
101         /// </summary>
102         public void Dispose()
103         {
104             if (_application != null)
105             {
106                 _application.Dispose();
107             }
108             if (_windowSize != null)
109             {
110                 _windowSize.Dispose();
111             }
112             if (_windowPosition != null)
113             {
114                 _windowPosition.Dispose();
115             }
116         }
117
118         /// <summary>
119         /// The Exit application.
120         /// </summary>
121         public void Exit()
122         {
123             if (_application != null)
124             {
125                 _application.Quit();
126             }
127         }
128
129         /// <summary>
130         /// Ensures that the function passed in is called from the main loop when it is idle.
131         /// </summary>
132         /// <param name="func">The function to call</param>
133         /// <returns>true if added successfully, false otherwise</returns>
134         public bool AddIdle(System.Delegate func)
135         {
136             return _application.AddIdle(func);
137         }
138
139         /// <summary>
140         /// The Run application.
141         /// </summary>
142         /// <param name="args">The arguments from commandline.</param>
143         public void Run(string[] args)
144         {
145             TizenSynchronizationContext.Initialize();
146             NDalicPINVOKE.SWIGStringHelper.RegistCallback();
147
148             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
149             if (string.IsNullOrEmpty(args[0]))
150             {
151                 args[0] = this.GetType().Assembly.FullName;
152             }
153
154             if (args.Length == 1)
155             {
156                 if (_windowSize != null)
157                 {
158                     _application = Application.NewApplication(_stylesheet, (Application.WindowMode)_windowMode, new Rectangle(_windowPosition.X, _windowPosition.Y, _windowSize.Width, _windowSize.Height));
159                 }
160                 else
161                 {
162                     _application = Application.NewApplication(_stylesheet, (Application.WindowMode)_windowMode);
163                 }
164             }
165             else if (args.Length > 1)
166             {
167                 if (_windowSize != null)
168                 {
169                     _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode, new Rectangle(_windowPosition.X, _windowPosition.Y, _windowSize.Width, _windowSize.Height));
170                 }
171                 else
172                 {
173                     _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
174                 }
175             }
176
177             _application.BatteryLow += OnBatteryLow;
178             _application.LanguageChanged += OnLanguageChanged;
179             _application.MemoryLow += OnMemoryLow;
180             _application.RegionChanged += OnRegionChanged;
181
182             _application.Initialized += OnInitialized;
183             _application.Resumed += OnResumed;
184             _application.Terminating += OnTerminated;
185             _application.Paused += OnPaused;
186             _application.AppControl += OnAppControl;
187
188             _application.MainLoop();
189             _application.Dispose();
190         }
191
192         /// <summary>
193         /// The Region changed event callback function.
194         /// </summary>
195         /// <param name="source">The application instance.</param>
196         /// <param name="e">The event argument for RegionChanged.</param>
197         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
198         {
199             Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
200             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
201             handler?.Invoke(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
202         }
203
204         /// <summary>
205         /// The Memory Low event callback function.
206         /// </summary>
207         /// <param name="source">The application instance.</param>
208         /// <param name="e">The event argument for MemoryLow.</param>
209         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
210         {
211             Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
212             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
213
214             switch (e.MemoryStatus)
215             {
216                 case Application.MemoryStatus.Normal:
217                     {
218                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
219                         break;
220                     }
221                 case Application.MemoryStatus.Low:
222                     {
223                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
224                         break;
225                     }
226                 case Application.MemoryStatus.CriticallyLow:
227                     {
228                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
229                         break;
230                     }
231             }
232         }
233
234         /// <summary>
235         /// The Language changed event callback function.
236         /// </summary>
237         /// <param name="source">The application instance.</param>
238         /// <param name="e">The event argument for LanguageChanged.</param>
239         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
240         {
241             Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
242             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
243             handler?.Invoke(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
244         }
245
246         /// <summary>
247         /// The Battery Low event callback function.
248         /// </summary>
249         /// <param name="source">The application instance.</param>
250         /// <param name="e">The event argument for BatteryLow.</param>
251         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
252         {
253             Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
254             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
255             switch (e.BatteryStatus)
256             {
257                 case Application.BatteryStatus.Normal:
258                     {
259                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
260                         break;
261                     }
262                 case Application.BatteryStatus.CriticallyLow:
263                     {
264                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
265                         break;
266                     }
267                 case Application.BatteryStatus.PowerOff:
268                     {
269                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
270                         break;
271                     }
272             }
273         }
274
275         /// <summary>
276         /// The Initialized event callback function.
277         /// </summary>
278         /// <param name="source">The application instance.</param>
279         /// <param name="e">The event argument for Initialized.</param>
280         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
281         {
282             Log.Info("NUI", "NUICorebackend OnPreCreated Called");
283             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
284             preCreateHandler?.Invoke();
285
286             Log.Info("NUI", "NUICorebackend OnCreate Called");
287             var createHandler = Handlers[EventType.Created] as Action;
288             createHandler?.Invoke();
289         }
290
291         /// <summary>
292         /// The Terminated event callback function.
293         /// </summary>
294         /// <param name="source">The application instance.</param>
295         /// <param name="e">The event argument for Terminated.</param>
296         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
297         {
298             Log.Info("NUI", "NUICorebackend OnTerminated Called");
299             var handler = Handlers[EventType.Terminated] as Action;
300             handler?.Invoke();
301         }
302
303         /// <summary>
304         /// The Resumed event callback function.
305         /// </summary>
306         /// <param name="source">The application instance.</param>
307         /// <param name="e">The event argument for Resumed.</param>
308         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
309         {
310             Log.Info("NUI", "NUICorebackend OnResumed Called");
311             var handler = Handlers[EventType.Resumed] as Action;
312             handler?.Invoke();
313         }
314
315         /// <summary>
316         /// The App control event callback function.
317         /// </summary>
318         /// <param name="source">The application instance.</param>
319         /// <param name="e">The event argument for AppControl.</param>
320         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
321         {
322             Log.Info("NUI", "NUICorebackend OnAppControl Called");
323             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
324             SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
325             handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
326         }
327
328         /// <summary>
329         /// The Paused event callback function.
330         /// </summary>
331         /// <param name="source">The application instance.</param>
332         /// <param name="e">The event argument for Paused.</param>
333         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
334         {
335             Log.Info("NUI", "NUICorebackend OnPaused Called");
336             var handler = Handlers[EventType.Paused] as Action;
337             handler?.Invoke();
338         }
339
340         internal Application ApplicationHandle
341         {
342             get
343             {
344                 return _application;
345             }
346         }
347     }
348 }