Merge remote-tracking branch 'origin/API9' into tizen_6.5
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Common / Tizen.Applications.CoreBackend / DefaultCoreBackend.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 using System.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.Internals.Errors;
21
22 namespace Tizen.Applications.CoreBackend
23 {
24     /// <summary>
25     /// An abstract class to provide default event handlers for apps.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public abstract class DefaultCoreBackend : ICoreBackend
29     {
30         /// <summary>
31         /// Low level event types.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         public enum AppEventType
35         {
36             /// <summary>
37             /// The low memory event.
38             /// </summary>
39             LowMemory = 0,
40
41             /// <summary>
42             /// The low battery event.
43             /// </summary>
44             LowBattery,
45
46             /// <summary>
47             /// The system language changed event.
48             /// </summary>
49             LanguageChanged,
50
51             /// <summary>
52             /// The device orientation changed event.
53             /// </summary>
54             DeviceOrientationChanged,
55
56             /// <summary>
57             /// The region format changed event.
58             /// </summary>
59             RegionFormatChanged,
60
61             /// <summary>
62             /// The suspended state changed event of the application.
63             /// </summary>
64             SuspendedStateChanged
65         }
66
67         /// <summary>
68         /// Tag string for this class.
69         /// </summary>
70         /// <since_tizen> 3 </since_tizen>
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         protected static readonly string LogTag = typeof(DefaultCoreBackend).Namespace;
73
74         /// <summary>
75         /// Data structure for event handlers.
76         /// </summary>
77         /// <since_tizen> 3 </since_tizen>
78         [EditorBrowsable(EditorBrowsableState.Never)]
79         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
80
81         /// <summary>
82         /// Constructor of DefaultCoreBackend class.
83         /// </summary>
84         /// <since_tizen> 3 </since_tizen>
85         public DefaultCoreBackend()
86         {
87         }
88
89         /// <summary>
90         /// Finalizer of DefaultCoreBackend class.
91         /// </summary>
92         ~DefaultCoreBackend()
93         {
94             Dispose(false);
95         }
96
97         /// <summary>
98         /// Adds an event handler.
99         /// </summary>
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)
104         {
105             Handlers.Add(evType, handler);
106         }
107
108         /// <summary>
109         /// Adds an event handler.
110         /// </summary>
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
116         {
117             Handlers.Add(evType, handler);
118         }
119
120         /// <summary>
121         /// Runs the mainloop of the backend.
122         /// </summary>
123         /// <param name="args"></param>
124         /// <since_tizen> 3 </since_tizen>
125         public virtual void Run(string[] args)
126         {
127             TizenSynchronizationContext.Initialize();
128         }
129
130         /// <summary>
131         /// Exits the mainloop of the backend.
132         /// </summary>
133         /// <since_tizen> 3 </since_tizen>
134         public abstract void Exit();
135
136         /// <summary>
137         /// Releases all resources.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140         public void Dispose()
141         {
142             Dispose(true);
143             GC.SuppressFinalize(this);
144         }
145
146         /// <summary>
147         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
148         /// </summary>
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);
152
153         /// <summary>
154         /// Default implementation for the low memory event.
155         /// </summary>
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)
161         {
162             LowMemoryStatus status = LowMemoryStatus.None;
163             ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status);
164             if (err != ErrorCode.None)
165             {
166                 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
167             }
168             if (Handlers.ContainsKey(EventType.LowMemory))
169             {
170                 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
171                 handler?.Invoke(new LowMemoryEventArgs(status));
172             }
173         }
174
175         /// <summary>
176         /// Default implementation for the low battery event.
177         /// </summary>
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)
183         {
184             LowBatteryStatus status = LowBatteryStatus.None;
185             ErrorCode err = Interop.AppCommon.AppEventGetLowBatteryStatus(infoHandle, out status);
186             if (err != ErrorCode.None)
187             {
188                 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
189             }
190             if (Handlers.ContainsKey(EventType.LowBattery))
191             {
192                 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
193                 handler?.Invoke(new LowBatteryEventArgs(status));
194             }
195         }
196
197         /// <summary>
198         /// Default implementation for the system language changed event.
199         /// </summary>
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)
205         {
206             string lang;
207             ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang);
208             if (err != ErrorCode.None)
209             {
210                 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
211             }
212             if (Handlers.ContainsKey(EventType.LocaleChanged))
213             {
214                 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
215                 handler?.Invoke(new LocaleChangedEventArgs(lang));
216             }
217         }
218
219         /// <summary>
220         /// Default implementation for the region format changed event.
221         /// </summary>
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)
227         {
228             string region;
229             ErrorCode err = Interop.AppCommon.AppEventGetRegionFormat(infoHandle, out region);
230             if (err != ErrorCode.None)
231             {
232                 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
233             }
234             if (Handlers.ContainsKey(EventType.RegionFormatChanged))
235             {
236                 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
237                 handler?.Invoke(new RegionFormatChangedEventArgs(region));
238             }
239         }
240
241         /// <summary>
242         /// Default implementation for the device orientation changed event.
243         /// </summary>
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)
249         {
250             DeviceOrientation orientation;
251             ErrorCode err = Interop.AppCommon.AppEventGetDeviceOrientation(infoHandle, out orientation);
252             if (err != ErrorCode.None)
253             {
254                 Log.Error(LogTag, "Failed to get device orientation. Err = " + err);
255             }
256             if (Handlers.ContainsKey(EventType.DeviceOrientationChanged))
257             {
258                 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
259                 handler?.Invoke(new DeviceOrientationEventArgs(orientation));
260             }
261         }
262
263         /// <summary>
264         /// Default implementation for the device orientation changed event.
265         /// </summary>
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)
271         {
272             SuspendedState state;
273             ErrorCode err = Interop.AppCommon.AppEventGetSuspendedState(infoHandle, out state);
274             if (err != ErrorCode.None)
275             {
276                 Log.Error(LogTag, "Failed to get device orientation. Err = " + err);
277             }
278             if (Handlers.ContainsKey(EventType.SuspendedStateChanged))
279             {
280                 var handler = Handlers[EventType.SuspendedStateChanged] as Action<SuspendedStateEventArgs>;
281                 handler?.Invoke(new SuspendedStateEventArgs(state));
282             }
283         }
284     }
285 }