Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.WidgetApplication / Tizen.Applications.CoreBackend / WidgetCoreBackend.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using Tizen.Internals.Errors;
20
21 namespace Tizen.Applications.CoreBackend
22 {
23     internal class WidgetCoreBackend : ICoreBackend
24     {
25         protected static readonly string LogTag = typeof(WidgetCoreBackend).Namespace;
26
27         private Dictionary<EventType, object> _handlers = new Dictionary<EventType, object>();
28
29         private bool _disposedValue = false;
30
31         private Interop.Widget.AppEventCallback _lowMemoryCallback;
32         private Interop.Widget.AppEventCallback _lowBatteryCallback;
33         private Interop.Widget.AppEventCallback _localeChangedCallback;
34         private Interop.Widget.AppEventCallback _regionChangedCallback;
35
36         private IntPtr _lowMemoryEventHandle = IntPtr.Zero;
37         private IntPtr _lowBatteryEventHandle = IntPtr.Zero;
38         private IntPtr _localeChangedEventHandle = IntPtr.Zero;
39         private IntPtr _regionChangedEventHandle = IntPtr.Zero;
40
41         private Interop.Widget.WidgetAppLifecycleCallbacks _callbacks;
42
43         internal IList<WidgetType> WidgetTypes = new List<WidgetType>();
44
45         public WidgetCoreBackend()
46         {
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);
51
52             _callbacks.OnCreate = new Interop.Widget.WidgetAppCreateCallback(OnCreateNative);
53             _callbacks.OnTerminate = new Interop.Widget.WidgetAppTerminateCallback(OnTerminateNative);
54         }
55
56         ~WidgetCoreBackend()
57         {
58             Dispose(false);
59         }
60
61         internal void CreateWidgetTypes(IDictionary<Type, string> typeInfo)
62         {
63             foreach (Type w in typeInfo.Keys)
64             {
65                 WidgetTypes.Add(new WidgetType(w, typeInfo[w]));
66             }
67         }
68
69         public void Dispose()
70         {
71             Dispose(true);
72             GC.SuppressFinalize(this);
73         }
74
75         protected virtual void Dispose(bool disposing)
76         {
77             if (!_disposedValue)
78             {
79                 if (disposing)
80                 {
81                     // Release disposable objects
82                 }
83
84                 if (_lowMemoryEventHandle != IntPtr.Zero)
85                 {
86                     Interop.Widget.RemoveEventHandler(_lowMemoryEventHandle);
87                 }
88                 if (_lowBatteryEventHandle != IntPtr.Zero)
89                 {
90                     Interop.Widget.RemoveEventHandler(_lowBatteryEventHandle);
91                 }
92                 if (_localeChangedEventHandle != IntPtr.Zero)
93                 {
94                     Interop.Widget.RemoveEventHandler(_localeChangedEventHandle);
95                 }
96                 if (_regionChangedEventHandle != IntPtr.Zero)
97                 {
98                     Interop.Widget.RemoveEventHandler(_regionChangedEventHandle);
99                 }
100
101                 _disposedValue = true;
102             }
103         }
104
105         public void Exit()
106         {
107             Interop.Widget.Exit();
108         }
109
110         public void Run(string[] args)
111         {
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)
115             {
116                 Log.Error(LogTag, "Failed to add event handler for LowMemory event. Err = " + err);
117             }
118             err = Interop.Widget.AddEventHandler(out _lowBatteryEventHandle, Interop.Widget.AppEventType.LowBattery, _lowBatteryCallback, IntPtr.Zero);
119             if (err != Interop.Widget.ErrorCode.None)
120             {
121                 Log.Error(LogTag, "Failed to add event handler for LowBattery event. Err = " + err);
122             }
123
124             err = Interop.Widget.AddEventHandler(out _localeChangedEventHandle, Interop.Widget.AppEventType.LanguageChanged, _localeChangedCallback, IntPtr.Zero);
125             if (err != Interop.Widget.ErrorCode.None)
126             {
127                 Log.Error(LogTag, "Failed to add event handler for LocaleChanged event. Err = " + err);
128             }
129
130             err = Interop.Widget.AddEventHandler(out _regionChangedEventHandle, Interop.Widget.AppEventType.RegionFormatChanged, _regionChangedCallback, IntPtr.Zero);
131             if (err != Interop.Widget.ErrorCode.None)
132             {
133                 Log.Error(LogTag, "Failed to add event handler for RegionFormatChanged event. Err = " + err);
134             }
135
136             err = Interop.Widget.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
137             if (err != Interop.Widget.ErrorCode.None)
138             {
139                 Log.Error(LogTag, "Failed to run the application. Err = " + err);
140             }
141         }
142
143         private IntPtr OnCreateNative(IntPtr data)
144         {
145             if (_handlers.ContainsKey(EventType.Created))
146             {
147                 var handler = _handlers[EventType.Created] as Action;
148                 handler?.Invoke();
149             }
150
151             IntPtr h = IntPtr.Zero;
152             foreach (WidgetType type in WidgetTypes)
153                 h = type.Bind(h);
154
155             return h;
156         }
157
158         private void OnTerminateNative(IntPtr data)
159         {
160             if (_handlers.ContainsKey(EventType.Terminated))
161             {
162                 var handler = _handlers[EventType.Terminated] as Action;
163                 handler?.Invoke();
164             }
165         }
166
167         public void AddEventHandler(EventType evType, Action handler)
168         {
169             _handlers.Add(evType, handler);
170         }
171
172         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
173         {
174             _handlers.Add(evType, handler);
175         }
176
177         private void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
178         {
179             LowMemoryStatus status = LowMemoryStatus.None;
180             ErrorCode err = Interop.Widget.AppEventGetLowMemoryStatus(infoHandle, out status);
181             if (err != ErrorCode.None)
182             {
183                 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
184             }
185             if (_handlers.ContainsKey(EventType.LowMemory))
186             {
187                 var handler = _handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
188                 handler?.Invoke(new LowMemoryEventArgs(status));
189             }
190         }
191
192         private void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
193         {
194             LowBatteryStatus status = LowBatteryStatus.None;
195             ErrorCode err = Interop.Widget.AppEventGetLowBatteryStatus(infoHandle, out status);
196             if (err != ErrorCode.None)
197             {
198                 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
199             }
200             if (_handlers.ContainsKey(EventType.LowBattery))
201             {
202                 var handler = _handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
203                 handler?.Invoke(new LowBatteryEventArgs(status));
204             }
205         }
206
207         private void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
208         {
209             string lang;
210             ErrorCode err = Interop.Widget.AppEventGetLanguage(infoHandle, out lang);
211             if (err != ErrorCode.None)
212             {
213                 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
214             }
215             if (_handlers.ContainsKey(EventType.LocaleChanged))
216             {
217                 var handler = _handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
218                 handler?.Invoke(new LocaleChangedEventArgs(lang));
219             }
220         }
221
222         private void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
223         {
224             string region;
225             ErrorCode err = Interop.Widget.AppEventGetRegionFormat(infoHandle, out region);
226             if (err != ErrorCode.None)
227             {
228                 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
229             }
230             if (_handlers.ContainsKey(EventType.RegionFormatChanged))
231             {
232                 var handler = _handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
233                 handler?.Invoke(new RegionFormatChangedEventArgs(region));
234             }
235         }
236
237     }
238 }