Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.WatchApplication / Tizen.Applications.CoreBackend / WatchCoreBackend.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 Tizen.Internals.Errors;
20
21 namespace Tizen.Applications.CoreBackend
22 {
23     internal class WatchCoreBackend : ICoreBackend
24     {
25         private const string LOGTAG = "Tizen.Applications.WatchApplication";
26
27         private Dictionary<EventType, object> _handlers = new Dictionary<EventType, object>();
28
29         private bool _disposedValue = false;
30
31         private Interop.Watch.AppEventCallback _lowMemoryCallback;
32         private Interop.Watch.AppEventCallback _lowBatteryCallback;
33         private Interop.Watch.AppEventCallback _localeChangedCallback;
34         private Interop.Watch.AppEventCallback _regionChnagedCallback;
35
36         private IntPtr _lowMemoryEventHandle = IntPtr.Zero;
37         private IntPtr _lowBatteryEventHandle = IntPtr.Zero;
38         private IntPtr _localeChangedEventHandle = IntPtr.Zero;
39         private IntPtr _regionChnagedEventHandle = IntPtr.Zero;
40
41         private Interop.Watch.WatchAppLifecycleCallbacks _callbacks;
42
43         public WatchCoreBackend()
44         {
45             _lowMemoryCallback = new Interop.Watch.AppEventCallback(OnLowMemoryNative);
46             _lowBatteryCallback = new Interop.Watch.AppEventCallback(OnLowBatteryNative);
47             _localeChangedCallback = new Interop.Watch.AppEventCallback(OnLocaleChangedNative);
48             _regionChnagedCallback = new Interop.Watch.AppEventCallback(OnRegionChangedNative);
49
50             _callbacks.OnCreate = new Interop.Watch.WatchAppCreateCallback(OnCreateNative);
51             _callbacks.OnTerminate = new Interop.Watch.WatchAppTerminateCallback(OnTerminateNative);
52             _callbacks.OnResume = new Interop.Watch.WatchAppResumeCallback(OnResumeNative);
53             _callbacks.OnPause = new Interop.Watch.WatchAppPauseCallback(OnPauseNative);
54             _callbacks.OnAppControl = new Interop.Watch.WatchAppControlCallback(OnAppControlNative);
55
56             _callbacks.OnTick = new Interop.Watch.WatchAppTimeTickCallback(OnTimeTickNative);
57             _callbacks.OnAmbientTick = new Interop.Watch.WatchAppAmbientTickCallback(OnAmbientTickNative);
58             _callbacks.OnAmbientChanged = new Interop.Watch.WatchAppAmbientChangedCallback(OnAmbientChangedNative);
59         }
60
61         ~WatchCoreBackend()
62         {
63             Dispose(false);
64         }
65
66         public void AddEventHandler(EventType evType, Action handler)
67         {
68             _handlers.Add(evType, handler);
69         }
70
71         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
72         {
73             _handlers.Add(evType, handler);
74         }
75
76         public void Dispose()
77         {
78             Dispose(true);
79             GC.SuppressFinalize(this);
80         }
81
82         protected virtual void Dispose(bool disposing)
83         {
84             if (!_disposedValue)
85             {
86                 if (disposing)
87                 {
88                     // Release disposable objects
89                 }
90
91                 if (_lowMemoryEventHandle != IntPtr.Zero)
92                 {
93                     Interop.Watch.RemoveEventHandler(_lowMemoryEventHandle);
94                 }
95                 if (_lowBatteryEventHandle != IntPtr.Zero)
96                 {
97                     Interop.Watch.RemoveEventHandler(_lowBatteryEventHandle);
98                 }
99                 if (_localeChangedEventHandle != IntPtr.Zero)
100                 {
101                     Interop.Watch.RemoveEventHandler(_localeChangedEventHandle);
102                 }
103                 if (_regionChnagedEventHandle != IntPtr.Zero)
104                 {
105                     Interop.Watch.RemoveEventHandler(_regionChnagedEventHandle);
106                 }
107
108                 _disposedValue = true;
109             }
110         }
111
112         public void Exit()
113         {
114             Interop.Watch.Exit();
115         }
116
117         public void Run(string[] args)
118         {
119             Interop.Watch.ErrorCode err = Interop.Watch.ErrorCode.None;
120
121             err = Interop.Watch.AddEventHandler(out _lowMemoryEventHandle, Interop.Watch.AppEventType.LowMemory, _lowMemoryCallback, IntPtr.Zero);
122             if (err != Interop.Watch.ErrorCode.None)
123             {
124                 Log.Error(LOGTAG, "Failed to add event handler for LowMemory event, Err = " + err);
125             }
126
127             err = Interop.Watch.AddEventHandler(out _lowBatteryEventHandle, Interop.Watch.AppEventType.LowBattery, _lowBatteryCallback, IntPtr.Zero);
128             if (err != Interop.Watch.ErrorCode.None)
129             {
130                 Log.Error(LOGTAG, "Failed to add event handler for LowBattery event, Err = " + err);
131             }
132
133             err = Interop.Watch.AddEventHandler(out _localeChangedEventHandle, Interop.Watch.AppEventType.LanguageChanged, _localeChangedCallback, IntPtr.Zero);
134             if (err != Interop.Watch.ErrorCode.None)
135             {
136                 Log.Error(LOGTAG, "Failed to add event handler for LocaleChanged event, Err = " + err);
137             }
138
139             err = Interop.Watch.AddEventHandler(out _regionChnagedEventHandle, Interop.Watch.AppEventType.RegionFormatChanged, _regionChnagedCallback, IntPtr.Zero);
140             if (err != Interop.Watch.ErrorCode.None)
141             {
142                 Log.Error(LOGTAG, "Failed to add event handler for RegionFormatChanged event, Err = " + err);
143             }
144
145             err = Interop.Watch.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
146             if (err != Interop.Watch.ErrorCode.None)
147             {
148                 Log.Error(LOGTAG, "Failed to run the Watch application, Err = " + err);
149             }
150         }
151
152         private void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
153         {
154             LowMemoryStatus status = LowMemoryStatus.None;
155             ErrorCode err = Interop.Watch.AppEventGetLowMemoryStatus(infoHandle, out status);
156             if (err != ErrorCode.None)
157             {
158                 Log.Error(LOGTAG, "Failed to get memory status, Err = " + err);
159             }
160             if (_handlers.ContainsKey(WatchEventType.LowMemory))
161             {
162                 var handler = _handlers[WatchEventType.LowMemory] as Action<LowMemoryEventArgs>;
163                 handler?.Invoke(new LowMemoryEventArgs(status));
164             }
165         }
166
167         private void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
168         {
169             LowBatteryStatus status = LowBatteryStatus.None;
170             ErrorCode err = Interop.Watch.AppEventGetLowBatteryStatus(infoHandle, out status);
171             if (err != Tizen.Internals.Errors.ErrorCode.None)
172             {
173                 Log.Error(LOGTAG, "Failed to get battery status, Err = " + err);
174             }
175             if (_handlers.ContainsKey(WatchEventType.LowBattery))
176             {
177                 var handler = _handlers[WatchEventType.LowBattery] as Action<LowBatteryEventArgs>;
178                 handler?.Invoke(new LowBatteryEventArgs(status));
179             }
180         }
181         private void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
182         {
183             string lang;
184             ErrorCode err = Interop.Watch.AppEventGetLanguage(infoHandle, out lang);
185             if (err != ErrorCode.None)
186             {
187                 Log.Error(LOGTAG, "Failed to get changed language. Err = " + err);
188             }
189             if (_handlers.ContainsKey(WatchEventType.LocaleChanged))
190             {
191                 var handler = _handlers[WatchEventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
192                 handler?.Invoke(new LocaleChangedEventArgs(lang));
193             }
194         }
195         private void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
196         {
197             string region;
198             ErrorCode err = Interop.Watch.AppEventGetRegionFormat(infoHandle, out region);
199             if (err != ErrorCode.None)
200             {
201                 Log.Error(LOGTAG, "Failed to get changed region format. Err = " + err);
202             }
203             if (_handlers.ContainsKey(WatchEventType.RegionFormatChanged))
204             {
205                 var handler = _handlers[WatchEventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
206                 handler?.Invoke(new RegionFormatChangedEventArgs(region));
207             }
208         }
209         private bool OnCreateNative(int width, int height, IntPtr data)
210         {
211             if (_handlers.ContainsKey(WatchEventType.PreCreated))
212             {
213                 var handler = _handlers[WatchEventType.PreCreated] as Action;
214                 handler?.Invoke();
215             }
216
217             if (_handlers.ContainsKey(WatchEventType.Created))
218             {
219                 var handler = _handlers[WatchEventType.Created] as Action;
220                 handler?.Invoke();
221             }
222             return true;
223         }
224
225         private void OnTerminateNative(IntPtr data)
226         {
227             if (_handlers.ContainsKey(WatchEventType.Terminated))
228             {
229                 var handler = _handlers[WatchEventType.Terminated] as Action;
230                 handler?.Invoke();
231             }
232         }
233
234         private void OnAppControlNative(IntPtr appControlHandle, IntPtr data)
235         {
236             if (_handlers.ContainsKey(WatchEventType.AppControlReceived))
237             {
238                 SafeAppControlHandle safeHandle = new SafeAppControlHandle(appControlHandle, false);
239
240                 var handler = _handlers[WatchEventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
241
242                 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(safeHandle)));
243             }
244         }
245
246         private void OnResumeNative(IntPtr data)
247         {
248             if (_handlers.ContainsKey(WatchEventType.Resumed))
249             {
250                 var handler = _handlers[WatchEventType.Resumed] as Action;
251                 handler?.Invoke();
252             }
253         }
254
255         private void OnPauseNative(IntPtr data)
256         {
257             if (_handlers.ContainsKey(WatchEventType.Paused))
258             {
259                 var handler = _handlers[WatchEventType.Paused] as Action;
260                 handler?.Invoke();
261             }
262         }
263
264         private void OnTimeTickNative(IntPtr watchTime, IntPtr userData)
265         {
266             if (_handlers.ContainsKey(WatchEventType.TimeTick))
267             {
268                 var handler = _handlers[WatchEventType.TimeTick] as Action<TimeEventArgs>;
269                 handler?.Invoke(new TimeEventArgs()
270                 {
271                     Time = new WatchTime(new SafeWatchTimeHandle(watchTime, false))
272                 });
273             }
274         }
275
276         private void OnAmbientTickNative(IntPtr watchTime, IntPtr userData)
277         {
278             if (_handlers.ContainsKey(WatchEventType.AmbientTick))
279             {
280                 var handler = _handlers[WatchEventType.AmbientTick] as Action<TimeEventArgs>;
281                 handler?.Invoke(new TimeEventArgs()
282                 {
283                     Time = new WatchTime(new SafeWatchTimeHandle(watchTime, false))
284                 });
285             }
286         }
287
288         private void OnAmbientChangedNative(bool ambientMode, IntPtr userData)
289         {
290             if (_handlers.ContainsKey(WatchEventType.AmbientChanged))
291             {
292                 var handler = _handlers[WatchEventType.AmbientChanged] as Action<AmbientEventArgs>;
293                 handler?.Invoke(new AmbientEventArgs()
294                 {
295                     Enabled = ambientMode
296                 });
297             }
298         }
299     }
300 }