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;
19 using Tizen.Internals.Errors;
21 namespace Tizen.Applications.CoreBackend
23 internal class WatchCoreBackend : ICoreBackend
25 private const string LOGTAG = "Tizen.Applications.WatchApplication";
27 private Dictionary<EventType, object> _handlers = new Dictionary<EventType, object>();
29 private bool _disposedValue = false;
31 private Interop.Watch.AppEventCallback _lowMemoryCallback;
32 private Interop.Watch.AppEventCallback _lowBatteryCallback;
33 private Interop.Watch.AppEventCallback _localeChangedCallback;
34 private Interop.Watch.AppEventCallback _regionChnagedCallback;
36 private IntPtr _lowMemoryEventHandle = IntPtr.Zero;
37 private IntPtr _lowBatteryEventHandle = IntPtr.Zero;
38 private IntPtr _localeChangedEventHandle = IntPtr.Zero;
39 private IntPtr _regionChnagedEventHandle = IntPtr.Zero;
41 private Interop.Watch.WatchAppLifecycleCallbacks _callbacks;
43 public WatchCoreBackend()
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);
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);
56 _callbacks.OnTick = new Interop.Watch.WatchAppTimeTickCallback(OnTimeTickNative);
57 _callbacks.OnAmbientTick = new Interop.Watch.WatchAppAmbientTickCallback(OnAmbientTickNative);
58 _callbacks.OnAmbientChanged = new Interop.Watch.WatchAppAmbientChangedCallback(OnAmbientChangedNative);
66 public void AddEventHandler(EventType evType, Action handler)
68 _handlers.Add(evType, handler);
71 public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
73 _handlers.Add(evType, handler);
79 GC.SuppressFinalize(this);
82 protected virtual void Dispose(bool disposing)
88 // Release disposable objects
91 if (_lowMemoryEventHandle != IntPtr.Zero)
93 Interop.Watch.RemoveEventHandler(_lowMemoryEventHandle);
95 if (_lowBatteryEventHandle != IntPtr.Zero)
97 Interop.Watch.RemoveEventHandler(_lowBatteryEventHandle);
99 if (_localeChangedEventHandle != IntPtr.Zero)
101 Interop.Watch.RemoveEventHandler(_localeChangedEventHandle);
103 if (_regionChnagedEventHandle != IntPtr.Zero)
105 Interop.Watch.RemoveEventHandler(_regionChnagedEventHandle);
108 _disposedValue = true;
114 Interop.Watch.Exit();
117 public void Run(string[] args)
119 Interop.Watch.ErrorCode err = Interop.Watch.ErrorCode.None;
121 err = Interop.Watch.AddEventHandler(out _lowMemoryEventHandle, Interop.Watch.AppEventType.LowMemory, _lowMemoryCallback, IntPtr.Zero);
122 if (err != Interop.Watch.ErrorCode.None)
124 Log.Error(LOGTAG, "Failed to add event handler for LowMemory event, Err = " + err);
127 err = Interop.Watch.AddEventHandler(out _lowBatteryEventHandle, Interop.Watch.AppEventType.LowBattery, _lowBatteryCallback, IntPtr.Zero);
128 if (err != Interop.Watch.ErrorCode.None)
130 Log.Error(LOGTAG, "Failed to add event handler for LowBattery event, Err = " + err);
133 err = Interop.Watch.AddEventHandler(out _localeChangedEventHandle, Interop.Watch.AppEventType.LanguageChanged, _localeChangedCallback, IntPtr.Zero);
134 if (err != Interop.Watch.ErrorCode.None)
136 Log.Error(LOGTAG, "Failed to add event handler for LocaleChanged event, Err = " + err);
139 err = Interop.Watch.AddEventHandler(out _regionChnagedEventHandle, Interop.Watch.AppEventType.RegionFormatChanged, _regionChnagedCallback, IntPtr.Zero);
140 if (err != Interop.Watch.ErrorCode.None)
142 Log.Error(LOGTAG, "Failed to add event handler for RegionFormatChanged event, Err = " + err);
145 err = Interop.Watch.Main(args.Length, args, ref _callbacks, IntPtr.Zero);
146 if (err != Interop.Watch.ErrorCode.None)
148 Log.Error(LOGTAG, "Failed to run the Watch application, Err = " + err);
152 private void OnLowMemoryNative(IntPtr infoHandle, IntPtr data)
154 LowMemoryStatus status = LowMemoryStatus.None;
155 ErrorCode err = Interop.Watch.AppEventGetLowMemoryStatus(infoHandle, out status);
156 if (err != ErrorCode.None)
158 Log.Error(LOGTAG, "Failed to get memory status, Err = " + err);
160 if (_handlers.ContainsKey(WatchEventType.LowMemory))
162 var handler = _handlers[WatchEventType.LowMemory] as Action<LowMemoryEventArgs>;
163 handler?.Invoke(new LowMemoryEventArgs(status));
167 private void OnLowBatteryNative(IntPtr infoHandle, IntPtr data)
169 LowBatteryStatus status = LowBatteryStatus.None;
170 ErrorCode err = Interop.Watch.AppEventGetLowBatteryStatus(infoHandle, out status);
171 if (err != Tizen.Internals.Errors.ErrorCode.None)
173 Log.Error(LOGTAG, "Failed to get battery status, Err = " + err);
175 if (_handlers.ContainsKey(WatchEventType.LowBattery))
177 var handler = _handlers[WatchEventType.LowBattery] as Action<LowBatteryEventArgs>;
178 handler?.Invoke(new LowBatteryEventArgs(status));
181 private void OnLocaleChangedNative(IntPtr infoHandle, IntPtr data)
184 ErrorCode err = Interop.Watch.AppEventGetLanguage(infoHandle, out lang);
185 if (err != ErrorCode.None)
187 Log.Error(LOGTAG, "Failed to get changed language. Err = " + err);
189 if (_handlers.ContainsKey(WatchEventType.LocaleChanged))
191 var handler = _handlers[WatchEventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
192 handler?.Invoke(new LocaleChangedEventArgs(lang));
195 private void OnRegionChangedNative(IntPtr infoHandle, IntPtr data)
198 ErrorCode err = Interop.Watch.AppEventGetRegionFormat(infoHandle, out region);
199 if (err != ErrorCode.None)
201 Log.Error(LOGTAG, "Failed to get changed region format. Err = " + err);
203 if (_handlers.ContainsKey(WatchEventType.RegionFormatChanged))
205 var handler = _handlers[WatchEventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
206 handler?.Invoke(new RegionFormatChangedEventArgs(region));
209 private bool OnCreateNative(int width, int height, IntPtr data)
211 if (_handlers.ContainsKey(WatchEventType.PreCreated))
213 var handler = _handlers[WatchEventType.PreCreated] as Action;
217 if (_handlers.ContainsKey(WatchEventType.Created))
219 var handler = _handlers[WatchEventType.Created] as Action;
225 private void OnTerminateNative(IntPtr data)
227 if (_handlers.ContainsKey(WatchEventType.Terminated))
229 var handler = _handlers[WatchEventType.Terminated] as Action;
234 private void OnAppControlNative(IntPtr appControlHandle, IntPtr data)
236 if (_handlers.ContainsKey(WatchEventType.AppControlReceived))
238 SafeAppControlHandle safeHandle = new SafeAppControlHandle(appControlHandle, false);
240 var handler = _handlers[WatchEventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
242 handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(safeHandle)));
246 private void OnResumeNative(IntPtr data)
248 if (_handlers.ContainsKey(WatchEventType.Resumed))
250 var handler = _handlers[WatchEventType.Resumed] as Action;
255 private void OnPauseNative(IntPtr data)
257 if (_handlers.ContainsKey(WatchEventType.Paused))
259 var handler = _handlers[WatchEventType.Paused] as Action;
264 private void OnTimeTickNative(IntPtr watchTime, IntPtr userData)
266 if (_handlers.ContainsKey(WatchEventType.TimeTick))
268 var handler = _handlers[WatchEventType.TimeTick] as Action<TimeEventArgs>;
269 handler?.Invoke(new TimeEventArgs()
271 Time = new WatchTime(new SafeWatchTimeHandle(watchTime, false))
276 private void OnAmbientTickNative(IntPtr watchTime, IntPtr userData)
278 if (_handlers.ContainsKey(WatchEventType.AmbientTick))
280 var handler = _handlers[WatchEventType.AmbientTick] as Action<TimeEventArgs>;
281 handler?.Invoke(new TimeEventArgs()
283 Time = new WatchTime(new SafeWatchTimeHandle(watchTime, false))
288 private void OnAmbientChangedNative(bool ambientMode, IntPtr userData)
290 if (_handlers.ContainsKey(WatchEventType.AmbientChanged))
292 var handler = _handlers[WatchEventType.AmbientChanged] as Action<AmbientEventArgs>;
293 handler?.Invoke(new AmbientEventArgs()
295 Enabled = ambientMode