From bbc4e348f18d5a51e3d3afb8413e1782d04be340 Mon Sep 17 00:00:00 2001 From: Pierre-Eric Pelloux-Prayer Date: Tue, 27 Mar 2012 16:43:01 +0200 Subject: [PATCH] Notify the user (once) if during a video call the battery level goes below 10% --- Classes/CallDelegate.h | 3 +- Classes/IncallViewController.m | 48 +++++++++++++++++++++++++++- Classes/LinphoneAppDelegate.m | 2 ++ Classes/LinphoneUI/LinphoneManager.h | 4 +++ Classes/LinphoneUI/LinphoneManager.m | 9 ++++++ 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/Classes/CallDelegate.h b/Classes/CallDelegate.h index d32864706..f09b1fdbf 100644 --- a/Classes/CallDelegate.h +++ b/Classes/CallDelegate.h @@ -24,7 +24,8 @@ enum CallDelegateType { CD_UNDEFINED = 0, CD_NEW_CALL, CD_ZRTP, - CD_VIDEO_UPDATE + CD_VIDEO_UPDATE, + CD_STOP_VIDEO_ON_LOW_BATTERY }; @protocol UIActionSheetCustomDelegate diff --git a/Classes/IncallViewController.m b/Classes/IncallViewController.m index c1a03b788..1b0d2a049 100644 --- a/Classes/IncallViewController.m +++ b/Classes/IncallViewController.m @@ -207,6 +207,38 @@ void addAnimationFadeTransition(UIView* view, float duration) { hideControlsTimer = nil; } +-(void) batteryLevelChanged: (NSNotification*) notif { + LinphoneCall* call = linphone_core_get_current_call([LinphoneManager getLc]); + if (!call || !linphone_call_params_video_enabled(linphone_call_get_current_params(call))) + return; + LinphoneCallAppData* appData = (LinphoneCallAppData*) linphone_call_get_user_pointer(call); + if ([UIDevice currentDevice].batteryState == UIDeviceBatteryStateUnplugged) { + float level = [UIDevice currentDevice].batteryLevel; + ms_message("Video call is running. Battery level: %.2f", level); + if (level < 0.1 && !appData->batteryWarningShown) { + // notify user + CallDelegate* cd = [[CallDelegate alloc] init]; + cd.eventType = CD_STOP_VIDEO_ON_LOW_BATTERY; + cd.delegate = self; + cd.call = call; + + if (visibleActionSheet != nil) { + [visibleActionSheet dismissWithClickedButtonIndex:visibleActionSheet.cancelButtonIndex animated:TRUE]; + } + NSString* title = NSLocalizedString(@"Battery is running low. Stop video ?",nil); + visibleActionSheet = [[UIActionSheet alloc] initWithTitle:title + delegate:cd + cancelButtonTitle:NSLocalizedString(@"Continue video",nil) + destructiveButtonTitle:NSLocalizedString(@"Stop video",nil) + otherButtonTitles:nil]; + + visibleActionSheet.actionSheetStyle = UIActionSheetStyleDefault; + [visibleActionSheet showInView:self.view]; + appData->batteryWarningShown = TRUE; + } + } +} + -(void) enableVideoDisplay { [self orientationChanged:nil]; @@ -233,6 +265,8 @@ void addAnimationFadeTransition(UIView* view, float duration) { NSLog(@"new center: %f %f", videoView.center.x, videoView.center.y); done = true; } + + [self batteryLevelChanged:nil]; } -(void) disableVideoDisplay { @@ -392,7 +426,9 @@ void addAnimationFadeTransition(UIView* view, float duration) { [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; - + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; + + [videoCameraSwitch setPreview:videoPreview]; addVideo.videoUpdateIndicator = videoUpdateIndicator; @@ -976,6 +1012,16 @@ void addAnimationFadeTransition(UIView* view, float duration) { visibleActionSheet = nil; break; } + case CD_STOP_VIDEO_ON_LOW_BATTERY: { + LinphoneCall* call = (LinphoneCall*)datas; + LinphoneCallParams* paramsCopy = linphone_call_params_copy(linphone_call_get_current_params(call)); + if ([visibleActionSheet destructiveButtonIndex] == buttonIndex) { + // stop video + linphone_call_params_enable_video(paramsCopy, FALSE); + linphone_core_update_call([LinphoneManager getLc], call, paramsCopy); + } + break; + } default: ms_error("Unhandled CallDelegate event of type: %d received - ignoring", type); } diff --git a/Classes/LinphoneAppDelegate.m b/Classes/LinphoneAppDelegate.m index c0f8723b6..138693a3d 100644 --- a/Classes/LinphoneAppDelegate.m +++ b/Classes/LinphoneAppDelegate.m @@ -207,6 +207,8 @@ int __aeabi_idiv(int a, int b) { [window makeKeyAndVisible]; [[LinphoneManager instance] setCallDelegate:myPhoneViewController]; + + [UIDevice currentDevice].batteryMonitoringEnabled = YES; } -(void) setupGSMInteraction { diff --git a/Classes/LinphoneUI/LinphoneManager.h b/Classes/LinphoneUI/LinphoneManager.h index 0977ce2ee..701e4bfa4 100644 --- a/Classes/LinphoneUI/LinphoneManager.h +++ b/Classes/LinphoneUI/LinphoneManager.h @@ -42,6 +42,10 @@ struct NetworkReachabilityContext { void (*networkStateChanged) (Connectivity newConnectivity); }; +typedef struct _LinphoneCallAppData { + bool_t batteryWarningShown; +} LinphoneCallAppData; + @interface LinphoneManager : NSObject { @protected diff --git a/Classes/LinphoneUI/LinphoneManager.m b/Classes/LinphoneUI/LinphoneManager.m index f04fac224..af4ca92da 100644 --- a/Classes/LinphoneUI/LinphoneManager.m +++ b/Classes/LinphoneUI/LinphoneManager.m @@ -150,6 +150,12 @@ extern void libmsbcg729_init(); bool canHideInCallView = (linphone_core_get_calls([LinphoneManager getLc]) == NULL); + if (!linphone_call_get_user_pointer(call)) { + LinphoneCallAppData* data = (LinphoneCallAppData*) malloc(sizeof(LinphoneCallAppData)); + data->batteryWarningShown = FALSE; + linphone_call_set_user_pointer(call, data); + } + switch (new_state) { case LinphoneCallIncomingReceived: [callDelegate displayIncomingCall:call @@ -262,6 +268,9 @@ extern void libmsbcg729_init(); [callDelegate displayInCall:call FromUI:mCurrentViewController forUser:lUserName withDisplayName:lDisplayName]; } break; + case LinphoneCallReleased: + free (linphone_call_get_user_pointer(call)); + break; default: break; }