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;
27 class NUICoreBackend : ICoreBackend
30 /// The Application instance to connect event.
32 protected Application _application;
33 private string _stylesheet = "";
34 private NUIApplication.WindowMode _windowMode = NUIApplication.WindowMode.Opaque;
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 /// Adds NUIApplication event to Application.
67 /// Puts each type of event callback in Dictionary.
69 /// <param name="evType">The type of event.</param>
70 /// <param name="handler">The event callback.</param>
71 public void AddEventHandler(EventType evType, Action handler)
73 Handlers.Add(evType, handler);
77 /// Adds NUIApplication event to Application.
78 /// Puts each type of event callback in Dictionary.
80 /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
81 /// <param name="evType">The type of event.</param>
82 /// <param name="handler">The event callback.</param>
83 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
85 Handlers.Add(evType, handler);
90 /// The Dispose function.
94 if(_application != null)
96 _application.Dispose();
101 /// The Exit application.
105 if(_application != null)
112 /// Ensures that the function passed in is called from the main loop when it is idle.
114 /// <param name="func">The function to call</param>
115 /// <returns>true if added successfully, false otherwise</returns>
116 public bool AddIdle(System.Delegate func)
118 return _application.AddIdle(func);
122 /// The Run application.
124 /// <param name="args">The arguments from commandline.</param>
125 public void Run(string[] args)
127 TizenSynchronizationContext.Initialize();
129 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
130 if (args.Length == 1)
132 _application = Application.NewApplication();
134 else if (args.Length > 1)
136 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
139 _application.BatteryLow += OnBatteryLow;
140 _application.LanguageChanged += OnLanguageChanged;
141 _application.MemoryLow += OnMemoryLow;
142 _application.RegionChanged += OnRegionChanged;
144 _application.Initialized += OnInitialized;
145 _application.Resumed += OnResumed;
146 _application.Terminating += OnTerminated;
147 _application.Paused += OnPaused;
148 _application.AppControl += OnAppControl;
150 _application.MainLoop();
154 /// The Region changed event callback function.
156 /// <param name="source">The application instance.</param>
157 /// <param name="e">The event argument for RegionChanged.</param>
158 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
160 Log.Debug("NUI", "NUICorebackend OnRegionChanged Called");
161 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
162 // Need to make new signal return in native to return right value.
163 handler?.Invoke( new RegionFormatChangedEventArgs(e.Application.GetRegion()));
167 /// The Memory Low event callback function.
169 /// <param name="source">The application instance.</param>
170 /// <param name="e">The event argument for MemoryLow.</param>
171 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
173 Log.Debug("NUI", "NUICorebackend OnMemoryLow Called");
174 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
175 // Need to make new signal return in native to return right value.
176 switch( e.MemoryStatus )
178 case Application.MemoryStatus.Normal:
180 handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
183 case Application.MemoryStatus.SoftWarning:
185 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
188 case Application.MemoryStatus.HardWarning:
190 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
197 /// The Language changed event callback function.
199 /// <param name="source">The application instance.</param>
200 /// <param name="e">The event argument for LanguageChanged.</param>
201 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
203 Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
204 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
205 // Need to make new signal return in native to return right value.
206 handler?.Invoke( new LocaleChangedEventArgs(e.Application.GetLanguage()));
210 /// The Battery Low event callback function.
212 /// <param name="source">The application instance.</param>
213 /// <param name="e">The event argument for BatteryLow.</param>
214 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
216 Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
217 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
218 // Need to make new signal return in native to return right value.
219 switch( e.BatteryStatus )
221 case Application.BatteryStatus.Normal:
223 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
226 case Application.BatteryStatus.CriticalLow:
228 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
231 case Application.BatteryStatus.PowerOff:
233 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
240 /// The Initialized event callback function.
242 /// <param name="source">The application instance.</param>
243 /// <param name="e">The event argument for Initialized.</param>
244 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
246 Log.Debug("NUI", "NUICorebackend OnPreCreated Called");
247 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
248 preCreateHandler?.Invoke();
250 Log.Debug("NUI", "NUICorebackend OnCreate Called");
251 var createHandler = Handlers[EventType.Created] as Action;
252 createHandler?.Invoke();
256 /// The Terminated event callback function.
258 /// <param name="source">The application instance.</param>
259 /// <param name="e">The event argument for Terminated.</param>
260 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
262 Log.Debug("NUI", "NUICorebackend OnTerminated Called");
263 var handler = Handlers[EventType.Terminated] as Action;
268 /// The Resumed event callback function.
270 /// <param name="source">The application instance.</param>
271 /// <param name="e">The event argument for Resumed.</param>
272 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
274 Log.Debug("NUI", "NUICorebackend OnResumed Called");
275 var handler = Handlers[EventType.Resumed] as Action;
280 /// The App control event callback function.
282 /// <param name="source">The application instance.</param>
283 /// <param name="e">The event argument for AppControl.</param>
284 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
286 Log.Debug("NUI", "NUICorebackend OnAppControl Called");
287 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
288 SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
289 handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
293 /// The Paused event callback function.
295 /// <param name="source">The application instance.</param>
296 /// <param name="e">The event argument for Paused.</param>
297 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
299 Log.Debug("NUI", "NUICorebackend OnPaused Called");
300 var handler = Handlers[EventType.Paused] as Action;
305 internal Application ApplicationHandle