Release 4.0.0-preview1-00279
[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     public abstract class DefaultCoreBackend : ICoreBackend
28     {
29         /// <summary>
30         /// Low level event types.
31         /// </summary>
32         public enum AppEventType
33         {
34             /// <summary>
35             /// The low memory event.
36             /// </summary>
37             LowMemory = 0,
38
39             /// <summary>
40             /// The low battery event.
41             /// </summary>
42             LowBattery,
43
44             /// <summary>
45             /// The system language changed event.
46             /// </summary>
47             LanguageChanged,
48
49             /// <summary>
50             /// The device orientation changed event.
51             /// </summary>
52             DeviceOrientationChanged,
53
54             /// <summary>
55             /// The region format changed event.
56             /// </summary>
57             RegionFormatChanged,
58
59             /// <summary>
60             /// The suspended state changed event of the application.
61             /// </summary>
62             SuspendedStateChanged
63         }
64
65         /// <summary>
66         /// Tag string for this class.
67         /// </summary>
68         [EditorBrowsable(EditorBrowsableState.Never)]
69         protected static readonly string LogTag = typeof(DefaultCoreBackend).Namespace;
70
71         /// <summary>
72         /// Data structure for event handlers.
73         /// </summary>
74         [EditorBrowsable(EditorBrowsableState.Never)]
75         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
76
77         /// <summary>
78         /// Constructor of DefaultCoreBackend class.
79         /// </summary>
80         public DefaultCoreBackend()
81         {
82         }
83
84         /// <summary>
85         /// Finalizer of DefaultCoreBackend class.
86         /// </summary>
87         ~DefaultCoreBackend()
88         {
89             Dispose(false);
90         }
91
92         /// <summary>
93         /// Adds an event handler.
94         /// </summary>
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)
98         {
99             Handlers.Add(evType, handler);
100         }
101
102         /// <summary>
103         /// Adds an event handler.
104         /// </summary>
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
109         {
110             Handlers.Add(evType, handler);
111         }
112
113         /// <summary>
114         /// Runs the mainloop of the backend.
115         /// </summary>
116         /// <param name="args"></param>
117         public virtual void Run(string[] args)
118         {
119             TizenSynchronizationContext.Initialize();
120         }
121
122         /// <summary>
123         /// Exits the mainloop of the backend.
124         /// </summary>
125         public abstract void Exit();
126
127         /// <summary>
128         /// Releases all resources.
129         /// </summary>
130         public void Dispose()
131         {
132             Dispose(true);
133             GC.SuppressFinalize(this);
134         }
135
136         /// <summary>
137         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
138         /// </summary>
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);
141
142         /// <summary>
143         /// Default implementation for the low memory event.
144         /// </summary>
145         /// <param name="infoHandle"></param>
146         /// <param name="data"></param>
147         [EditorBrowsable(EditorBrowsableState.Never)]
148         protected virtual void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
149         {
150             LowMemoryStatus status = LowMemoryStatus.None;
151             ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status);
152             if (err != ErrorCode.None)
153             {
154                 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
155             }
156             if (Handlers.ContainsKey(EventType.LowMemory))
157             {
158                 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
159                 handler?.Invoke(new LowMemoryEventArgs(status));
160             }
161         }
162
163         /// <summary>
164         /// Default implementation for the low battery event.
165         /// </summary>
166         /// <param name="infoHandle"></param>
167         /// <param name="data"></param>
168         [EditorBrowsable(EditorBrowsableState.Never)]
169         protected virtual void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
170         {
171             LowBatteryStatus status = LowBatteryStatus.None;
172             ErrorCode err = Interop.AppCommon.AppEventGetLowBatteryStatus(infoHandle, out status);
173             if (err != ErrorCode.None)
174             {
175                 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
176             }
177             if (Handlers.ContainsKey(EventType.LowBattery))
178             {
179                 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
180                 handler?.Invoke(new LowBatteryEventArgs(status));
181             }
182         }
183
184         /// <summary>
185         /// Default implementation for the system language changed event.
186         /// </summary>
187         /// <param name="infoHandle"></param>
188         /// <param name="data"></param>
189         [EditorBrowsable(EditorBrowsableState.Never)]
190         protected virtual void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
191         {
192             string lang;
193             ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang);
194             if (err != ErrorCode.None)
195             {
196                 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
197             }
198             if (Handlers.ContainsKey(EventType.LocaleChanged))
199             {
200                 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
201                 handler?.Invoke(new LocaleChangedEventArgs(lang));
202             }
203         }
204
205         /// <summary>
206         /// Default implementation for the region format changed event.
207         /// </summary>
208         /// <param name="infoHandle"></param>
209         /// <param name="data"></param>
210         [EditorBrowsable(EditorBrowsableState.Never)]
211         protected virtual void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
212         {
213             string region;
214             ErrorCode err = Interop.AppCommon.AppEventGetRegionFormat(infoHandle, out region);
215             if (err != ErrorCode.None)
216             {
217                 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
218             }
219             if (Handlers.ContainsKey(EventType.RegionFormatChanged))
220             {
221                 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
222                 handler?.Invoke(new RegionFormatChangedEventArgs(region));
223             }
224         }
225
226         /// <summary>
227         /// Default implementation for the suspended state changed event.
228         /// </summary>
229         /// <param name="infoHandle"></param>
230         /// <param name="data"></param>
231         [EditorBrowsable(EditorBrowsableState.Never)]
232         protected virtual void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
233         {
234             DeviceOrientation orientation;
235             ErrorCode err = Interop.AppCommon.AppEventGetDeviceOrientation(infoHandle, out orientation);
236             if (err != ErrorCode.None)
237             {
238                 Log.Error(LogTag, "Failed to get deivce orientation. Err = " + err);
239             }
240             if (Handlers.ContainsKey(EventType.DeviceOrientationChanged))
241             {
242                 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
243                 handler?.Invoke(new DeviceOrientationEventArgs(orientation));
244             }
245         }
246     }
247 }