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.
19 using Tizen.Internals.Errors;
21 namespace Tizen.Applications.CoreBackend
23 internal class ServiceCoreBackend : DefaultCoreBackend
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;
38 public ServiceCoreBackend()
40 _callbacks.OnCreate = new Interop.Service.ServiceAppCreateCallback(OnCreateNative);
41 _callbacks.OnTerminate = new Interop.Service.ServiceAppTerminateCallback(OnTerminateNative);
42 _callbacks.OnAppControl = new Interop.Service.ServiceAppControlCallback(OnAppControlNative);
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);
51 public override void Exit()
53 Interop.Service.Exit();
56 public override void Run(string[] args)
60 ErrorCode err = ErrorCode.None;
61 err = Interop.Service.AddEventHandler(out _lowMemoryEventHandle, AppEventType.LowMemory, _onLowMemoryNative, IntPtr.Zero);
62 if (err != ErrorCode.None)
64 Log.Error(LogTag, "Failed to add event handler for LowMemory event. Err = " + err);
66 err = Interop.Service.AddEventHandler(out _lowBatteryEventHandle, AppEventType.LowBattery, _onLowBatteryNative, IntPtr.Zero);
67 if (err != ErrorCode.None)
69 Log.Error(LogTag, "Failed to add event handler for LowBattery event. Err = " + err);
72 err = Interop.Service.AddEventHandler(out _localeChangedEventHandle, AppEventType.LanguageChanged, _onLocaleChangedNative, IntPtr.Zero);
73 if (err != ErrorCode.None)
75 Log.Error(LogTag, "Failed to add event handler for LocaleChanged event. Err = " + err);
78 err = Interop.Service.AddEventHandler(out _regionChangedEventHandle, AppEventType.RegionFormatChanged, _onRegionChangedNative, IntPtr.Zero);
79 if (err != ErrorCode.None)
81 Log.Error(LogTag, "Failed to add event handler for RegionFormatChanged event. Err = " + err);
84 err = Interop.Service.AddEventHandler(out _deviceOrientationChangedEventHandle, AppEventType.DeviceOrientationChanged, _onDeviceOrientationChangedNative, IntPtr.Zero);
85 if (err != ErrorCode.None)
87 Log.Error(LogTag, "Failed to add event handler for DeviceOrientationChanged event. Err = " + err);
90 err = Interop.Service.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
91 if (err != ErrorCode.None)
93 Log.Error(LogTag, "Failed to run the application. Err = " + err);
97 protected override void Dispose(bool disposing)
103 // Release disposable objects
106 if (_lowMemoryEventHandle != IntPtr.Zero)
108 Interop.Service.RemoveEventHandler(_lowMemoryEventHandle);
110 if (_lowBatteryEventHandle != IntPtr.Zero)
112 Interop.Service.RemoveEventHandler(_lowBatteryEventHandle);
114 if (_localeChangedEventHandle != IntPtr.Zero)
116 Interop.Service.RemoveEventHandler(_localeChangedEventHandle);
118 if (_regionChangedEventHandle != IntPtr.Zero)
120 Interop.Service.RemoveEventHandler(_regionChangedEventHandle);
123 if (_deviceOrientationChangedEventHandle != IntPtr.Zero)
125 Interop.Service.RemoveEventHandler(_deviceOrientationChangedEventHandle);
128 _disposedValue = true;
132 private bool OnCreateNative(IntPtr data)
134 if (Handlers.ContainsKey(EventType.Created))
136 var handler = Handlers[EventType.Created] as Action;
142 private void OnTerminateNative(IntPtr data)
144 if (Handlers.ContainsKey(EventType.Terminated))
146 var handler = Handlers[EventType.Terminated] as Action;
151 private void OnAppControlNative(IntPtr appControlHandle, IntPtr data)
153 if (Handlers.ContainsKey(EventType.AppControlReceived))
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);
159 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
160 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(safeHandle)));
164 protected override void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
166 base.OnLowMemoryNative(infoHandle, data);
169 protected override void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
171 base.OnLowBatteryNative(infoHandle, data);
174 protected override void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
176 base.OnLocaleChangedNative(infoHandle, data);
179 protected override void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
181 base.OnRegionChangedNative(infoHandle, data);
184 protected override void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
186 base.OnDeviceOrientationChangedNative(infoHandle, data);