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;
26 class NUIWidgetCoreBackend : ICoreBackend
29 /// Application instance to connect event.
31 protected WidgetApplication _application;
32 private string _stylesheet = "";
33 Dictionary<System.Type, string> _widgetInfo;
36 /// Dictionary to contain each type of event callback.
38 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
41 /// The default Constructor.
43 public NUIWidgetCoreBackend()
48 /// The constructor with stylesheet.
50 public NUIWidgetCoreBackend(string stylesheet)
52 _stylesheet = stylesheet;
56 /// Add NUIWidgetApplication event to Application.
57 /// Put each type of event callback in Dictionary.
59 /// <param name="evType">Type of event</param>
60 /// <param name="handler">Event callback</param>
61 public void AddEventHandler(EventType evType, Action handler)
63 Handlers.Add(evType, handler);
67 /// Add NUIWidgetApplication event to Application.
68 /// Put each type of event callback in Dictionary.
70 /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
71 /// <param name="evType">Type of event</param>
72 /// <param name="handler">Event callback</param>
73 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
75 Handlers.Add(evType, handler);
84 if (_application != null)
86 _application.Dispose();
95 if (_application != null)
101 public void RegisterWidgetInfo(Dictionary<System.Type, string> widgetInfo)
103 _widgetInfo = widgetInfo;
109 /// <param name="args">Arguments from commandline.</param>
110 public void Run(string[] args)
112 TizenSynchronizationContext.Initialize();
114 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
115 _application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
116 _application.RegisterWidgetInfo(_widgetInfo);
118 _application.BatteryLow += OnBatteryLow;
119 _application.LanguageChanged += OnLanguageChanged;
120 _application.MemoryLow += OnMemoryLow;
121 _application.RegionChanged += OnRegionChanged; ;
122 _application.Initialized += OnInitialized;
123 _application.Terminating += OnTerminated;
125 _application.MainLoop();
129 /// The Initialized event callback function.
131 /// <param name="source">The application instance.</param>
132 /// <param name="e">The event argument for Initialized.</param>
133 private void OnInitialized(object source, NUIApplicationInitEventArgs e)
135 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
136 preCreateHandler?.Invoke();
138 var createHandler = Handlers[EventType.Created] as Action;
139 createHandler?.Invoke();
140 _application.RegisterWidgetCreatingFunction();
144 /// The Terminated event callback function.
146 /// <param name="source">The application instance.</param>
147 /// <param name="e">The event argument for Terminated.</param>
148 private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
150 var handler = Handlers[EventType.Terminated] as Action;
155 /// The Region changed event callback function.
157 /// <param name="source">The application instance.</param>
158 /// <param name="e">The event argument for RegionChanged.</param>
159 private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
161 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
162 handler?.Invoke(new RegionFormatChangedEventArgs(e.Application.GetRegion()));
166 /// The Language changed event callback function.
168 /// <param name="source">The application instance.</param>
169 /// <param name="e">The event argument for LanguageChanged.</param>
170 private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
172 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
173 handler?.Invoke(new LocaleChangedEventArgs(e.Application.GetLanguage()));
177 /// The Memory Low event callback function.
179 /// <param name="source">The application instance.</param>
180 /// <param name="e">The event argument for MemoryLow.</param>
181 private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
183 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
185 switch (e.MemoryStatus)
187 case Application.MemoryStatus.Normal:
189 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
192 case Application.MemoryStatus.Low:
194 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
197 case Application.MemoryStatus.CriticallyLow:
199 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
206 /// The Battery Low event callback function.
208 /// <param name="source">The application instance.</param>
209 /// <param name="e">The event argument for BatteryLow.</param>
210 private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
212 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
213 switch (e.BatteryStatus)
215 case Application.BatteryStatus.Normal:
217 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
220 case Application.BatteryStatus.CriticallyLow:
222 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
225 case Application.BatteryStatus.PowerOff:
227 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
233 internal WidgetApplication WidgetApplicationHandle