4848#define KEY_REPEAT_TIMEOUT 0.1
4949#define GESTUREZONE_RADIUS 20.0
5050#define GESTUREZONE_BORDERWIDTH 1.0
51+ #define KEYPATH_TORCHACTIVE @" torchActive"
52+ #define KEYPATH_TORCHAVAILABLE @" torchAvailable"
53+
54+ static void *TorchRemoteContext = &TorchRemoteContext;
5155
5256@interface RemoteController ()
5357
@@ -1037,12 +1041,43 @@ - (void)viewWillAppear:(BOOL)animated {
10371041 selector: @selector (handleDidBecomeActive )
10381042 name: UIApplicationDidBecomeActiveNotification
10391043 object: nil ];
1044+
1045+ [self .avCaptureDevice addObserver: self
1046+ forKeyPath: KEYPATH_TORCHACTIVE
1047+ options: NSKeyValueObservingOptionNew
1048+ context: TorchRemoteContext];
1049+
1050+ [self .avCaptureDevice addObserver: self
1051+ forKeyPath: KEYPATH_TORCHAVAILABLE
1052+ options: NSKeyValueObservingOptionNew
1053+ context: TorchRemoteContext];
1054+ }
1055+
1056+ - (void )observeValueForKeyPath : (NSString *)keyPath
1057+ ofObject : (id )object
1058+ change : (NSDictionary *)change
1059+ context : (void *)context {
1060+ if (context == TorchRemoteContext) {
1061+ if ([keyPath isEqualToString: KEYPATH_TORCHACTIVE]) {
1062+ torchIsOn = [change[NSKeyValueChangeNewKey ] boolValue ];
1063+ [self setTorchIcon: torchIsOn];
1064+ }
1065+ else if ([keyPath isEqualToString: KEYPATH_TORCHAVAILABLE]) {
1066+ torchButton.enabled = [change[NSKeyValueChangeNewKey ] boolValue ];
1067+ }
1068+ }
1069+ else {
1070+ [super observeValueForKeyPath: keyPath
1071+ ofObject: object
1072+ change: change
1073+ context: context];
1074+ }
10401075}
10411076
10421077- (void )handleDidBecomeActive {
10431078 // Update torch mode
1044- torchIsOn = [Utilities isTorchOn ] ;
1045- [Utilities turnTorchOn: torchButton on : torchIsOn];
1079+ torchIsOn = self. avCaptureDevice . torchActive ;
1080+ [self setTorchIcon : torchIsOn];
10461081}
10471082
10481083- (void )enablePopGestureRecognizer {
@@ -1074,9 +1109,19 @@ - (void)viewWillDisappear:(BOOL)animated {
10741109 self.slidingViewController .panGesture .delegate = nil ;
10751110}
10761111
1077- - (void )turnTorchOn : ( id ) sender {
1112+ - (void )toggleTorch {
10781113 torchIsOn = !torchIsOn;
1079- [Utilities turnTorchOn: sender on: torchIsOn];
1114+
1115+ AVCaptureDevice *device = self.avCaptureDevice ;
1116+ [device lockForConfiguration: nil ];
1117+ device.torchMode = torchIsOn ? AVCaptureTorchModeOn : AVCaptureTorchModeOff;
1118+ [device unlockForConfiguration ];
1119+ }
1120+
1121+ - (void )setTorchIcon : (BOOL )torchActive {
1122+ UIImage *buttonImage = [UIImage imageNamed: torchActive ? @" torch_on" : @" torch" ];
1123+ buttonImage = [Utilities colorizeImage: buttonImage withColor: ICON_TINT_COLOR];
1124+ [torchButton setImage: buttonImage forState: UIControlStateNormal];
10801125}
10811126
10821127- (void )dismissModal {
@@ -1102,7 +1147,7 @@ - (void)setRemoteToPosition {
11021147}
11031148
11041149- (void )createRemoteToolbarWidth : (CGFloat)width xMin : (CGFloat)xMin yMax : (CGFloat)yMax {
1105- torchIsOn = [Utilities isTorchOn ] ;
1150+ torchIsOn = self. avCaptureDevice . torchActive ;
11061151 // iPhone layout has 5 buttons (Gesture > Keyboard > Info > Torch > Additional) with flex spaces around buttons.
11071152 // iPad layout has 6 buttons (Settings > Gesture > Keyboard > Info > Torch > Additional) with flex spaces around buttons.
11081153 // iPhone has an addtional button to toggle the remote's vertical position
@@ -1170,11 +1215,9 @@ - (void)createRemoteToolbarWidth:(CGFloat)width xMin:(CGFloat)xMin yMax:(CGFloat
11701215 frame.origin .x += ToolbarPadding;
11711216 torchButton.frame = frame;
11721217 torchButton.showsTouchWhenHighlighted = YES ;
1173- buttonImage = [UIImage imageNamed: torchIsOn ? @" torch_on" : @" torch" ];
1174- buttonImage = [Utilities colorizeImage: buttonImage withColor: ICON_TINT_COLOR];
1175- [torchButton setImage: buttonImage forState: UIControlStateNormal];
1176- [torchButton addTarget: self action: @selector (turnTorchOn: ) forControlEvents: UIControlEventTouchUpInside];
1177- torchButton.enabled = [Utilities hasTorch ];
1218+ torchButton.enabled = self.avCaptureDevice .torchAvailable ;
1219+ [self setTorchIcon: torchIsOn];
1220+ [torchButton addTarget: self action: @selector (toggleTorch ) forControlEvents: UIControlEventTouchUpInside];
11781221 [remoteToolbar addSubview: torchButton];
11791222
11801223 if (IS_IPHONE) {
@@ -1208,6 +1251,7 @@ - (void)viewDidLoad {
12081251 [super viewDidLoad ];
12091252
12101253 self.edgesForExtendedLayout = UIRectEdgeNone;
1254+ self.avCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
12111255
12121256 [self loadRemoteMode ];
12131257 [self configureView ];
0 commit comments