@@ -47,6 +47,7 @@ import androidx.core.content.ContextCompat
47
47
import androidx.core.content.FileProvider
48
48
import androidx.core.content.PermissionChecker
49
49
import dagger.hilt.android.AndroidEntryPoint
50
+ import androidx.core.view.isVisible
50
51
import im.vector.app.R
51
52
import im.vector.app.core.platform.VectorBaseFragment
52
53
import im.vector.app.core.platform.VectorMenuProvider
@@ -109,7 +110,8 @@ class AttachmentsCameraFragment :
109
110
requestPermissionLauncher.launch(REQUIRED_PERMISSIONS )
110
111
}
111
112
112
- setButtons()
113
+ setCaptureModeButtons()
114
+ setFlashButton()
113
115
114
116
views.attachmentsCameraCaptureAction.debouncedClicks {
115
117
capture()
@@ -122,6 +124,11 @@ class AttachmentsCameraFragment :
122
124
views.attachmentsCameraFlip.debouncedClicks {
123
125
changeLensFacing()
124
126
}
127
+
128
+ views.attachmentsCameraFlash.debouncedClicks {
129
+ rotateFlashMode()
130
+ }
131
+
125
132
val scaleGestureDetector = ScaleGestureDetector (context, gestureListener)
126
133
127
134
views.root.setOnTouchListener { _, event ->
@@ -142,11 +149,11 @@ class AttachmentsCameraFragment :
142
149
MediaType .IMAGE -> MediaType .VIDEO
143
150
MediaType .VIDEO -> MediaType .IMAGE
144
151
}
145
- setButtons ()
152
+ setCaptureModeButtons ()
146
153
}
147
154
148
155
@SuppressLint(" UseCompatLoadingForDrawables" )
149
- private fun setButtons () {
156
+ private fun setCaptureModeButtons () {
150
157
when (captureMode) {
151
158
MediaType .VIDEO -> {
152
159
views.attachmentsCameraCaptureAction.setImageDrawable(
@@ -171,6 +178,36 @@ class AttachmentsCameraFragment :
171
178
}
172
179
}
173
180
}
181
+ setFlashButton()
182
+ }
183
+
184
+ @SuppressLint(" UseCompatLoadingForDrawables" )
185
+ private fun setFlashButton () {
186
+ if (captureMode == MediaType .VIDEO || cameraSelector != CameraSelector .DEFAULT_BACK_CAMERA ) {
187
+ views.attachmentsCameraFlash.isVisible = false
188
+ } else {
189
+ views.attachmentsCameraFlash.apply {
190
+ isVisible = true
191
+ setImageDrawable(
192
+ context?.getDrawable(
193
+ when (flashMode) {
194
+ ImageCapture .FLASH_MODE_AUTO -> R .drawable.ic_flash_auto
195
+ ImageCapture .FLASH_MODE_OFF -> R .drawable.ic_flash_off
196
+ ImageCapture .FLASH_MODE_ON -> R .drawable.ic_flash_on
197
+ else -> R .drawable.ic_flash_auto
198
+ }
199
+ )
200
+ )
201
+ contentDescription = context?.getString(
202
+ when (flashMode) {
203
+ ImageCapture .FLASH_MODE_AUTO -> R .string.attachment_camera_disable_flash
204
+ ImageCapture .FLASH_MODE_OFF -> R .string.attachment_camera_enable_flash
205
+ ImageCapture .FLASH_MODE_ON -> R .string.attachment_camera_auto_flash
206
+ else -> R .string.attachment_camera_disable_flash
207
+ }
208
+ )
209
+ }
210
+ }
174
211
}
175
212
176
213
private fun changeLensFacing () {
@@ -179,6 +216,7 @@ class AttachmentsCameraFragment :
179
216
} else {
180
217
CameraSelector .DEFAULT_BACK_CAMERA
181
218
}
219
+ setFlashButton()
182
220
startCamera()
183
221
}
184
222
@@ -187,7 +225,16 @@ class AttachmentsCameraFragment :
187
225
MediaType .IMAGE -> takePhoto()
188
226
MediaType .VIDEO -> captureVideo()
189
227
}
228
+ }
190
229
230
+ private fun rotateFlashMode () {
231
+ flashMode = when (flashMode) {
232
+ ImageCapture .FLASH_MODE_AUTO -> ImageCapture .FLASH_MODE_OFF
233
+ ImageCapture .FLASH_MODE_OFF -> ImageCapture .FLASH_MODE_ON
234
+ ImageCapture .FLASH_MODE_ON -> ImageCapture .FLASH_MODE_AUTO
235
+ else -> ImageCapture .FLASH_MODE_OFF
236
+ }
237
+ setFlashButton()
191
238
}
192
239
193
240
private fun takePhoto () {
@@ -196,6 +243,8 @@ class AttachmentsCameraFragment :
196
243
// Get a stable reference of the modifiable image capture use case
197
244
val imageCapture = imageCapture ? : return
198
245
246
+ imageCapture.flashMode = flashMode
247
+
199
248
val file = createTempFile(MediaType .IMAGE )
200
249
val outputUri = getUri(context, file)
201
250
@@ -378,6 +427,7 @@ class AttachmentsCameraFragment :
378
427
companion object {
379
428
private var captureMode = MediaType .IMAGE
380
429
private var cameraSelector = CameraSelector .DEFAULT_BACK_CAMERA
430
+ private var flashMode = ImageCapture .FLASH_MODE_AUTO
381
431
private val REQUIRED_PERMISSIONS =
382
432
mutableListOf (
383
433
Manifest .permission.CAMERA ,
0 commit comments