Release 4.0.0-preview1-00051
[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
20 using Tizen.Internals.Errors;
21
22 namespace Tizen.Applications.CoreBackend
23 {
24     /// <summary>
25     /// 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             LowMemory = 0,
35             LowBattery,
36             LanguageChanged,
37             DeviceOrientationChanged,
38             RegionFormatChanged,
39             SuspendedStateChanged
40         }
41
42         protected static readonly string LogTag = typeof(DefaultCoreBackend).Namespace;
43
44         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
45
46         public DefaultCoreBackend()
47         {
48         }
49
50         ~DefaultCoreBackend()
51         {
52             Dispose(false);
53         }
54
55         public void AddEventHandler(EventType evType, Action handler)
56         {
57             Handlers.Add(evType, handler);
58         }
59
60         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
61         {
62             Handlers.Add(evType, handler);
63         }
64
65         public virtual void Run(string[] args)
66         {
67             TizenSynchronizationContext.Initialize();
68         }
69
70         public abstract void Exit();
71
72         public void Dispose()
73         {
74             Dispose(true);
75             GC.SuppressFinalize(this);
76         }
77
78         protected abstract void Dispose(bool disposing);
79
80         protected virtual void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
81         {
82             LowMemoryStatus status = LowMemoryStatus.None;
83             ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status);
84             if (err != ErrorCode.None)
85             {
86                 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
87             }
88             if (Handlers.ContainsKey(EventType.LowMemory))
89             {
90                 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
91                 handler?.Invoke(new LowMemoryEventArgs(status));
92             }
93         }
94
95         protected virtual void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
96         {
97             LowBatteryStatus status = LowBatteryStatus.None;
98             ErrorCode err = Interop.AppCommon.AppEventGetLowBatteryStatus(infoHandle, out status);
99             if (err != ErrorCode.None)
100             {
101                 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
102             }
103             if (Handlers.ContainsKey(EventType.LowBattery))
104             {
105                 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
106                 handler?.Invoke(new LowBatteryEventArgs(status));
107             }
108         }
109
110         protected virtual void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
111         {
112             string lang;
113             ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang);
114             if (err != ErrorCode.None)
115             {
116                 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
117             }
118             if (Handlers.ContainsKey(EventType.LocaleChanged))
119             {
120                 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
121                 handler?.Invoke(new LocaleChangedEventArgs(lang));
122             }
123         }
124
125         protected virtual void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
126         {
127             string region;
128             ErrorCode err = Interop.AppCommon.AppEventGetRegionFormat(infoHandle, out region);
129             if (err != ErrorCode.None)
130             {
131                 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
132             }
133             if (Handlers.ContainsKey(EventType.RegionFormatChanged))
134             {
135                 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
136                 handler?.Invoke(new RegionFormatChangedEventArgs(region));
137             }
138         }
139
140         protected virtual void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
141         {
142             DeviceOrientation orientation;
143             ErrorCode err = Interop.AppCommon.AppEventGetDeviceOrientation(infoHandle, out orientation);
144             if (err != ErrorCode.None)
145             {
146                 Log.Error(LogTag, "Failed to get deivce orientation. Err = " + err);
147             }
148             if (Handlers.ContainsKey(EventType.DeviceOrientationChanged))
149             {
150                 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
151                 handler?.Invoke(new DeviceOrientationEventArgs(orientation));
152             }
153         }
154     }
155 }