示例#1
0
        public void AddFileToDownload(List <RemoteFileInfo> remoteFileInfoList)
        {
            if (!AppGlobalStateManager.Current.GlobalState.IsMusicCachingEnabled)
            {
                return;
            }
            Execute.ExecuteOnUIThread((Action)(() =>
            {
                foreach (RemoteFileInfo remoteFileInfo1 in remoteFileInfoList)
                {
                    RemoteFileInfo remoteFileInfo = remoteFileInfo1;
                    if (!this._downloadedDict.ContainsKey(remoteFileInfo.UniqueId))
                    {
                        InProgressDownloadInfo progressDownloadInfo1 = this._inProgressDownloads.FirstOrDefault <InProgressDownloadInfo>((Func <InProgressDownloadInfo, bool>)(inPr => inPr.RemoteFile.UniqueId == remoteFileInfo.UniqueId));

                        if (progressDownloadInfo1 != null)
                        {
                            progressDownloadInfo1.RemoteFile.UriStr = remoteFileInfo.UriStr;
                            progressDownloadInfo1.LastDownloadResult = DownloadResult.OK;
                        }
                        else
                        {
                            this._inProgressDownloads.Add(new InProgressDownloadInfo(remoteFileInfo));
                        }
                    }
                }
                this.DownloadNextOneIfNeeded();
            }));
        }
示例#2
0
        private void DownloadNextOneIfNeeded()
        {
            if (!AppGlobalStateManager.Current.GlobalState.IsMusicCachingEnabled || VKClient.Common.Library.NetworkStatusInfo.Instance.NetworkStatus == NetworkStatus.MobileRestricted)
            {
                return;
            }
            Execute.ExecuteOnUIThread((Action)(() =>
            {
                if (this._isDownloading)
                {
                    return;
                }
                Logger.Instance.Info("AudioCacheManager.DownloadNextOneIfNeeded starting: InProgressDownload.Count == " + this._inProgressDownloads.Count);
                InProgressDownloadInfo currentDownload = this._inProgressDownloads.LastOrDefault <InProgressDownloadInfo>((Func <InProgressDownloadInfo, bool>)(ipd => ipd.LastDownloadResult != DownloadResult.UriNoLongerValid));
                if (currentDownload == null)
                {
                    Logger.Instance.Info("AudioCacheManager.DownloadNextOneIfNeeded: no files to download");
                }
                else
                {
                    Logger.Instance.Info("AudioCacheManager.DownloadNextOneIfNeeded: found download " + currentDownload.ToString());
                    this._isDownloading = true;
                    this.PerformChunkDownload(currentDownload, (Action)(() =>
                    {
                        DownloadResult lastDownloadResult = currentDownload.LastDownloadResult;
                        Logger.Instance.Info("AudioCacheManager.DownloadNextOneIfNeeded: download completed with result " + lastDownloadResult);
                        if (lastDownloadResult != DownloadResult.OK)
                        {
                            Execute.ExecuteOnUIThread((Action)(() =>
                            {
                                this._inProgressDownloads.Remove(currentDownload);
                                this._inProgressDownloads.Insert(0, currentDownload);
                            }));
                        }
                        else if (currentDownload.DownloadedToByte == currentDownload.Length - 1L)
                        {
                            Logger.Instance.Info("AudioCacheManager.DownloadNextOneIfNeeded: downloaded the full file!");
                            this.ProcessFullyDownloadedFile(currentDownload);
                            Execute.ExecuteOnUIThread((Action)(() => this._inProgressDownloads.Remove(currentDownload)));
                        }

                        /*
                         * float temp = (float)currentDownload.DownloadedToByte / (float)currentDownload.Length;
                         *
                         * EventAggregator.Current.Publish(new AudioTrackDownloadProgress
                         * {
                         *  Id = currentDownload.RemoteFile.UniqueId,
                         *  Progress = (temp * 100f)
                         * });
                         */
                        this._isDownloading = false;
                        this.DownloadNextOneIfNeeded();
                    }));
                }
            }));
        }
示例#3
0
        private void ProcessFullyDownloadedFile(InProgressDownloadInfo currentDownload)
        {
            List <string> ids = new List <string>();

            ids.Add(currentDownload.RemoteFile.UniqueId);
            Action <BackendResult <List <AudioObj>, ResultCode> > callback = (Action <BackendResult <List <AudioObj>, ResultCode> >)(res =>
            {
                if (res.ResultCode != ResultCode.Succeeded)
                {
                    return;
                }
                this._downloadedDict[currentDownload.RemoteFile.UniqueId] = currentDownload.LocalFileId;
                AudioObj audioObj = res.ResultData[0];
                audioObj.url = currentDownload.LocalFileId;
                this._downloadedList.Add(audioObj);
            });

            AudioService.Instance.GetAudio(ids, callback);
        }
示例#4
0
 private void PerformChunkDownload(InProgressDownloadInfo di, Action resultCallback)
 {
     JsonWebRequest.Download(di.RemoteFile.UriStr, di.DownloadedToByte + 1L, di.DownloadedToByte + (long)this.DEFAULT_CHUNK_SIZE, (Action <HttpStatusCode, long, byte[]>)((statusCode, length, dataBytes) =>
     {
         if (statusCode == HttpStatusCode.NotFound)
         {
             di.LastDownloadResult = DownloadResult.UriNoLongerValid;
         }
         else if (length > 0L && dataBytes != null)
         {
             CacheManager.TrySaveRawCachedData(dataBytes, di.LocalFileId, FileMode.Append);
             di.DownloadedToByte  += (long)dataBytes.Length;
             di.Length             = length;
             di.LastDownloadResult = DownloadResult.OK;
         }
         else
         {
             di.LastDownloadResult = DownloadResult.Failed;
         }
         resultCallback();
     }));
 }