2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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.
18 using System.Collections.Generic;
19 using Tizen.Internals.Errors;
21 namespace Tizen.Applications.CoreBackend
23 internal class WidgetCoreBackend : ICoreBackend
25 protected static readonly string LogTag = typeof(WidgetCoreBackend).Namespace;
27 private Dictionary<EventType, object> _handlers = new Dictionary<EventType, object>();
29 private bool _disposedValue = false;
31 private Interop.Widget.AppEventCallback _lowMemoryCallback;
32 private Interop.Widget.AppEventCallback _lowBatteryCallback;
33 private Interop.Widget.AppEventCallback _localeChangedCallback;
34 private Interop.Widget.AppEventCallback _regionChangedCallback;
36 private IntPtr _lowMemoryEventHandle = IntPtr.Zero;
37 private IntPtr _lowBatteryEventHandle = IntPtr.Zero;
38 private IntPtr _localeChangedEventHandle = IntPtr.Zero;
39 private IntPtr _regionChangedEventHandle = IntPtr.Zero;
41 private Interop.Widget.WidgetAppLifecycleCallbacks _callbacks;
43 internal IList<WidgetType> WidgetTypes = new List<WidgetType>();
45 public WidgetCoreBackend()
47 _lowMemoryCallback = new Interop.Widget.AppEventCallback(OnLowMemoryNative);
48 _lowBatteryCallback = new Interop.Widget.AppEventCallback(OnLowBatteryNative);
49 _localeChangedCallback = new Interop.Widget.AppEventCallback(OnLocaleChangedNative);
50 _regionChangedCallback = new Interop.Widget.AppEventCallback(OnRegionChangedNative);
52 _callbacks.OnCreate = new Interop.Widget.WidgetAppCreateCallback(OnCreateNative);
53 _callbacks.OnTerminate = new Interop.Widget.WidgetAppTerminateCallback(OnTerminateNative);
61 internal void CreateWidgetTypes(IDictionary<Type, string> typeInfo)
63 foreach (Type w in typeInfo.Keys)
65 WidgetTypes.Add(new WidgetType(w, typeInfo[w]));
72 GC.SuppressFinalize(this);
75 protected virtual void Dispose(bool disposing)
81 // Release disposable objects
84 if (_lowMemoryEventHandle != IntPtr.Zero)
86 Interop.Widget.RemoveEventHandler(_lowMemoryEventHandle);
88 if (_lowBatteryEventHandle != IntPtr.Zero)
90 Interop.Widget.RemoveEventHandler(_lowBatteryEventHandle);
92 if (_localeChangedEventHandle != IntPtr.Zero)
94 Interop.Widget.RemoveEventHandler(_localeChangedEventHandle);
96 if (_regionChangedEventHandle != IntPtr.Zero)
98 Interop.Widget.RemoveEventHandler(_regionChangedEventHandle);
101 _disposedValue = true;
107 Interop.Widget.Exit();
110 public void Run(string[] args)
112 Interop.Widget.ErrorCode err = Interop.Widget.ErrorCode.None;
113 err = Interop.Widget.AddEventHandler(out _lowMemoryEventHandle, Interop.Widget.AppEventType.LowMemory, _lowMemoryCallback, IntPtr.Zero);
114 if (err != Interop.Widget.ErrorCode.None)
116 Log.Error(LogTag, "Failed to add event handler for LowMemory event. Err = " + err);
118 err = Interop.Widget.AddEventHandler(out _lowBatteryEventHandle, Interop.Widget.AppEventType.LowBattery, _lowBatteryCallback, IntPtr.Zero);
119 if (err != Interop.Widget.ErrorCode.None)
121 Log.Error(LogTag, "Failed to add event handler for LowBattery event. Err = " + err);
124 err = Interop.Widget.AddEventHandler(out _localeChangedEventHandle, Interop.Widget.AppEventType.LanguageChanged, _localeChangedCallback, IntPtr.Zero);
125 if (err != Interop.Widget.ErrorCode.None)
127 Log.Error(LogTag, "Failed to add event handler for LocaleChanged event. Err = " + err);
130 err = Interop.Widget.AddEventHandler(out _regionChangedEventHandle, Interop.Widget.AppEventType.RegionFormatChanged, _regionChangedCallback, IntPtr.Zero);
131 if (err != Interop.Widget.ErrorCode.None)
133 Log.Error(LogTag, "Failed to add event handler for RegionFormatChanged event. Err = " + err);
136 err = Interop.Widget.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
137 if (err != Interop.Widget.ErrorCode.None)
139 Log.Error(LogTag, "Failed to run the application. Err = " + err);
143 private IntPtr OnCreateNative(IntPtr data)
145 if (_handlers.ContainsKey(EventType.Created))
147 var handler = _handlers[EventType.Created] as Action;
151 IntPtr h = IntPtr.Zero;
152 foreach (WidgetType type in WidgetTypes)
158 private void OnTerminateNative(IntPtr data)
160 if (_handlers.ContainsKey(EventType.Terminated))
162 var handler = _handlers[EventType.Terminated] as Action;
167 public void AddEventHandler(EventType evType, Action handler)
169 _handlers.Add(evType, handler);
172 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
174 _handlers.Add(evType, handler);
177 private void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
179 LowMemoryStatus status = LowMemoryStatus.None;
180 ErrorCode err = Interop.Widget.AppEventGetLowMemoryStatus(infoHandle, out status);
181 if (err != ErrorCode.None)
183 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
185 if (_handlers.ContainsKey(EventType.LowMemory))
187 var handler = _handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
188 handler?.Invoke(new LowMemoryEventArgs(status));
192 private void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
194 LowBatteryStatus status = LowBatteryStatus.None;
195 ErrorCode err = Interop.Widget.AppEventGetLowBatteryStatus(infoHandle, out status);
196 if (err != ErrorCode.None)
198 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
200 if (_handlers.ContainsKey(EventType.LowBattery))
202 var handler = _handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
203 handler?.Invoke(new LowBatteryEventArgs(status));
207 private void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
210 ErrorCode err = Interop.Widget.AppEventGetLanguage(infoHandle, out lang);
211 if (err != ErrorCode.None)
213 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
215 if (_handlers.ContainsKey(EventType.LocaleChanged))
217 var handler = _handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
218 handler?.Invoke(new LocaleChangedEventArgs(lang));
222 private void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
225 ErrorCode err = Interop.Widget.AppEventGetRegionFormat(infoHandle, out region);
226 if (err != ErrorCode.None)
228 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
230 if (_handlers.ContainsKey(EventType.RegionFormatChanged))
232 var handler = _handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
233 handler?.Invoke(new RegionFormatChangedEventArgs(region));