From b4931989b330330fcc495b2e69b8b6ce59ed8c25 Mon Sep 17 00:00:00 2001 From: zhoubinjia Date: Thu, 18 Jun 2026 14:48:07 +0800 Subject: [PATCH] feat: modify hdr bracket side ev to 2.5 --- .../mycamera/camera/Camera2Controller.kt | 5 +- .../mycamera/camera/HdrBracketConfig.kt | 5 + .../hinnka/mycamera/gallery/GalleryManager.kt | 152 +++++++++++++++--- .../mycamera/viewmodel/GalleryViewModel.kt | 42 +++-- 4 files changed, 158 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/com/hinnka/mycamera/camera/HdrBracketConfig.kt diff --git a/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt b/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt index da97aa63..87ab19a6 100644 --- a/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt +++ b/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt @@ -80,7 +80,6 @@ class Camera2Controller(private val context: Context) { private const val BURST_CAPTURE_BATCH_SIZE = 8 private const val HDR_BRACKET_BASE_CAPTURE_COUNT = 3 private const val HDR_BRACKET_SIDE_FRAME_COUNT = 2 - private const val HDR_BRACKET_SIDE_EV = 2.0f // 拍照状态机常量 private const val STATE_PREVIEW = 0 // Showing camera preview. @@ -3990,8 +3989,8 @@ class Camera2Controller(private val context: Context) { private fun buildHdrBracketEvOffsets(zeroEvFrameCount: Int): List { return buildList { add(0f) - add(HDR_BRACKET_SIDE_EV) - add(-HDR_BRACKET_SIDE_EV) + add(HdrBracketConfig.SIDE_EV) + add(-HdrBracketConfig.SIDE_EV) repeat((zeroEvFrameCount - 1).coerceAtLeast(0)) { add(0f) } diff --git a/app/src/main/java/com/hinnka/mycamera/camera/HdrBracketConfig.kt b/app/src/main/java/com/hinnka/mycamera/camera/HdrBracketConfig.kt new file mode 100644 index 00000000..fe5bb0c4 --- /dev/null +++ b/app/src/main/java/com/hinnka/mycamera/camera/HdrBracketConfig.kt @@ -0,0 +1,5 @@ +package com.hinnka.mycamera.camera + +internal object HdrBracketConfig { + const val SIDE_EV = 2.5f +} diff --git a/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt b/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt index aa7b2931..61b725c5 100644 --- a/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt +++ b/app/src/main/java/com/hinnka/mycamera/gallery/GalleryManager.kt @@ -1,6 +1,7 @@ package com.hinnka.mycamera.gallery import android.app.PendingIntent +import android.content.ContentUris import android.content.ContentValues import android.content.Context import android.graphics.* @@ -15,6 +16,7 @@ import android.provider.MediaStore import androidx.core.graphics.createBitmap import androidx.exifinterface.media.ExifInterface import com.hinnka.mycamera.camera.AspectRatio +import com.hinnka.mycamera.camera.HdrBracketConfig import com.hinnka.mycamera.data.ContentRepository import com.hinnka.mycamera.gallery.db.GalleryMediaStore import com.hinnka.mycamera.hdr.GainmapResult @@ -89,11 +91,9 @@ object GalleryManager { private const val DETAIL_HDR_FILE = "detail_hdr.jpg" private const val MULTIPLE_EXPOSURE_DIR = "multiple_exposure_sessions" private const val MULTIPLE_EXPOSURE_PREVIEW_FILE = "preview.jpg" - private const val RAW_HDR_DNG_BASELINE_EXPOSURE_EV_FALLBACK = 2.0f private const val HDR_BRACKET_ZERO_INDEX = 0 private const val HDR_BRACKET_HIGH_INDEX = 1 private const val HDR_BRACKET_LOW_INDEX = 2 - private const val HDR_BRACKET_SIDE_EV = 2.0 private data class HdrBracketFrameSet( val zeroEvFrames: List, @@ -1930,9 +1930,10 @@ object GalleryManager { } private fun fallbackHdrExposureProduct(index: Int): Float { + val sideEv = HdrBracketConfig.SIDE_EV.toDouble() return when { - index == HDR_BRACKET_HIGH_INDEX -> Math.pow(2.0, HDR_BRACKET_SIDE_EV).toFloat() - index == HDR_BRACKET_LOW_INDEX -> Math.pow(2.0, -HDR_BRACKET_SIDE_EV).toFloat() + index == HDR_BRACKET_HIGH_INDEX -> Math.pow(2.0, sideEv).toFloat() + index == HDR_BRACKET_LOW_INDEX -> Math.pow(2.0, -sideEv).toFloat() else -> 1f } } @@ -2643,13 +2644,13 @@ object GalleryManager { ): Float { val referenceProduct = rawExposureProduct(referenceResult) .takeIf { it.isFinite() && it > 0.0 } - ?: return RAW_HDR_DNG_BASELINE_EXPOSURE_EV_FALLBACK + ?: return HdrBracketConfig.SIDE_EV val baseProduct = captureResults .mapNotNull { result -> rawExposureProduct(result).takeIf { it.isFinite() && it > 0.0 } } .minOrNull() - ?: return RAW_HDR_DNG_BASELINE_EXPOSURE_EV_FALLBACK + ?: return HdrBracketConfig.SIDE_EV val baselineExposureEv = if (baseProduct < referenceProduct) { (ln(referenceProduct / baseProduct) / ln(2.0)).toFloat() } else { @@ -3176,6 +3177,122 @@ object GalleryManager { } } + private fun isMediaStoreItemUri(uri: Uri): Boolean { + if (uri.scheme != "content" || uri.authority != MediaStore.AUTHORITY) return false + return runCatching { + ContentUris.parseId(uri) + true + }.getOrDefault(false) + } + + private fun deleteDocumentExportUri(context: Context, uri: Uri): Boolean { + return runCatching { + DocumentsContract.deleteDocument(context.contentResolver, uri) + }.onSuccess { deleted -> + if (deleted) { + PLog.d(TAG, "Deleted exported document URI: $uri") + } else { + PLog.w(TAG, "Document provider refused to delete exported URI: $uri") + } + }.onFailure { e -> + PLog.e(TAG, "Failed to delete exported document URI: $uri", e) + }.getOrDefault(false) + } + + private fun collectExportedDeleteUriStrings(metadata: MediaMetadata?): List { + return buildList { + addAll(metadata?.exportedUris ?: emptyList()) + val sourceUri = metadata?.sourceUri + if (metadata?.mediaType == MediaType.VIDEO && metadata.isImported != true && !sourceUri.isNullOrBlank()) { + add(sourceUri) + } + } + } + + private fun parseDeleteUris(uriStrings: Collection, logContext: String): List { + return uriStrings.mapNotNull { uriString -> + try { + Uri.parse(uriString) + } catch (e: Exception) { + PLog.e(TAG, "Invalid delete URI for $logContext: $uriString", e) + null + } + } + } + + private fun deleteDocumentExportUris( + context: Context, + uris: Collection, + logContext: String + ): Int { + var deletedCount = 0 + uris.distinctBy { it.toString() }.forEach { uri -> + if (uri.scheme == "content" && + !isMediaStoreItemUri(uri) && + DocumentsContract.isDocumentUri(context, uri) + ) { + if (deleteDocumentExportUri(context, uri)) { + deletedCount++ + } + } + } + if (deletedCount > 0) { + PLog.d(TAG, "Deleted $deletedCount exported document URIs for $logContext") + } + return deletedCount + } + + private fun createMediaStoreDeleteRequest( + context: Context, + uris: Collection, + logContext: String + ): PendingIntent? { + if (uris.isEmpty()) return null + + val mediaStoreUris = mutableListOf() + uris.distinctBy { it.toString() }.forEach { uri -> + when { + isMediaStoreItemUri(uri) -> mediaStoreUris.add(uri) + uri.scheme == "content" && DocumentsContract.isDocumentUri(context, uri) -> { + PLog.d(TAG, "Skipping document URI in MediaStore delete request for $logContext: $uri") + } + uri.scheme == "content" -> { + PLog.w(TAG, "Ignoring non-MediaStore delete URI for $logContext: $uri") + } + else -> { + PLog.w(TAG, "Ignoring non-content delete URI for $logContext: $uri") + } + } + } + + if (mediaStoreUris.isEmpty()) { + return null + } + + return try { + MediaStore.createDeleteRequest(context.contentResolver, mediaStoreUris) + } catch (e: Exception) { + PLog.e(TAG, "Failed to create MediaStore delete request for $logContext", e) + null + } + } + + fun createDeleteRequest(context: Context, uris: Collection): PendingIntent? { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { + PLog.w(TAG, "createDeleteRequest requires Android 11+") + return null + } + return createMediaStoreDeleteRequest(context, uris, "exported media") + } + + suspend fun deleteExportedDocumentUris(context: Context, photoId: String): Int { + return withContext(Dispatchers.IO) { + val metadata = loadMetadata(context, photoId) + val uris = parseDeleteUris(collectExportedDeleteUriStrings(metadata), "photo: $photoId") + deleteDocumentExportUris(context, uris, "photo: $photoId") + } + } + /** * 创建删除系统相册照片的请求(弹出确认对话框) * @@ -3193,13 +3310,7 @@ object GalleryManager { val metadata = runBlocking { loadMetadata(context, photoId) } - val exportedUris = buildList { - addAll(metadata?.exportedUris ?: emptyList()) - val sourceUri = metadata?.sourceUri - if (metadata?.mediaType == MediaType.VIDEO && metadata.isImported != true && !sourceUri.isNullOrBlank()) { - add(sourceUri) - } - } + val exportedUris = collectExportedDeleteUriStrings(metadata) if (exportedUris.isEmpty()) { PLog.d(TAG, "No exported URIs to delete for photo: $photoId") @@ -3207,25 +3318,14 @@ object GalleryManager { } // 将字符串 URI 转换为 Uri 对象列表,并过滤非法 URI - val uriList = exportedUris.mapNotNull { uriString -> - try { - val uri = Uri.parse(uriString) - if (uri.scheme == "content") uri else { - PLog.w(TAG, "Ignoring non-content URI: $uriString") - null - } - } catch (e: Exception) { - PLog.e(TAG, "Invalid URI: $uriString", e) - null - } - } + val uriList = parseDeleteUris(exportedUris, "photo: $photoId") if (uriList.isEmpty()) { return null } // 创建删除请求(会弹出系统确认对话框) - MediaStore.createDeleteRequest(context.contentResolver, uriList) + createMediaStoreDeleteRequest(context, uriList, "photo: $photoId") } catch (e: Exception) { PLog.e(TAG, "Failed to create delete request for photo: $photoId", e) null diff --git a/app/src/main/java/com/hinnka/mycamera/viewmodel/GalleryViewModel.kt b/app/src/main/java/com/hinnka/mycamera/viewmodel/GalleryViewModel.kt index 3f8c56d1..cb98481d 100644 --- a/app/src/main/java/com/hinnka/mycamera/viewmodel/GalleryViewModel.kt +++ b/app/src/main/java/com/hinnka/mycamera/viewmodel/GalleryViewModel.kt @@ -1290,18 +1290,24 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application) deletePendingIntent = pendingIntent PLog.d(TAG, "Set delete pending intent for photo ${photo.id}") } else { - // 没有导出的照片,直接删除应用内照片 - deletePhotoOnlyInternal(photo) + // 没有需要系统确认的 MediaStore 项,直接删除 SAF 导出文件和应用内照片 + deletePhotoOnlyInternal(photo, deleteExportedDocuments = true) } } /** * 仅删除应用内部照片(不删除系统相册) */ - private fun deletePhotoOnlyInternal(photo: MediaData) { + private fun deletePhotoOnlyInternal( + photo: MediaData, + deleteExportedDocuments: Boolean = false + ) { viewModelScope.launch { val context = getApplication() val success = withContext(Dispatchers.IO) { + if (deleteExportedDocuments) { + GalleryManager.deleteExportedDocumentUris(context, photo.id) + } GalleryManager.deletePhotoOnly(context, photo.id) } if (success) { @@ -1344,6 +1350,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application) viewModelScope.launch { val context = getApplication() val success = withContext(Dispatchers.IO) { + GalleryManager.deleteExportedDocumentUris(context, photo.id) GalleryManager.deletePhotoOnly(context, photo.id) } if (success) { @@ -1489,22 +1496,16 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application) } } - val validExportedUris = allExportedUris.filter { it.scheme == "content" } - - if (validExportedUris.isNotEmpty()) { - // 有导出的照片,需要用户确认 - try { - val pendingIntent = MediaStore.createDeleteRequest( - context.contentResolver, - validExportedUris - ) + if (allExportedUris.isNotEmpty()) { + // MediaStore 导出项需要系统确认;SAF 文档会在最终删除时直接处理 + val pendingIntent = GalleryManager.createDeleteRequest(context, allExportedUris) + if (pendingIntent != null) { pendingDeletePhotos = toDelete batchDeletePendingIntent = pendingIntent PLog.d(TAG, "Set batch delete pending intent for ${toDelete.size} photos") - } catch (e: Exception) { - PLog.w(TAG, "Failed to create batch delete request", e) - // 创建请求失败,直接删除应用内照片 - deleteBatchPhotosOnlyInternal(toDelete) + } else { + // 没有需要系统确认的 MediaStore 项,直接删除应用内照片 + deleteBatchPhotosOnlyInternal(toDelete, deleteExportedDocuments = true) } } else { // 没有导出的照片,直接删除应用内照片 @@ -1516,7 +1517,10 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application) /** * 仅删除应用内部照片(批量,不删除系统相册) */ - private fun deleteBatchPhotosOnlyInternal(photos: List) { + private fun deleteBatchPhotosOnlyInternal( + photos: List, + deleteExportedDocuments: Boolean = false + ) { viewModelScope.launch { val context = getApplication() var deletedCount = 0 @@ -1524,6 +1528,9 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application) withContext(Dispatchers.IO) { photos.forEach { photo -> + if (deleteExportedDocuments) { + GalleryManager.deleteExportedDocumentUris(context, photo.id) + } val success = GalleryManager.deletePhotoOnly(context, photo.id) if (success) { deletedCount++ @@ -1568,6 +1575,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application) withContext(Dispatchers.IO) { photos.forEach { photo -> + GalleryManager.deleteExportedDocumentUris(context, photo.id) val success = GalleryManager.deletePhotoOnly(context, photo.id) if (success) { deletedCount++