2016-11-04 23:41:37 +01:00
|
|
|
//
|
2019-03-21 15:55:04 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2016-11-04 23:41:37 +01:00
|
|
|
//
|
2017-05-05 18:39:21 +02:00
|
|
|
|
2017-11-07 16:32:28 +01:00
|
|
|
#import "UIViewController+Permissions.h"
|
2019-05-02 23:58:48 +02:00
|
|
|
#import "Session-Swift.h"
|
2017-11-08 20:04:51 +01:00
|
|
|
#import <AVFoundation/AVFoundation.h>
|
2018-01-17 21:41:08 +01:00
|
|
|
#import <Photos/Photos.h>
|
2020-11-12 00:41:45 +01:00
|
|
|
#import <SignalCoreKit/Threading.h>
|
2016-11-04 23:41:37 +01:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-11-07 16:32:28 +01:00
|
|
|
@implementation UIViewController (Permissions)
|
2016-11-04 23:41:37 +01:00
|
|
|
|
2017-11-07 16:38:36 +01:00
|
|
|
- (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callbackParam
|
2017-06-09 00:10:02 +02:00
|
|
|
{
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class));
|
2017-11-07 16:04:58 +01:00
|
|
|
|
2017-11-07 16:38:36 +01:00
|
|
|
// Ensure callback is invoked on main thread.
|
|
|
|
void (^callback)(BOOL) = ^(BOOL granted) {
|
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
callbackParam(granted);
|
|
|
|
});
|
|
|
|
};
|
2017-06-09 00:10:02 +02:00
|
|
|
|
2017-11-07 16:46:03 +01:00
|
|
|
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(@"Skipping camera permissions request when app is in background.");
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2017-11-07 16:04:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-04 23:41:37 +01:00
|
|
|
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(@"Camera ImagePicker source not available");
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2016-11-04 23:41:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-06-09 00:10:02 +02:00
|
|
|
|
2016-11-04 23:41:37 +01:00
|
|
|
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
|
|
|
|
if (status == AVAuthorizationStatusDenied) {
|
2017-11-07 16:32:28 +01:00
|
|
|
UIAlertController *alert = [UIAlertController
|
2020-01-09 23:52:30 +01:00
|
|
|
alertControllerWithTitle:NSLocalizedString(@"Session needs camera access to scan QR codes.", @"")
|
2019-08-23 07:12:08 +02:00
|
|
|
message:NSLocalizedString(@"You can enable camera access in your device settings.", @"")
|
2017-11-07 16:32:28 +01:00
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
|
|
|
UIAlertAction *openSettingsAction =
|
2018-01-17 21:41:08 +01:00
|
|
|
[UIAlertAction actionWithTitle:CommonStrings.openSettingsButton
|
2017-11-07 16:32:28 +01:00
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
[[UIApplication sharedApplication] openSystemSettings];
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2017-11-07 16:32:28 +01:00
|
|
|
}];
|
2016-11-08 22:28:00 +01:00
|
|
|
[alert addAction:openSettingsAction];
|
|
|
|
|
2017-07-13 20:53:24 +02:00
|
|
|
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
|
2016-11-08 22:28:00 +01:00
|
|
|
style:UIAlertActionStyleCancel
|
2017-06-09 00:10:02 +02:00
|
|
|
handler:^(UIAlertAction *action) {
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2017-06-09 00:10:02 +02:00
|
|
|
}];
|
2016-11-08 22:28:00 +01:00
|
|
|
[alert addAction:dismissAction];
|
|
|
|
|
2019-03-21 15:55:04 +01:00
|
|
|
[self presentAlert:alert];
|
2016-11-04 23:41:37 +01:00
|
|
|
} else if (status == AVAuthorizationStatusAuthorized) {
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(YES);
|
2016-11-04 23:41:37 +01:00
|
|
|
} else if (status == AVAuthorizationStatusNotDetermined) {
|
2017-06-09 00:10:02 +02:00
|
|
|
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
|
2017-11-09 09:14:58 +01:00
|
|
|
completionHandler:callback];
|
2016-11-04 23:41:37 +01:00
|
|
|
} else {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status);
|
2017-11-07 16:38:36 +01:00
|
|
|
callback(NO);
|
2016-11-04 23:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 21:41:08 +01:00
|
|
|
- (void)ows_askForMediaLibraryPermissions:(void (^)(BOOL granted))callbackParam
|
|
|
|
{
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogVerbose(@"[%@] ows_askForMediaLibraryPermissions", NSStringFromClass(self.class));
|
2018-01-17 21:41:08 +01:00
|
|
|
|
|
|
|
// Ensure callback is invoked on main thread.
|
|
|
|
void (^completionCallback)(BOOL) = ^(BOOL granted) {
|
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
callbackParam(granted);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
void (^presentSettingsDialog)(void) = ^(void) {
|
2018-02-27 14:47:14 +01:00
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
UIAlertController *alert = [UIAlertController
|
|
|
|
alertControllerWithTitle:NSLocalizedString(@"MISSING_MEDIA_LIBRARY_PERMISSION_TITLE",
|
|
|
|
@"Alert title when user has previously denied media library access")
|
|
|
|
message:NSLocalizedString(@"MISSING_MEDIA_LIBRARY_PERMISSION_MESSAGE",
|
|
|
|
@"Alert body when user has previously denied media library access")
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
|
|
|
UIAlertAction *openSettingsAction =
|
|
|
|
[UIAlertAction actionWithTitle:CommonStrings.openSettingsButton
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
handler:^(UIAlertAction *_Nonnull action) {
|
|
|
|
[[UIApplication sharedApplication] openSystemSettings];
|
|
|
|
completionCallback(NO);
|
|
|
|
}];
|
|
|
|
[alert addAction:openSettingsAction];
|
|
|
|
|
|
|
|
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton
|
|
|
|
style:UIAlertActionStyleCancel
|
|
|
|
handler:^(UIAlertAction *action) {
|
|
|
|
completionCallback(NO);
|
|
|
|
}];
|
|
|
|
[alert addAction:dismissAction];
|
|
|
|
|
2019-03-21 15:55:04 +01:00
|
|
|
[self presentAlert:alert];
|
2018-02-27 14:47:14 +01:00
|
|
|
});
|
2018-01-17 21:41:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(@"Skipping media library permissions request when app is in background.");
|
2018-01-17 21:41:08 +01:00
|
|
|
completionCallback(NO);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"PhotoLibrary ImagePicker source not available");
|
2018-01-17 21:41:08 +01:00
|
|
|
completionCallback(NO);
|
|
|
|
}
|
|
|
|
|
|
|
|
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case PHAuthorizationStatusAuthorized: {
|
|
|
|
completionCallback(YES);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case PHAuthorizationStatusDenied: {
|
|
|
|
presentSettingsDialog();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case PHAuthorizationStatusNotDetermined: {
|
|
|
|
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus newStatus) {
|
|
|
|
if (newStatus == PHAuthorizationStatusAuthorized) {
|
|
|
|
completionCallback(YES);
|
|
|
|
} else {
|
|
|
|
presentSettingsDialog();
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case PHAuthorizationStatusRestricted: {
|
|
|
|
// when does this happen?
|
2018-08-27 16:29:51 +02:00
|
|
|
OWSFailDebug(@"PHAuthorizationStatusRestricted");
|
2018-01-17 21:41:08 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 16:32:28 +01:00
|
|
|
- (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callbackParam
|
|
|
|
{
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogVerbose(@"[%@] ows_askForMicrophonePermissions", NSStringFromClass(self.class));
|
2017-11-07 16:32:28 +01:00
|
|
|
|
|
|
|
// Ensure callback is invoked on main thread.
|
|
|
|
void (^callback)(BOOL) = ^(BOOL granted) {
|
2017-11-07 16:38:36 +01:00
|
|
|
DispatchMainThreadSafe(^{
|
2017-11-07 16:32:28 +01:00
|
|
|
callbackParam(granted);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-07 16:46:03 +01:00
|
|
|
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogError(@"Skipping microphone permissions request when app is in background.");
|
2017-11-07 16:32:28 +01:00
|
|
|
callback(NO);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-09 09:14:58 +01:00
|
|
|
[[AVAudioSession sharedInstance] requestRecordPermission:callback];
|
2017-11-07 16:32:28 +01:00
|
|
|
}
|
|
|
|
|
2016-11-04 23:41:37 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|