/** * Move the downloaded file to it's destination */ public bool MoveDownloadedFile(DownloadFileImplementation file, NSUrl location, string destinationPathName) { var fileManager = NSFileManager.DefaultManager; NSError removeCopy; NSError errorCopy; bool success = false; string nameFile = destinationPathName.Split('/').Last(); var URLs = fileManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User); NSUrl documentsDictionry = URLs.First(); NSUrl destinationURL = documentsDictionry.Append(nameFile, false); try { fileManager.Remove(destinationURL, out removeCopy); success = fileManager.Copy(location, destinationURL, out errorCopy); if (!success) { file.StatusDetails = errorCopy.LocalizedDescription; file.Status = DownloadFileStatus.FAILED; } else { file.DestinationPathName = destinationURL.Path; } } catch (Exception ex) { System.Diagnostics.Debugger.Break(); } return(success); }
public void Abort(DownloadFileImplementation file) { file.Status = DownloadFileStatus.CANCELED; file.Task?.Cancel(); RemoveFile(file); }
private bool IsDownloading(DownloadFileImplementation downloadFile) { if (downloadFile == null) { return(false); } switch (downloadFile.Status) { case DownloadFileStatus.INITIALIZED: case DownloadFileStatus.PAUSED: case DownloadFileStatus.PENDING: case DownloadFileStatus.RUNNING: return(true); case DownloadFileStatus.COMPLETED: case DownloadFileStatus.CANCELED: case DownloadFileStatus.FAILED: case DownloadFileStatus.NONE: return(false); default: throw new ArgumentOutOfRangeException(); } }
/** * Update the properties for a file by it's cursor. * This method should be called in an interval and on reinitialization. */ public void UpdateFileProperties(ICursor cursor, DownloadFileImplementation downloadFile) { downloadFile.TotalBytesWritten = cursor.GetFloat(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnBytesDownloadedSoFar)); downloadFile.TotalBytesExpected = cursor.GetFloat(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnTotalSizeBytes)); string reason = cursor.GetString(cursor.GetColumnIndex("reason")); switch ((DownloadStatus)cursor.GetInt(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnStatus))) { case DownloadStatus.Successful: string title = cursor.GetString(cursor.GetColumnIndex("title")); string pathFile = cursor.GetString(cursor.GetColumnIndex("local_uri")); string type = cursor.GetString(cursor.GetColumnIndex("media_type")); string path = pathFile.Replace("file://", ""); downloadFile.DestinationPathName = path; downloadFile.MimeType = type; downloadFile.StatusDetails = default(string); downloadFile.Status = DownloadFileStatus.COMPLETED; IDictionary <string, string> dict = new Dictionary <string, string>(); dict.Add(DownloadFile_NotifyKey, path); dict.Add("type", type); RemoveFile(downloadFile); //CrossLocalNotifications_Droid.Current.PushNotify( // downloadFile.NameFile, // "Tải tập tin thành công", // new Action<NotificationResult>(result => { // if (result != null && result.Action == NotificationAction.Clicked) // { // IDocumentViewer documentViewer = DependencyService.Get<IDocumentViewer>(); // documentViewer.OpenFileDownloaded(path, type); // } // }), // dict); break; case DownloadStatus.Failed: var reasonFailed = cursor.GetInt(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnReason)); if (reasonFailed < 600) { downloadFile.StatusDetails = "Error.HttpCode: " + reasonFailed; } else { switch ((DownloadError)reasonFailed) { case DownloadError.CannotResume: downloadFile.StatusDetails = "Error.CannotResume"; break; case DownloadError.DeviceNotFound: downloadFile.StatusDetails = "Error.DeviceNotFound"; break; case DownloadError.FileAlreadyExists: downloadFile.StatusDetails = "Error.FileAlreadyExists"; break; case DownloadError.FileError: downloadFile.StatusDetails = "Error.FileError"; break; case DownloadError.HttpDataError: downloadFile.StatusDetails = "Error.HttpDataError"; break; case DownloadError.InsufficientSpace: downloadFile.StatusDetails = "Error.InsufficientSpace"; break; case DownloadError.TooManyRedirects: downloadFile.StatusDetails = "Error.TooManyRedirects"; break; case DownloadError.UnhandledHttpCode: downloadFile.StatusDetails = "Error.UnhandledHttpCode"; break; case DownloadError.Unknown: downloadFile.StatusDetails = "Error.Unknown"; break; default: downloadFile.StatusDetails = "Error.Unregistered: " + reasonFailed; break; } } downloadFile.Status = DownloadFileStatus.FAILED; RemoveFile(downloadFile); //CrossLocalNotifications_Droid.Current.PushNotify(downloadFile.NameFile, "Tải tập tin không thành công", null); break; case DownloadStatus.Paused: var reasonPaused = cursor.GetInt(cursor.GetColumnIndex(Android.App.DownloadManager.ColumnReason)); switch ((DownloadPausedReason)reasonPaused) { case DownloadPausedReason.QueuedForWifi: downloadFile.StatusDetails = "Paused.QueuedForWifi"; break; case DownloadPausedReason.WaitingToRetry: downloadFile.StatusDetails = "Paused.WaitingToRetry"; break; case DownloadPausedReason.WaitingForNetwork: downloadFile.StatusDetails = "Paused.WaitingForNetwork"; break; case DownloadPausedReason.Unknown: downloadFile.StatusDetails = "Paused.Unknown"; break; default: downloadFile.StatusDetails = "Paused.Unregistered: " + reasonPaused; break; } downloadFile.Status = DownloadFileStatus.PAUSED; break; case DownloadStatus.Pending: downloadFile.StatusDetails = default(string); downloadFile.Status = DownloadFileStatus.PENDING; break; case DownloadStatus.Running: downloadFile.StatusDetails = default(string); downloadFile.Status = DownloadFileStatus.RUNNING; break; } }
public void Abort(DownloadFileImplementation file) { file.Status = DownloadFileStatus.CANCELED; _downloadManager.Remove(file.Id); RemoveFile(file); }