Prechádzať zdrojové kódy

Background Task for HTTP Downloads

- HTTP downloads via UPnP servers or the WiFi Upload feature create an
iOS background task which allows them to continue for a certain
duration in the background.  This prevents short phone calls from
interrupting downloads.

Signed-off-by: Felix Paul Kühne <fkuehne@videolan.org>
Ron Soffer 11 rokov pred
rodič
commit
1de2b08e73

+ 28 - 3
Sources/VLCDownloadViewController.m

@@ -37,6 +37,8 @@
     WRRequestDownload *_FTPDownloadRequest;
     NSTimeInterval _lastStatsUpdate;
     CGFloat _averageSpeed;
+
+    UIBackgroundTaskIdentifier _backgroundTaskIdentifier;
 }
 @end
 
@@ -137,6 +139,8 @@
 #pragma mark - download management
 - (void)_triggerNextDownload
 {
+    BOOL downloadWasStarted = NO;
+
     if ([_currentDownloads count] > 0) {
         [self.activityIndicator startAnimating];
         NSString *downloadScheme = [_currentDownloads[0] scheme];
@@ -155,8 +159,11 @@
                     [_httpDownloader downloadFileFromURL:_currentDownloads[0]];
                     _humanReadableFilename = _httpDownloader.userReadableDownloadName;
                 }
-                    [_currentDownloads removeObjectAtIndex:0];
-                    [_currentDownloadFilename removeObjectAtIndex:0];
+
+                [_currentDownloads removeObjectAtIndex:0];
+                [_currentDownloadFilename removeObjectAtIndex:0];
+
+                downloadWasStarted = YES;
             }
         } else if ([downloadScheme isEqualToString:@"ftp"]) {
             _currentDownloadType = kVLCDownloadViaFTP;
@@ -164,12 +171,30 @@
             _humanReadableFilename = [_currentDownloads[0] lastPathComponent];
             [_currentDownloads removeObjectAtIndex:0];
             [_currentDownloadFilename removeObjectAtIndex:0];
+            downloadWasStarted = YES;
         } else
             APLog(@"Unknown download scheme '%@'", downloadScheme);
 
+
+        if (downloadWasStarted) {
+            if (!_backgroundTaskIdentifier || _backgroundTaskIdentifier == UIBackgroundTaskInvalid) {
+                _backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"VLCDownloader" expirationHandler:^{
+                    APLog(@"Downloads were interrupted after being in background too long, time remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
+                    [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
+                    _backgroundTaskIdentifier = 0;
+                }];
+            }
+        }
+
         [self _updateUI];
-    } else
+    } else {
         _currentDownloadType = 0;
+
+        if (_backgroundTaskIdentifier && _backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
+            [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
+            _backgroundTaskIdentifier = 0;
+        }
+    }
 }
 
 - (IBAction)cancelDownload:(id)sender

+ 22 - 3
Sources/VLCHTTPUploaderController.m

@@ -23,6 +23,9 @@
 #import <arpa/inet.h>
 
 @implementation VLCHTTPUploaderController
+{
+    UIBackgroundTaskIdentifier _backgroundTaskIdentifier;
+}
 
 - (id)init
 {
@@ -39,12 +42,26 @@
 
 - (void)applicationDidBecomeActive: (NSNotification *)notification
 {
-    [self changeHTTPServerState:[[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus]];
+    if (!self.httpServer.isRunning)
+        [self changeHTTPServerState:[[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus]];
+
+    if (_backgroundTaskIdentifier && _backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
+        [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
+        _backgroundTaskIdentifier = 0;
+    }
 }
 
 - (void)applicationDidEnterBackground: (NSNotification *)notification
 {
-    [self changeHTTPServerState:NO];
+    if (self.httpServer.isRunning) {
+        if (!_backgroundTaskIdentifier || _backgroundTaskIdentifier == UIBackgroundTaskInvalid) {
+            _backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"VLCUploader" expirationHandler:^{
+                [self changeHTTPServerState:NO];
+                [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
+                _backgroundTaskIdentifier = 0;
+            }];
+        }
+    }
 }
 
 - (BOOL)changeHTTPServerState:(BOOL)state
@@ -88,8 +105,10 @@
                 return true;
         }
 
-        if (error.code != 0)
+        if (error.code != 0) {
             APLog(@"Error starting HTTP Server: %@", error.localizedDescription);
+            [self.httpServer stop];
+        }
         return false;
     }
     return true;