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 UICoreBackend : DefaultCoreBackend
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;
38 public UICoreBackend()
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);
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);
53 public override void Exit()
55 Interop.Application.Exit();
58 public override void Run(string[] args)
62 ErrorCode err = ErrorCode.None;
63 err = Interop.Application.AddEventHandler(out _lowMemoryEventHandle, AppEventType.LowMemory, _onLowMemoryNative, IntPtr.Zero);
64 if (err != ErrorCode.None)
66 Log.Error(LogTag, "Failed to add event handler for LowMemory event. Err = " + err);
68 err = Interop.Application.AddEventHandler(out _lowBatteryEventHandle, AppEventType.LowBattery, _onLowBatteryNative, IntPtr.Zero);
69 if (err != ErrorCode.None)
71 Log.Error(LogTag, "Failed to add event handler for LowBattery event. Err = " + err);
74 err = Interop.Application.AddEventHandler(out _localeChangedEventHandle, AppEventType.LanguageChanged, _onLocaleChangedNative, IntPtr.Zero);
75 if (err != ErrorCode.None)
77 Log.Error(LogTag, "Failed to add event handler for LocaleChanged event. Err = " + err);
80 err = Interop.Application.AddEventHandler(out _regionChangedEventHandle, AppEventType.RegionFormatChanged, _onRegionChangedNative, IntPtr.Zero);
81 if (err != ErrorCode.None)
83 Log.Error(LogTag, "Failed to add event handler for RegionFormatChanged event. Err = " + err);
86 err = Interop.Application.AddEventHandler(out _deviceOrientationChangedEventHandle, AppEventType.DeviceOrientationChanged, _onDeviceOrientationChangedNative, IntPtr.Zero);
87 if (err != ErrorCode.None)
89 Log.Error(LogTag, "Failed to add event handler for DeviceOrientationChanged event. Err = " + err);
92 err = Interop.Application.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
93 if (err != ErrorCode.None)
95 Log.Error(LogTag, "Failed to run the application. Err = " + err);
99 protected override void Dispose(bool disposing)
105 // Release disposable objects
108 if (_lowMemoryEventHandle != IntPtr.Zero)
110 Interop.Application.RemoveEventHandler(_lowMemoryEventHandle);
112 if (_lowBatteryEventHandle != IntPtr.Zero)
114 Interop.Application.RemoveEventHandler(_lowBatteryEventHandle);
116 if (_localeChangedEventHandle != IntPtr.Zero)
118 Interop.Application.RemoveEventHandler(_localeChangedEventHandle);
120 if (_regionChangedEventHandle != IntPtr.Zero)
122 Interop.Application.RemoveEventHandler(_regionChangedEventHandle);
124 if (_deviceOrientationChangedEventHandle != IntPtr.Zero)
126 Interop.Application.RemoveEventHandler(_deviceOrientationChangedEventHandle);
129 _disposedValue = true;
133 private bool OnCreateNative(IntPtr data)
135 if (Handlers.ContainsKey(EventType.PreCreated))
137 var handler = Handlers[EventType.PreCreated] as Action;
141 if (Handlers.ContainsKey(EventType.Created))
143 var handler = Handlers[EventType.Created] as Action;
149 private void OnTerminateNative(IntPtr data)
151 if (Handlers.ContainsKey(EventType.Terminated))
153 var handler = Handlers[EventType.Terminated] as Action;
158 private void OnAppControlNative(IntPtr appControlHandle, IntPtr data)
160 if (Handlers.ContainsKey(EventType.AppControlReceived))
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);
166 var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
167 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(safeHandle)));
171 private void OnResumeNative(IntPtr data)
173 if (Handlers.ContainsKey(EventType.Resumed))
175 var handler = Handlers[EventType.Resumed] as Action;
180 private void OnPauseNative(IntPtr data)
182 if (Handlers.ContainsKey(EventType.Paused))
184 var handler = Handlers[EventType.Paused] as Action;
189 protected override void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
191 base.OnLowMemoryNative(infoHandle, data);
194 protected override void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
196 base.OnLowBatteryNative(infoHandle, data);
199 protected override void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
201 base.OnLocaleChangedNative(infoHandle, data);
204 protected override void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
206 base.OnRegionChangedNative(infoHandle, data);
209 protected override void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
211 base.OnDeviceOrientationChangedNative(infoHandle, data);