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 public abstract class DefaultCoreBackend : ICoreBackend
30 /// Low level event types.
32 public enum AppEventType
35 /// The low memory event.
40 /// The low battery event.
45 /// The system language changed event.
50 /// The device orientation changed event.
52 DeviceOrientationChanged,
55 /// The region format changed event.
60 /// The suspended state changed event of the application.
66 /// Tag string for this class.
68 [EditorBrowsable(EditorBrowsableState.Never)]
69 protected static readonly string LogTag = typeof(DefaultCoreBackend).Namespace;
72 /// Data structure for event handlers.
74 [EditorBrowsable(EditorBrowsableState.Never)]
75 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
78 /// Constructor of DefaultCoreBackend class.
80 public DefaultCoreBackend()
85 /// Finalizer of DefaultCoreBackend class.
93 /// Adds an event handler.
95 /// <param name="evType">The type of event.</param>
96 /// <param name="handler">The handler method without arguments.</param>
97 public virtual void AddEventHandler(EventType evType, Action handler)
99 Handlers.Add(evType, handler);
103 /// Adds an event handler.
105 /// <typeparam name="TEventArgs">The EventArgs type used in arguments of the handler method.</typeparam>
106 /// <param name="evType">The type of event.</param>
107 /// <param name="handler">The handler method with a TEventArgs type argument.</param>
108 public virtual void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
110 Handlers.Add(evType, handler);
114 /// Runs the mainloop of the backend.
116 /// <param name="args"></param>
117 public virtual void Run(string[] args)
119 TizenSynchronizationContext.Initialize();
123 /// Exits the mainloop of the backend.
125 public abstract void Exit();
128 /// Releases all resources.
130 public void Dispose()
133 GC.SuppressFinalize(this);
137 /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
139 /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
140 protected abstract void Dispose(bool disposing);
143 /// Default implementation for the low memory event.
145 /// <param name="infoHandle"></param>
146 /// <param name="data"></param>
147 [EditorBrowsable(EditorBrowsableState.Never)]
148 protected virtual void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
150 LowMemoryStatus status = LowMemoryStatus.None;
151 ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status);
152 if (err != ErrorCode.None)
154 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
156 if (Handlers.ContainsKey(EventType.LowMemory))
158 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
159 handler?.Invoke(new LowMemoryEventArgs(status));
164 /// Default implementation for the low battery event.
166 /// <param name="infoHandle"></param>
167 /// <param name="data"></param>
168 [EditorBrowsable(EditorBrowsableState.Never)]
169 protected virtual void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
171 LowBatteryStatus status = LowBatteryStatus.None;
172 ErrorCode err = Interop.AppCommon.AppEventGetLowBatteryStatus(infoHandle, out status);
173 if (err != ErrorCode.None)
175 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
177 if (Handlers.ContainsKey(EventType.LowBattery))
179 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
180 handler?.Invoke(new LowBatteryEventArgs(status));
185 /// Default implementation for the system language changed event.
187 /// <param name="infoHandle"></param>
188 /// <param name="data"></param>
189 [EditorBrowsable(EditorBrowsableState.Never)]
190 protected virtual void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
193 ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang);
194 if (err != ErrorCode.None)
196 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
198 if (Handlers.ContainsKey(EventType.LocaleChanged))
200 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
201 handler?.Invoke(new LocaleChangedEventArgs(lang));
206 /// Default implementation for the region format changed event.
208 /// <param name="infoHandle"></param>
209 /// <param name="data"></param>
210 [EditorBrowsable(EditorBrowsableState.Never)]
211 protected virtual void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
214 ErrorCode err = Interop.AppCommon.AppEventGetRegionFormat(infoHandle, out region);
215 if (err != ErrorCode.None)
217 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
219 if (Handlers.ContainsKey(EventType.RegionFormatChanged))
221 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
222 handler?.Invoke(new RegionFormatChangedEventArgs(region));
227 /// Default implementation for the suspended state changed event.
229 /// <param name="infoHandle"></param>
230 /// <param name="data"></param>
231 [EditorBrowsable(EditorBrowsableState.Never)]
232 protected virtual void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
234 DeviceOrientation orientation;
235 ErrorCode err = Interop.AppCommon.AppEventGetDeviceOrientation(infoHandle, out orientation);
236 if (err != ErrorCode.None)
238 Log.Error(LogTag, "Failed to get deivce orientation. Err = " + err);
240 if (Handlers.ContainsKey(EventType.DeviceOrientationChanged))
242 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
243 handler?.Invoke(new DeviceOrientationEventArgs(orientation));