Commit 020c3127 authored by Paul Cartier's avatar Paul Cartier

Move UIApplication calls to bctoolbox

parent 85568973
......@@ -165,7 +165,7 @@ set(BELLE_SIP_SOURCE_FILES_CXX
)
if(APPLE)
set(BELLE_SIP_SOURCE_FILES_OBJC
backgroundtask.m
backgroundtask.mm
)
endif()
......
......@@ -24,79 +24,29 @@
#if TARGET_OS_IPHONE
#include <UIKit/UIApplication.h>
#include "bctoolbox/ios_utils.hh"
unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
__block UIBackgroundTaskIdentifier bgid = UIBackgroundTaskInvalid;
dispatch_block_t block = ^{
UIApplication *app=[UIApplication sharedApplication];
@try {
if (cb==NULL){
belle_sip_error("belle_sip_begin_background_task(): the callback must not be NULL. Application must be aware that the background task needs to be terminated.");
bgid = UIBackgroundTaskInvalid;
@throw([NSException exceptionWithName:@"LinphoneCoreException" reason:@"Background task has no callback" userInfo:nil]);
}
void (^handler)() = ^{
cb(data);
};
if([app respondsToSelector:@selector(beginBackgroundTaskWithName:expirationHandler:)]){
bgid = [app beginBackgroundTaskWithName:[NSString stringWithUTF8String:name] expirationHandler:handler];
} else {
bgid = [app beginBackgroundTaskWithExpirationHandler:handler];
}
if (bgid==UIBackgroundTaskInvalid){
belle_sip_error("Could not start background task %s.", name);
bgid = 0;
@throw([NSException exceptionWithName:@"LinphoneCoreException" reason:@"Could not start background task" userInfo:nil]);
}
// backgroundTimeRemaining is properly set only when running background... but not immediately!
if (app.applicationState != UIApplicationStateBackground || (app.backgroundTimeRemaining == DBL_MAX)) {
belle_sip_message("Background task %s started. Unknown remaining time since application is not fully in background.", name);
} else {
belle_sip_message("Background task %s started. Remaining time %.1f secs", name, app.backgroundTimeRemaining);
}
}
@catch (NSException*) {
// do nothing
}
};
if( [NSThread isMainThread] ) {
block();
}
else {
dispatch_sync(dispatch_get_main_queue(), block);
}
using namespace bctoolbox;
return (unsigned long)bgid;
unsigned long belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
auto &iOSUtils = IOSUtils::getUtils();
std::function<void()> callback;
if (cb) {
callback = std::bind(cb, data);
}
return iOSUtils.beginBackgroundTask(name, callback);
}
void belle_sip_end_background_task(unsigned long id){
dispatch_block_t block = ^{
UIApplication *app=[UIApplication sharedApplication];
if (id != UIBackgroundTaskInvalid){
[app endBackgroundTask:(UIBackgroundTaskIdentifier)id];
}
};
if( [NSThread isMainThread] ) {
block();
}
else {
dispatch_sync(dispatch_get_main_queue(), block);
}
auto &iOSUtils = IOSUtils::getUtils();
iOSUtils.endBackgroundTask(id);
}
#else
/*mac*/
@import Foundation;
#import <Foundation/Foundation.h>
static unsigned long dummy_id=0;
static id activity_id=0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment