Example #1
0
        /// <summary>
        /// 继续一个下载任务
        /// </summary>
        /// <param name="info"></param>
        protected bool ResumeOneDownload(DownloadInfo info)
        {
            if (string.IsNullOrEmpty(info.RequestId)) return false;

            var transferRequest = BackgroundTransferService.Find(info.RequestId);
            if (transferRequest != null
                && transferRequest.TransferStatus == TransferStatus.Transferring)
            {
                try
                {
                    _isDownloading = true;
                    info.DownloadState = DownloadState.Downloading;
                    HandleDownload(transferRequest, info, false);
                    return true;
                }
                catch (Exception ex)
                {
                    DownloadFailture(info, ex.Message);
                    _isDownloading = false;
                }
            }
            else
            {
                _isDownloading = true;
                info.DownloadState = DownloadState.Downloading;
                transferRequest = new BackgroundTransferRequest(new Uri(info.DownloadUri), new Uri(info.LocalFileName, UriKind.Relative));
                HandleDownload(transferRequest, info, true);
                return true;
            }
            return false;
        }
Example #2
0
        /// <summary>
        /// 开始下载一个新任务,此方法为后台线程调用
        /// </summary>
        /// <param name="info"></param>
        protected void StartOneDownload(DownloadInfo info)
        {
            _isDownloading = true;

            var destinationPath = string.Format("/shared/transfers/{0}", info.LocalFileName);
            var destinationFile = new Uri(destinationPath, UriKind.Relative);
            var transferRequest = new BackgroundTransferRequest(new Uri(info.DownloadUri), destinationFile);
            info.LocalFileName = transferRequest.DownloadLocation.OriginalString;
            HandleDownload(transferRequest, info, true);
        }
Example #3
0
        /// <summary>
        /// 暂停一个下载任务
        /// </summary>
        /// <param name="info"></param>
        protected void PauseOneDownload(DownloadInfo info)
        {
            if (string.IsNullOrEmpty(info.RequestId)) return;

            var transferRequest = BackgroundTransferService.Find(info.RequestId);
            if (transferRequest != null 
                && transferRequest.TransferStatus == TransferStatus.Transferring)
            {
                BackgroundTransferService.Remove(transferRequest);
                transferRequest.Dispose();
                info.DownloadState = DownloadState.Pause;
            }
        }
Example #4
0
 protected abstract void DownloadFailture(DownloadInfo info, string errMessage);
Example #5
0
 protected abstract void DownloadCompleted(DownloadInfo info);
Example #6
0
 protected abstract void DownloadProgress(BackgroundTransferRequest request, DownloadInfo info);
Example #7
0
 protected void CancelDownload(DownloadInfo info)
 {
     if (string.IsNullOrEmpty(info.RequestId)) return;
     
     var transferRequest = BackgroundTransferService.Find(info.RequestId);
     if (transferRequest != null)
     {
         BackgroundTransferService.Remove(transferRequest);
         transferRequest.Dispose();
     }
 }
Example #8
0
 /// <summary>
 /// 暂停一个下载
 /// </summary>
 /// <param name="info"></param>
 public void PauseDownload(DownloadInfo info)
 {
     PauseOneDownload(info);
     ReadyToNextDownload();
 }
Example #9
0
 protected override void DownloadFailture(DownloadInfo info, string errMessage)
 {
     var handler = _dispatcher.BeginInvoke(() =>
     {
         info.DownloadState = DownloadState.DownloadError;
         ReadyToNextDownload();
     });
 }
Example #10
0
 private void ReadyToDownload(DownloadInfo downInfo)
 {
     var result = ResumeOneDownload(downInfo);
     if (!result)
     {
         _isDownloading = true;
         downInfo.DownloadState = DownloadState.Downloading;
         var playInfoFactory = new Play2InfoFactory(true, true);
         playInfoFactory.HttpSucessHandler += (o, e) =>
         {
             downInfo.DownloadUri = playInfoFactory.CreatePlayUri();
             StartOneDownload(downInfo);
             DownloadingViewModel.Instance.SubmitChange();
         };
         playInfoFactory.HttpFailorTimeOut = () =>
         {
             var handler = _dispatcher.BeginInvoke(() =>
             {
                 downInfo.DownloadState = DownloadState.PlayInfoError;
                 ReadyToNextDownload();
             });
         };
         playInfoFactory.DownLoadDatas(downInfo.ChannelId);
     }
 }
Example #11
0
 protected override void DownloadCompleted(DownloadInfo info)
 {
     WAYLocalFactory.Instance.AddDownloadCount();
     try
     {
         using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
         {
             var localFile = string.Format("/PersonalCenter/MyDownload/{0}.mp4", info.Title);
             if (isoStore.FileExists(localFile))
                 isoStore.DeleteFile(localFile);
             isoStore.MoveFile(info.LocalFileName, localFile);
             info.LocalFileName = localFile;
         }
         
     }
     catch
     { }
     var handler = _dispatcher.BeginInvoke(() =>
     {
         info.DownloadState = DownloadState.Downloaded;
         DownloadingViewModel.Instance.DeleteRecord(info);
         DownloadedViewModel.Instance.InsertRecord(info);
         DownloadingChange();
         ReadyToNextDownload();
     });
 }
Example #12
0
 protected override void DownloadProgress(BackgroundTransferRequest request, DownloadInfo info)
 {
     var handler = _dispatcher.BeginInvoke(() =>
     {
         double percent = 0;
         if (request.TotalBytesToReceive > 0)
             percent = request.BytesReceived * 100 / request.TotalBytesToReceive;
         if (request.TotalBytesToReceive != info.TotalBytesReceive)
             info.TotalBytesReceive = request.TotalBytesToReceive;
         info.BytesReceived = request.BytesReceived;
         info.DownloadPercent = percent;
         DownloadingViewModel.Instance.SubmitChange();
     });
 }
Example #13
0
 /// <summary>
 /// 处理一个下载
 /// </summary>
 /// <param name="downInfo"></param>
 public void HandleDownload(DownloadInfo downInfo)
 {
     switch (downInfo.DownloadState)
     {
         case DownloadState.Downloading:
             DownloadViewModel.Instance.PauseDownload(downInfo);
             break;
         case DownloadState.Pause:
         case DownloadState.PlayInfoError:
         case DownloadState.DownloadError:
             DownloadViewModel.Instance.ResumeDownload(downInfo);
             break;
         case DownloadState.Await:
             downInfo.DownloadState = DownloadState.Pause;
             break;
         case DownloadState.WaitingForWiFi:
         case DownloadState.WaitingForExternalPower:
             DownloadViewModel.Instance.RestartOneDonwload(downInfo);
             break;
     }
 }
Example #14
0
        /// <summary>
        /// 重新启动一个下载任务
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        protected bool RestartOneDonwload(DownloadInfo info)
        {
            if (string.IsNullOrEmpty(info.RequestId)) return false;

            var transferRequest = BackgroundTransferService.Find(info.RequestId);
            if (transferRequest != null
                && transferRequest.TransferStatus == TransferStatus.Transferring)
            {
                try
                {
                    _isDownloading = true;
                    info.DownloadState = DownloadState.Downloading;
                    HandleDownload(transferRequest, info, false);
                    return true;
                }
                catch (Exception ex)
                {
                    DownloadFailture(info, ex.Message);
                    _isDownloading = false;
                }
            }
            return false;
        }
Example #15
0
        private void HandleDownload(BackgroundTransferRequest transferRequest, DownloadInfo info, bool isAddQueue)
        {
            transferRequest.TransferStatusChanged += (o, e) =>
            {
                switch(e.Request.TransferStatus)
                {
                    case TransferStatus.Completed:
                        if (e.Request.StatusCode == 200 || e.Request.StatusCode == 206)
                        {
                            DownloadCompleted(info);
                            BackgroundTransferService.Remove(e.Request);
                            e.Request.Dispose();
                        }
                        break;
                    case TransferStatus.WaitingForWiFi:
                    case TransferStatus.WaitingForNonVoiceBlockingNetwork:
                        info.DownloadState = DownloadState.WaitingForWiFi;
                        Utils.TileUtils.CreateBasicToast("需要在WiFi环境下才能下载");
                        break;
                    case TransferStatus.WaitingForExternalPower:
                    case TransferStatus.WaitingForExternalPowerDueToBatterySaverMode:
                        info.DownloadState = DownloadState.WaitingForExternalPower;
                        Utils.TileUtils.CreateBasicToast("需要外接电源才能下载");
                        break;
                    default:
                        break;
                }
            };
            transferRequest.TransferProgressChanged += (o, e) =>
            {
                DownloadProgress(e.Request, info);
            };

            if (isAddQueue)
            {
                transferRequest.Method = "GET";
                info.RequestId = transferRequest.RequestId;

                try
                {
                    if (BackgroundTransferService.Find(info.RequestId) == null)
                        BackgroundTransferService.Add(transferRequest);
                }
                catch (Exception ex)
                {
                    _isDownloading = false;
                    DownloadFailture(info, ex.Message);
                }
            }
        }
Example #16
0
 private void ApplicationBarDownload_Click(object sender, EventArgs e)
 {
     try
     {
         if (PersonalFactory.Instance.Logined)
         {
             var downinfos = new List<DownloadInfo>(_downloadList.Count);
             foreach (var downInfo in _downloadList)
             {
                 var index = ChannelUtils.CreateProgramIndex(downInfo.Index, _dataSource);
                 downInfo.IsSelected = false;
                 downInfo.IsDownloading = true;
                 var down = new DownloadInfo()
                 {
                     TypeId = _dataSource.Type,
                     ParentId = _dataSource.Id,
                     ParentName = _dataSource.Title,
                     ProgramIndex = downInfo.Index,
                     ChannelId = downInfo.ChannelId,
                     ImageUri = _dataSource.ImageUri,
                     DownloadState = DownloadState.Await,
                     Title = ChannelUtils.CreateChannelTitle(index, _dataSource, false)
                 };
                 down.LocalFileName = string.Format("{0}.mp4", down.Title);
                 downinfos.Add(down);
             }
             DownloadViewModel.Instance.AddDownloads(downinfos);
             if (_downloadList.Count > 0)
                 TileUtils.CreateBasicToast("已加入下载,请到个人中心查看");
             _downloadList.Clear();
             _downloadBar.IsEnabled = false;
         }
         else
         {
             TileUtils.CreateBasicToast("请先至个人中心登录");
         }
     }
     catch
     {
         TileUtils.CreateBasicToast("加入下载失败");
     }
 }
Example #17
0
 /// <summary>
 /// 继续一个下载
 /// </summary>
 /// <param name="info"></param>
 public void ResumeDownload(DownloadInfo info)
 {
     if (_isDownloading)
     {
         info.DownloadState = DownloadState.Await;
     }
     else
     {
         ReadyToDownload(info);
     }
 }