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.

105 lines
3.3 KiB

  1. //
  2. // SCManagedFrontFlashController.m
  3. // Snapchat
  4. //
  5. // Created by Liu Liu on 5/4/15.
  6. // Copyright (c) 2015 Liu Liu. All rights reserved.
  7. //
  8. #import "SCManagedFrontFlashController.h"
  9. #import <SCFoundation/SCAssertWrapper.h>
  10. #import <SCFoundation/SCLog.h>
  11. #import <SCFoundation/SCThreadHelpers.h>
  12. #import <SCFoundation/SCTrace.h>
  13. @import UIKit;
  14. @implementation SCManagedFrontFlashController {
  15. BOOL _active;
  16. UIView *_brightView;
  17. CGFloat _brightnessWhenFlashAndTorchOff;
  18. }
  19. - (void)_setScreenWithFrontViewFlashActive:(BOOL)flashActive torchActive:(BOOL)torchActive
  20. {
  21. SCTraceStart();
  22. SCAssertMainThread();
  23. BOOL wasActive = _active;
  24. _active = flashActive || torchActive;
  25. if (!wasActive && _active) {
  26. [self _activateFlash:flashActive];
  27. } else if (wasActive && !_active) {
  28. [self _deactivateFlash];
  29. }
  30. }
  31. - (void)_activateFlash:(BOOL)flashActive
  32. {
  33. UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
  34. if (!_brightView) {
  35. CGRect frame = [mainWindow bounds];
  36. CGFloat maxLength = MAX(CGRectGetWidth(frame), CGRectGetHeight(frame));
  37. frame.size = CGSizeMake(maxLength, maxLength);
  38. // Using the max length on either side to be compatible with different orientations
  39. _brightView = [[UIView alloc] initWithFrame:frame];
  40. _brightView.userInteractionEnabled = NO;
  41. _brightView.backgroundColor = [UIColor whiteColor];
  42. }
  43. _brightnessWhenFlashAndTorchOff = [UIScreen mainScreen].brightness;
  44. SCLogGeneralInfo(@"[SCManagedFrontFlashController] Activating flash, setting screen brightness from %f to 1.0",
  45. _brightnessWhenFlashAndTorchOff);
  46. [self _brightenLoop];
  47. _brightView.alpha = flashActive ? 1.0 : 0.75;
  48. [mainWindow addSubview:_brightView];
  49. }
  50. - (void)_deactivateFlash
  51. {
  52. SCLogGeneralInfo(@"[SCManagedFrontFlashController] Deactivating flash, setting screen brightness from %f to %f",
  53. [UIScreen mainScreen].brightness, _brightnessWhenFlashAndTorchOff);
  54. [UIScreen mainScreen].brightness = _brightnessWhenFlashAndTorchOff;
  55. if (_brightView) {
  56. [_brightView removeFromSuperview];
  57. }
  58. }
  59. - (void)_brightenLoop
  60. {
  61. if (_active) {
  62. SCLogGeneralInfo(@"[SCManagedFrontFlashController] In brighten loop, setting brightness from %f to 1.0",
  63. [UIScreen mainScreen].brightness);
  64. [UIScreen mainScreen].brightness = 1.0;
  65. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 2), dispatch_get_main_queue(), ^(void) {
  66. [self _brightenLoop];
  67. });
  68. } else {
  69. SCLogGeneralInfo(@"[SCManagedFrontFlashController] Recording is done, brighten loop ends");
  70. }
  71. }
  72. - (void)setFlashActive:(BOOL)flashActive
  73. {
  74. SCTraceStart();
  75. if (_flashActive != flashActive) {
  76. _flashActive = flashActive;
  77. BOOL torchActive = _torchActive;
  78. runOnMainThreadAsynchronously(^{
  79. [self _setScreenWithFrontViewFlashActive:flashActive torchActive:torchActive];
  80. });
  81. }
  82. }
  83. - (void)setTorchActive:(BOOL)torchActive
  84. {
  85. SCTraceStart();
  86. if (_torchActive != torchActive) {
  87. _torchActive = torchActive;
  88. BOOL flashActive = _flashActive;
  89. runOnMainThreadAsynchronously(^{
  90. [self _setScreenWithFrontViewFlashActive:flashActive torchActive:torchActive];
  91. });
  92. }
  93. }
  94. @end