2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Collections.Generic;
20 using Tizen.Applications.CoreBackend;
21 using Tizen.Applications;
25 class NUICoreBackend : ICoreBackend
28 /// The Application instance to connect event.
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;
37 /// The Dictionary to contain each type of event callback.
39 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
42 /// The default Constructor.
44 public NUICoreBackend()
49 /// The constructor with stylesheet.
51 public NUICoreBackend(string stylesheet)
53 _stylesheet = stylesheet;
57 /// The constructor with stylesheet and window mode.
59 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
61 _stylesheet = stylesheet;
62 _windowMode = windowMode;
66 /// The constructor with stylesheet, window mode, window size and window position.
68 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
70 _stylesheet = stylesheet;
71 _windowMode = windowMode;
72 _windowSize = windowSize;
73 _windowPosition = windowPosition;
77 /// Adds NUIApplication event to Application.
78 /// Puts each type of event callback in Dictionary.
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)
84 Handlers.Add(evType, handler);
88 /// Adds NUIApplication event to Application.
89 /// Puts each type of event callback in Dictionary.
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
96 Handlers.Add(evType, handler);
100 /// The Dispose function.
102 public void Dispose()
104 if(_application != null)
106 _application.Dispose();
111 /// The Exit application.
115 if(_application != null)
122 /// Ensures that the function passed in is called from the main loop when it is idle.
124 /// <param name="func">The function to call</param>
125 /// <returns>true if added successfully, false otherwise</returns>
126 public bool AddIdle(System.Delegate func)
128 return _application.AddIdle(func);
132 /// The Run application.
134 /// <param name="args">The arguments from commandline.</param>
135 public void Run(string[] args)
137 TizenSynchronizationContext.Initialize();
138 NDalicPINVOKE.SWIGStringHelper.RegistCallback();
140 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
143 args[0] = this.GetType().Assembly.FullName;
146 if (args.Length == 1)
148 if (_windowSize != null)
150 _application = Application.NewApplication(_stylesheet, (Application.WindowMode)_windowMode, new Rectangle(_windowPosition.X, _windowPosition.Y, _windowSize.Width, _windowSize.Height));
154 _application = Application.NewApplication(_stylesheet, (Application.WindowMode)_windowMode);
157 else if (args.Length > 1)
159 if (_windowSize != null)
161 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode, new Rectangle(_windowPosition.X, _windowPosition.Y, _windowSize.Width, _windowSize.Height));
165 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
169 _application.BatteryLow += OnBatteryLow;
170 _application.LanguageChanged += OnLanguageChanged;
171 _application.MemoryLow += OnMemoryLow;
172 _application.RegionChanged += OnRegionChanged;
174 _application.Initialized += OnInitialized;
175 _application.Resumed += OnResumed;
176 _application.Terminating += OnTerminated;
177 _application.Paused += OnPaused;
178 _application.AppControl += OnAppControl;
180 _application.MainLoop();
181 _application.Dispose();
185 /// The Region changed event callback function.
187 /// <param name="source">The application instance.</param>
188 /// <param name="e">The event argument for RegionChanged.</param>
189 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
191 Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
192 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
193 handler?.Invoke( new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
197 /// The Memory Low event callback function.
199 /// <param name="source">The application instance.</param>
200 /// <param name="e">The event argument for MemoryLow.</param>
201 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
203 Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
204 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
206 switch ( e.MemoryStatus )
208 case Application.MemoryStatus.Normal:
210 handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
213 case Application.MemoryStatus.Low:
215 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
218 case Application.MemoryStatus.CriticallyLow:
220 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
227 /// The Language changed event callback function.
229 /// <param name="source">The application instance.</param>
230 /// <param name="e">The event argument for LanguageChanged.</param>
231 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
233 Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
234 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
235 handler?.Invoke( new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
239 /// The Battery Low event callback function.
241 /// <param name="source">The application instance.</param>
242 /// <param name="e">The event argument for BatteryLow.</param>
243 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
245 Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
246 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
247 switch( e.BatteryStatus )
249 case Application.BatteryStatus.Normal:
251 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
254 case Application.BatteryStatus.CriticallyLow:
256 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
259 case Application.BatteryStatus.PowerOff:
261 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
268 /// The Initialized event callback function.
270 /// <param name="source">The application instance.</param>
271 /// <param name="e">The event argument for Initialized.</param>
272 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
274 Log.Info("NUI", "NUICorebackend OnPreCreated Called");
275 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
276 preCreateHandler?.Invoke();
278 Log.Info("NUI", "NUICorebackend OnCreate Called");
279 var createHandler = Handlers[EventType.Created] as Action;
280 createHandler?.Invoke();
284 /// The Terminated event callback function.
286 /// <param name="source">The application instance.</param>
287 /// <param name="e">The event argument for Terminated.</param>
288 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
290 Log.Info("NUI", "NUICorebackend OnTerminated Called");
291 var handler = Handlers[EventType.Terminated] as Action;
296 /// The Resumed event callback function.
298 /// <param name="source">The application instance.</param>
299 /// <param name="e">The event argument for Resumed.</param>
300 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
302 Log.Info("NUI", "NUICorebackend OnResumed Called");
303 var handler = Handlers[EventType.Resumed] as Action;
308 /// The App control event callback function.
310 /// <param name="source">The application instance.</param>
311 /// <param name="e">The event argument for AppControl.</param>
312 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
314 Log.Info("NUI", "NUICorebackend OnAppControl Called");
315 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
316 SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
317 handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
321 /// The Paused event callback function.
323 /// <param name="source">The application instance.</param>
324 /// <param name="e">The event argument for Paused.</param>
325 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
327 Log.Info("NUI", "NUICorebackend OnPaused Called");
328 var handler = Handlers[EventType.Paused] as Action;
332 internal Application ApplicationHandle