Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.UI / Tizen.Applications.CoreBackend / UICoreBackend.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
19 using Tizen.Internals.Errors;
20
21 namespace Tizen.Applications.CoreBackend
22 {
23     internal class UICoreBackend : DefaultCoreBackend
24     {
25         private Interop.Application.UIAppLifecycleCallbacks _callbacks;
26         private IntPtr _lowMemoryEventHandle = IntPtr.Zero;
27         private IntPtr _lowBatteryEventHandle = IntPtr.Zero;
28         private IntPtr _localeChangedEventHandle = IntPtr.Zero;
29         private IntPtr _regionChangedEventHandle = IntPtr.Zero;
30         private IntPtr _deviceOrientationChangedEventHandle = IntPtr.Zero;
31         private bool _disposedValue = false;
32         private Interop.Application.AppEventCallback _onLowMemoryNative;
33         private Interop.Application.AppEventCallback _onLowBatteryNative;
34         private Interop.Application.AppEventCallback _onLocaleChangedNative;
35         private Interop.Application.AppEventCallback _onRegionChangedNative;
36         private Interop.Application.AppEventCallback _onDeviceOrientationChangedNative;
37
38         public UICoreBackend()
39         {
40             _callbacks.OnCreate = new Interop.Application.AppCreateCallback(OnCreateNative);
41             _callbacks.OnTerminate = new Interop.Application.AppTerminateCallback(OnTerminateNative);
42             _callbacks.OnAppControl = new Interop.Application.AppControlCallback(OnAppControlNative);
43             _callbacks.OnResume = new Interop.Application.AppResumeCallback(OnResumeNative);
44             _callbacks.OnPause = new Interop.Application.AppPauseCallback(OnPauseNative);
45
46             _onLowMemoryNative = new Interop.Application.AppEventCallback(OnLowMemoryNative);
47             _onLowBatteryNative = new Interop.Application.AppEventCallback(OnLowBatteryNative);
48             _onLocaleChangedNative = new Interop.Application.AppEventCallback(OnLocaleChangedNative);
49             _onRegionChangedNative = new Interop.Application.AppEventCallback(OnRegionChangedNative);
50             _onDeviceOrientationChangedNative = new Interop.Application.AppEventCallback(OnDeviceOrientationChangedNative);
51         }
52
53         public override void Exit()
54         {
55             Interop.Application.Exit();
56         }
57
58         public override void Run(string[] args)
59         {
60             base.Run(args);
61
62             ErrorCode err = ErrorCode.None;
63             err = Interop.Application.AddEventHandler(out _lowMemoryEventHandle, AppEventType.LowMemory, _onLowMemoryNative, IntPtr.Zero);
64             if (err != ErrorCode.None)
65             {
66                 Log.Error(LogTag, "Failed to add event handler for LowMemory event. Err = " + err);
67             }
68             err = Interop.Application.AddEventHandler(out _lowBatteryEventHandle, AppEventType.LowBattery, _onLowBatteryNative, IntPtr.Zero);
69             if (err != ErrorCode.None)
70             {
71                 Log.Error(LogTag, "Failed to add event handler for LowBattery event. Err = " + err);
72             }
73
74             err = Interop.Application.AddEventHandler(out _localeChangedEventHandle, AppEventType.LanguageChanged, _onLocaleChangedNative, IntPtr.Zero);
75             if (err != ErrorCode.None)
76             {
77                 Log.Error(LogTag, "Failed to add event handler for LocaleChanged event. Err = " + err);
78             }
79
80             err = Interop.Application.AddEventHandler(out _regionChangedEventHandle, AppEventType.RegionFormatChanged, _onRegionChangedNative, IntPtr.Zero);
81             if (err != ErrorCode.None)
82             {
83                 Log.Error(LogTag, "Failed to add event handler for RegionFormatChanged event. Err = " + err);
84             }
85
86             err = Interop.Application.AddEventHandler(out _deviceOrientationChangedEventHandle, AppEventType.DeviceOrientationChanged, _onDeviceOrientationChangedNative, IntPtr.Zero);
87             if (err != ErrorCode.None)
88             {
89                 Log.Error(LogTag, "Failed to add event handler for DeviceOrientationChanged event. Err = " + err);
90             }
91
92             err = Interop.Application.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
93             if (err != ErrorCode.None)
94             {
95                 Log.Error(LogTag, "Failed to run the application. Err = " + err);
96             }
97         }
98
99         protected override void Dispose(bool disposing)
100         {
101             if (!_disposedValue)
102             {
103                 if (disposing)
104                 {
105                     // Release disposable objects
106                 }
107
108                 if (_lowMemoryEventHandle != IntPtr.Zero)
109                 {
110                     Interop.Application.RemoveEventHandler(_lowMemoryEventHandle);
111                 }
112                 if (_lowBatteryEventHandle != IntPtr.Zero)
113                 {
114                     Interop.Application.RemoveEventHandler(_lowBatteryEventHandle);
115                 }
116                 if (_localeChangedEventHandle != IntPtr.Zero)
117                 {
118                     Interop.Application.RemoveEventHandler(_localeChangedEventHandle);
119                 }
120                 if (_regionChangedEventHandle != IntPtr.Zero)
121                 {
122                     Interop.Application.RemoveEventHandler(_regionChangedEventHandle);
123                 }
124                 if (_deviceOrientationChangedEventHandle != IntPtr.Zero)
125                 {
126                     Interop.Application.RemoveEventHandler(_deviceOrientationChangedEventHandle);
127                 }
128
129                 _disposedValue = true;
130             }
131         }
132
133         private bool OnCreateNative(IntPtr data)
134         {
135             if (Handlers.ContainsKey(EventType.PreCreated))
136             {
137                 var handler = Handlers[EventType.PreCreated] as Action;
138                 handler?.Invoke();
139             }
140
141             if (Handlers.ContainsKey(EventType.Created))
142             {
143                 var handler = Handlers[EventType.Created] as Action;
144                 handler?.Invoke();
145             }
146             return true;
147         }
148
149         private void OnTerminateNative(IntPtr data)
150         {
151             if (Handlers.ContainsKey(EventType.Terminated))
152             {
153                 var handler = Handlers[EventType.Terminated] as Action;
154                 handler?.Invoke();
155             }
156         }
157
158         private void OnAppControlNative(IntPtr appControlHandle, IntPtr data)
159         {
160             if (Handlers.ContainsKey(EventType.AppControlReceived))
161             {
162                 // Create a SafeAppControlHandle but the ownsHandle is false,
163                 // because the appControlHandle will be closed by native appfw after this method automatically.
164                 SafeAppControlHandle safeHandle = new SafeAppControlHandle(appControlHandle, false);
165
166                 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
167                 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(safeHandle)));
168             }
169         }
170
171         private void OnResumeNative(IntPtr data)
172         {
173             if (Handlers.ContainsKey(EventType.Resumed))
174             {
175                 var handler = Handlers[EventType.Resumed] as Action;
176                 handler?.Invoke();
177             }
178         }
179
180         private void OnPauseNative(IntPtr data)
181         {
182             if (Handlers.ContainsKey(EventType.Paused))
183             {
184                 var handler = Handlers[EventType.Paused] as Action;
185                 handler?.Invoke();
186             }
187         }
188
189         protected override void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
190         {
191             base.OnLowMemoryNative(infoHandle, data);
192         }
193
194         protected override void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
195         {
196             base.OnLowBatteryNative(infoHandle, data);
197         }
198
199         protected override void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
200         {
201             base.OnLocaleChangedNative(infoHandle, data);
202         }
203
204         protected override void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
205         {
206             base.OnRegionChangedNative(infoHandle, data);
207         }
208
209         protected override void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
210         {
211             base.OnDeviceOrientationChangedNative(infoHandle, data);
212         }
213     }
214 }