using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tizen.Security; using Tizen.Sensor; using Tizen.System; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace TizenWatchfaceApp1 { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class WatchFaceApplication : Application { private const string heartRatePrivilege = "http://tizen.org/privilege/healthinfo"; private HeartRateMonitor heartRateMonitor = null; public ClockViewModel ViewModel { get; set; } = null; public WatchFaceApplication() { InitializeComponent(); setupPrivilegeHandler(); checkPrivilegeHandler(); this.ViewModel = new ClockViewModel(); this.BindingContext = this.ViewModel; } protected override void OnSleep() { base.OnSleep(); if (heartRateMonitor != null) { heartRateMonitor.Stop(); } } protected override void OnResume() { base.OnResume(); setupPrivilegeHandler(); if (heartRateMonitor != null) { heartRateMonitor.Start(); } } private void heartRateMonitor_DataUpdated(object sender, HeartRateMonitorDataUpdatedEventArgs e) { if (this.ViewModel != null) this.ViewModel.HeartRate = e.HeartRate; } private void setupPrivilegeHandler() { PrivacyPrivilegeManager.ResponseContext context = null; if (PrivacyPrivilegeManager.GetResponseContext(heartRatePrivilege).TryGetTarget(out context)) { context.ResponseFetched += privilege_ResponseFetched; } } private void privilege_ResponseFetched(object sender, RequestResponseEventArgs e) { if (e.cause == CallCause.Error) { return; } switch (e.result) { case RequestResult.AllowForever: if (HeartRateMonitor.IsSupported) { heartRateMonitor = new HeartRateMonitor(); heartRateMonitor.DataUpdated += heartRateMonitor_DataUpdated; heartRateMonitor.Start(); } break; case RequestResult.DenyForever: case RequestResult.DenyOnce: break; } } private void checkPrivilegeHandler() { CheckResult rs = PrivacyPrivilegeManager.CheckPermission(heartRatePrivilege); switch (rs) { case CheckResult.Allow: if (HeartRateMonitor.IsSupported) { heartRateMonitor = new HeartRateMonitor(); heartRateMonitor.DataUpdated += heartRateMonitor_DataUpdated; } break; case CheckResult.Deny: // do nothing break; case CheckResult.Ask: PrivacyPrivilegeManager.RequestPermission(heartRatePrivilege); break; } } } }