cleanup selection of media size

- add MediaSizeStore to manage write/read operations for the media size
This commit is contained in:
darthpaul 2022-08-25 08:42:13 +01:00
parent fd5f64ecf3
commit 0fdb751909
6 changed files with 54 additions and 49 deletions

View File

@ -702,29 +702,16 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
resolutions: List<ResolutionOption>, resolutions: List<ResolutionOption>,
isPhotoCapture: Boolean, isPhotoCapture: Boolean,
isFrontCamera: Boolean, isFrontCamera: Boolean,
onSelect: (changed: Boolean) -> Unit onSelect: (index: Int, changed: Boolean) -> Unit
) { ) {
media_size_toggle_group.removeAllViews() media_size_toggle_group.removeAllViews()
media_size_toggle_group.clearChecked() media_size_toggle_group.clearChecked()
val onItemClick = { clickedViewId: Int -> val onItemClick = { clickedViewId: Int ->
val index = resolutions.indexOfFirst { it.buttonViewId == clickedViewId }
if (isPhotoCapture) {
if (isFrontCamera) {
config.frontPhotoResIndex = index
} else {
config.backPhotoResIndex = index
}
} else {
if (isFrontCamera) {
config.frontVideoResIndex = index
} else {
config.backVideoResIndex = index
}
}
closeOptions() closeOptions()
onSelect.invoke(selectedResolution.buttonViewId != clickedViewId) val index = resolutions.indexOfFirst { it.buttonViewId == clickedViewId }
onSelect.invoke(index, selectedResolution.buttonViewId != clickedViewId)
} }
resolutions.map { resolutions.map {

View File

@ -20,8 +20,8 @@ class ImageQualityManager(
} }
private val cameraManager = activity.getSystemService(Context.CAMERA_SERVICE) as CameraManager private val cameraManager = activity.getSystemService(Context.CAMERA_SERVICE) as CameraManager
private val config = activity.config
private val imageQualities = mutableListOf<CameraSelectorImageQualities>() private val imageQualities = mutableListOf<CameraSelectorImageQualities>()
private val mediaSizeStore = MediaSizeStore(activity.config)
fun initSupportedQualities() { fun initSupportedQualities() {
if (imageQualities.isEmpty()) { if (imageQualities.isEmpty()) {
@ -52,7 +52,8 @@ class ImageQualityManager(
fun getUserSelectedResolution(cameraSelector: CameraSelector): MySize { fun getUserSelectedResolution(cameraSelector: CameraSelector): MySize {
val resolutions = getSupportedResolutions(cameraSelector) val resolutions = getSupportedResolutions(cameraSelector)
var index = if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA) config.frontPhotoResIndex else config.backPhotoResIndex val isFrontCamera = cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA
var index = mediaSizeStore.getCurrentSizeIndex(isPhotoCapture = true, isFrontCamera = isFrontCamera)
index = index.coerceAtMost(resolutions.lastIndex).coerceAtLeast(0) index = index.coerceAtMost(resolutions.lastIndex).coerceAtLeast(0)
return resolutions[index] return resolutions[index]
} }

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.camera.helpers
class MediaSizeStore(
private val config: Config,
) {
fun storeSize(isPhotoCapture: Boolean, isFrontCamera: Boolean, currentIndex: Int) {
if (isPhotoCapture) {
if (isFrontCamera) {
config.frontPhotoResIndex = currentIndex
} else {
config.backPhotoResIndex = currentIndex
}
} else {
if (isFrontCamera) {
config.frontVideoResIndex = currentIndex
} else {
config.backVideoResIndex = currentIndex
}
}
}
fun getCurrentSizeIndex(isPhotoCapture: Boolean, isFrontCamera: Boolean): Int {
return if (isPhotoCapture) {
if (isFrontCamera) {
config.frontPhotoResIndex
} else {
config.backPhotoResIndex
}
} else {
if (isFrontCamera) {
config.frontVideoResIndex
} else {
config.backVideoResIndex
}
}
}
}

View File

@ -20,8 +20,8 @@ class VideoQualityManager(
private val CAMERA_SELECTORS = arrayOf(CameraSelector.DEFAULT_BACK_CAMERA, CameraSelector.DEFAULT_FRONT_CAMERA) private val CAMERA_SELECTORS = arrayOf(CameraSelector.DEFAULT_BACK_CAMERA, CameraSelector.DEFAULT_FRONT_CAMERA)
} }
private val config = activity.config
private val videoQualities = mutableListOf<CameraSelectorVideoQualities>() private val videoQualities = mutableListOf<CameraSelectorVideoQualities>()
private val mediaSizeStore = MediaSizeStore(activity.config)
fun initSupportedQualities(cameraProvider: ProcessCameraProvider) { fun initSupportedQualities(cameraProvider: ProcessCameraProvider) {
if (videoQualities.isEmpty()) { if (videoQualities.isEmpty()) {
@ -45,8 +45,8 @@ class VideoQualityManager(
} }
fun getUserSelectedQuality(cameraSelector: CameraSelector): VideoQuality { fun getUserSelectedQuality(cameraSelector: CameraSelector): VideoQuality {
var selectionIndex = if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA) config.frontVideoResIndex else config.backVideoResIndex val isFrontCamera = cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA
selectionIndex = selectionIndex.coerceAtLeast(0) val selectionIndex = mediaSizeStore.getCurrentSizeIndex(isPhotoCapture = false, isFrontCamera = isFrontCamera).coerceAtLeast(0)
return getSupportedQualities(cameraSelector).getOrElse(selectionIndex) { VideoQuality.HD } return getSupportedQualities(cameraSelector).getOrElse(selectionIndex) { VideoQuality.HD }
} }

View File

@ -59,6 +59,7 @@ class CameraXPreview(
private val videoQualityManager = VideoQualityManager(activity) private val videoQualityManager = VideoQualityManager(activity)
private val imageQualityManager = ImageQualityManager(activity) private val imageQualityManager = ImageQualityManager(activity)
private val exifRemover = ExifRemover(contentResolver) private val exifRemover = ExifRemover(contentResolver)
private val mediaSizeStore = MediaSizeStore(config)
private val orientationEventListener = object : OrientationEventListener(activity, SensorManager.SENSOR_DELAY_NORMAL) { private val orientationEventListener = object : OrientationEventListener(activity, SensorManager.SENSOR_DELAY_NORMAL) {
@SuppressLint("RestrictedApi") @SuppressLint("RestrictedApi")
@ -359,10 +360,11 @@ class CameraXPreview(
resolutions = resolutions, resolutions = resolutions,
isPhotoCapture = isPhotoCapture, isPhotoCapture = isPhotoCapture,
isFrontCamera = isFrontCameraInUse() isFrontCamera = isFrontCameraInUse()
) { changed -> ) { index, changed ->
if (changed) { if (changed) {
currentRecording?.stop() currentRecording?.stop()
} }
mediaSizeStore.storeSize(isPhotoCapture, isFrontCameraInUse(), index)
startCamera() startCamera()
} }
} else { } else {
@ -371,19 +373,7 @@ class CameraXPreview(
} }
private fun toggleResolutions(resolutions: List<ResolutionOption>) { private fun toggleResolutions(resolutions: List<ResolutionOption>) {
val currentIndex = if (isPhotoCapture) { val currentIndex = mediaSizeStore.getCurrentSizeIndex(isPhotoCapture, isFrontCameraInUse())
if (isFrontCameraInUse()) {
config.frontPhotoResIndex
} else {
config.backPhotoResIndex
}
} else {
if (isFrontCameraInUse()) {
config.frontVideoResIndex
} else {
config.backVideoResIndex
}
}
val nextIndex = if (currentIndex >= resolutions.lastIndex) { val nextIndex = if (currentIndex >= resolutions.lastIndex) {
0 0
@ -391,19 +381,7 @@ class CameraXPreview(
currentIndex + 1 currentIndex + 1
} }
if (isPhotoCapture) { mediaSizeStore.storeSize(isPhotoCapture, isFrontCameraInUse(), nextIndex)
if (isFrontCameraInUse()) {
config.frontPhotoResIndex = nextIndex
} else {
config.backPhotoResIndex = nextIndex
}
} else {
if (isFrontCameraInUse()) {
config.frontVideoResIndex = nextIndex
} else {
config.backVideoResIndex = nextIndex
}
}
currentRecording?.stop() currentRecording?.stop()
startCamera() startCamera()

View File

@ -26,7 +26,8 @@ interface CameraXPreviewListener {
resolutions: List<ResolutionOption>, resolutions: List<ResolutionOption>,
isPhotoCapture: Boolean, isPhotoCapture: Boolean,
isFrontCamera: Boolean, isFrontCamera: Boolean,
onSelect: (changed:Boolean) -> Unit, onSelect: (index: Int, changed: Boolean) -> Unit,
) )
fun showFlashOptions(photoCapture: Boolean) fun showFlashOptions(photoCapture: Boolean)
} }