public override bool OnStartJob(JobParameters jobParams) { //NOTE: Lazy loading of service for platform specific version #pragma warning disable CS0436 // Type conflicts with imported type IWalkingDataService walkingDataService = CrossWalkingDataService.Current; IUpdateService updateService = CrossUpdateService.Current; #pragma warning restore CS0436 // Type conflicts with imported type CrossServiceContainer.SetWalkingDataService(walkingDataService); CrossServiceContainer.SetUpdateService(updateService); //NOTE: As mainactivity might not be run / we need to set this icon LocalNotificationsImplementation.NotificationIconId = Resource.Mipmap.notification_icon; Task.Run(async() => { if (updateService != null) { //NOTE: When Service is created - a separate instance of CrossUpdateService may be created where the default mode is foreground, with no lifecycle events //NOTE: Thus we check if the app is running. Note the different packagenames for application and this service if (!AppLifecycleUtil.IsRunning(this, Application.Context.PackageName)) { updateService.SetUpdateMode(UpdateMode.Background); } Log.Debug("UpdateJob", "Running update job with updateservice address: " + updateService.ToString()); if (updateService.GetUpdateMode() == UpdateMode.Background) { #if DEBUG Stopwatch timer = Stopwatch.StartNew(); #endif await updateService.UpdateAllDataServices(); Analytics.TrackEvent(TrackingEvents.Background, new TrackingEvents.BackgroundArgs(TrackingEvents.BackgroundEvents.BackgroundFetch)); #if DEBUG Console.WriteLine("Completed background update of walking data and achievements in {0} ms", timer.ElapsedMilliseconds); #endif } else { #if DEBUG Console.WriteLine("Skipped background update since we're not in background"); #endif } } //NOTE: Return whether or not we want to reschedule the job //NOTE: We do not, as JobInfo.Builder.setPeriodic() takes care of resheduling for us! JobFinished(jobParams, false); }); //Continue this job until Jobfinished is called return(true); }
protected override void OnCreate(Bundle bundle) { AppDomain.CurrentDomain.UnhandledException += PlatformError.CurrentDomainUnhandledException; TaskScheduler.UnobservedTaskException += PlatformError.TaskSchedulerUnobservedTaskException; App.ScreenHeight = (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density); App.ScreenWidth = (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density); App.VersionName = Application.ApplicationContext.PackageManager .GetPackageInfo(Application.ApplicationContext.PackageName, 0) .VersionName; AccessibilityManager accessibilityManager = (AccessibilityManager)GetSystemService(AccessibilityService); //NOTE: ref: https://stackoverflow.com/a/12362545 we only use touch exploration events for checking if talkback is enabled accessibilityManager.AddTouchExplorationStateChangeListener(this); Accessibility.AccessibilityEnabled = accessibilityManager.IsTouchExplorationEnabled; Xamarin.Forms.Forms.Init(this, bundle); UserDialogs.Init(this); CarouselViewRenderer.Init(); CachedImageRenderer.Init(true); FlowListView.Init(); Rg.Plugins.Popup.Popup.Init(this, bundle); LocalNotificationsImplementation.NotificationIconId = Resource.Mipmap.notification_icon; //NOTE: We know. Lazy loading of service for platform specific version #pragma warning disable CS0436 // Type conflicts with imported type IWalkingDataService walkingDataService = CrossWalkingDataService.Current; IUpdateService updateService = CrossUpdateService.Current; IScreenshotService screenshotService = CrossScreenshotService.Current; IShareImageService shareImageService = CrossImageShareService.Current; #pragma warning restore CS0436 // Type conflicts with imported type ScreenshotService.Activity = this; ShareImageService.Context = this; CrossServiceContainer.SetWalkingDataService(walkingDataService); CrossServiceContainer.SetUpdateService(updateService); CrossServiceContainer.SetScreenshotService(screenshotService); CrossServiceContainer.SetShareImageService(shareImageService); Log.Debug("MainActivity", "Mainactivity updateservice: " + updateService.ToString()); base.OnCreate(bundle); Instance = this; OnActivityCreated?.Invoke(this); if (Settings.CanRequestReview()) { RequestReview(); } }
public void OnFinishedLaunching(NSDictionary options = null) { AppDomain.CurrentDomain.UnhandledException += PlatformError.CurrentDomainUnhandledException; TaskScheduler.UnobservedTaskException += PlatformError.TaskSchedulerUnobservedTaskException; //NOTE: this is in seconds //UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(Config.BackgroundUpdateIntervalMS / 1000); UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum); App.ScreenHeight = (int)UIScreen.MainScreen.Bounds.Height; App.ScreenWidth = (int)UIScreen.MainScreen.Bounds.Width; App.VersionName = NSBundle.MainBundle.InfoDictionary[new NSString("CFBundleVersion")].ToString(); Accessibility.AccessibilityEnabled = UIAccessibility.IsVoiceOverRunning; NSNotificationCenter.DefaultCenter.AddObserver(UIView.VoiceOverStatusDidChangeNotification, notification => Accessibility.AccessibilityEnabled = UIAccessibility.IsVoiceOverRunning); Xamarin.Forms.Forms.Init(); AnimationViewRenderer.Init(); CarouselViewRenderer.Init(); CachedImageRenderer.Init(); Rg.Plugins.Popup.Popup.Init(); CarouselViewRenderer.Init(); CachedImageRenderer.Init(); ShapeRenderer.Init(); FlowListView.Init(); //Setup accessibility callbacks Xamarin.Forms.Forms.Init(); LoadApplication(new App()); GetNotificationPermissions(); //NOTE: We know. Lazy loading of service for platform specific version #pragma warning disable CS0436 // Type conflicts with imported type IWalkingDataService walkingDataService = CrossWalkingDataService.Current; IUpdateService updateService = CrossUpdateService.Current; IScreenshotService screenshotService = CrossScreenshotService.Current; IShareImageService shareImageService = CrossImageShareService.Current; #pragma warning restore CS0436 // Type conflicts with imported type CrossServiceContainer.SetWalkingDataService(walkingDataService); CrossServiceContainer.SetUpdateService(updateService); CrossServiceContainer.SetScreenshotService(screenshotService); CrossServiceContainer.SetShareImageService(shareImageService); Console.WriteLine("Appdelegate unning updateService: " + updateService.ToString()); }
public override async void PerformFetch(UIApplication application, Action <UIBackgroundFetchResult> completionHandler) { Analytics.TrackEvent(TrackingEvents.BackgroundEvents.BackgroundFetch); //NOTE: We know. Lazy loading of service for platform specific version #pragma warning disable CS0436 // Type conflicts with imported type IWalkingDataService walkingDataService = CrossWalkingDataService.Current; IUpdateService updateService = CrossUpdateService.Current; #pragma warning restore CS0436 // Type conflicts with imported type CrossServiceContainer.SetWalkingDataService(walkingDataService); CrossServiceContainer.SetUpdateService(updateService); Console.WriteLine("Background Fetch running updateService: " + updateService.ToString()); var result = UIBackgroundFetchResult.NoData; var minutesBefore = walkingDataService.GetTodaysHistory().minutesBriskWalkToday + walkingDataService.GetTodaysHistory().minutesRegularWalkToday; try { await updateService.UpdateAllDataServices(); var minutesAfter = walkingDataService.GetTodaysHistory().minutesBriskWalkToday + walkingDataService.GetTodaysHistory().minutesRegularWalkToday;; if (minutesBefore != minutesAfter) { result = UIBackgroundFetchResult.NewData; } } catch { Crashes.TrackError(new BackgroundException(UIBackgroundFetchResult.Failed.ToString())); result = UIBackgroundFetchResult.Failed; } var output = "Ran background fetch with result: " + Enum.GetName(typeof(UIBackgroundFetchResult), result); #if DEBUG LiveLogService.Current.LogLine(output); #endif completionHandler(result); Analytics.TrackEvent(TrackingEvents.Background, new TrackingEvents.BackgroundArgs(result.ToString())); }
public static void SetWalkingDataService(IWalkingDataService walkingDataService) { WalkingDataService = walkingDataService; }
public async Task UpdateAllDataServices() { Debug.WriteLine($"Updating all data services in mode: {currentUpdateMode}"); try { IWalkingDataService walkingDataService = CrossServiceContainer.WalkingDataService; if (!walkingDataService.GetIsConnectedToOSService()) { walkingDataService.ConnectToOSService(); return; } TodaysWalkingModel todaysWalking = null; if (walkingDataService != null) { await walkingDataService.RequestUpdate(); todaysWalking = walkingDataService.GetTodaysHistory(); } IGoalService goalService = GoalService.Current; if (goalService != null && todaysWalking != null) { if (await goalService.CheckIfGoalCompleted(todaysWalking)) { if (!App.IsForeground && Settings.WillSendNotifications) { var id = (Config.NotificationIdGoal + (int)Settings.CurrentGoal); CrossLocalNotifications.Current.Show(AppText.notification_title, AppText.notification_goal_reached, id); Analytics.TrackEvent(TrackingEvents.BackgroundEvents.NotificationSent); } } } IAchievementService achievementService = AchievementService.Current; if (achievementService != null && todaysWalking != null) { //await achievementService.CheckAllAchievementsCriteria(todaysWalking); await achievementService.CheckTieredAchievementsProgress(todaysWalking); } var lastWalkingDateEventDate = Settings.LastWalkingDataEventDate; if (lastWalkingDateEventDate.AddDays(1) < DateTime.Today) { IStorageService storageService = StorageService.Current; if (storageService != null) { var days = await storageService.GetWalkingDaysSinceInclusive(lastWalkingDateEventDate.AddDays(1)); var today = DateTime.Today; var daysToTrack = days.Where(d => Settings.LastWalkingDataEventDate < d.Day && d.Day < today); foreach (var d in daysToTrack) { Analytics.TrackEvent(TrackingEvents.BriskWalking, new TrackingEvents.WalkingDataArgs(d.Day.DayOfWeek, d.MinutesBriskWalking)); Analytics.TrackEvent(TrackingEvents.RegularWalking, new TrackingEvents.WalkingDataArgs(d.Day.DayOfWeek, d.MinutesRegularWalking)); Analytics.TrackEvent(TrackingEvents.WalkingRaw, new TrackingEvents.WalkingRawDataArgs(d.Day.Date, d.MinutesBriskWalking, d.MinutesRegularWalking, d.MinutesUnknownWalking, Settings.UserHeight)); } //Last date stored is always today - 1 Settings.LastWalkingDataEventDate = today.AddDays(-1); // log history var all = await storageService.GetWalkingDaysSinceInclusive(DateTime.MinValue); Analytics.TrackEvent(TrackingEvents.DailyHistory, new TrackingEvents.WalkingDataArgs(all)); } } UpdateCompleted?.Invoke(); } catch (Exception e) { #if DEBUG Device.BeginInvokeOnMainThread(() => throw e); #else Crashes.TrackError(e); #endif } }