16
16
import android .content .Context ;
17
17
import android .content .Intent ;
18
18
import android .content .IntentFilter ;
19
+ import android .hardware .Camera ;
19
20
import android .media .AudioAttributes ;
20
21
import android .media .AudioFocusRequest ;
21
22
import android .media .AudioManager ;
37
38
import com .twilio .video .AudioTrackPublication ;
38
39
import com .twilio .video .BaseTrackStats ;
39
40
import com .twilio .video .CameraCapturer ;
41
+ import com .twilio .video .CameraParameterUpdater ;
40
42
import com .twilio .video .ConnectOptions ;
41
43
import com .twilio .video .LocalAudioTrack ;
42
44
import com .twilio .video .LocalAudioTrackPublication ;
81
83
82
84
import static com .twiliorn .library .CustomTwilioVideoView .Events .ON_AUDIO_CHANGED ;
83
85
import static com .twiliorn .library .CustomTwilioVideoView .Events .ON_CAMERA_SWITCHED ;
86
+ import static com .twiliorn .library .CustomTwilioVideoView .Events .ON_CAMERA_FLASH_TOGGLED ;
84
87
import static com .twiliorn .library .CustomTwilioVideoView .Events .ON_CONNECTED ;
85
88
import static com .twiliorn .library .CustomTwilioVideoView .Events .ON_CONNECT_FAILURE ;
86
89
import static com .twiliorn .library .CustomTwilioVideoView .Events .ON_DISCONNECTED ;
@@ -132,7 +135,8 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
132
135
Events .ON_PARTICIPANT_DISABLED_AUDIO_TRACK ,
133
136
Events .ON_STATS_RECEIVED ,
134
137
Events .ON_NETWORK_QUALITY_LEVELS_CHANGED ,
135
- Events .ON_DOMINANT_SPEAKER_CHANGED
138
+ Events .ON_DOMINANT_SPEAKER_CHANGED ,
139
+ Events .ON_CAMERA_FLASH_TOGGLED
136
140
})
137
141
public @interface Events {
138
142
String ON_CAMERA_SWITCHED = "onCameraSwitched" ;
@@ -157,6 +161,7 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
157
161
String ON_STATS_RECEIVED = "onStatsReceived" ;
158
162
String ON_NETWORK_QUALITY_LEVELS_CHANGED = "onNetworkQualityLevelsChanged" ;
159
163
String ON_DOMINANT_SPEAKER_CHANGED = "onDominantSpeakerDidChange" ;
164
+ String ON_CAMERA_FLASH_TOGGLED = "onCameraFlashToggled" ;
160
165
}
161
166
162
167
private final ThemedReactContext themedReactContext ;
@@ -565,6 +570,32 @@ public void switchCamera() {
565
570
}
566
571
}
567
572
573
+ private final CameraParameterUpdater flashToggler = parameters -> {
574
+ if (parameters .getFlashMode () != null ) {
575
+ String flashMode = Camera .Parameters .FLASH_MODE_OFF .equals (parameters .getFlashMode ()) ?
576
+ Camera .Parameters .FLASH_MODE_TORCH :
577
+ Camera .Parameters .FLASH_MODE_OFF ;
578
+ parameters .setFlashMode (flashMode );
579
+ WritableMap event = new WritableNativeMap ();
580
+ event .putBoolean ("isFlashOn" , flashMode == Camera .Parameters .FLASH_MODE_TORCH );
581
+ pushEvent (CustomTwilioVideoView .this , ON_CAMERA_FLASH_TOGGLED , event );
582
+ } else {
583
+ WritableMap event = new WritableNativeMap ();
584
+ event .putString ("error" , "Flash is not supported in current camera mode" );
585
+ pushEvent (CustomTwilioVideoView .this , ON_CAMERA_FLASH_TOGGLED , event );
586
+ }
587
+ };
588
+
589
+ public void toggleFlash () {
590
+ if (cameraCapturer != null ) {
591
+ cameraCapturer .updateCameraParameters (flashToggler );
592
+ } else {
593
+ WritableMap event = new WritableNativeMap ();
594
+ event .putString ("error" , "There's no camera available" );
595
+ pushEvent (CustomTwilioVideoView .this , ON_CAMERA_FLASH_TOGGLED , event );
596
+ }
597
+ }
598
+
568
599
public void toggleVideo (boolean enabled ) {
569
600
isVideoEnabled = enabled ;
570
601
if (localVideoTrack != null ) {
0 commit comments