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