public static async Task <StorageFile> DownloadImageFromUri(Uri uri, BackgroundTaskType type) { // Save the image to storage Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(); var referer = uri.ToString(); client.DefaultRequestHeaders.Add("Referer", referer); client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko"); var downloadTask = client.GetBufferAsync(uri); var resultBuffer = (await downloadTask); StorageFolder localFolder = ApplicationData.Current.LocalFolder; var imageFolder = await localFolder.CreateFolderAsync(type == BackgroundTaskType.LockScreen? "Lockscreens" : "Wallpapers", CreationCollisionOption.OpenIfExists); var imageFiles = await imageFolder.GetFilesAsync(); foreach (var oldImageFile in imageFiles) { try { await oldImageFile.DeleteAsync(StorageDeleteOption.PermanentDelete); } catch (Exception ex) { } } var imageFile = await imageFolder.CreateFileAsync(DateTime.UtcNow.Ticks.ToString() + ".jpg", CreationCollisionOption.ReplaceExisting); await FileIO.WriteBufferAsync(imageFile, resultBuffer); return(imageFile); }
private void LogTaskRun(bool success, int updateCount, string environment) { _backgroundTaskRepository.LogTaskRun( success: success, updateCount: updateCount, environment: environment, taskName: BackgroundTaskType.ToString()); }
/// <summary> /// Registers a particular bg tasks specified by <paramref name="backgroundTask"/> /// </summary> /// <param name="backgroundTask">The background task to register</param> /// <param name="restart">Indicates if the registration of the bg task is mandatory(True by default)</param> public static void RegisterBackgroundTask(BackgroundTaskType backgroundTask, int bgTaskInterval = 0, bool restart = true) { string bgTaskName; Type bgTaskType; IBackgroundTrigger trigger; var conditions = new List <IBackgroundCondition>(); switch (backgroundTask) { case BackgroundTaskType.ANY: throw new ArgumentException("Is not allowed to register all bg tasks at the same time"); case BackgroundTaskType.SYNC: bgTaskName = nameof(SyncBackgroundTask); bgTaskType = typeof(SyncBackgroundTask); trigger = new TimeTrigger((uint)bgTaskInterval, false); conditions.Add(new SystemCondition(SystemConditionType.FreeNetworkAvailable)); conditions.Add(new SystemCondition(SystemConditionType.InternetAvailable)); break; case BackgroundTaskType.MARK_AS_COMPLETED: bgTaskName = nameof(MarkAsCompletedBackgroundTask); bgTaskType = typeof(MarkAsCompletedBackgroundTask); trigger = new ToastNotificationActionTrigger(); break; default: throw new ArgumentOutOfRangeException($"Provided bg task {backgroundTask} does not exists"); } bool isBgTaskAlreadyRegistered = BackgroundTaskHelper.IsBackgroundTaskRegistered(bgTaskType); if (isBgTaskAlreadyRegistered && !restart) { return; } if (isBgTaskAlreadyRegistered) { UnregisterBackgroundTask(backgroundTask); } if (bgTaskInterval <= 0 && backgroundTask != BackgroundTaskType.MARK_AS_COMPLETED) { return; } BackgroundTaskHelper.Register( bgTaskName, trigger, false, true, conditions.ToArray()); }
private bool IsTaskRegistered(BackgroundTaskType type) { var taskName = GetTaskName(type); foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name.Contains(taskName)) { return(true); } } return(false); }
public void RegisterBackgroundTasks(BackgroundTaskType backgroundTask, bool restart = true) { switch (backgroundTask) { case BackgroundTaskType.SYNC: RegisterTask(SyncId, (long)_appSettings.SyncBackgroundTaskInterval); break; case BackgroundTaskType.MARK_AS_COMPLETED: case BackgroundTaskType.ANY: default: throw new NotSupportedException("The bg task is not supported"); } }
public void UnregisterBackgroundTasks(BackgroundTaskType backgroundTask = BackgroundTaskType.ANY) { switch (backgroundTask) { case BackgroundTaskType.ANY: case BackgroundTaskType.SYNC: WorkManager.Instance.CancelAllWorkByTag($"{SyncId}"); break; case BackgroundTaskType.MARK_AS_COMPLETED: default: throw new NotSupportedException("The bg task is not supported"); } }
private void UnregisterTask(BackgroundTaskType type) { //BackgroundExecutionManager.RequestAccessAsync().AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); foreach (var task in BackgroundTaskRegistration.AllTasks) { var taskName = GetTaskName(type); if (task.Value.Name.Contains(taskName)) { // Unregister the old bg task task.Value.Unregister(true); Debug.WriteLine($"Unregistered old {taskName}"); } } }
private string GetTaskName(BackgroundTaskType type) { switch (type) { case BackgroundTaskType.Lockscreen: return(nameof(LockscreenUpdateTask)); case BackgroundTaskType.Wallpaper: return(nameof(WallpaperUpdateTask)); case BackgroundTaskType.Tile: return(nameof(TileUpdateTask)); default: break; } return(nameof(TileUpdateTask)); }
private uint GetTaskTimeSpan(BackgroundTaskType type) { switch (type) { case BackgroundTaskType.Lockscreen: return(LockscreenUpdateTaskTimeSpan); case BackgroundTaskType.Wallpaper: return(WallpaperUpdateTaskTimeSpan); case BackgroundTaskType.Tile: return(TileUpdateTaskTimeSpan); default: break; } return(TileUpdateTaskTimeSpan); }
private bool GetTaskOnMeteredNetwork(BackgroundTaskType type) { switch (type) { case BackgroundTaskType.Lockscreen: return(LockscreenUpdateTaskOnMeteredNetwork); case BackgroundTaskType.Wallpaper: return(WallpaperUpdateTaskOnMeteredNetwork); case BackgroundTaskType.Tile: return(TileUpdateTaskOnMeteredNetwork); default: break; } return(TileUpdateTaskOnMeteredNetwork); }
public void RegisterBackgroundTasks(BackgroundTaskType backgroundTask, bool restart = true) { int bgTaskInterval = 0; switch (backgroundTask) { case BackgroundTaskType.ANY: throw new ArgumentException("Is not allowed to register all bg tasks at the same time"); case BackgroundTaskType.SYNC: bgTaskInterval = (int)_appSettings.SyncBackgroundTaskInterval; break; default: throw new ArgumentOutOfRangeException(nameof(backgroundTask), backgroundTask, "The bg task cant be registered because it doesnt exits"); } BackgroundTasksManager.RegisterBackgroundTask(backgroundTask, bgTaskInterval, restart); }
private void SetTaskAppVersion(BackgroundTaskType type, string value) { switch (type) { case BackgroundTaskType.Lockscreen: LockscreenUpdateTaskAppVersion = value; break; case BackgroundTaskType.Wallpaper: WallpaperUpdateTaskAppVersion = value; break; case BackgroundTaskType.Tile: TileUpdateTaskAppVersion = value; break; default: break; } }
private void RegisterTask(BackgroundTaskType type) { try { var builder = new BackgroundTaskBuilder(); builder.Name = GetTaskName(type); #if DEBUG builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false)); builder.SetTrigger(new TimeTrigger(GetTaskTimeSpan(type), false)); #else builder.SetTrigger(new TimeTrigger(GetTaskTimeSpan(type), false)); #endif builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable)); if (!GetTaskOnMeteredNetwork(type)) { builder.AddCondition(new SystemCondition(SystemConditionType.FreeNetworkAvailable)); } // If the condition changes while the background task is executing then it will be canceled. //builder.CancelOnConditionLoss = true; var r = builder.Register(); // Update the version of the task SetTaskAppVersion(type, CurrentAppVersion); Debug.WriteLine($"Registered {builder.Name}"); } catch (Exception ex) { // TODO: debug //new MessageDialog(ex.Message).ShowAsync(); throw; } }
/// <summary> /// Unregister a background task specified by <paramref name="backgroundTask"/> /// </summary> /// <param name="backgroundTask">The background task to unregister</param> public static void UnregisterBackgroundTask(BackgroundTaskType backgroundTask = BackgroundTaskType.ANY) { switch (backgroundTask) { case BackgroundTaskType.ANY: BackgroundTaskHelper.Unregister(nameof(SyncBackgroundTask)); BackgroundTaskHelper.Unregister(nameof(MarkAsCompletedBackgroundTask)); break; case BackgroundTaskType.SYNC: BackgroundTaskHelper.Unregister(nameof(SyncBackgroundTask)); break; case BackgroundTaskType.MARK_AS_COMPLETED: BackgroundTaskHelper.Unregister(nameof(MarkAsCompletedBackgroundTask)); break; default: throw new ArgumentOutOfRangeException($"The provided BackgroundTaskType doesnt exists {backgroundTask}"); } }
public async void StartBackgroundTask(BackgroundTaskType backgroundTask, BackgroundTaskParameter parameter) { switch (backgroundTask) { case BackgroundTaskType.SYNC: await Task.Delay(10); var top = Mvx.IoCProvider.Resolve <IMvxAndroidCurrentTopActivity>(); Bundle bundle = null; if (parameter != null) { bundle = new Bundle(); bundle.PutInt(SyncBackgroundService.TaskListIdToSyncKey, parameter.SyncOnlyTaskListId); } top.Activity.StartForegroundServiceCompat <SyncBackgroundService>(bundle); break; case BackgroundTaskType.ANY: case BackgroundTaskType.MARK_AS_COMPLETED: default: throw new NotSupportedException("The bg task is not supported"); } }
public void StartBackgroundTask(BackgroundTaskType backgroundTask, BackgroundTaskParameter parameter) { //TODO: HANDLE MULTIPLE BG TASK START, YOU COULD DO IT BY USING APP SETTINGS switch (backgroundTask) { case BackgroundTaskType.ANY: throw new ArgumentOutOfRangeException("Its not allowed to start all the bg tasks at the same time"); case BackgroundTaskType.SYNC: if (parameter is null) { new SyncBackgroundTask().Run(null); } else { new SyncBackgroundTask(parameter.SyncOnlyTaskListId).Run(null); } break; default: throw new ArgumentOutOfRangeException($"Cant start the provided bg task = {backgroundTask}"); } }
private void ReregisterTask(BackgroundTaskType type) { UnregisterTask(type); RegisterTask(type); }
public async void StartBackgroundTask(BackgroundTaskType backgroundTask) { await Task.Delay(1); StartBackgroundTask(backgroundTask, null); }
private string GetSubjectPrefix() { return($"{_environment}, {BackgroundTaskType.ToString()}:"); }
public void UnregisterBackgroundTasks(BackgroundTaskType backgroundTask = BackgroundTaskType.ANY) { BackgroundTasksManager.UnregisterBackgroundTask(backgroundTask); }
public void StartBackgroundTask(BackgroundTaskType backgroundTask) { StartBackgroundTask(backgroundTask, null); }
public BackgroundTaskPayloadBase(string TaskId, BackgroundTaskType TaskType) { this.TaskId = TaskId; this.TaskType = TaskType; }