[Applications.Common] Remove warning messages (#5532)
[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 #pragma warning disable CA1051
80         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
81 #pragma warning restore CA1051
82
83         /// <summary>
84         /// Constructor of DefaultCoreBackend class.
85         /// </summary>
86         /// <since_tizen> 3 </since_tizen>
87         public DefaultCoreBackend()
88         {
89         }
90
91         /// <summary>
92         /// Finalizer of DefaultCoreBackend class.
93         /// </summary>
94         ~DefaultCoreBackend()
95         {
96             Dispose(false);
97         }
98
99         /// <summary>
100         /// Adds an event handler.
101         /// </summary>
102         /// <param name="evType">The type of event.</param>
103         /// <param name="handler">The handler method without arguments.</param>
104         /// <since_tizen> 3 </since_tizen>
105         public virtual void AddEventHandler(EventType evType, Action handler)
106         {
107             Handlers.Add(evType, handler);
108         }
109
110         /// <summary>
111         /// Adds an event handler.
112         /// </summary>
113         /// <typeparam name="TEventArgs">The EventArgs type used in arguments of the handler method.</typeparam>
114         /// <param name="evType">The type of event.</param>
115         /// <param name="handler">The handler method with a TEventArgs type argument.</param>
116         /// <since_tizen> 3 </since_tizen>
117         public virtual void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
118         {
119             Handlers.Add(evType, handler);
120         }
121
122         /// <summary>
123         /// Runs the mainloop of the backend.
124         /// </summary>
125         /// <param name="args"></param>
126         /// <since_tizen> 3 </since_tizen>
127         public virtual void Run(string[] args)
128         {
129             TizenSynchronizationContext.Initialize();
130         }
131
132         /// <summary>
133         /// Exits the mainloop of the backend.
134         /// </summary>
135         /// <since_tizen> 3 </since_tizen>
136 #pragma warning disable CA1716
137         public abstract void Exit();
138 #pragma warning restore CA1716
139
140         /// <summary>
141         /// Releases all resources.
142         /// </summary>
143         /// <since_tizen> 3 </since_tizen>
144         public void Dispose()
145         {
146             Dispose(true);
147             GC.SuppressFinalize(this);
148         }
149
150         /// <summary>
151         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
152         /// </summary>
153         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
154         /// <since_tizen> 3 </since_tizen>
155         protected abstract void Dispose(bool disposing);
156
157         /// <summary>
158         /// Default implementation for the low memory event.
159         /// </summary>
160         /// <param name="infoHandle"></param>
161         /// <param name="data"></param>
162         /// <since_tizen> 3 </since_tizen>
163         [EditorBrowsable(EditorBrowsableState.Never)]
164         protected virtual void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
165         {
166             LowMemoryStatus status = LowMemoryStatus.None;
167             ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status);
168             if (err != ErrorCode.None)
169             {
170                 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
171             }
172             if (Handlers.ContainsKey(EventType.LowMemory))
173             {
174                 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
175                 handler?.Invoke(new LowMemoryEventArgs(status));
176             }
177         }
178
179         /// <summary>
180         /// Default implementation for the low battery event.
181         /// </summary>
182         /// <param name="infoHandle"></param>
183         /// <param name="data"></param>
184         /// <since_tizen> 3 </since_tizen>
185         [EditorBrowsable(EditorBrowsableState.Never)]
186         protected virtual void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
187         {
188             LowBatteryStatus status = LowBatteryStatus.None;
189             ErrorCode err = Interop.AppCommon.AppEventGetLowBatteryStatus(infoHandle, out status);
190             if (err != ErrorCode.None)
191             {
192                 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
193             }
194             if (Handlers.ContainsKey(EventType.LowBattery))
195             {
196                 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
197                 handler?.Invoke(new LowBatteryEventArgs(status));
198             }
199         }
200
201         /// <summary>
202         /// Default implementation for the system language changed event.
203         /// </summary>
204         /// <param name="infoHandle"></param>
205         /// <param name="data"></param>
206         /// <since_tizen> 3 </since_tizen>
207         [EditorBrowsable(EditorBrowsableState.Never)]
208         protected virtual void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
209         {
210             string lang;
211             ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang);
212             if (err != ErrorCode.None)
213             {
214                 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
215             }
216             if (Handlers.ContainsKey(EventType.LocaleChanged))
217             {
218                 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
219                 handler?.Invoke(new LocaleChangedEventArgs(lang));
220             }
221         }
222
223         /// <summary>
224         /// Default implementation for the region format changed event.
225         /// </summary>
226         /// <param name="infoHandle"></param>
227         /// <param name="data"></param>
228         /// <since_tizen> 3 </since_tizen>
229         [EditorBrowsable(EditorBrowsableState.Never)]
230         protected virtual void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
231         {
232             string region;
233             ErrorCode err = Interop.AppCommon.AppEventGetRegionFormat(infoHandle, out region);
234             if (err != ErrorCode.None)
235             {
236                 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
237             }
238             if (Handlers.ContainsKey(EventType.RegionFormatChanged))
239             {
240                 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
241                 handler?.Invoke(new RegionFormatChangedEventArgs(region));
242             }
243         }
244
245         /// <summary>
246         /// Default implementation for the device orientation changed event.
247         /// </summary>
248         /// <param name="infoHandle"></param>
249         /// <param name="data"></param>
250         /// <since_tizen> 3 </since_tizen>
251         [EditorBrowsable(EditorBrowsableState.Never)]
252         protected virtual void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
253         {
254             DeviceOrientation orientation;
255             ErrorCode err = Interop.AppCommon.AppEventGetDeviceOrientation(infoHandle, out orientation);
256             if (err != ErrorCode.None)
257             {
258                 Log.Error(LogTag, "Failed to get device orientation. Err = " + err);
259             }
260             if (Handlers.ContainsKey(EventType.DeviceOrientationChanged))
261             {
262                 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
263                 handler?.Invoke(new DeviceOrientationEventArgs(orientation));
264             }
265         }
266
267         /// <summary>
268         /// Default implementation for the device orientation changed event.
269         /// </summary>
270         /// <param name="infoHandle"></param>
271         /// <param name="data"></param>
272         /// <since_tizen> 6 </since_tizen>
273         [EditorBrowsable(EditorBrowsableState.Never)]
274         protected virtual void OnSuspendedStateChangedNative(IntPtr infoHandle, IntPtr data)
275         {
276             SuspendedState state;
277             ErrorCode err = Interop.AppCommon.AppEventGetSuspendedState(infoHandle, out state);
278             if (err != ErrorCode.None)
279             {
280                 Log.Error(LogTag, "Failed to get device orientation. Err = " + err);
281             }
282             if (Handlers.ContainsKey(EventType.SuspendedStateChanged))
283             {
284                 var handler = Handlers[EventType.SuspendedStateChanged] as Action<SuspendedStateEventArgs>;
285                 handler?.Invoke(new SuspendedStateEventArgs(state));
286             }
287         }
288     }
289 }