mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			177 lines
		
	
	
		
			7.1 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			177 lines
		
	
	
		
			7.1 KiB
		
	
	
	
		
			Matlab
		
	
| 
											9 years ago
										 | // | ||
| 
											7 years ago
										 | //  Copyright (c) 2019 Open Whisper Systems. All rights reserved. | ||
| 
											9 years ago
										 | // | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 | #import "UIViewController+Permissions.h" | ||
| 
											7 years ago
										 | #import "Session-Swift.h" | ||
| 
											8 years ago
										 | #import <AVFoundation/AVFoundation.h> | ||
| 
											8 years ago
										 | #import <Photos/Photos.h> | ||
| 
											5 years ago
										 | #import <SignalCoreKit/Threading.h> | ||
| 
											5 years ago
										 | #import <SignalUtilitiesKit/UIUtil.h> | ||
| 
											9 years ago
										 | 
 | ||
|  | NS_ASSUME_NONNULL_BEGIN | ||
|  | 
 | ||
| 
											8 years ago
										 | @implementation UIViewController (Permissions) | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 | - (void)ows_askForCameraPermissions:(void (^)(BOOL granted))callbackParam | ||
| 
											9 years ago
										 | { | ||
| 
											7 years ago
										 |     OWSLogVerbose(@"[%@] ows_askForCameraPermissions", NSStringFromClass(self.class)); | ||
| 
											8 years ago
										 | 
 | ||
| 
											8 years ago
										 |     // Ensure callback is invoked on main thread. | ||
|  |     void (^callback)(BOOL) = ^(BOOL granted) { | ||
|  |         DispatchMainThreadSafe(^{ | ||
|  |             callbackParam(granted); | ||
|  |         }); | ||
|  |     }; | ||
| 
											9 years ago
										 | 
 | ||
| 
											8 years ago
										 |     if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { | ||
| 
											7 years ago
										 |         OWSLogError(@"Skipping camera permissions request when app is in background."); | ||
| 
											8 years ago
										 |         callback(NO); | ||
| 
											8 years ago
										 |         return; | ||
|  |     } | ||
|  | 
 | ||
| 
											9 years ago
										 |     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { | ||
| 
											7 years ago
										 |         OWSLogError(@"Camera ImagePicker source not available"); | ||
| 
											8 years ago
										 |         callback(NO); | ||
| 
											9 years ago
										 |         return; | ||
|  |     } | ||
| 
											9 years ago
										 | 
 | ||
| 
											9 years ago
										 |     AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; | ||
|  |     if (status == AVAuthorizationStatusDenied) { | ||
| 
											8 years ago
										 |         UIAlertController *alert = [UIAlertController | ||
| 
											6 years ago
										 |             alertControllerWithTitle:NSLocalizedString(@"Session needs camera access to scan QR codes.", @"") | ||
| 
											6 years ago
										 |                              message:NSLocalizedString(@"You can enable camera access in your device settings.", @"") | ||
| 
											8 years ago
										 |                       preferredStyle:UIAlertControllerStyleAlert]; | ||
|  | 
 | ||
|  |         UIAlertAction *openSettingsAction = | ||
| 
											8 years ago
										 |             [UIAlertAction actionWithTitle:CommonStrings.openSettingsButton | ||
| 
											8 years ago
										 |                                      style:UIAlertActionStyleDefault | ||
|  |                                    handler:^(UIAlertAction *_Nonnull action) { | ||
|  |                                        [[UIApplication sharedApplication] openSystemSettings]; | ||
| 
											8 years ago
										 |                                        callback(NO); | ||
| 
											8 years ago
										 |                                    }]; | ||
| 
											9 years ago
										 |         [alert addAction:openSettingsAction]; | ||
|  | 
 | ||
| 
											8 years ago
										 |         UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:CommonStrings.dismissButton | ||
| 
											9 years ago
										 |                                                                 style:UIAlertActionStyleCancel | ||
| 
											9 years ago
										 |                                                               handler:^(UIAlertAction *action) { | ||
| 
											8 years ago
										 |                                                                   callback(NO); | ||
| 
											9 years ago
										 |                                                               }]; | ||
| 
											9 years ago
										 |         [alert addAction:dismissAction]; | ||
|  | 
 | ||
| 
											7 years ago
										 |         [self presentAlert:alert]; | ||
| 
											9 years ago
										 |     } else if (status == AVAuthorizationStatusAuthorized) { | ||
| 
											8 years ago
										 |         callback(YES); | ||
| 
											9 years ago
										 |     } else if (status == AVAuthorizationStatusNotDetermined) { | ||
| 
											9 years ago
										 |         [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo | ||
| 
											8 years ago
										 |                                  completionHandler:callback]; | ||
| 
											9 years ago
										 |     } else { | ||
| 
											7 years ago
										 |         OWSLogError(@"Unknown AVAuthorizationStatus: %ld", (long)status); | ||
| 
											8 years ago
										 |         callback(NO); | ||
| 
											9 years ago
										 |     } | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (void)ows_askForMediaLibraryPermissions:(void (^)(BOOL granted))callbackParam | ||
|  | { | ||
| 
											7 years ago
										 |     OWSLogVerbose(@"[%@] ows_askForMediaLibraryPermissions", NSStringFromClass(self.class)); | ||
| 
											8 years ago
										 | 
 | ||
|  |     // Ensure callback is invoked on main thread. | ||
|  |     void (^completionCallback)(BOOL) = ^(BOOL granted) { | ||
|  |         DispatchMainThreadSafe(^{ | ||
|  |             callbackParam(granted); | ||
|  |         }); | ||
|  |     }; | ||
|  | 
 | ||
|  |     void (^presentSettingsDialog)(void) = ^(void) { | ||
| 
											8 years ago
										 |         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]; | ||
|  | 
 | ||
| 
											7 years ago
										 |             [self presentAlert:alert]; | ||
| 
											8 years ago
										 |         }); | ||
| 
											8 years ago
										 |     }; | ||
|  | 
 | ||
|  |     if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { | ||
| 
											7 years ago
										 |         OWSLogError(@"Skipping media library permissions request when app is in background."); | ||
| 
											8 years ago
										 |         completionCallback(NO); | ||
|  |         return; | ||
|  |     } | ||
|  | 
 | ||
|  |     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { | ||
| 
											7 years ago
										 |         OWSLogError(@"PhotoLibrary ImagePicker source not available"); | ||
| 
											8 years ago
										 |         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? | ||
| 
											7 years ago
										 |             OWSFailDebug(@"PHAuthorizationStatusRestricted"); | ||
| 
											8 years ago
										 |             return; | ||
|  |         } | ||
|  |     } | ||
|  | } | ||
|  | 
 | ||
| 
											8 years ago
										 | - (void)ows_askForMicrophonePermissions:(void (^)(BOOL granted))callbackParam | ||
|  | { | ||
| 
											7 years ago
										 |     OWSLogVerbose(@"[%@] ows_askForMicrophonePermissions", NSStringFromClass(self.class)); | ||
| 
											8 years ago
										 | 
 | ||
|  |     // Ensure callback is invoked on main thread. | ||
|  |     void (^callback)(BOOL) = ^(BOOL granted) { | ||
| 
											8 years ago
										 |         DispatchMainThreadSafe(^{ | ||
| 
											8 years ago
										 |             callbackParam(granted); | ||
|  |         }); | ||
|  |     }; | ||
|  | 
 | ||
| 
											8 years ago
										 |     if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { | ||
| 
											7 years ago
										 |         OWSLogError(@"Skipping microphone permissions request when app is in background."); | ||
| 
											8 years ago
										 |         callback(NO); | ||
|  |         return; | ||
|  |     } | ||
|  | 
 | ||
| 
											8 years ago
										 |     [[AVAudioSession sharedInstance] requestRecordPermission:callback]; | ||
| 
											8 years ago
										 | } | ||
|  | 
 | ||
| 
											9 years ago
										 | @end | ||
|  | 
 | ||
|  | NS_ASSUME_NONNULL_END |