9
9
import android .hardware .Camera ;
10
10
import android .hardware .camera2 .CameraAccessException ;
11
11
import android .hardware .camera2 .CameraCaptureSession ;
12
+ import android .hardware .camera2 .CameraCharacteristics ;
12
13
import android .hardware .camera2 .CameraDevice ;
13
14
import android .hardware .camera2 .CameraManager ;
14
15
import android .hardware .camera2 .CaptureRequest ;
@@ -814,6 +815,65 @@ void hasTorch(String trackId, Result result) {
814
815
return ;
815
816
}
816
817
818
+ if (videoCapturer instanceof Camera2Capturer ) {
819
+ Object session ;
820
+ try {
821
+ Field currentSessionField = Camera2Capturer .class .getSuperclass ().getDeclaredField ("currentSession" );
822
+ currentSessionField .setAccessible (true );
823
+ session = currentSessionField .get (videoCapturer );
824
+ } catch (NoSuchFieldException e ) {
825
+ // Most likely the upstream Camera1Capturer class have changed
826
+ Log .e (TAG , "[TORCH] Failed to get `currentSession` from `Camera1Capturer`" );
827
+ result .error ("Failed to get `currentSession` from `Camera1Capturer`" , null , null );
828
+ return ;
829
+ } catch (IllegalAccessException e ) {
830
+ // Should never happen since we are calling `setAccessible(true)`
831
+ throw new RuntimeException (e );
832
+ }
833
+
834
+ CameraManager manager ;
835
+ try {
836
+ Field field = videoCapturer .getClass ().getDeclaredField ("cameraManager" );
837
+ field .setAccessible (true );
838
+ manager = (CameraManager ) field .get (videoCapturer );
839
+ } catch (NoSuchFieldException e ) {
840
+ // Most likely the upstream Camera2Capturer class have changed
841
+ Log .e (TAG , "[TORCH] Failed to get `cameraManager` from `Camera2Capturer`" );
842
+ result .error ("Failed to get `cameraManager` from `Camera2Capturer`" , null , null );
843
+ return ;
844
+ } catch (IllegalAccessException e ) {
845
+ // Should never happen since we are calling `setAccessible(true)`
846
+ throw new RuntimeException (e );
847
+ }
848
+
849
+ CameraDevice cameraDevice ;
850
+ try {
851
+ Field field = session .getClass ().getDeclaredField ("cameraDevice" );
852
+ field .setAccessible (true );
853
+ cameraDevice = (CameraDevice ) field .get (session );
854
+ } catch (NoSuchFieldException e ) {
855
+ // Most likely the upstream Camera2Capturer class have changed
856
+ Log .e (TAG , "[TORCH] Failed to get `cameraDevice` from `Camera2Capturer`" );
857
+ result .error ("Failed to get `cameraDevice` from `Camera2Capturer`" , null , null );
858
+ return ;
859
+ } catch (IllegalAccessException e ) {
860
+ // Should never happen since we are calling `setAccessible(true)`
861
+ throw new RuntimeException (e );
862
+ }
863
+
864
+ boolean flashIsAvailable ;
865
+ try {
866
+ CameraCharacteristics characteristics = manager .getCameraCharacteristics (cameraDevice .getId ());
867
+ flashIsAvailable = characteristics .get (CameraCharacteristics .FLASH_INFO_AVAILABLE );
868
+ } catch (CameraAccessException e ) {
869
+ // Should never happen since we are already accessing the camera
870
+ throw new RuntimeException (e );
871
+ }
872
+
873
+ result .success (flashIsAvailable );
874
+ return ;
875
+ }
876
+
817
877
if (videoCapturer instanceof Camera1Capturer ) {
818
878
Object session ;
819
879
try {
<
34D4
svg aria-hidden="true" focusable="false" class="octicon octicon-fold-down" viewBox="0 0 16 16" width="16" height="16" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align:text-bottom">
0 commit comments