Exemplo n.º 1
0
        /// <summary>
        /// Actually does the update
        /// </summary>
        private async Task KickOffUpdate(bool force, RefCountedDeferral refDeferral)
        {
            TelemetryManager.ReportLog(this, "Updater updating.");
            try
            {
                // Figure out if we need to do a full update
                var fullUpdateFrequencyMin  = UpdateFrequency * CMaxImageCacheCount;
                var timeSinceLastFullUpdate = DateTime.Now - LastFullUpdate;

                if (timeSinceLastFullUpdate.TotalMinutes > fullUpdateFrequencyMin || force)
                {
                    TelemetryManager.ReportLog(this, "Running full update");

                    // Update lock screen images
                    if (IsLockScreenEnabled)
                    {
                        TelemetryManager.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!
                        _lockScreenRefDeferral = refDeferral;
                        _lockScreenRefDeferral.AddRef();

                        // Kick off the update, this will happen async.
                        GetSubredditStories(LockScreenSubredditName, UpdateTypes.LockScreen);
                    }

                    if (IsDesktopEnabled)
                    {
                        TelemetryManager.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))
                        {
                            TelemetryManager.ReportLog(this, "Desktop same sub as lockscreen, skipping image update.", SeverityLevel.Verbose);
                        }
                        else
                        {
                            TelemetryManager.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!
                            _mDesktopRefDeferral = refDeferral;
                            _mDesktopRefDeferral.AddRef();

                            // Kick off the update, this will happen async.
                            GetSubredditStories(DesktopSubredditName, UpdateTypes.Desktop);
                        }
                    }

                    // Update lock screen images
                    if (IsBandWallpaperEnabled)
                    {
                        TelemetryManager.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!
                        _mBandRefDeferral = refDeferral;
                        _mBandRefDeferral.AddRef();

                        // Kick off the update, this will happen async.
                        GetSubredditStories(BandSubredditName, UpdateTypes.Band);
                    }
                }
                // Else full update
                else
                {
                    TelemetryManager.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)
                    {
                        _isRunning = false;
                    }
                }
            }
            catch (Exception e)
            {
                _baconMan.MessageMan.DebugDia("Failed to set background image", e);
                TelemetryManager.ReportUnexpectedEvent(this, "Failed to set background image", e);
            }
        }
Exemplo n.º 2
0
        /// <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
        }