Skip to content

Commit c4c5d0b

Browse files
committed
Added Torch support in android
1 parent 0030ed6 commit c4c5d0b

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

android/src/main/java/com/twiliorn/library/CustomTwilioVideoView.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.content.Context;
1717
import android.content.Intent;
1818
import android.content.IntentFilter;
19+
import android.hardware.Camera;
1920
import android.media.AudioAttributes;
2021
import android.media.AudioFocusRequest;
2122
import android.media.AudioManager;
@@ -37,6 +38,7 @@
3738
import com.twilio.video.AudioTrackPublication;
3839
import com.twilio.video.BaseTrackStats;
3940
import com.twilio.video.CameraCapturer;
41+
import com.twilio.video.CameraParameterUpdater;
4042
import com.twilio.video.ConnectOptions;
4143
import com.twilio.video.LocalAudioTrack;
4244
import com.twilio.video.LocalAudioTrackPublication;
@@ -81,6 +83,7 @@
8183

8284
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_AUDIO_CHANGED;
8385
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_CAMERA_SWITCHED;
86+
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_CAMERA_FLASH_TOGGLED;
8487
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_CONNECTED;
8588
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_CONNECT_FAILURE;
8689
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_DISCONNECTED;
@@ -132,7 +135,8 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
132135
Events.ON_PARTICIPANT_DISABLED_AUDIO_TRACK,
133136
Events.ON_STATS_RECEIVED,
134137
Events.ON_NETWORK_QUALITY_LEVELS_CHANGED,
135-
Events.ON_DOMINANT_SPEAKER_CHANGED
138+
Events.ON_DOMINANT_SPEAKER_CHANGED,
139+
Events.ON_CAMERA_FLASH_TOGGLED
136140
})
137141
public @interface Events {
138142
String ON_CAMERA_SWITCHED = "onCameraSwitched";
@@ -157,6 +161,7 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
157161
String ON_STATS_RECEIVED = "onStatsReceived";
158162
String ON_NETWORK_QUALITY_LEVELS_CHANGED = "onNetworkQualityLevelsChanged";
159163
String ON_DOMINANT_SPEAKER_CHANGED = "onDominantSpeakerDidChange";
164+
String ON_CAMERA_FLASH_TOGGLED = "onCameraFlashToggled";
160165
}
161166

162167
private final ThemedReactContext themedReactContext;
@@ -565,6 +570,32 @@ public void switchCamera() {
565570
}
566571
}
567572

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+
568599
public void toggleVideo(boolean enabled) {
569600
isVideoEnabled = enabled;
570601
if (localVideoTrack != null) {

android/src/main/java/com/twiliorn/library/CustomTwilioVideoViewManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_AUDIO_CHANGED;
2222
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_CAMERA_SWITCHED;
23+
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_CAMERA_FLASH_TOGGLED;
2324
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_CONNECTED;
2425
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_CONNECT_FAILURE;
2526
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_DISCONNECTED;
@@ -59,6 +60,7 @@ public class CustomTwilioVideoViewManager extends SimpleViewManager<CustomTwilio
5960
private static final int SEND_STRING = 12;
6061
private static final int PUBLISH_VIDEO = 13;
6162
private static final int PUBLISH_AUDIO = 14;
63+
private static final int TOGGLE_FLASH = 15;
6264

6365
@Override
6466
public String getName() {
@@ -127,6 +129,9 @@ public void receiveCommand(CustomTwilioVideoView view, int commandId, @Nullable
127129
case PUBLISH_AUDIO:
128130
view.publishLocalAudio(args.getBoolean(0));
129131
break;
132+
case TOGGLE_FLASH:
133+
view.toggleFlash();
134+
break;
130135
}
131136
}
132137

@@ -143,6 +148,10 @@ public Map getExportedCustomDirectEventTypeConstants() {
143148
ON_PARTICIPANT_CONNECTED, MapBuilder.of("registrationName", ON_PARTICIPANT_CONNECTED)
144149
);
145150

151+
map.putAll(MapBuilder.of(
152+
ON_CAMERA_FLASH_TOGGLED, MapBuilder.of("registrationName", ON_CAMERA_FLASH_TOGGLED)
153+
));
154+
146155
map.putAll(MapBuilder.of(
147156
ON_PARTICIPANT_DISCONNECTED, MapBuilder.of("registrationName", ON_PARTICIPANT_DISCONNECTED),
148157
ON_DATATRACK_MESSAGE_RECEIVED, MapBuilder.of("registrationName", ON_DATATRACK_MESSAGE_RECEIVED),
@@ -177,6 +186,7 @@ public Map<String, Integer> getCommandsMap() {
177186
.put("connectToRoom", CONNECT_TO_ROOM)
178187
.put("disconnect", DISCONNECT)
179188
.put("switchCamera", SWITCH_CAMERA)
189+
.put("toggleFlash", TOGGLE_FLASH)
180190
.put("toggleVideo", TOGGLE_VIDEO)
181191
.put("toggleSound", TOGGLE_SOUND)
182192
.put("getStats", GET_STATS)

src/TwilioVideo.android.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ const propTypes = {
143143
* Called when dominant speaker changes
144144
* @param {{ participant, room }} dominant participant and room
145145
*/
146-
onDominantSpeakerDidChange: PropTypes.func
146+
onDominantSpeakerDidChange: PropTypes.func,
147+
/**
148+
* Called when torch is done attempting to toggle
149+
* @param {{ error, isFlashOn }}
150+
*/
151+
onCameraFlashToggled: PropTypes.func,
147152
}
148153

149154
const nativeEvents = {
@@ -160,7 +165,8 @@ const nativeEvents = {
160165
toggleBluetoothHeadset: 11,
161166
sendString: 12,
162167
publishVideo: 13,
163-
publishAudio: 14
168+
publishAudio: 14,
169+
toggleFlash: 15
164170
}
165171

166172
class CustomTwilioVideoView extends Component {
@@ -218,6 +224,10 @@ class CustomTwilioVideoView extends Component {
218224
this.runCommand(nativeEvents.switchCamera, [])
219225
}
220226

227+
toggleFlash() {
228+
this.runCommand(nativeEvents.toggleFlash, []);
229+
}
230+
221231
setLocalVideoEnabled (enabled) {
222232
this.runCommand(nativeEvents.toggleVideo, [enabled])
223233
return Promise.resolve(enabled)
@@ -287,7 +297,8 @@ class CustomTwilioVideoView extends Component {
287297
'onParticipantDisabledAudioTrack',
288298
'onStatsReceived',
289299
'onNetworkQualityLevelsChanged',
290-
'onDominantSpeakerDidChange'
300+
'onDominantSpeakerDidChange',
301+
'onCameraFlashToggled'
291302
].reduce((wrappedEvents, eventName) => {
292303
if (this.props[eventName]) {
293304
return {

0 commit comments

Comments
 (0)