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;
20 using Tizen.Applications.CoreBackend;
21 using Tizen.Applications;
25 class NUIWidgetCoreBackend : ICoreBackend
28 /// Application instance to connect event.
30 protected WidgetApplication _application;
31 private string _stylesheet = "";
32 Dictionary<System.Type, string> _widgetInfo;
35 /// Dictionary to contain each type of event callback.
37 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
40 /// The default Constructor.
42 public NUIWidgetCoreBackend()
47 /// The constructor with stylesheet.
49 public NUIWidgetCoreBackend(string stylesheet)
51 _stylesheet = stylesheet;
55 /// Add NUIWidgetApplication event to Application.
56 /// Put each type of event callback in Dictionary.
58 /// <param name="evType">Type of event</param>
59 /// <param name="handler">Event callback</param>
60 public void AddEventHandler(EventType evType, Action handler)
62 Handlers.Add(evType, handler);
66 /// Add NUIWidgetApplication event to Application.
67 /// Put each type of event callback in Dictionary.
69 /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
70 /// <param name="evType">Type of event</param>
71 /// <param name="handler">Event callback</param>
72 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
74 Handlers.Add(evType, handler);
83 if (_application != null)
85 _application.Dispose();
94 if (_application != null)
100 public void RegisterWidgetInfo(Dictionary<System.Type, string> widgetInfo)
102 _widgetInfo = widgetInfo;
105 public void AddWidgetInfo(Dictionary<System.Type, string> widgetInfo)
107 if (_application != null)
109 _application.AddWidgetInfo(widgetInfo);
116 /// <param name="args">Arguments from commandline.</param>
117 public void Run(string[] args)
119 TizenSynchronizationContext.Initialize();
121 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
122 _application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
123 _application.RegisterWidgetInfo(_widgetInfo);
125 _application.BatteryLow += OnBatteryLow;
126 _application.LanguageChanged += OnLanguageChanged;
127 _application.MemoryLow += OnMemoryLow;
128 _application.RegionChanged += OnRegionChanged; ;
129 _application.Initialized += OnInitialized;
130 _application.Terminating += OnTerminated;
132 _application.MainLoop();
136 /// The Initialized event callback function.
138 /// <param name="source">The application instance.</param>
139 /// <param name="e">The event argument for Initialized.</param>
140 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
142 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
143 preCreateHandler?.Invoke();
145 var createHandler = Handlers[EventType.Created] as Action;
146 createHandler?.Invoke();
147 _application.RegisterWidgetCreatingFunction();
151 /// The Terminated event callback function.
153 /// <param name="source">The application instance.</param>
154 /// <param name="e">The event argument for Terminated.</param>
155 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
157 var handler = Handlers[EventType.Terminated] as Action;
162 /// The Region changed event callback function.
164 /// <param name="source">The application instance.</param>
165 /// <param name="e">The event argument for RegionChanged.</param>
166 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
168 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
169 handler?.Invoke(new RegionFormatChangedEventArgs(e.Application.GetRegion()));
173 /// The Language changed event callback function.
175 /// <param name="source">The application instance.</param>
176 /// <param name="e">The event argument for LanguageChanged.</param>
177 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
179 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
180 handler?.Invoke(new LocaleChangedEventArgs(e.Application.GetLanguage()));
184 /// The Memory Low event callback function.
186 /// <param name="source">The application instance.</param>
187 /// <param name="e">The event argument for MemoryLow.</param>
188 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
190 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
192 switch (e.MemoryStatus)
194 case Application.MemoryStatus.Normal:
196 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
199 case Application.MemoryStatus.Low:
201 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
204 case Application.MemoryStatus.CriticallyLow:
206 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
213 /// The Battery Low event callback function.
215 /// <param name="source">The application instance.</param>
216 /// <param name="e">The event argument for BatteryLow.</param>
217 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
219 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
220 switch (e.BatteryStatus)
222 case Application.BatteryStatus.Normal:
224 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
227 case Application.BatteryStatus.CriticallyLow:
229 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
232 case Application.BatteryStatus.PowerOff:
234 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
240 internal WidgetApplication WidgetApplicationHandle