Skip to content

Commit d534a7d

Browse files
committed
Control flash in camera view
1 parent 90e922b commit d534a7d

File tree

6 files changed

+85
-3
lines changed

6 files changed

+85
-3
lines changed

vector/src/main/java/im/vector/app/features/attachments/camera/AttachmentsCameraFragment.kt

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import androidx.core.content.ContextCompat
4747
import androidx.core.content.FileProvider
4848
import androidx.core.content.PermissionChecker
4949
import dagger.hilt.android.AndroidEntryPoint
50+
import androidx.core.view.isVisible
5051
import im.vector.app.R
5152
import im.vector.app.core.platform.VectorBaseFragment
5253
import im.vector.app.core.platform.VectorMenuProvider
@@ -109,7 +110,8 @@ class AttachmentsCameraFragment :
109110
requestPermissionLauncher.launch(REQUIRED_PERMISSIONS)
110111
}
111112

112-
setButtons()
113+
setCaptureModeButtons()
114+
setFlashButton()
113115

114116
views.attachmentsCameraCaptureAction.debouncedClicks {
115117
capture()
@@ -122,6 +124,11 @@ class AttachmentsCameraFragment :
122124
views.attachmentsCameraFlip.debouncedClicks {
123125
changeLensFacing()
124126
}
127+
128+
views.attachmentsCameraFlash.debouncedClicks {
129+
rotateFlashMode()
130+
}
131+
125132
val scaleGestureDetector = ScaleGestureDetector(context, gestureListener)
126133

127134
views.root.setOnTouchListener { _, event ->
@@ -142,11 +149,11 @@ class AttachmentsCameraFragment :
142149
MediaType.IMAGE -> MediaType.VIDEO
143150
MediaType.VIDEO -> MediaType.IMAGE
144151
}
145-
setButtons()
152+
setCaptureModeButtons()
146153
}
147154

148155
@SuppressLint("UseCompatLoadingForDrawables")
149-
private fun setButtons() {
156+
private fun setCaptureModeButtons() {
150157
when (captureMode) {
151158
MediaType.VIDEO -> {
152159
views.attachmentsCameraCaptureAction.setImageDrawable(
@@ -171,6 +178,36 @@ class AttachmentsCameraFragment :
171178
}
172179
}
173180
}
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+
}
174211
}
175212

176213
private fun changeLensFacing() {
@@ -179,6 +216,7 @@ class AttachmentsCameraFragment :
179216
} else {
180217
CameraSelector.DEFAULT_BACK_CAMERA
181218
}
219+
setFlashButton()
182220
startCamera()
183221
}
184222

@@ -187,7 +225,16 @@ class AttachmentsCameraFragment :
187225
MediaType.IMAGE -> takePhoto()
188226
MediaType.VIDEO -> captureVideo()
189227
}
228+
}
190229

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()
191238
}
192239

193240
private fun takePhoto() {
@@ -196,6 +243,8 @@ class AttachmentsCameraFragment :
196243
// Get a stable reference of the modifiable image capture use case
197244
val imageCapture = imageCapture ?: return
198245

246+
imageCapture.flashMode = flashMode
247+
199248
val file = createTempFile(MediaType.IMAGE)
200249
val outputUri = getUri(context, file)
201250

@@ -378,6 +427,7 @@ class AttachmentsCameraFragment :
378427
companion object {
379428
private var captureMode = MediaType.IMAGE
380429
private var cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
430+
private var flashMode = ImageCapture.FLASH_MODE_AUTO
381431
private val REQUIRED_PERMISSIONS =
382432
mutableListOf (
383433
Manifest.permission.CAMERA,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M3,2v12h3v9l7,-12L9,11l4,-9L3,2zM19,2h-2l-3.2,9h1.9l0.7,-2h3.2l0.7,2h1.9L19,2zM16.85,7.65L18,4l1.15,3.65h-2.3z"/>
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M3.27,3L2,4.27l5,5V13h3v9l3.58,-6.14L17.73,20 19,18.73 3.27,3zM17,10h-4l4,-8H7v2.18l8.46,8.46L17,10z"/>
5+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#000000"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M7,2v11h3v9l7,-12h-4l4,-8z"/>
5+
</vector>

vector/src/main/res/layout/fragment_attachments_camera.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@
5252
app:layout_constraintEnd_toStartOf="@id/vertical_rightline"
5353
android:contentDescription="@string/attachment_camera_flip" />
5454

55+
<com.google.android.material.floatingactionbutton.FloatingActionButton
56+
android:id="@+id/attachmentsCameraFlash"
57+
android:layout_width="wrap_content"
58+
android:layout_height="wrap_content"
59+
android:layout_marginTop="25dp"
60+
android:layout_marginEnd="25dp"
61+
android:elevation="2dp"
62+
android:scaleX="0.7"
63+
android:scaleY="0.7"
64+
android:src="@drawable/ic_flash_auto"
65+
app:layout_constraintTop_toTopOf="parent"
66+
app:layout_constraintEnd_toEndOf="parent"
67+
android:contentDescription="@string/attachment_camera_disable_flash" />
68+
5569
<androidx.constraintlayout.widget.Guideline
5670
android:id="@+id/vertical_leftline"
5771
android:layout_width="wrap_content"

vector/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,6 +3202,9 @@
32023202
<string name="attachment_camera_capture">Capture</string>
32033203
<string name="attachment_camera_flip">Change camera</string>
32043204
<string name="labs_enable_builtin_camera">Enable Built-in Camera</string>
3205+
<string name="attachment_camera_disable_flash">Disable flash</string>
3206+
<string name="attachment_camera_enable_flash">Enable flash</string>
3207+
<string name="attachment_camera_auto_flash">Set flash automatically</string>
32053208

32063209
<plurals name="room_removed_messages">
32073210
<item quantity="one">%d message removed</item>

0 commit comments

Comments
 (0)