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;
108 /// <param name="args">Arguments from commandline.</param>
109 public void Run(string[] args)
111 TizenSynchronizationContext.Initialize();
113 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
114 _application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
115 _application.RegisterWidgetInfo(_widgetInfo);
117 _application.BatteryLow += OnBatteryLow;
118 _application.LanguageChanged += OnLanguageChanged;
119 _application.MemoryLow += OnMemoryLow;
120 _application.RegionChanged += OnRegionChanged; ;
121 _application.Initialized += OnInitialized;
122 _application.Terminating += OnTerminated;
124 _application.MainLoop();
128 /// The Initialized event callback function.
130 /// <param name="source">The application instance.</param>
131 /// <param name="e">The event argument for Initialized.</param>
132 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
134 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
135 preCreateHandler?.Invoke();
137 var createHandler = Handlers[EventType.Created] as Action;
138 createHandler?.Invoke();
139 _application.RegisterWidgetCreatingFunction();
143 /// The Terminated event callback function.
145 /// <param name="source">The application instance.</param>
146 /// <param name="e">The event argument for Terminated.</param>
147 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
149 var handler = Handlers[EventType.Terminated] as Action;
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 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
161 handler?.Invoke(new RegionFormatChangedEventArgs(e.Application.GetRegion()));
165 /// The Language changed event callback function.
167 /// <param name="source">The application instance.</param>
168 /// <param name="e">The event argument for LanguageChanged.</param>
169 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
171 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
172 handler?.Invoke(new LocaleChangedEventArgs(e.Application.GetLanguage()));
176 /// The Memory Low event callback function.
178 /// <param name="source">The application instance.</param>
179 /// <param name="e">The event argument for MemoryLow.</param>
180 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
182 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
184 switch (e.MemoryStatus)
186 case Application.MemoryStatus.Normal:
188 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
191 case Application.MemoryStatus.Low:
193 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
196 case Application.MemoryStatus.CriticallyLow:
198 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
205 /// The Battery Low event callback function.
207 /// <param name="source">The application instance.</param>
208 /// <param name="e">The event argument for BatteryLow.</param>
209 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
211 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
212 switch (e.BatteryStatus)
214 case Application.BatteryStatus.Normal:
216 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
219 case Application.BatteryStatus.CriticallyLow:
221 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
224 case Application.BatteryStatus.PowerOff:
226 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
232 internal WidgetApplication WidgetApplicationHandle