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)
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);
124 /// <param name="args">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 /// Region changed event callback function.
156 /// <param name="source">Application instance</param>
157 /// <param name="e">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(""));
167 /// Memory Low event callback function.
169 /// <param name="source">Application instance</param>
170 /// <param name="e">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 handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
180 /// Language changed event callback function.
182 /// <param name="source">Application instance</param>
183 /// <param name="e">Event argument for LanguageChanged</param>
184 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
186 Log.Debug("NUI", "NUICorebackend OnLanguageChanged Called");
187 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
188 // Need to make new signal return in native to return right value.
189 handler?.Invoke( new LocaleChangedEventArgs(""));
193 /// Battery low event callback function.
195 /// <param name="source">Application instance</param>
196 /// <param name="e">Event argument for BatteryLow</param>
197 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
199 Log.Debug("NUI", "NUICorebackend OnBatteryLow Called");
200 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
201 // Need to make new signal return in native to return right value.
202 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
206 /// Initialized event callback function.
208 /// <param name="source">Application instance</param>
209 /// <param name="e">Event argument for Initialized</param>
210 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
212 Log.Debug("NUI", "NUICorebackend OnPreCreated Called");
213 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
214 preCreateHandler?.Invoke();
216 Log.Debug("NUI", "NUICorebackend OnCreate Called");
217 var createHandler = Handlers[EventType.Created] as Action;
218 createHandler?.Invoke();
222 /// Terminated event callback function.
224 /// <param name="source">Application instance</param>
225 /// <param name="e">Event argument for Terminated</param>
226 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
228 Log.Debug("NUI", "NUICorebackend OnTerminated Called");
229 var handler = Handlers[EventType.Terminated] as Action;
234 /// Resumed event callback function.
236 /// <param name="source">Application instance</param>
237 /// <param name="e">Event argument for Resumed</param>
238 private void OnResumed(object source, NUIApplicationResumedEventArgs e)
240 Log.Debug("NUI", "NUICorebackend OnResumed Called");
241 var handler = Handlers[EventType.Resumed] as Action;
246 /// App control event callback function.
248 /// <param name="source">Application instance</param>
249 /// <param name="e">Event argument for AppControl</param>
250 private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
252 Log.Debug("NUI", "NUICorebackend OnAppControl Called");
253 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
254 SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
255 handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
259 /// Paused event callback function.
261 /// <param name="source">Application instance</param>
262 /// <param name="e">Event argument for Paused</param>
263 private void OnPaused(object source, NUIApplicationPausedEventArgs e)
265 Log.Debug("NUI", "NUICorebackend OnPaused Called");
266 var handler = Handlers[EventType.Paused] as Action;
271 internal Application ApplicationHandle