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.
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.Internals.Errors;
22 namespace Tizen.Applications.CoreBackend
25 /// An abstract class to provide default event handlers for apps.
27 /// <since_tizen> 3 </since_tizen>
28 public abstract class DefaultCoreBackend : ICoreBackend
31 /// Low level event types.
33 /// <since_tizen> 3 </since_tizen>
34 public enum AppEventType
37 /// The low memory event.
42 /// The low battery event.
47 /// The system language changed event.
52 /// The device orientation changed event.
54 DeviceOrientationChanged,
57 /// The region format changed event.
62 /// The suspended state changed event of the application.
68 /// Tag string for this class.
70 /// <since_tizen> 3 </since_tizen>
71 [EditorBrowsable(EditorBrowsableState.Never)]
72 protected static readonly string LogTag = typeof(DefaultCoreBackend).Namespace;
75 /// Data structure for event handlers.
77 /// <since_tizen> 3 </since_tizen>
78 [EditorBrowsable(EditorBrowsableState.Never)]
79 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
82 /// Constructor of DefaultCoreBackend class.
84 /// <since_tizen> 3 </since_tizen>
85 public DefaultCoreBackend()
90 /// Finalizer of DefaultCoreBackend class.
98 /// Adds an event handler.
100 /// <param name="evType">The type of event.</param>
101 /// <param name="handler">The handler method without arguments.</param>
102 /// <since_tizen> 3 </since_tizen>
103 public virtual void AddEventHandler(EventType evType, Action handler)
105 Handlers.Add(evType, handler);
109 /// Adds an event handler.
111 /// <typeparam name="TEventArgs">The EventArgs type used in arguments of the handler method.</typeparam>
112 /// <param name="evType">The type of event.</param>
113 /// <param name="handler">The handler method with a TEventArgs type argument.</param>
114 /// <since_tizen> 3 </since_tizen>
115 public virtual void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
117 Handlers.Add(evType, handler);
121 /// Runs the mainloop of the backend.
123 /// <param name="args"></param>
124 /// <since_tizen> 3 </since_tizen>
125 public virtual void Run(string[] args)
127 TizenSynchronizationContext.Initialize();
131 /// Exits the mainloop of the backend.
133 /// <since_tizen> 3 </since_tizen>
134 public abstract void Exit();
137 /// Releases all resources.
139 /// <since_tizen> 3 </since_tizen>
140 public void Dispose()
143 GC.SuppressFinalize(this);
147 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
149 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
150 /// <since_tizen> 3 </since_tizen>
151 protected abstract void Dispose(bool disposing);
154 /// Default implementation for the low memory event.
156 /// <param name="infoHandle"></param>
157 /// <param name="data"></param>
158 /// <since_tizen> 3 </since_tizen>
159 [EditorBrowsable(EditorBrowsableState.Never)]
160 protected virtual void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
162 LowMemoryStatus status = LowMemoryStatus.None;
163 ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status);
164 if (err != ErrorCode.None)
166 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
168 if (Handlers.ContainsKey(EventType.LowMemory))
170 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
171 handler?.Invoke(new LowMemoryEventArgs(status));
176 /// Default implementation for the low battery event.
178 /// <param name="infoHandle"></param>
179 /// <param name="data"></param>
180 /// <since_tizen> 3 </since_tizen>
181 [EditorBrowsable(EditorBrowsableState.Never)]
182 protected virtual void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
184 LowBatteryStatus status = LowBatteryStatus.None;
185 ErrorCode err = Interop.AppCommon.AppEventGetLowBatteryStatus(infoHandle, out status);
186 if (err != ErrorCode.None)
188 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
190 if (Handlers.ContainsKey(EventType.LowBattery))
192 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
193 handler?.Invoke(new LowBatteryEventArgs(status));
198 /// Default implementation for the system language changed event.
200 /// <param name="infoHandle"></param>
201 /// <param name="data"></param>
202 /// <since_tizen> 3 </since_tizen>
203 [EditorBrowsable(EditorBrowsableState.Never)]
204 protected virtual void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
207 ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang);
208 if (err != ErrorCode.None)
210 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
212 if (Handlers.ContainsKey(EventType.LocaleChanged))
214 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
215 handler?.Invoke(new LocaleChangedEventArgs(lang));
220 /// Default implementation for the region format changed event.
222 /// <param name="infoHandle"></param>
223 /// <param name="data"></param>
224 /// <since_tizen> 3 </since_tizen>
225 [EditorBrowsable(EditorBrowsableState.Never)]
226 protected virtual void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
229 ErrorCode err = Interop.AppCommon.AppEventGetRegionFormat(infoHandle, out region);
230 if (err != ErrorCode.None)
232 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
234 if (Handlers.ContainsKey(EventType.RegionFormatChanged))
236 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
237 handler?.Invoke(new RegionFormatChangedEventArgs(region));
242 /// Default implementation for the device orientation changed event.
244 /// <param name="infoHandle"></param>
245 /// <param name="data"></param>
246 /// <since_tizen> 3 </since_tizen>
247 [EditorBrowsable(EditorBrowsableState.Never)]
248 protected virtual void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
250 DeviceOrientation orientation;
251 ErrorCode err = Interop.AppCommon.AppEventGetDeviceOrientation(infoHandle, out orientation);
252 if (err != ErrorCode.None)
254 Log.Error(LogTag, "Failed to get device orientation. Err = " + err);
256 if (Handlers.ContainsKey(EventType.DeviceOrientationChanged))
258 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
259 handler?.Invoke(new DeviceOrientationEventArgs(orientation));
264 /// Default implementation for the device orientation changed event.
266 /// <param name="infoHandle"></param>
267 /// <param name="data"></param>
268 /// <since_tizen> 6 </since_tizen>
269 [EditorBrowsable(EditorBrowsableState.Never)]
270 protected virtual void OnSuspendedStateChangedNative(IntPtr infoHandle, IntPtr data)
272 SuspendedState state;
273 ErrorCode err = Interop.AppCommon.AppEventGetSuspendedState(infoHandle, out state);
274 if (err != ErrorCode.None)
276 Log.Error(LogTag, "Failed to get device orientation. Err = " + err);
278 if (Handlers.ContainsKey(EventType.SuspendedStateChanged))
280 var handler = Handlers[EventType.SuspendedStateChanged] as Action<SuspendedStateEventArgs>;
281 handler?.Invoke(new SuspendedStateEventArgs(state));