Revert "[Tizen] Add OnPreCreate and change OnPause OnResume to virtual function"
[platform/core/csapi/nui.git] / Tizen.NUI / src / internal / NUICoreBackend.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
18 using System;
19 using System.Collections.Generic;
20
21 using Tizen.Applications.CoreBackend;
22 using Tizen.Applications;
23 using Tizen.NUI;
24
25 namespace Tizen.NUI
26 {
27     class NUICoreBackend : ICoreBackend
28     {
29         /// <summary>
30         /// Application instance to connect event.
31         /// </summary>
32         protected Application _application;
33
34         /// <summary>
35         /// Dictionary to contain each type of event callback.
36         /// </summary>
37         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
38
39         /// <summary>
40         /// The default Constructor.
41         /// </summary>
42         public NUICoreBackend()
43         {
44             _application = Application.NewApplication();
45         }
46
47         /// <summary>
48         /// The constructor with stylesheet.
49         /// </summary>
50         public NUICoreBackend(string stylesheet)
51         {
52             _application = Application.NewApplication(stylesheet);
53         }
54
55         /// <summary>
56         /// The constructor with stylesheet and window mode.
57         /// </summary>
58         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
59         {
60             _application = Application.NewApplication(stylesheet, (Application.WindowMode)windowMode );
61         }
62
63         /// <summary>
64         /// Add NUIApplication event to Application.
65         /// Put each type of event callback in Dictionary.
66         /// </summary>
67         /// <param name="evType">Type of event</param>
68         /// <param name="handler">Event callback</param>
69         public void AddEventHandler(EventType evType, Action handler)
70         {
71             Handlers.Add(evType, handler);
72         }
73
74         /// <summary>
75         /// Add NUIApplication event to Application.
76         /// Put each type of event callback in Dictionary.
77         /// </summary>
78         /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
79         /// <param name="evType">Type of event</param>
80         /// <param name="handler">Event callback</param>
81         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
82         {
83             Handlers.Add(evType, handler);
84         }
85
86
87         /// <summary>
88         /// Dispose function.
89         /// </summary>
90         public void Dispose()
91         {
92             _application.Dispose();
93         }
94
95         /// <summary>
96         /// Exit Application.
97         /// </summary>
98         public void Exit()
99         {
100             _application.Quit();
101         }
102
103         /// <summary>
104         /// Run Application.
105         /// </summary>
106         /// <param name="args">Arguments from commandline.</param>
107         public void Run(string[] args)
108         {
109             _application.BatteryLow += OnBatteryLow;
110             _application.LanguageChanged += OnLanguageChanged;
111             _application.MemoryLow += OnMemoryLow;
112             _application.RegionChanged += OnRegionChanged;
113
114             _application.Initialized += OnInitialized;
115             _application.Resumed += OnResumed;
116             _application.Terminating += OnTerminated;
117             _application.Paused += OnPaused;
118             _application.AppControl += OnAppControl;
119
120             _application.MainLoop();
121         }
122
123         /// <summary>
124         /// Region changed event callback function.
125         /// </summary>
126         /// <param name="source">Application instance</param>
127         /// <param name="e">Event argument for RegionChanged</param>
128         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
129         {
130             Log.Debug("NUI", "NUICorebackend OnRegionChanged Called");
131             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
132             // Need to make new signal return in native to return right value.
133             handler?.Invoke( new RegionFormatChangedEventArgs(""));
134         }
135
136         /// <summary>
137         /// Memory Low event callback function.
138         /// </summary>
139         /// <param name="source">Application instance</param>
140         /// <param name="e">Event argument for MemoryLow</param>
141         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
142         {
143             Log.Debug("NUI", "NUICorebackend OnMemoryLow Called");
144             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
145             // Need to make new signal return in native to return right value.
146             handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
147         }
148
149         /// <summary>
150         /// Language changed event callback function.
151         /// </summary>
152         /// <param name="source">Application instance</param>
153         /// <param name="e">Event argument for LanguageChanged</param>
154         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
155         {
156             Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
157             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
158             // Need to make new signal return in native to return right value.
159             handler?.Invoke( new LocaleChangedEventArgs(""));
160         }
161
162         /// <summary>
163         /// Battery low event callback function.
164         /// </summary>
165         /// <param name="source">Application instance</param>
166         /// <param name="e">Event argument for BatteryLow</param>
167         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
168         {
169             Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
170             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
171             // Need to make new signal return in native to return right value.
172             handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
173         }
174
175         /// <summary>
176         /// Initialized event callback function.
177         /// </summary>
178         /// <param name="source">Application instance</param>
179         /// <param name="e">Event argument for Initialized</param>
180         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
181         {
182             Log.Debug("NUI", "NUICorebackend OnInitialized Called");
183             var handler = Handlers[EventType.Created] as Action;
184             handler?.Invoke();
185         }
186
187         /// <summary>
188         /// Terminated event callback function.
189         /// </summary>
190         /// <param name="source">Application instance</param>
191         /// <param name="e">Event argument for Terminated</param>
192         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
193         {
194             Log.Debug("NUI", "NUICorebackend OnTerminated Called");
195             var handler = Handlers[EventType.Terminated] as Action;
196             handler?.Invoke();
197         }
198
199         /// <summary>
200         /// Resumed event callback function.
201         /// </summary>
202         /// <param name="source">Application instance</param>
203         /// <param name="e">Event argument for Resumed</param>
204         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
205         {
206             Log.Debug("NUI", "NUICorebackend OnResumed Called");
207             var handler = Handlers[EventType.Resumed] as Action;
208             handler?.Invoke();
209         }
210
211         /// <summary>
212         /// App control event callback function.
213         /// </summary>
214         /// <param name="source">Application instance</param>
215         /// <param name="e">Event argument for AppControl</param>
216         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
217         {
218             Log.Debug("NUI", "NUICorebackend OnAppControl Called");
219             /* can invoke after making new api which getting control handle at application.
220             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
221             handler?.Invoke();
222             */
223         }
224
225         /// <summary>
226         /// Paused event callback function.
227         /// </summary>
228         /// <param name="source">Application instance</param>
229         /// <param name="e">Event argument for Paused</param>
230         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
231         {
232             Log.Debug("NUI", "NUICorebackend OnPaused Called");
233             var handler = Handlers[EventType.Paused] as Action;
234             handler?.Invoke();
235         }
236
237
238         internal Application ApplicationHandle
239         {
240                 get
241                 {
242                         return _application;
243                 }
244         }
245
246     }
247 }