private BackgroundDownloadOperation(Builder builder)
 {
     this.requestUri  = builder.RequestUri;
     this.accessToken = builder.AccessToken;
     this.downloadLocationOnDevice  = builder.DownloadLocationOnDevice;
     this.backgroundTransferService = builder.BackgroundTransferService;
     this.progress            = builder.Progress;
     this.tcs                 = new TaskCompletionSource <LiveOperationResult>();
     this.status              = OperationStatus.NotStarted;
     this.transferPreferences =
         BackgroundTransferHelper.GetTransferPreferences(builder.BackgroundTransferPreferences);
 }
 private BackgroundUploadOperation(Builder builder)
 {
     this.path = builder.Path;
     this.uploadLocationOnDevice = builder.UploadLocationOnDevice;
     this.client = builder.Client;
     this.backgroundTransferService = builder.BackgroundTransferService;
     this.overwriteOption           = builder.OverwriteOption;
     this.progress = builder.Progress;
     this.tcs      = new TaskCompletionSource <LiveOperationResult>();
     this.requestAddedToService = false;
     this.status = OperationStatus.NotStarted;
     this.transferPreferences =
         BackgroundTransferHelper.GetTransferPreferences(builder.BackgroundTransferPreferences);
 }
Example #3
0
        public void TestCreateBackgroundTransferRequestForUpload()
        {
            const string accessToken      = "accessToken";
            var          requestUri       = new Uri("https://apis.live.net/v5.0/me/skydrive/files");
            var          uploadLocation   = new Uri(@"\shared\transfers\myFile.txt", UriKind.RelativeOrAbsolute);
            var          downloadLocation = new Uri(uploadLocation.OriginalString + ".json", UriKind.RelativeOrAbsolute);
            const TransferPreferences transferPreferences = TransferPreferences.AllowCellularAndBattery;

            var builder = new BackgroundUploadRequestBuilder()
            {
                AccessToken = accessToken,
                DownloadLocationOnDevice = downloadLocation,
                RequestUri             = requestUri,
                UploadLocationOnDevice = uploadLocation,
                TransferPreferences    = transferPreferences
            };

            BackgroundTransferRequest request = builder.Build();

            var expectedRequestUri = new Uri(requestUri.OriginalString + "?method=PUT");

            Assert.AreEqual(expectedRequestUri, request.RequestUri, "request.RequestUri was not set properly.");

            Assert.AreEqual(
                downloadLocation,
                request.DownloadLocation,
                "request.DownloadLocation was not set properly.");

            Assert.AreEqual(
                uploadLocation,
                request.UploadLocation,
                "request.UploadLocation was not set properly.");

            Assert.AreEqual(request.Tag, BackgroundTransferHelper.Tag);

            Assert.AreEqual(
                "bearer " + accessToken,
                request.Headers["Authorization"],
                "Authorization header was not set properly.");

            Assert.AreEqual(
                Platform.GetLibraryHeaderValue(),
                request.Headers["X-HTTP-Live-Library"],
                "Library Header not set properly.");

            Assert.AreEqual(transferPreferences, request.TransferPreferences);
        }
        public void TestCreateBackgroundTransferRequestForDownload()
        {
            var          requestUri       = new Uri("https://apis.live.net/v5.0/foo/bar");
            var          downloadLocation = new Uri(@"\shared\transfers\MyFile.xml", UriKind.RelativeOrAbsolute);
            const string accessToken      = "myAccessToken";
            const TransferPreferences transferPreferences = TransferPreferences.AllowCellularAndBattery;

            var backgroundDownloadRequestBuilder = new BackgroundDownloadRequestBuilder
            {
                RequestUri  = requestUri,
                AccessToken = accessToken,
                DownloadLocationOnDevice = downloadLocation,
                TransferPreferences      = transferPreferences
            };

            BackgroundTransferRequest request = backgroundDownloadRequestBuilder.Build();

            Assert.AreEqual(requestUri, request.RequestUri, "request.RequestUri was not set properly.");

            Assert.AreEqual(
                downloadLocation,
                request.DownloadLocation,
                "request.DownloadLocationOnDevice was not set properly.");

            Assert.AreEqual(request.Tag, BackgroundTransferHelper.Tag);

            Assert.AreEqual(
                "bearer " + accessToken,
                request.Headers["Authorization"],
                "Authorization header was not set properly.");

            Assert.AreEqual(
                Platform.GetLibraryHeaderValue(),
                request.Headers["X-HTTP-Live-Library"],
                "Library Header not set properly.");

            Assert.AreEqual(transferPreferences, request.TransferPreferences);
        }
 private BackgroundDownloadOperation(Builder builder)
 {
     this.requestUri = builder.RequestUri;
     this.accessToken = builder.AccessToken;
     this.downloadLocationOnDevice = builder.DownloadLocationOnDevice;
     this.backgroundTransferService = builder.BackgroundTransferService;
     this.progress = builder.Progress;
     this.tcs = new TaskCompletionSource<LiveOperationResult>();
     this.status = OperationStatus.NotStarted;
     this.transferPreferences =
         BackgroundTransferHelper.GetTransferPreferences(builder.BackgroundTransferPreferences);
 }
        private void startNextEpisodeDownload(TransferPreferences useTransferPreferences = TransferPreferences.AllowCellularAndBattery)
        {
            if (BackgroundTransferService.Requests.Count() > 0)
            {
                // For some reason there are still old requests in the background transfer service.
                // Let's clean everything and start over.
                foreach (BackgroundTransferRequest t in BackgroundTransferService.Requests.AsEnumerable())
                {
                    BackgroundTransferService.Remove(t);
                }
            }

            if (m_episodeDownloadQueue.Count > 0)
            {
                m_currentEpisodeDownload = m_episodeDownloadQueue.Peek();
                Uri downloadUri;
                try
                {
                    downloadUri = new Uri(m_currentEpisodeDownload.EpisodeDownloadUri, UriKind.Absolute);
                }
                catch (Exception e)
                {
                    App.showErrorToast("Cannot download the episode.");
                    Debug.WriteLine("Cannot download the episode. URI exception: " + e.Message);
                    m_currentEpisodeDownload.EpisodeDownloadState = PodcastEpisodeModel.EpisodeDownloadStateEnum.Idle;
                    m_episodeDownloadQueue.Dequeue();
                    saveEpisodeInfoToDB(m_currentEpisodeDownload);
                    startNextEpisodeDownload();
                    return;
                }

                m_currentEpisodeDownload.EpisodeFile = generateLocalEpisodeFileName(m_currentEpisodeDownload);
                if (string.IsNullOrEmpty(m_currentEpisodeDownload.EpisodeFile))
                {
                    App.showErrorToast("Cannot download the episode.");
                    Debug.WriteLine("Cannot download the episode. Episode file name is null or empty.");
                    m_currentEpisodeDownload.EpisodeDownloadState = PodcastEpisodeModel.EpisodeDownloadStateEnum.Idle;
                    m_episodeDownloadQueue.Dequeue();
                    saveEpisodeInfoToDB(m_currentEpisodeDownload);
                    startNextEpisodeDownload();
                    return;
                }

                // Create a new background transfer request for the podcast episode download.
                m_currentBackgroundTransfer = new BackgroundTransferRequest(downloadUri,
                                                                            new Uri(m_currentEpisodeDownload.EpisodeFile, UriKind.Relative));
                if (useTransferPreferences == TransferPreferences.None)
                {
                    m_currentBackgroundTransfer.TransferPreferences = TransferPreferences.None;
                }
                else if (canAllowCellularDownload(m_currentEpisodeDownload))
                {
                    bool settingsAllowCellular = false;
                    using (var db = new PodcastSqlModel())
                    {
                        settingsAllowCellular =  db.settings().IsUseCellularData;
                    }

                    Debug.WriteLine("Settings: Allow cellular download: " + settingsAllowCellular);

                    if (settingsAllowCellular && canDownloadOverCellular())
                    {
                        m_currentBackgroundTransfer.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
                    }
                    else
                    {
                        m_currentBackgroundTransfer.TransferPreferences = TransferPreferences.AllowBattery;
                    }
                } else {
                    m_currentBackgroundTransfer.TransferPreferences = TransferPreferences.None;
                }

                Debug.WriteLine("m_currentBackgroundTransfer.TransferPreferences = " + m_currentBackgroundTransfer.TransferPreferences.ToString());

                m_currentBackgroundTransfer.TransferStatusChanged += new EventHandler<BackgroundTransferEventArgs>(backgroundTransferStatusChanged);

                // Store request to the episode.
                m_currentEpisodeDownload.DownloadRequest = m_currentBackgroundTransfer;

                m_applicationSettings.Remove(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID);
                m_applicationSettings.Add(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID, m_currentEpisodeDownload.EpisodeId);
                m_applicationSettings.Save();

                try
                {
                    BackgroundTransferService.Add(m_currentBackgroundTransfer);
                }
                catch (InvalidOperationException)
                {
                    foreach (BackgroundTransferRequest r in BackgroundTransferService.Requests)
                    {
                        BackgroundTransferService.Remove(r);
                    }

                    BackgroundTransferService.Add(m_currentBackgroundTransfer);
                }
            }
        }
 private BackgroundUploadOperation(Builder builder)
 {
     this.path = builder.Path;
     this.uploadLocationOnDevice = builder.UploadLocationOnDevice;
     this.client = builder.Client;
     this.backgroundTransferService = builder.BackgroundTransferService;
     this.overwriteOption = builder.OverwriteOption;
     this.progress = builder.Progress;
     this.tcs = new TaskCompletionSource<LiveOperationResult>();
     this.requestAddedToService = false;
     this.status = OperationStatus.NotStarted;
     this.transferPreferences =
         BackgroundTransferHelper.GetTransferPreferences(builder.BackgroundTransferPreferences);
 }
Example #8
0
        private void startNextEpisodeDownload(TransferPreferences useTransferPreferences = TransferPreferences.AllowCellularAndBattery)
        {
            if (BackgroundTransferService.Requests.Count() > 0)
            {
                // For some reason there are still old requests in the background transfer service.
                // Let's clean everything and start over.
                foreach (BackgroundTransferRequest t in BackgroundTransferService.Requests.AsEnumerable())
                {
                    BackgroundTransferService.Remove(t);
                }
            }

            if (m_episodeDownloadQueue.Count > 0)
            {
                m_currentEpisodeDownload = m_episodeDownloadQueue.Peek();
                Uri downloadUri;
                try
                {
                    downloadUri = new Uri(m_currentEpisodeDownload.EpisodeDownloadUri, UriKind.Absolute);
                }
                catch (Exception e)
                {
                    App.showErrorToast("Cannot download the episode.");
                    Debug.WriteLine("Cannot download the episode. URI exception: " + e.Message);
                    m_currentEpisodeDownload.EpisodeDownloadState = PodcastEpisodeModel.EpisodeDownloadStateEnum.Idle;
                    m_episodeDownloadQueue.Dequeue();
                    saveEpisodeInfoToDB(m_currentEpisodeDownload);
                    startNextEpisodeDownload();
                    return;
                }

                m_currentEpisodeDownload.EpisodeFile = generateLocalEpisodeFileName(m_currentEpisodeDownload);
                if (string.IsNullOrEmpty(m_currentEpisodeDownload.EpisodeFile))
                {
                    App.showErrorToast("Cannot download the episode.");
                    Debug.WriteLine("Cannot download the episode. Episode file name is null or empty.");
                    m_currentEpisodeDownload.EpisodeDownloadState = PodcastEpisodeModel.EpisodeDownloadStateEnum.Idle;
                    m_episodeDownloadQueue.Dequeue();
                    saveEpisodeInfoToDB(m_currentEpisodeDownload);
                    startNextEpisodeDownload();
                    return;
                }

                // Create a new background transfer request for the podcast episode download.
                m_currentBackgroundTransfer = new BackgroundTransferRequest(downloadUri,
                                                                            new Uri(m_currentEpisodeDownload.EpisodeFile, UriKind.Relative));
                if (useTransferPreferences == TransferPreferences.None)
                {
                    m_currentBackgroundTransfer.TransferPreferences = TransferPreferences.None;
                }
                else if (canAllowCellularDownload(m_currentEpisodeDownload))
                {
                    bool settingsAllowCellular = false;
                    using (var db = new PodcastSqlModel())
                    {
                        settingsAllowCellular = db.settings().IsUseCellularData;
                    }

                    Debug.WriteLine("Settings: Allow cellular download: " + settingsAllowCellular);

                    if (settingsAllowCellular && canDownloadOverCellular())
                    {
                        m_currentBackgroundTransfer.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
                    }
                    else
                    {
                        m_currentBackgroundTransfer.TransferPreferences = TransferPreferences.AllowBattery;
                    }
                }
                else
                {
                    m_currentBackgroundTransfer.TransferPreferences = TransferPreferences.None;
                }

                Debug.WriteLine("m_currentBackgroundTransfer.TransferPreferences = " + m_currentBackgroundTransfer.TransferPreferences.ToString());

                m_currentBackgroundTransfer.TransferStatusChanged += new EventHandler <BackgroundTransferEventArgs>(backgroundTransferStatusChanged);

                // Store request to the episode.
                m_currentEpisodeDownload.DownloadRequest = m_currentBackgroundTransfer;

                m_applicationSettings.Remove(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID);
                m_applicationSettings.Add(App.LSKEY_PODCAST_EPISODE_DOWNLOADING_ID, m_currentEpisodeDownload.EpisodeId);
                m_applicationSettings.Save();

                try
                {
                    BackgroundTransferService.Add(m_currentBackgroundTransfer);
                }
                catch (InvalidOperationException)
                {
                    foreach (BackgroundTransferRequest r in BackgroundTransferService.Requests)
                    {
                        BackgroundTransferService.Remove(r);
                    }

                    BackgroundTransferService.Add(m_currentBackgroundTransfer);
                }
            }
        }