Don't ask for camera permissions if app is not active.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent 5e61307ce3
commit 5cc292fb6c

@ -4,8 +4,8 @@
#import "AttachmentSharing.h" #import "AttachmentSharing.h"
#import "TSAttachmentStream.h" #import "TSAttachmentStream.h"
#import "Threading.h"
#import "UIUtil.h" #import "UIUtil.h"
#import <SignalServiceKit/Threading.h>
@implementation AttachmentSharing @implementation AttachmentSharing

@ -2473,7 +2473,10 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
- (void)takePictureOrVideo - (void)takePictureOrVideo
{ {
[self ows_askForCameraPermissions:^{ [self ows_askForCameraPermissions:^(BOOL granted) {
if (!granted) {
return;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init]; UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = @[ (__bridge NSString *)kUTTypeImage, (__bridge NSString *)kUTTypeMovie ]; picker.mediaTypes = @[ (__bridge NSString *)kUTTypeImage, (__bridge NSString *)kUTTypeMovie ];
@ -3060,33 +3063,13 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) {
return; 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) { if (granted) {
[strongSelf startRecordingVoiceMemo]; [strongSelf startRecordingVoiceMemo];
} else { } else {
DDLogInfo(@"%@ we do not have recording permission.", self.tag); DDLogInfo(@"%@ we do not have recording permission.", self.logTag);
[strongSelf cancelVoiceMemo]; [strongSelf cancelVoiceMemo];
[OWSAlerts showNoMicrophonePermissionAlert]; [OWSAlerts showNoMicrophonePermissionAlert];
} }
>>>>>>> Don't ask for microphone permissions if app is not active.
}]; }];
} }

@ -118,19 +118,19 @@ NS_ASSUME_NONNULL_BEGIN
{ {
[super viewDidAppear:animated]; [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. DDLogInfo(@"%@ Showing Scanner", self.logTag);
// Also, it's less obvious whats being "shared" at this point,
// so just disable sharing when in capture mode.
DDLogInfo(@"%@ Showing Scanner", self.logTag); [self.qrScanningController startCapture];
} else {
[self.qrScanningController startCapture];
}
failureCallback:^{
[self.navigationController popViewControllerAnimated:YES]; [self.navigationController popViewControllerAnimated:YES];
}]; }
}];
} }
#pragma mark - OWSQRScannerDelegate #pragma mark - OWSQRScannerDelegate

@ -256,7 +256,10 @@ int const OWSLinkedDevicesTableViewControllerSectionAddDevice = 1;
if (indexPath.section == OWSLinkedDevicesTableViewControllerSectionAddDevice) if (indexPath.section == OWSLinkedDevicesTableViewControllerSectionAddDevice)
{ {
[self ows_askForCameraPermissions:^{ [self ows_askForCameraPermissions:^(BOOL granted) {
if (!granted) {
return;
}
[self performSegueWithIdentifier:@"LinkDeviceSegue" sender:self]; [self performSegueWithIdentifier:@"LinkDeviceSegue" sender:self];
}]; }];
} }

@ -1108,16 +1108,15 @@ protocol CallServiceObserver: class {
return return
} }
frontmostViewController.ows_ask(forCameraPermissions: { [weak self] in frontmostViewController.ows_ask(forCameraPermissions: { [weak self] granted in
// Success callback; camera permissions are granted.
guard let strongSelf = self else { guard let strongSelf = self else {
return return
} }
strongSelf.setHasLocalVideoWithCameraPermissions(hasLocalVideo: hasLocalVideo) if (granted) {
// Success callback; camera permissions are granted.
}, failureCallback: { strongSelf.setHasLocalVideoWithCameraPermissions(hasLocalVideo: hasLocalVideo)
} else {
// Failed callback; camera permissions are _NOT_ granted. // Failed callback; camera permissions are _NOT_ granted.
// We don't need to worry about the user granting or remoting this permission // 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. // permission kills the app.
OWSAlerts.showAlert(withTitle: NSLocalizedString("MISSING_CAMERA_PERMISSION_TITLE", comment: "Alert title when camera is not authorized"), 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")) message: NSLocalizedString("MISSING_CAMERA_PERMISSION_MESSAGE", comment: "Alert body when camera is not authorized"))
}
}) })
} }

@ -8,8 +8,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (Permissions) @interface UIViewController (Permissions)
- (void)ows_askForCameraPermissions:(void (^)())successCallback; - (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callback;
- (void)ows_askForCameraPermissions:(void (^)())successCallback failureCallback:(nullable void (^)())failureCallback;
- (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callback; - (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callback;

@ -6,35 +6,32 @@
#import "UIUtil.h" #import "UIUtil.h"
#import "UIViewController+Permissions.h" #import "UIViewController+Permissions.h"
#import <AVFoundation/AVFoundation.h> #import <AVFoundation/AVFoundation.h>
#import <SignalServiceKit/Threading.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@implementation UIViewController (Permissions) @implementation UIViewController (Permissions)
- (void)ows_askForCameraPermissions:(void (^)(void))successCallback - (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callbackParam
{
[self ows_askForCameraPermissions:successCallback failureCallback:nil];
}
- (void)ows_askForCameraPermissions:(void (^)(void))successCallback failureCallback:(nullable void (^)(void))failureCallback
{ {
DDLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class)); DDLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class));
// Avoid nil tests below. // Ensure callback is invoked on main thread.
if (!failureCallback) { void (^callback)(BOOL) = ^(BOOL granted) {
failureCallback = ^{ DispatchMainThreadSafe(^{
}; callbackParam(granted);
} });
};
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
DDLogError(@"Skipping camera permissions request when app is not active."); DDLogError(@"Skipping camera permissions request when app is not active.");
failureCallback(); callback(NO);
return; return;
} }
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
DDLogError(@"Camera ImagePicker source not available"); DDLogError(@"Camera ImagePicker source not available");
failureCallback(); callback(NO);
return; return;
} }
@ -52,34 +49,28 @@ NS_ASSUME_NONNULL_BEGIN
style:UIAlertActionStyleDefault style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) { handler:^(UIAlertAction *_Nonnull action) {
[[UIApplication sharedApplication] openSystemSettings]; [[UIApplication sharedApplication] openSystemSettings];
failureCallback(); callback(NO);
}]; }];
[alert addAction:openSettingsAction]; [alert addAction:openSettingsAction];
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
style:UIAlertActionStyleCancel style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) { handler:^(UIAlertAction *action) {
failureCallback(); callback(NO);
}]; }];
[alert addAction:dismissAction]; [alert addAction:dismissAction];
[self presentViewController:alert animated:YES completion:nil]; [self presentViewController:alert animated:YES completion:nil];
} else if (status == AVAuthorizationStatusAuthorized) { } else if (status == AVAuthorizationStatusAuthorized) {
successCallback(); callback(YES);
} else if (status == AVAuthorizationStatusNotDetermined) { } else if (status == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
completionHandler:^(BOOL granted) { completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{ callback(granted);
if (granted) {
successCallback();
} else {
failureCallback();
}
});
}]; }];
} else { } else {
DDLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status); DDLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
failureCallback(); callback(NO);
} }
} }
@ -89,7 +80,7 @@ NS_ASSUME_NONNULL_BEGIN
// Ensure callback is invoked on main thread. // Ensure callback is invoked on main thread.
void (^callback)(BOOL) = ^(BOOL granted) { void (^callback)(BOOL) = ^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(), ^{ DispatchMainThreadSafe(^{
callbackParam(granted); callbackParam(granted);
}); });
}; };

Loading…
Cancel
Save