2 * Copyright (c) 2017 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;
21 using Tizen.Applications.CoreBackend;
22 using Tizen.Applications;
26 class NUICoreBackend : ICoreBackend
29 /// The Application instance to connect event.
31 protected Application _application;
32 private string _stylesheet = "";
33 private NUIApplication.WindowMode _windowMode = NUIApplication.WindowMode.Opaque;
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 _stylesheet = stylesheet;
56 /// The constructor with stylesheet and window mode.
58 public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
60 _stylesheet = stylesheet;
61 _windowMode = windowMode;
65 /// Adds NUIApplication event to Application.
66 /// Puts each type of event callback in Dictionary.
68 /// <param name="evType">The type of event.</param>
69 /// <param name="handler">The event callback.</param>
70 public void AddEventHandler(EventType evType, Action handler)
72 Handlers.Add(evType, handler);
76 /// Adds NUIApplication event to Application.
77 /// Puts each type of event callback in Dictionary.
79 /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
80 /// <param name="evType">The type of event.</param>
81 /// <param name="handler">The event callback.</param>
82 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
84 Handlers.Add(evType, handler);
89 /// The Dispose function.
93 if(_application != null)
95 _application.Dispose();
100 /// The Exit application.
104 if(_application != null)
111 /// Ensures that the function passed in is called from the main loop when it is idle.
113 /// <param name="func">The function to call</param>
114 /// <returns>true if added successfully, false otherwise</returns>
115 public bool AddIdle(System.Delegate func)
117 return _application.AddIdle(func);
121 /// The Run application.
123 /// <param name="args">The arguments from commandline.</param>
124 public void Run(string[] args)
126 TizenSynchronizationContext.Initialize();
128 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
129 if (args.Length == 1)
131 _application = Application.NewApplication();
133 else if (args.Length > 1)
135 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
138 _application.BatteryLow += OnBatteryLow;
139 _application.LanguageChanged += OnLanguageChanged;
140 _application.MemoryLow += OnMemoryLow;
141 _application.RegionChanged += OnRegionChanged;
143 _application.Initialized += OnInitialized;
144 _application.Resumed += OnResumed;
145 _application.Terminating += OnTerminated;
146 _application.Paused += OnPaused;
147 _application.AppControl += OnAppControl;
149 _application.MainLoop();
151 if(Graphics.Backend == Graphics.BackendType.Gles)
153 //Temporary fix. VK backend make crash. this will be removed after VK memory issue is resolved.
154 _application.Dispose();
160 /// The Region changed event callback function.
162 /// <param name="source">The application instance.</param>
163 /// <param name="e">The event argument for RegionChanged.</param>
164 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
166 Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
167 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
168 handler?.Invoke( new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
172 /// The Memory Low event callback function.
174 /// <param name="source">The application instance.</param>
175 /// <param name="e">The event argument for MemoryLow.</param>
176 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
178 Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
179 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
181 switch ( e.MemoryStatus )
183 case Application.MemoryStatus.Normal:
185 handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
188 case Application.MemoryStatus.Low:
190 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
193 case Application.MemoryStatus.CriticallyLow:
195 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
202 /// The Language changed event callback function.
204 /// <param name="source">The application instance.</param>
205 /// <param name="e">The event argument for LanguageChanged.</param>
206 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
208 Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
209 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
210 handler?.Invoke( new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
214 /// The Battery Low event callback function.
216 /// <param name="source">The application instance.</param>
217 /// <param name="e">The event argument for BatteryLow.</param>
218 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
220 Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
221 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
222 switch( e.BatteryStatus )
224 case Application.BatteryStatus.Normal:
226 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
229 case Application.BatteryStatus.CriticallyLow:
231 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
234 case Application.BatteryStatus.PowerOff:
236 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
243 /// The Initialized event callback function.
245 /// <param name="source">The application instance.</param>
246 /// <param name="e">The event argument for Initialized.</param>
247 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
249 Log.Info("NUI", "NUICorebackend OnPreCreated Called");
250 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
251 preCreateHandler?.Invoke();
253 Log.Info("NUI", "NUICorebackend OnCreate Called");
254 var createHandler = Handlers[EventType.Created] as Action;
255 createHandler?.Invoke();
259 /// The Terminated event callback function.
261 /// <param name="source">The application instance.</param>
262 /// <param name="e">The event argument for Terminated.</param>
263 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
265 Log.Info("NUI", "NUICorebackend OnTerminated Called");
266 var handler = Handlers[EventType.Terminated] as Action;
271 /// The Resumed event callback function.
273 /// <param name="source">The application instance.</param>
274 /// <param name="e">The event argument for Resumed.</param>
275 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
277 Log.Info("NUI", "NUICorebackend OnResumed Called");
278 var handler = Handlers[EventType.Resumed] as Action;
283 /// The App control event callback function.
285 /// <param name="source">The application instance.</param>
286 /// <param name="e">The event argument for AppControl.</param>
287 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
289 Log.Info("NUI", "NUICorebackend OnAppControl Called");
290 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
291 SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
292 handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
296 /// The Paused event callback function.
298 /// <param name="source">The application instance.</param>
299 /// <param name="e">The event argument for Paused.</param>
300 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
302 Log.Info("NUI", "NUICorebackend OnPaused Called");
303 var handler = Handlers[EventType.Paused] as Action;
308 internal Application ApplicationHandle