From 0fdb7519095fa61f9f79eac9d9f88f05a3b98aa1 Mon Sep 17 00:00:00 2001 From: darthpaul Date: Thu, 25 Aug 2022 08:42:13 +0100 Subject: [PATCH] cleanup selection of media size - add MediaSizeStore to manage write/read operations for the media size --- .../camera/activities/MainActivity.kt | 19 ++-------- .../camera/helpers/ImageQualityManager.kt | 5 ++- .../camera/helpers/MediaSizeStore.kt | 38 +++++++++++++++++++ .../camera/helpers/VideoQualityManager.kt | 6 +-- .../camera/implementations/CameraXPreview.kt | 32 +++------------- .../implementations/CameraXPreviewListener.kt | 3 +- 6 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaSizeStore.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt index c066820b..a7282c26 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt @@ -702,29 +702,16 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera resolutions: List, isPhotoCapture: Boolean, isFrontCamera: Boolean, - onSelect: (changed: Boolean) -> Unit + onSelect: (index: Int, changed: Boolean) -> Unit ) { media_size_toggle_group.removeAllViews() media_size_toggle_group.clearChecked() 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() - onSelect.invoke(selectedResolution.buttonViewId != clickedViewId) + val index = resolutions.indexOfFirst { it.buttonViewId == clickedViewId } + onSelect.invoke(index, selectedResolution.buttonViewId != clickedViewId) } resolutions.map { diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt index 198a942e..9177c781 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt @@ -20,8 +20,8 @@ class ImageQualityManager( } private val cameraManager = activity.getSystemService(Context.CAMERA_SERVICE) as CameraManager - private val config = activity.config private val imageQualities = mutableListOf() + private val mediaSizeStore = MediaSizeStore(activity.config) fun initSupportedQualities() { if (imageQualities.isEmpty()) { @@ -52,7 +52,8 @@ class ImageQualityManager( fun getUserSelectedResolution(cameraSelector: CameraSelector): MySize { 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) return resolutions[index] } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaSizeStore.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaSizeStore.kt new file mode 100644 index 00000000..8702eb66 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaSizeStore.kt @@ -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 + } + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/VideoQualityManager.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/VideoQualityManager.kt index 0a3d784a..d768deec 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/VideoQualityManager.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/VideoQualityManager.kt @@ -20,8 +20,8 @@ class VideoQualityManager( private val CAMERA_SELECTORS = arrayOf(CameraSelector.DEFAULT_BACK_CAMERA, CameraSelector.DEFAULT_FRONT_CAMERA) } - private val config = activity.config private val videoQualities = mutableListOf() + private val mediaSizeStore = MediaSizeStore(activity.config) fun initSupportedQualities(cameraProvider: ProcessCameraProvider) { if (videoQualities.isEmpty()) { @@ -45,8 +45,8 @@ class VideoQualityManager( } fun getUserSelectedQuality(cameraSelector: CameraSelector): VideoQuality { - var selectionIndex = if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA) config.frontVideoResIndex else config.backVideoResIndex - selectionIndex = selectionIndex.coerceAtLeast(0) + val isFrontCamera = cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA + val selectionIndex = mediaSizeStore.getCurrentSizeIndex(isPhotoCapture = false, isFrontCamera = isFrontCamera).coerceAtLeast(0) return getSupportedQualities(cameraSelector).getOrElse(selectionIndex) { VideoQuality.HD } } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt index 2050280e..a5915fb4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt @@ -59,6 +59,7 @@ class CameraXPreview( private val videoQualityManager = VideoQualityManager(activity) private val imageQualityManager = ImageQualityManager(activity) private val exifRemover = ExifRemover(contentResolver) + private val mediaSizeStore = MediaSizeStore(config) private val orientationEventListener = object : OrientationEventListener(activity, SensorManager.SENSOR_DELAY_NORMAL) { @SuppressLint("RestrictedApi") @@ -359,10 +360,11 @@ class CameraXPreview( resolutions = resolutions, isPhotoCapture = isPhotoCapture, isFrontCamera = isFrontCameraInUse() - ) { changed -> + ) { index, changed -> if (changed) { currentRecording?.stop() } + mediaSizeStore.storeSize(isPhotoCapture, isFrontCameraInUse(), index) startCamera() } } else { @@ -371,19 +373,7 @@ class CameraXPreview( } private fun toggleResolutions(resolutions: List) { - val currentIndex = if (isPhotoCapture) { - if (isFrontCameraInUse()) { - config.frontPhotoResIndex - } else { - config.backPhotoResIndex - } - } else { - if (isFrontCameraInUse()) { - config.frontVideoResIndex - } else { - config.backVideoResIndex - } - } + val currentIndex = mediaSizeStore.getCurrentSizeIndex(isPhotoCapture, isFrontCameraInUse()) val nextIndex = if (currentIndex >= resolutions.lastIndex) { 0 @@ -391,19 +381,7 @@ class CameraXPreview( currentIndex + 1 } - if (isPhotoCapture) { - if (isFrontCameraInUse()) { - config.frontPhotoResIndex = nextIndex - } else { - config.backPhotoResIndex = nextIndex - } - } else { - if (isFrontCameraInUse()) { - config.frontVideoResIndex = nextIndex - } else { - config.backVideoResIndex = nextIndex - } - } + mediaSizeStore.storeSize(isPhotoCapture, isFrontCameraInUse(), nextIndex) currentRecording?.stop() startCamera() diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreviewListener.kt b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreviewListener.kt index 69d8cc3e..2e5fe335 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreviewListener.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreviewListener.kt @@ -26,7 +26,8 @@ interface CameraXPreviewListener { resolutions: List, isPhotoCapture: Boolean, isFrontCamera: Boolean, - onSelect: (changed:Boolean) -> Unit, + onSelect: (index: Int, changed: Boolean) -> Unit, ) + fun showFlashOptions(photoCapture: Boolean) }