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;
20 using Tizen.Internals.Errors;
22 namespace Tizen.Applications.CoreBackend
25 /// Abstract class to provide default event handlers for apps.
27 public abstract class DefaultCoreBackend : ICoreBackend
30 /// Low level event types
32 public enum AppEventType
37 DeviceOrientationChanged,
42 protected static readonly string LogTag = typeof(DefaultCoreBackend).Namespace;
44 protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
46 public DefaultCoreBackend()
55 public void AddEventHandler(EventType evType, Action handler)
57 Handlers.Add(evType, handler);
60 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
62 Handlers.Add(evType, handler);
65 public virtual void Run(string[] args)
67 TizenSynchronizationContext.Initialize();
70 public abstract void Exit();
75 GC.SuppressFinalize(this);
78 protected abstract void Dispose(bool disposing);
80 protected virtual void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
82 LowMemoryStatus status = LowMemoryStatus.None;
83 ErrorCode err = Interop.AppCommon.AppEventGetLowMemoryStatus(infoHandle, out status);
84 if (err != ErrorCode.None)
86 Log.Error(LogTag, "Failed to get memory status. Err = " + err);
88 if (Handlers.ContainsKey(EventType.LowMemory))
90 var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
91 handler?.Invoke(new LowMemoryEventArgs(status));
95 protected virtual void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
97 LowBatteryStatus status = LowBatteryStatus.None;
98 ErrorCode err = Interop.AppCommon.AppEventGetLowBatteryStatus(infoHandle, out status);
99 if (err != ErrorCode.None)
101 Log.Error(LogTag, "Failed to get battery status. Err = " + err);
103 if (Handlers.ContainsKey(EventType.LowBattery))
105 var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
106 handler?.Invoke(new LowBatteryEventArgs(status));
110 protected virtual void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
113 ErrorCode err = Interop.AppCommon.AppEventGetLanguage(infoHandle, out lang);
114 if (err != ErrorCode.None)
116 Log.Error(LogTag, "Failed to get changed language. Err = " + err);
118 if (Handlers.ContainsKey(EventType.LocaleChanged))
120 var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
121 handler?.Invoke(new LocaleChangedEventArgs(lang));
125 protected virtual void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
128 ErrorCode err = Interop.AppCommon.AppEventGetRegionFormat(infoHandle, out region);
129 if (err != ErrorCode.None)
131 Log.Error(LogTag, "Failed to get changed region format. Err = " + err);
133 if (Handlers.ContainsKey(EventType.RegionFormatChanged))
135 var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
136 handler?.Invoke(new RegionFormatChangedEventArgs(region));
140 protected virtual void OnDeviceOrientationChangedNative(IntPtr infoHandle, IntPtr data)
142 DeviceOrientation orientation;
143 ErrorCode err = Interop.AppCommon.AppEventGetDeviceOrientation(infoHandle, out orientation);
144 if (err != ErrorCode.None)
146 Log.Error(LogTag, "Failed to get deivce orientation. Err = " + err);
148 if (Handlers.ContainsKey(EventType.DeviceOrientationChanged))
150 var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
151 handler?.Invoke(new DeviceOrientationEventArgs(orientation));