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 Rectangle windowRectangle = null;
36 /// The Dictionary to contain each type of event callback.
38 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
41 /// The default Constructor.
43 public NUICoreBackend()
48 /// The constructor with stylesheet.
50 public NUICoreBackend(string stylesheet)
52 this.stylesheet = stylesheet;
56 /// The constructor with stylesheet and window mode.
58 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
60 this.stylesheet = stylesheet;
61 this.windowMode = windowMode;
65 /// The constructor with stylesheet, window mode, window size and window position.
67 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
69 this.stylesheet = stylesheet;
70 this.windowMode = windowMode;
71 if (windowSize != null && windowPosition != null)
73 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
78 /// Adds NUIApplication event to Application.
79 /// Puts each type of event callback in Dictionary.
81 /// <param name="evType">The type of event.</param>
82 /// <param name="handler">The event callback.</param>
83 public void AddEventHandler(EventType evType, Action handler)
85 Handlers.Add(evType, handler);
89 /// Adds NUIApplication event to Application.
90 /// Puts each type of event callback in Dictionary.
92 /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
93 /// <param name="evType">The type of event.</param>
94 /// <param name="handler">The event callback.</param>
95 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
97 Handlers.Add(evType, handler);
101 /// The Dispose function.
103 public void Dispose()
105 if (application != null)
107 application.Dispose();
109 if (windowRectangle != null)
111 windowRectangle.Dispose();
116 /// The Exit application.
120 if (application != null)
127 /// Ensures that the function passed in is called from the main loop when it is idle.
129 /// <param name="func">The function to call</param>
130 /// <returns>true if added successfully, false otherwise</returns>
131 public bool AddIdle(System.Delegate func)
133 return application.AddIdle(func);
137 /// The Run application.
139 /// <param name="args">The arguments from commandline.</param>
140 public void Run(string[] args)
142 TizenSynchronizationContext.Initialize();
144 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
145 if (string.IsNullOrEmpty(args[0]))
147 args[0] = this.GetType().Assembly.FullName.Replace(" ", "");
150 if (windowRectangle != null)
152 application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle);
156 application = Application.NewApplication(args, stylesheet, windowMode);
159 application.BatteryLow += OnBatteryLow;
160 application.LanguageChanged += OnLanguageChanged;
161 application.MemoryLow += OnMemoryLow;
162 application.RegionChanged += OnRegionChanged;
164 application.Initialized += OnInitialized;
165 application.Resumed += OnResumed;
166 application.Terminating += OnTerminated;
167 application.Paused += OnPaused;
168 application.AppControl += OnAppControl;
170 application.MainLoop();
171 application.Dispose();
175 /// The Region changed event callback function.
177 /// <param name="source">The application instance.</param>
178 /// <param name="e">The event argument for RegionChanged.</param>
179 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
181 Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
182 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
183 handler?.Invoke(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
187 /// The Memory Low event callback function.
189 /// <param name="source">The application instance.</param>
190 /// <param name="e">The event argument for MemoryLow.</param>
191 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
193 Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
194 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
196 switch (e.MemoryStatus)
198 case Application.MemoryStatus.Normal:
200 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
203 case Application.MemoryStatus.Low:
205 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
208 case Application.MemoryStatus.CriticallyLow:
210 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
217 /// The Language changed event callback function.
219 /// <param name="source">The application instance.</param>
220 /// <param name="e">The event argument for LanguageChanged.</param>
221 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
223 Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
224 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
225 handler?.Invoke(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
229 /// The Battery Low event callback function.
231 /// <param name="source">The application instance.</param>
232 /// <param name="e">The event argument for BatteryLow.</param>
233 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
235 Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
236 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
237 switch (e.BatteryStatus)
239 case Application.BatteryStatus.Normal:
241 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
244 case Application.BatteryStatus.CriticallyLow:
246 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
249 case Application.BatteryStatus.PowerOff:
251 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
258 /// The Initialized event callback function.
260 /// <param name="source">The application instance.</param>
261 /// <param name="e">The event argument for Initialized.</param>
262 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
264 Log.Info("NUI", "NUICorebackend OnPreCreated Called");
265 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
266 preCreateHandler?.Invoke();
268 Log.Info("NUI", "NUICorebackend OnCreate Called");
269 var createHandler = Handlers[EventType.Created] as Action;
270 createHandler?.Invoke();
274 /// The Terminated event callback function.
276 /// <param name="source">The application instance.</param>
277 /// <param name="e">The event argument for Terminated.</param>
278 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
280 Log.Info("NUI", "NUICorebackend OnTerminated Called");
281 var handler = Handlers[EventType.Terminated] as Action;
286 /// The Resumed event callback function.
288 /// <param name="source">The application instance.</param>
289 /// <param name="e">The event argument for Resumed.</param>
290 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
292 Log.Info("NUI", "NUICorebackend OnResumed Called");
293 var handler = Handlers[EventType.Resumed] as Action;
298 /// The App control event callback function.
300 /// <param name="source">The application instance.</param>
301 /// <param name="e">The event argument for AppControl.</param>
302 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
304 Log.Info("NUI", "NUICorebackend OnAppControl Called");
305 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
306 SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
307 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
311 /// The Paused event callback function.
313 /// <param name="source">The application instance.</param>
314 /// <param name="e">The event argument for Paused.</param>
315 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
317 Log.Info("NUI", "NUICorebackend OnPaused Called");
318 var handler = Handlers[EventType.Paused] as Action;
322 internal Application ApplicationHandle