2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.0 KiB

  1. //
  2. // SCCaptureDeviceAuthorizationChecker.m
  3. // Snapchat
  4. //
  5. // Created by Sun Lei on 15/03/2018.
  6. //
  7. #import "SCCaptureDeviceAuthorizationChecker.h"
  8. #import <SCFoundation/SCQueuePerformer.h>
  9. #import <SCFoundation/SCTraceODPCompatible.h>
  10. @import AVFoundation;
  11. @interface SCCaptureDeviceAuthorizationChecker () {
  12. SCQueuePerformer *_performer;
  13. BOOL _videoCaptureAuthorizationCachedValue;
  14. }
  15. @end
  16. @implementation SCCaptureDeviceAuthorizationChecker
  17. - (instancetype)initWithPerformer:(SCQueuePerformer *)performer
  18. {
  19. SCTraceODPCompatibleStart(2);
  20. self = [super init];
  21. if (self) {
  22. _performer = performer;
  23. _videoCaptureAuthorizationCachedValue = NO;
  24. }
  25. return self;
  26. }
  27. - (void)preloadVideoCaptureAuthorization
  28. {
  29. SCTraceODPCompatibleStart(2);
  30. [_performer perform:^{
  31. SCTraceODPCompatibleStart(2);
  32. _videoCaptureAuthorizationCachedValue = [self authorizedForMediaType:AVMediaTypeVideo];
  33. }];
  34. }
  35. - (BOOL)authorizedForVideoCapture
  36. {
  37. SCTraceODPCompatibleStart(2);
  38. // Cache authorizedForVideoCapture for low devices if it's YES
  39. // [AVCaptureDevice authorizationStatusForMediaType:] is expensive on low devices like iPhone4
  40. if (_videoCaptureAuthorizationCachedValue) {
  41. // If the user authorizes and then unauthorizes, iOS would SIGKILL the app.
  42. // When the user opens the app, a pop-up tells the user to allow camera access in settings.
  43. // So 'return YES' makes sense here.
  44. return YES;
  45. } else {
  46. @weakify(self);
  47. [_performer performAndWait:^{
  48. @strongify(self);
  49. SC_GUARD_ELSE_RETURN(self);
  50. if (!_videoCaptureAuthorizationCachedValue) {
  51. _videoCaptureAuthorizationCachedValue = [self authorizedForMediaType:AVMediaTypeVideo];
  52. }
  53. }];
  54. return _videoCaptureAuthorizationCachedValue;
  55. }
  56. }
  57. - (BOOL)authorizedForMediaType:(NSString *)mediaType
  58. {
  59. return [AVCaptureDevice authorizationStatusForMediaType:mediaType] == AVAuthorizationStatusAuthorized;
  60. }
  61. @end