From 5cc292fb6c17f007919ae3f7c6a2a8c0dddf4c39 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 7 Nov 2017 10:38:36 -0500 Subject: [PATCH] Don't ask for camera permissions if app is not active. // FREEBIE --- .../src/ViewControllers/AttachmentSharing.m | 2 +- .../ConversationViewController.m | 27 +++--------- .../FingerprintViewScanController.m | 20 ++++----- .../OWSLinkedDevicesTableViewController.m | 5 ++- Signal/src/call/CallService.swift | 12 +++--- .../src/util/UIViewController+Permissions.h | 3 +- .../src/util/UIViewController+Permissions.m | 41 ++++++++----------- 7 files changed, 43 insertions(+), 67 deletions(-) diff --git a/Signal/src/ViewControllers/AttachmentSharing.m b/Signal/src/ViewControllers/AttachmentSharing.m index cf4f3f458..824f4f558 100644 --- a/Signal/src/ViewControllers/AttachmentSharing.m +++ b/Signal/src/ViewControllers/AttachmentSharing.m @@ -4,8 +4,8 @@ #import "AttachmentSharing.h" #import "TSAttachmentStream.h" -#import "Threading.h" #import "UIUtil.h" +#import @implementation AttachmentSharing diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index ec34e7997..4cf4b489f 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -2473,7 +2473,10 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { - (void)takePictureOrVideo { - [self ows_askForCameraPermissions:^{ + [self ows_askForCameraPermissions:^(BOOL granted) { + if (!granted) { + return; + } UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.mediaTypes = @[ (__bridge NSString *)kUTTypeImage, (__bridge NSString *)kUTTypeMovie ]; @@ -3060,33 +3063,13 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { return; } -<<<<<<< HEAD - if (granted) { - [strongSelf startRecordingVoiceMemo]; - } else { - DDLogInfo(@"%@ we do not have recording permission.", self.logTag); - [strongSelf cancelVoiceMemo]; - [OWSAlerts showNoMicrophonePermissionAlert]; - } - }); -||||||| merged common ancestors - if (granted) { - [strongSelf startRecordingVoiceMemo]; - } else { - DDLogInfo(@"%@ we do not have recording permission.", self.tag); - [strongSelf cancelVoiceMemo]; - [OWSAlerts showNoMicrophonePermissionAlert]; - } - }); -======= if (granted) { [strongSelf startRecordingVoiceMemo]; } else { - DDLogInfo(@"%@ we do not have recording permission.", self.tag); + DDLogInfo(@"%@ we do not have recording permission.", self.logTag); [strongSelf cancelVoiceMemo]; [OWSAlerts showNoMicrophonePermissionAlert]; } ->>>>>>> Don't ask for microphone permissions if app is not active. }]; } diff --git a/Signal/src/ViewControllers/FingerprintViewScanController.m b/Signal/src/ViewControllers/FingerprintViewScanController.m index 0436d7815..64ad54225 100644 --- a/Signal/src/ViewControllers/FingerprintViewScanController.m +++ b/Signal/src/ViewControllers/FingerprintViewScanController.m @@ -118,19 +118,19 @@ NS_ASSUME_NONNULL_BEGIN { [super viewDidAppear:animated]; - [self ows_askForCameraPermissions:^{ + [self ows_askForCameraPermissions:^(BOOL granted) { + if (granted) { + // Camera stops capturing when "sharing" while in capture mode. + // Also, it's less obvious whats being "shared" at this point, + // so just disable sharing when in capture mode. - // Camera stops capturing when "sharing" while in capture mode. - // Also, it's less obvious whats being "shared" at this point, - // so just disable sharing when in capture mode. + DDLogInfo(@"%@ Showing Scanner", self.logTag); - DDLogInfo(@"%@ Showing Scanner", self.logTag); - - [self.qrScanningController startCapture]; - } - failureCallback:^{ + [self.qrScanningController startCapture]; + } else { [self.navigationController popViewControllerAnimated:YES]; - }]; + } + }]; } #pragma mark - OWSQRScannerDelegate diff --git a/Signal/src/ViewControllers/OWSLinkedDevicesTableViewController.m b/Signal/src/ViewControllers/OWSLinkedDevicesTableViewController.m index 5ca3ed0fa..ebd4490e5 100644 --- a/Signal/src/ViewControllers/OWSLinkedDevicesTableViewController.m +++ b/Signal/src/ViewControllers/OWSLinkedDevicesTableViewController.m @@ -256,7 +256,10 @@ int const OWSLinkedDevicesTableViewControllerSectionAddDevice = 1; if (indexPath.section == OWSLinkedDevicesTableViewControllerSectionAddDevice) { - [self ows_askForCameraPermissions:^{ + [self ows_askForCameraPermissions:^(BOOL granted) { + if (!granted) { + return; + } [self performSegueWithIdentifier:@"LinkDeviceSegue" sender:self]; }]; } diff --git a/Signal/src/call/CallService.swift b/Signal/src/call/CallService.swift index 400556902..dfb04b02c 100644 --- a/Signal/src/call/CallService.swift +++ b/Signal/src/call/CallService.swift @@ -1108,16 +1108,15 @@ protocol CallServiceObserver: class { return } - frontmostViewController.ows_ask(forCameraPermissions: { [weak self] in - // Success callback; camera permissions are granted. - + frontmostViewController.ows_ask(forCameraPermissions: { [weak self] granted in guard let strongSelf = self else { return } - strongSelf.setHasLocalVideoWithCameraPermissions(hasLocalVideo: hasLocalVideo) - - }, failureCallback: { + if (granted) { + // Success callback; camera permissions are granted. + strongSelf.setHasLocalVideoWithCameraPermissions(hasLocalVideo: hasLocalVideo) + } else { // Failed callback; camera permissions are _NOT_ granted. // We don't need to worry about the user granting or remoting this permission @@ -1125,6 +1124,7 @@ protocol CallServiceObserver: class { // permission kills the app. OWSAlerts.showAlert(withTitle: NSLocalizedString("MISSING_CAMERA_PERMISSION_TITLE", comment: "Alert title when camera is not authorized"), message: NSLocalizedString("MISSING_CAMERA_PERMISSION_MESSAGE", comment: "Alert body when camera is not authorized")) + } }) } diff --git a/Signal/src/util/UIViewController+Permissions.h b/Signal/src/util/UIViewController+Permissions.h index 4d4387896..7919ad045 100644 --- a/Signal/src/util/UIViewController+Permissions.h +++ b/Signal/src/util/UIViewController+Permissions.h @@ -8,8 +8,7 @@ NS_ASSUME_NONNULL_BEGIN @interface UIViewController (Permissions) -- (void)ows_askForCameraPermissions:(void (^)())successCallback; -- (void)ows_askForCameraPermissions:(void (^)())successCallback failureCallback:(nullable void (^)())failureCallback; +- (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callback; - (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callback; diff --git a/Signal/src/util/UIViewController+Permissions.m b/Signal/src/util/UIViewController+Permissions.m index c27601563..ab4f22e6c 100644 --- a/Signal/src/util/UIViewController+Permissions.m +++ b/Signal/src/util/UIViewController+Permissions.m @@ -6,35 +6,32 @@ #import "UIUtil.h" #import "UIViewController+Permissions.h" #import +#import NS_ASSUME_NONNULL_BEGIN @implementation UIViewController (Permissions) -- (void)ows_askForCameraPermissions:(void (^)(void))successCallback -{ - [self ows_askForCameraPermissions:successCallback failureCallback:nil]; -} - -- (void)ows_askForCameraPermissions:(void (^)(void))successCallback failureCallback:(nullable void (^)(void))failureCallback +- (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callbackParam { DDLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class)); - // Avoid nil tests below. - if (!failureCallback) { - failureCallback = ^{ - }; - } + // Ensure callback is invoked on main thread. + void (^callback)(BOOL) = ^(BOOL granted) { + DispatchMainThreadSafe(^{ + callbackParam(granted); + }); + }; if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { DDLogError(@"Skipping camera permissions request when app is not active."); - failureCallback(); + callback(NO); return; } if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { DDLogError(@"Camera ImagePicker source not available"); - failureCallback(); + callback(NO); return; } @@ -52,34 +49,28 @@ NS_ASSUME_NONNULL_BEGIN style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) { [[UIApplication sharedApplication] openSystemSettings]; - failureCallback(); + callback(NO); }]; [alert addAction:openSettingsAction]; UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { - failureCallback(); + callback(NO); }]; [alert addAction:dismissAction]; [self presentViewController:alert animated:YES completion:nil]; } else if (status == AVAuthorizationStatusAuthorized) { - successCallback(); + callback(YES); } else if (status == AVAuthorizationStatusNotDetermined) { [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { - dispatch_async(dispatch_get_main_queue(), ^{ - if (granted) { - successCallback(); - } else { - failureCallback(); - } - }); + callback(granted); }]; } else { DDLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status); - failureCallback(); + callback(NO); } } @@ -89,7 +80,7 @@ NS_ASSUME_NONNULL_BEGIN // Ensure callback is invoked on main thread. void (^callback)(BOOL) = ^(BOOL granted) { - dispatch_async(dispatch_get_main_queue(), ^{ + DispatchMainThreadSafe(^{ callbackParam(granted); }); };