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 = "";
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()
44 //Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend called");
45 //_application = WidgetApplication.NewWidgetApplication();
49 /// The constructor with stylesheet.
51 public NUIWidgetCoreBackend(string stylesheet)
53 _stylesheet = stylesheet;
54 //_application = WidgetApplication.NewWidgetApplication(stylesheet);
58 /// Add NUIWidgetApplication event to Application.
59 /// Put each type of event callback in Dictionary.
61 /// <param name="evType">Type of event</param>
62 /// <param name="handler">Event callback</param>
63 public void AddEventHandler(EventType evType, Action handler)
65 Handlers.Add(evType, handler);
69 /// Add NUIWidgetApplication event to Application.
70 /// Put each type of event callback in Dictionary.
72 /// <typeparam name="TEventArgs">Argument type for the event</typeparam>
73 /// <param name="evType">Type of event</param>
74 /// <param name="handler">Event callback</param>
75 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
77 Handlers.Add(evType, handler);
86 Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend Dispose called");
87 if (_application != null)
89 _application.Dispose();
98 Tizen.Log.Fatal("NUI", "### NUIWidgetCoreBackend Exit called");
99 if (_application != null)
108 /// <param name="args">Arguments from commandline.</param>
109 public void Run(string[] args)
111 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
112 _application = WidgetApplication.NewWidgetApplication(args, _stylesheet);
114 TizenSynchronizationContext.Initialize();
115 _application.BatteryLow += OnBatteryLow;
116 _application.LanguageChanged += OnLanguageChanged;
117 _application.MemoryLow += OnMemoryLow;
118 _application.RegionChanged += OnRegionChanged; ;
120 _application.Init += OnInit;
121 _application.Terminate += OnTerminate;
123 _application.MainLoop();
126 private void OnInit(object sender, WidgetApplication.WidgetApplicationEventArgs e)
128 Log.Fatal("NUI", "NUIWidgetApplication OnPreCreated Called");
129 var preCreateHandler = Handlers[EventType.PreCreated] as Action;
130 preCreateHandler?.Invoke();
132 Log.Fatal("NUI", "NUIWidgetApplication OnCreate Called");
133 var createHandler = Handlers[EventType.Created] as Action;
134 createHandler?.Invoke();
138 private void OnTerminate(object sender, WidgetApplication.WidgetApplicationEventArgs e)
140 Log.Fatal("NUI", "NUIWidgetApplication OnTerminated Called");
141 var handler = Handlers[EventType.Terminated] as Action;
146 /// Region changed event callback function.
148 /// <param name="sender">Application instance</param>
149 /// <param name="e">Event argument for RegionChanged</param>
150 private void OnRegionChanged(object sender, WidgetApplication.WidgetApplicationEventArgs e)
152 Log.Fatal("NUI", "NUIWidgetApplication OnRegionChanged Called");
153 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
154 // Need to make new signal return in native to return right value.
155 handler?.Invoke(new RegionFormatChangedEventArgs(""));
159 /// Memory Low event callback function.
161 /// <param name="sender">Application instance</param>
162 /// <param name="e">Event argument for MemoryLow</param>
163 private void OnMemoryLow(object sender, WidgetApplication.WidgetApplicationEventArgs e)
165 Log.Fatal("NUI", "NUIWidgetApplication OnMemoryLow Called");
166 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
167 // Need to make new signal return in native to return right value.
168 handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
172 /// Language changed event callback function.
174 /// <param name="sender">Application instance</param>
175 /// <param name="e">Event argument for LanguageChanged</param>
176 private void OnLanguageChanged(object sender, WidgetApplication.WidgetApplicationEventArgs e)
179 Log.Fatal("NUI", "NUIWidgetApplication OnLanguageChanged Called");
180 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
181 // Need to make new signal return in native to return right value.
182 handler?.Invoke(new LocaleChangedEventArgs(""));
187 /// Battery low event callback function.
189 /// <param name="sender">Application instance</param>
190 /// <param name="e">Event argument for BatteryLow</param>
191 private void OnBatteryLow(object sender, WidgetApplication.WidgetApplicationEventArgs e)
193 Log.Fatal("NUI", "NUIWidgetApplication OnBatteryLow Called");
194 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
195 // Need to make new signal return in native to return right value.
196 handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
200 internal WidgetApplication WidgetApplicationHandle