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();
108 if (_windowSize != null)
110 _windowSize.Dispose();
112 if (_windowPosition != null)
114 _windowPosition.Dispose();
119 /// The Exit application.
123 if (_application != null)
130 /// Ensures that the function passed in is called from the main loop when it is idle.
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)
136 return _application.AddIdle(func);
140 /// The Run application.
142 /// <param name="args">The arguments from commandline.</param>
143 public void Run(string[] args)
145 TizenSynchronizationContext.Initialize();
146 NDalicPINVOKE.SWIGStringHelper.RegistCallback();
148 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
149 if (string.IsNullOrEmpty(args[0]))
151 args[0] = this.GetType().Assembly.FullName;
154 if (args.Length == 1)
156 if (_windowSize != null)
158 _application = Application.NewApplication(_stylesheet, (Application.WindowMode)_windowMode, new Rectangle(_windowPosition.X, _windowPosition.Y, _windowSize.Width, _windowSize.Height));
162 _application = Application.NewApplication(_stylesheet, (Application.WindowMode)_windowMode);
165 else if (args.Length > 1)
167 if (_windowSize != null)
169 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode, new Rectangle(_windowPosition.X, _windowPosition.Y, _windowSize.Width, _windowSize.Height));
173 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
177 _application.BatteryLow += OnBatteryLow;
178 _application.LanguageChanged += OnLanguageChanged;
179 _application.MemoryLow += OnMemoryLow;
180 _application.RegionChanged += OnRegionChanged;
182 _application.Initialized += OnInitialized;
183 _application.Resumed += OnResumed;
184 _application.Terminating += OnTerminated;
185 _application.Paused += OnPaused;
186 _application.AppControl += OnAppControl;
188 _application.MainLoop();
189 _application.Dispose();
193 /// The Region changed event callback function.
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)
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()));
205 /// The Memory Low event callback function.
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)
211 Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
212 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
214 switch (e.MemoryStatus)
216 case Application.MemoryStatus.Normal:
218 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
221 case Application.MemoryStatus.Low:
223 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
226 case Application.MemoryStatus.CriticallyLow:
228 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
235 /// The Language changed event callback function.
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)
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()));
247 /// The Battery Low event callback function.
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)
253 Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
254 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
255 switch (e.BatteryStatus)
257 case Application.BatteryStatus.Normal:
259 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
262 case Application.BatteryStatus.CriticallyLow:
264 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
267 case Application.BatteryStatus.PowerOff:
269 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
276 /// The Initialized event callback function.
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)
282 Log.Info("NUI", "NUICorebackend OnPreCreated Called");
283 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
284 preCreateHandler?.Invoke();
286 Log.Info("NUI", "NUICorebackend OnCreate Called");
287 var createHandler = Handlers[EventType.Created] as Action;
288 createHandler?.Invoke();
292 /// The Terminated event callback function.
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)
298 Log.Info("NUI", "NUICorebackend OnTerminated Called");
299 var handler = Handlers[EventType.Terminated] as Action;
304 /// The Resumed event callback function.
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)
310 Log.Info("NUI", "NUICorebackend OnResumed Called");
311 var handler = Handlers[EventType.Resumed] as Action;
316 /// The App control event callback function.
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)
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)));
329 /// The Paused event callback function.
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)
335 Log.Info("NUI", "NUICorebackend OnPaused Called");
336 var handler = Handlers[EventType.Paused] as Action;
340 internal Application ApplicationHandle