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 /// Application instance to connect event.
32 protected Application _application;
33 private string _stylesheet = "";
34 private NUIApplication.WindowMode _windowMode = NUIApplication.WindowMode.Opaque;
37 /// 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 /// Add NUIApplication event to Application.
67 /// Put each type of event callback in Dictionary.
69 /// <param name="evType">Type of event</param>
70 /// <param name="handler">Event callback</param>
71 public void AddEventHandler(EventType evType, Action handler)
73 Handlers.Add(evType, handler);
77 /// Add NUIApplication event to Application.
78 /// Put each type of event callback in Dictionary.
80 /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
81 /// <param name="evType">Type of event</param>
82 /// <param name="handler">Event callback</param>
83 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
85 Handlers.Add(evType, handler);
94 if(_application != null)
96 _application.Dispose();
101 /// Exit Application.
105 if(_application != null)
114 /// <param name="args">Arguments from commandline.</param>
115 public void Run(string[] args)
117 TizenSynchronizationContext.Initialize();
119 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
120 if (args.Length == 1)
122 _application = Application.NewApplication();
124 else if (args.Length > 1)
126 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
129 _application.BatteryLow += OnBatteryLow;
130 _application.LanguageChanged += OnLanguageChanged;
131 _application.MemoryLow += OnMemoryLow;
132 _application.RegionChanged += OnRegionChanged;
134 _application.Initialized += OnInitialized;
135 _application.Resumed += OnResumed;
136 _application.Terminating += OnTerminated;
137 _application.Paused += OnPaused;
138 _application.AppControl += OnAppControl;
140 _application.MainLoop();
144 /// Region changed event callback function.
146 /// <param name="source">Application instance</param>
147 /// <param name="e">Event argument for RegionChanged</param>
148 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
150 Log.Debug("NUI", "NUICorebackend OnRegionChanged Called");
151 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
152 // Need to make new signal return in native to return right value.
153 handler?.Invoke( new RegionFormatChangedEventArgs(""));
157 /// Memory Low event callback function.
159 /// <param name="source">Application instance</param>
160 /// <param name="e">Event argument for MemoryLow</param>
161 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
163 Log.Debug("NUI", "NUICorebackend OnMemoryLow Called");
164 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
165 // Need to make new signal return in native to return right value.
166 handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
170 /// Language changed event callback function.
172 /// <param name="source">Application instance</param>
173 /// <param name="e">Event argument for LanguageChanged</param>
174 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
176 Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
177 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
178 // Need to make new signal return in native to return right value.
179 handler?.Invoke( new LocaleChangedEventArgs(""));
183 /// Battery low event callback function.
185 /// <param name="source">Application instance</param>
186 /// <param name="e">Event argument for BatteryLow</param>
187 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
189 Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
190 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
191 // Need to make new signal return in native to return right value.
192 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
196 /// Initialized event callback function.
198 /// <param name="source">Application instance</param>
199 /// <param name="e">Event argument for Initialized</param>
200 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
202 Log.Debug("NUI", "NUICorebackend OnPreCreated Called");
203 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
204 preCreateHandler?.Invoke();
206 Log.Debug("NUI", "NUICorebackend OnCreate Called");
207 var createHandler = Handlers[EventType.Created] as Action;
208 createHandler?.Invoke();
212 /// Terminated event callback function.
214 /// <param name="source">Application instance</param>
215 /// <param name="e">Event argument for Terminated</param>
216 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
218 Log.Debug("NUI", "NUICorebackend OnTerminated Called");
219 var handler = Handlers[EventType.Terminated] as Action;
224 /// Resumed event callback function.
226 /// <param name="source">Application instance</param>
227 /// <param name="e">Event argument for Resumed</param>
228 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
230 Log.Debug("NUI", "NUICorebackend OnResumed Called");
231 var handler = Handlers[EventType.Resumed] as Action;
236 /// App control event callback function.
238 /// <param name="source">Application instance</param>
239 /// <param name="e">Event argument for AppControl</param>
240 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
242 Log.Debug("NUI", "NUICorebackend OnAppControl Called");
243 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
\r
244 SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
\r
245 handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
249 /// Paused event callback function.
251 /// <param name="source">Application instance</param>
252 /// <param name="e">Event argument for Paused</param>
253 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
255 Log.Debug("NUI", "NUICorebackend OnPaused Called");
256 var handler = Handlers[EventType.Paused] as Action;
261 internal Application ApplicationHandle