/// <summary> /// Rotate a single image type. /// </summary> /// <param name="type"></param> /// <returns></returns> private async Task <bool> DoSingleImageRotation(UpdateTypes type) { // Make sure we should do the update. if ((IsLockScreenEnabled && type == UpdateTypes.LockScreen) || (IsDeskopEnabled && type == UpdateTypes.Desktop) || (IsBandWallpaperEnabled && type == UpdateTypes.Band)) { // If the lock screen and desktop are the same subreddit use the same files. UpdateTypes fileCacheType = type; // If this is a desktop update, and lock is enabled, and they are the same subreddit... if (type == UpdateTypes.Desktop && IsLockScreenEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // If they are all the same use the lock screen cache for the desktop. fileCacheType = UpdateTypes.LockScreen; } // Get the current files.. IReadOnlyList <StorageFile> files = await GetCurrentCacheImages(fileCacheType); if (files != null && files.Count != 0) { int currentIndex = 0; if (type == UpdateTypes.LockScreen) { // Update the index CurrentLockScreenRotationIndex++; if (CurrentLockScreenRotationIndex >= files.Count) { CurrentLockScreenRotationIndex = 0; } currentIndex = CurrentLockScreenRotationIndex; } else if (type == UpdateTypes.Band) { // Update the index CurrentBandRotationIndex++; if (CurrentBandRotationIndex >= files.Count) { CurrentBandRotationIndex = 0; } currentIndex = CurrentBandRotationIndex; } else { // Update the index CurrentDesktopRotationIndex++; if (CurrentDesktopRotationIndex >= files.Count) { CurrentDesktopRotationIndex = 0; } currentIndex = CurrentDesktopRotationIndex; } // Set the image bool wasSuccess = await SetBackgroundImage(type, files[currentIndex]); return(wasSuccess); } } // Return true if we are disabled return(true); }
/// <summary> /// Actually does the update /// </summary> private async Task KickOffUpdate(bool force, RefCountedDeferral refDeferral) { m_baconMan.TelemetryMan.ReportLog(this, "Updater updating."); try { // Figure out if we need to do a full update int fullUpdateFequencyMins = UpdateFrquency * c_maxImageCacheCount; TimeSpan timeSinceLastFullUpdate = DateTime.Now - LastFullUpdate; if (timeSinceLastFullUpdate.TotalMinutes > fullUpdateFequencyMins || force) { m_baconMan.TelemetryMan.ReportLog(this, "Running full update"); // Update lock screen images if (IsLockScreenEnabled) { m_baconMan.TelemetryMan.ReportLog(this, "Updating lock screen", SeverityLevel.Verbose); // Make a deferral scope object so we can do our work without being killed. // Note! We need release this object or it will hang the app around forever! m_lockScreenRefDeferral = refDeferral; m_lockScreenRefDeferral.AddRef(); // Kick off the update, this will happen async. GetSubredditStories(LockScreenSubredditName, UpdateTypes.LockScreen); } if (IsDeskopEnabled) { m_baconMan.TelemetryMan.ReportLog(this, "Updating lock screen", SeverityLevel.Verbose); // Shortcut: If lock screen in enabled and it is the same subreddit just share the same cache. If not, // get the desktop images if (IsLockScreenEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { m_baconMan.TelemetryMan.ReportLog(this, "Desktop same sub as lockscreen, skipping image update.", SeverityLevel.Verbose); } else { m_baconMan.TelemetryMan.ReportLog(this, "Updating desktop image", SeverityLevel.Verbose); // Make a deferral scope object so we can do our work without being killed. // Note! We need release this object or it will hang the app around forever! m_desktopRefDeferral = refDeferral; m_desktopRefDeferral.AddRef(); // Kick off the update, this will happen async. GetSubredditStories(DesktopSubredditName, UpdateTypes.Desktop); } } // Update lock screen images if (IsBandWallpaperEnabled) { m_baconMan.TelemetryMan.ReportLog(this, "Updating band wallpaper", SeverityLevel.Verbose); // Make a deferral scope object so we can do our work without being killed. // Note! We need release this object or it will hang the app around forever! m_bandRefDeferral = refDeferral; m_bandRefDeferral.AddRef(); // Kick off the update, this will happen async. GetSubredditStories(BandSubredditName, UpdateTypes.Band); } } // Else full update else { m_baconMan.TelemetryMan.ReportLog(this, "No need for a full update, just rotating."); // If we aren't doing a full update just rotate the images. await DoImageRotation(UpdateTypes.All); // Stop the running state lock (this) { m_isRunning = false; } } } catch (Exception e) { m_baconMan.MessageMan.DebugDia("Failed to set background image", e); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "Failed to set background image", e); } }
/// <summary> /// Fired when an image request is done. /// </summary> /// <param name="sender"></param> /// <param name="response"></param> public async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove event listener ImageManager.ImageManagerRequest request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; UpdateTypes type = (UpdateTypes)response.Request.Context; // Make sure we were successfully. if (response.Success) { try { // Get the target size Size targetImageSize = LastKnownScreenResoultion; if (type == UpdateTypes.Band) { if (m_baconMan.BackgroundMan.BandMan.BandVersion == BandVersions.V1) { targetImageSize = new Size(310, 102); } else { targetImageSize = new Size(310, 128); } } // Resize the image to fit nicely InMemoryRandomAccessStream image = await ResizeImage(response.ImageStream, targetImageSize); // Write the file await WriteImageToFile(image, type); } catch (Exception e) { m_baconMan.MessageMan.DebugDia("Failed to write background image", e); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "Failed to write background image", e); } } // Indicate that this image is done. bool isDone = false; lock (m_getImageLock) { if (type == UpdateTypes.LockScreen) { m_lockScreenDoneCount++; isDone = m_lockScreenDoneCount >= m_lockScreenRequestCount; } else if (type == UpdateTypes.Band) { m_bandDoneCount++; isDone = m_bandDoneCount >= m_bandRequestCount; } else { m_desktopDoneCount++; isDone = m_desktopDoneCount >= m_desktopRequestCount; } } // if we are done done then tell the images to rotate and clean up. if (isDone) { // Set this high so we roll over. if (type == UpdateTypes.LockScreen) { CurrentLockScreenRotationIndex = 99; } else if (type == UpdateTypes.Band) { CurrentBandRotationIndex = 99; } else { CurrentDesktopRotationIndex = 99; } // Do the rotate await DoImageRotation(type); // If this is a lock screen update this might also be a desktop update. This happens when the lock screen and desktop // share the same subreddit, they share the same images. if (type == UpdateTypes.LockScreen && IsDeskopEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // Off set the two so they don't show the same image CurrentDesktopRotationIndex = 1; // We also need to update the desktop. await DoImageRotation(UpdateTypes.Desktop); } // We are done, set the last update time LastFullUpdate = DateTime.Now; // And kill the deferral ReleaseDeferral(type); // And set us to stopped. UnSetIsRunningIfDone(); } }
/// <summary> /// Rotate a single image type. /// </summary> /// <param name="type"></param> /// <returns></returns> private async Task <bool> DoSingleImageRotation(UpdateTypes type) { // Make sure we should do the update. if ((IsLockScreenEnabled && type == UpdateTypes.LockScreen) || (IsDeskopEnabled && type == UpdateTypes.Desktop) || (IsBandWallpaperEnabled && type == UpdateTypes.Band)) { // If the lock screen and desktop are the same subreddit use the same files. UpdateTypes fileCacheType = type; // If this is a desktop update, and lock is enabled, and they are the same subreddit... if (type == UpdateTypes.Desktop && IsLockScreenEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // If they are all the same use the lock screen cache for the desktop. fileCacheType = UpdateTypes.LockScreen; } // Get the current files.. IReadOnlyList <StorageFile> files = await GetCurrentCacheImages(fileCacheType); m_baconMan.TelemetryMan.ReportLog(this, "Current images in cache :" + files.Count); if (files != null && files.Count != 0) { int currentIndex = 0; if (type == UpdateTypes.LockScreen) { // Update the index CurrentLockScreenRotationIndex++; if (CurrentLockScreenRotationIndex >= files.Count) { CurrentLockScreenRotationIndex = 0; } currentIndex = CurrentLockScreenRotationIndex; } else if (type == UpdateTypes.Band) { // Update the index CurrentBandRotationIndex++; if (CurrentBandRotationIndex >= files.Count) { CurrentBandRotationIndex = 0; } currentIndex = CurrentBandRotationIndex; } else { // Update the index CurrentDesktopRotationIndex++; if (CurrentDesktopRotationIndex >= files.Count) { CurrentDesktopRotationIndex = 0; } currentIndex = CurrentDesktopRotationIndex; } m_baconMan.TelemetryMan.ReportLog(this, "Current index used to set image :" + currentIndex); // Set the image bool wasSuccess = await SetBackgroundImage(type, files[currentIndex]); if (!wasSuccess) { m_baconMan.TelemetryMan.ReportLog(this, "Image update failed", SeverityLevel.Error); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, type == UpdateTypes.LockScreen ? "LockscreenImageUpdateFailed" : "DesktopImageUpdateFailed"); } else { m_baconMan.TelemetryMan.ReportLog(this, "Image update success"); } return(wasSuccess); } } // Return true if we are disabled return(true); }
/// <summary> /// THREAD BLOCKING: Actually does the update, this will block the current thread. /// </summary> private bool ActuallyDoTheUpdate(bool force) { m_baconMan.TelemetryMan.ReportLog(this, "Updater updating."); bool wasSuccessfull = true; try { // Figure out if we need to do a full update int fullUpdateFequencyMins = UpdateFrquency * c_maxImageCacheCount; TimeSpan timeSinceLastFullUpdate = DateTime.Now - LastFullUpdate; if (timeSinceLastFullUpdate.TotalMinutes > fullUpdateFequencyMins || force) { m_baconMan.TelemetryMan.ReportLog(this, "Running full update"); // Update lock screen images if (IsLockScreenEnabled) { m_baconMan.TelemetryMan.ReportLog(this, "Updating lock screen", SeverityLevel.Verbose); // We are doing a full update, grab new stories for lock screen List <Post> posts = GetSubredditStories(LockScreenSubredditName); // Check we successfully got new posts if (posts != null) { m_baconMan.TelemetryMan.ReportLog(this, "Lock screen post retrieved, getting images", SeverityLevel.Verbose); // If so, get the images for the posts if (GetImagesFromPosts(posts, true)) { LastFullUpdate = DateTime.Now; // Set the index high so it will roll over. CurrentLockScreenRotationIndex = 99; } else { m_baconMan.TelemetryMan.ReportLog(this, "Updating lock screen update failed, we failed to get images", SeverityLevel.Error); wasSuccessfull = false; } } else { m_baconMan.TelemetryMan.ReportLog(this, "Updating lock screen update failed, we have no posts", SeverityLevel.Error); wasSuccessfull = false; } } if (IsDeskopEnabled) { m_baconMan.TelemetryMan.ReportLog(this, "Updating lock screen", SeverityLevel.Verbose); // Shortcut: If lock screen in enabled and it is the same subreddit just share the same cache. If not, // get the desktop images if (IsLockScreenEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // Set the desktop rotation to 1 so we offset lock screen CurrentDesktopRotationIndex = 1; m_baconMan.TelemetryMan.ReportLog(this, "Desktop same sub as lockscreen, skipping image update.", SeverityLevel.Verbose); } else { m_baconMan.TelemetryMan.ReportLog(this, "Updating desktop image", SeverityLevel.Verbose); // We are doing a full update, grab new stories for desktop List <Post> posts = GetSubredditStories(DesktopSubredditName); // Check we successfully got new posts if (posts != null) { m_baconMan.TelemetryMan.ReportLog(this, "Desktop posts retrieved, getting images", SeverityLevel.Verbose); // If so, get the images for the posts if (GetImagesFromPosts(posts, false)) { // Set the index high so it will roll over. LastFullUpdate = DateTime.Now; CurrentDesktopRotationIndex = 99; } else { m_baconMan.TelemetryMan.ReportLog(this, "Updating desktop image failed, we failed to get images", SeverityLevel.Error); wasSuccessfull = false; } } else { m_baconMan.TelemetryMan.ReportLog(this, "Updating desktop image failed, we failed to get posts", SeverityLevel.Error); wasSuccessfull = false; } } } } m_baconMan.TelemetryMan.ReportLog(this, "Image gathering done or skipped, moving on to setting."); // Now update the images using (AutoResetEvent aEvent = new AutoResetEvent(false)) { Task.Run(async() => { try { // Update the lock screen image IReadOnlyList <StorageFile> lockScreenFiles = null; if (IsLockScreenEnabled) { lockScreenFiles = await GetCurrentCacheImages(true); m_baconMan.TelemetryMan.ReportLog(this, "Current lockscreen images in cache :" + lockScreenFiles.Count); if (lockScreenFiles.Count != 0) { // Update the index CurrentLockScreenRotationIndex++; if (CurrentLockScreenRotationIndex > lockScreenFiles.Count) { CurrentLockScreenRotationIndex = 0; } m_baconMan.TelemetryMan.ReportLog(this, "Current lockscreen index used to set image :" + CurrentLockScreenRotationIndex); // Set the image bool localResult = await SetBackgroundImage(true, lockScreenFiles[CurrentLockScreenRotationIndex]); if (!localResult) { m_baconMan.TelemetryMan.ReportLog(this, "Lockscreen image update failed", SeverityLevel.Error); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "LockscreenImageUpdateFailed"); } else { m_baconMan.TelemetryMan.ReportLog(this, "Lockscreen image update success"); } // Only set the result if we were already successful if (wasSuccessfull) { wasSuccessfull = localResult; } } else { wasSuccessfull = false; } } // Update the desktop if (IsDeskopEnabled) { // If the lock screen and desktop are the same subreddit use the same files. IReadOnlyList <StorageFile> desktopFiles; if (IsLockScreenEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { desktopFiles = lockScreenFiles; } else { desktopFiles = await GetCurrentCacheImages(false); } m_baconMan.TelemetryMan.ReportLog(this, "Current desktop images in cache :" + lockScreenFiles.Count); if (desktopFiles != null && desktopFiles.Count != 0) { // Update the index CurrentDesktopRotationIndex++; if (CurrentDesktopRotationIndex > desktopFiles.Count) { CurrentDesktopRotationIndex = 0; } m_baconMan.TelemetryMan.ReportLog(this, "Current desktop index used to set image :" + CurrentDesktopRotationIndex); // Set the image bool localResult = await SetBackgroundImage(false, desktopFiles[CurrentDesktopRotationIndex]); if (!localResult) { m_baconMan.TelemetryMan.ReportLog(this, "Desktop image update failed", SeverityLevel.Error); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "DesktopImageUpdateFailed"); } else { m_baconMan.TelemetryMan.ReportLog(this, "Desktop image update success"); } // Only set the result if we were already successful if (wasSuccessfull) { wasSuccessfull = localResult; } } else { wasSuccessfull = false; } } } catch (Exception e) { m_baconMan.MessageMan.DebugDia("Failed to set background image", e); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "Failed to set background image", e); wasSuccessfull = false; } // Set that we are done! aEvent.Set(); }); // Wait for the image setting to finish aEvent.WaitOne(); } } catch (Exception e) { m_baconMan.MessageMan.DebugDia("Failed to set background image", e); m_baconMan.TelemetryMan.ReportUnExpectedEvent(this, "Failed to set background image", e); wasSuccessfull = false; } // Stop the running state lock (this) { m_isRunning = false; } return(wasSuccessfull); }
/// <summary> /// Fired when an image request is done. /// </summary> /// <param name="sender"></param> /// <param name="response"></param> private async void OnRequestComplete(object sender, ImageManager.ImageManagerResponseEventArgs response) { // Remove event listener var request = (ImageManager.ImageManagerRequest)sender; request.OnRequestComplete -= OnRequestComplete; var type = (UpdateTypes)response.Request.Context; // Make sure we were successfully. if (response.Success) { try { // Get the target size var targetImageSize = LastKnownScreenResolution; switch (type) { case UpdateTypes.Band when _baconMan.BackgroundMan.BandMan.BandVersion == BandVersions.V1: targetImageSize = new Size(310, 102); break; case UpdateTypes.Band: targetImageSize = new Size(310, 128); break; case UpdateTypes.Desktop when DeviceHelper.CurrentDevice() == DeviceTypes.Mobile: // If we are desktop on mobile we want to do a bit larger than the screen res because // there is a sliding image animation when you switch to all apps. Lets make the width 30% larger. targetImageSize.Width *= 1.3; break; case UpdateTypes.All: break; case UpdateTypes.LockScreen: break; default: throw new ArgumentOutOfRangeException(); } // Resize the image to fit nicely var image = await ResizeImage(response.ImageStream, targetImageSize); // Write the file await WriteImageToFile(image, type); } catch (Exception e) { _baconMan.MessageMan.DebugDia("Failed to write background image", e); TelemetryManager.ReportUnexpectedEvent(this, "Failed to write background image", e); } } // Indicate that this image is done. var isDone = false; lock (_getImageLock) { switch (type) { case UpdateTypes.LockScreen: _lockScreenDoneCount++; isDone = _lockScreenDoneCount >= _lockScreenRequestCount; break; case UpdateTypes.Band: _bandDoneCount++; isDone = _bandDoneCount >= _bandRequestCount; break; case UpdateTypes.All: break; case UpdateTypes.Desktop: break; default: _desktopDoneCount++; isDone = _desktopDoneCount >= _desktopRequestCount; break; } } // if we are done done then tell the images to rotate and clean up. if (!isDone) { return; } switch (type) { // Set this high so we roll over. case UpdateTypes.LockScreen: CurrentLockScreenRotationIndex = 99; break; case UpdateTypes.Band: CurrentBandRotationIndex = 99; break; case UpdateTypes.All: break; case UpdateTypes.Desktop: break; default: CurrentDesktopRotationIndex = 99; break; } // Do the rotate await DoImageRotation(type); // If this is a lock screen update this might also be a desktop update. This happens when the lock screen and desktop // share the same subreddit, they share the same images. if (type == UpdateTypes.LockScreen && IsDesktopEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // Off set the two so they don't show the same image CurrentDesktopRotationIndex = 1; // We also need to update the desktop. await DoImageRotation(UpdateTypes.Desktop); } // We are done, set the last update time LastFullUpdate = DateTime.Now; // And kill the deferral ReleaseDeferral(type); // And set us to stopped. UnSetIsRunningIfDone(); }
/// <summary> /// Rotate a single image type. /// </summary> /// <param name="type"></param> /// <returns></returns> private async Task <bool> DoSingleImageRotation(UpdateTypes type) { // Make sure we should do the update. if ((!IsLockScreenEnabled || type != UpdateTypes.LockScreen) && (!IsDesktopEnabled || type != UpdateTypes.Desktop) && (!IsBandWallpaperEnabled || type != UpdateTypes.Band)) { return(true); } // If the lock screen and desktop are the same subreddit use the same files. var fileCacheType = type; // If this is a desktop update, and lock is enabled, and they are the same subreddit... if (type == UpdateTypes.Desktop && IsLockScreenEnabled && LockScreenSubredditName.Equals(DesktopSubredditName)) { // If they are all the same use the lock screen cache for the desktop. fileCacheType = UpdateTypes.LockScreen; } // Get the current files.. var files = await GetCurrentCacheImages(fileCacheType); TelemetryManager.ReportLog(this, "Current images in cache :" + files.Count); if (files.Count == 0) { return(true); } var currentIndex = 0; switch (type) { case UpdateTypes.LockScreen: { // Update the index CurrentLockScreenRotationIndex++; if (CurrentLockScreenRotationIndex >= files.Count) { CurrentLockScreenRotationIndex = 0; } currentIndex = CurrentLockScreenRotationIndex; break; } case UpdateTypes.Band: { // Update the index CurrentBandRotationIndex++; if (CurrentBandRotationIndex >= files.Count) { CurrentBandRotationIndex = 0; } currentIndex = CurrentBandRotationIndex; break; } case UpdateTypes.All: break; case UpdateTypes.Desktop: break; default: { // Update the index CurrentDesktopRotationIndex++; if (CurrentDesktopRotationIndex >= files.Count) { CurrentDesktopRotationIndex = 0; } currentIndex = CurrentDesktopRotationIndex; break; } } TelemetryManager.ReportLog(this, "Current index used to set image :" + currentIndex); // Set the image var wasSuccess = await SetBackgroundImage(type, files[currentIndex]); if (!wasSuccess) { TelemetryManager.ReportLog(this, "Image update failed", SeverityLevel.Error); TelemetryManager.ReportUnexpectedEvent(this, type == UpdateTypes.LockScreen ? "LockscreenImageUpdateFailed" : "DesktopImageUpdateFailed"); } else { TelemetryManager.ReportLog(this, "Image update success"); } return(wasSuccess); // Return true if we are disabled }