From 3da7c99fd83be54ed1ca5b7be271963653368e5c Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 26 Nov 2019 09:42:28 +0100 Subject: [PATCH] MOBILE-2235 h5p: Download in background after play --- .../h5p/components/h5p-player/h5p-player.ts | 29 +++++++++++++++++++ src/providers/filepool.ts | 12 +++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/core/h5p/components/h5p-player/h5p-player.ts b/src/core/h5p/components/h5p-player/h5p-player.ts index 1a1cc7fb1..11a5901fc 100644 --- a/src/core/h5p/components/h5p-player/h5p-player.ts +++ b/src/core/h5p/components/h5p-player/h5p-player.ts @@ -138,6 +138,13 @@ export class CoreH5PPlayerComponent implements OnInit, OnChanges, OnDestroy { }).finally(() => { this.loading = false; this.showPackage = true; + + if (this.canDownload && (this.state == CoreConstants.OUTDATED || this.state == CoreConstants.NOT_DOWNLOADED)) { + // Download the package in background if the size is low. + this.downloadInBg().catch((error) => { + this.logger.error('Error downloading H5P in background', error); + }); + } }); } @@ -169,6 +176,28 @@ export class CoreH5PPlayerComponent implements OnInit, OnChanges, OnDestroy { }); } + /** + * Download the H5P in background if the size is low. + * + * @return Promise resolved when done. + */ + protected downloadInBg(): Promise { + if (this.urlParams && this.src && this.siteCanDownload && this.h5pProvider.canGetTrustedH5PFileInSite() && + this.appProvider.isOnline()) { + + // Get the file size. + return this.pluginFileDelegate.getFileSize({fileurl: this.urlParams.url}, this.siteId).then((size) => { + + if (this.filepoolProvider.shouldDownload(size)) { + // Download the file in background. + this.filepoolProvider.addToQueueByUrl(this.siteId, this.urlParams.url, this.component, this.componentId); + } + }); + } + + return Promise.resolve(); + } + /** * Add the resizer script if it hasn't been added already. */ diff --git a/src/providers/filepool.ts b/src/providers/filepool.ts index 5ad03bead..dca0d1e89 100644 --- a/src/providers/filepool.ts +++ b/src/providers/filepool.ts @@ -772,7 +772,7 @@ export class CoreFilepoolProvider { return this.addToQueueByUrl(siteId, fileUrl, component, componentId, timemodified, undefined, undefined, 0, options, revision, true); } - } else if (size <= this.DOWNLOAD_THRESHOLD || (isWifi && size <= this.WIFI_DOWNLOAD_THRESHOLD)) { + } else if (this.shouldDownload(size)) { return this.addToQueueByUrl(siteId, fileUrl, component, componentId, timemodified, undefined, undefined, 0, options, revision, true); } @@ -2779,6 +2779,16 @@ export class CoreFilepoolProvider { }); } + /** + * Check if a file should be downloaded based on its size. + * + * @param size File size. + * @return Whether file should be downloaded. + */ + shouldDownload(size: number): boolean { + return size <= this.DOWNLOAD_THRESHOLD || (this.appProvider.isWifi() && size <= this.WIFI_DOWNLOAD_THRESHOLD); + } + /** * Convenience function to check if a file should be downloaded before opening it. *