feat: photo picker migration and Live Photo support
This commit is contained in:
1 parent
e175865f75
commit
87c9558d66
8 files changed
+84
-107
No files matched your search
@@ -335,7 +335,7 @@ fun CameraScreen(
|
||||
)
|
||||
|
||||
val dcpImportLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.GetMultipleContents(),
|
||||
contract = ActivityResultContracts.OpenMultipleDocuments(),
|
||||
onResult = { uris ->
|
||||
if (uris.isNotEmpty()) {
|
||||
viewModel.importRawDcps(uris) { _, _ -> }
|
||||
@@ -1678,7 +1678,7 @@ fun CameraScreen(
|
||||
onRawDcpChange = { viewModel.setRawDcpId(it) },
|
||||
onRawDcpIdsByLensChange = { viewModel.setRawDcpIdsByLens(it) },
|
||||
onRawHncsFilmCurveModeChange = { viewModel.setRawHncsFilmCurveMode(it) },
|
||||
onImportRawDcp = { dcpImportLauncher.launch("*/*") },
|
||||
onImportRawDcp = { dcpImportLauncher.launch(arrayOf("*/*")) },
|
||||
onDeleteRawDcp = { dcp ->
|
||||
viewModel.deleteRawDcp(dcp.id) { success ->
|
||||
android.widget.Toast.makeText(
|
||||
|
||||
@@ -8,6 +8,7 @@ import android.os.Environment
|
||||
import android.provider.MediaStore
|
||||
import android.widget.Toast
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.PickVisualMediaRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
@@ -107,7 +108,7 @@ fun PuzzleScreen(
|
||||
|
||||
// 多图选择 Launcher
|
||||
val imagePickerLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.GetMultipleContents()
|
||||
contract = ActivityResultContracts.PickMultipleVisualMedia()
|
||||
) { uris ->
|
||||
if (uris.isNotEmpty()) {
|
||||
selectedPhotoUris.addAll(uris)
|
||||
@@ -185,7 +186,7 @@ fun PuzzleScreen(
|
||||
// 首次进入界面若无照片则自动调起选择器,极佳用户体验
|
||||
LaunchedEffect(Unit) {
|
||||
if (selectedPhotoUris.isEmpty()) {
|
||||
imagePickerLauncher.launch("image/*")
|
||||
imagePickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,7 +286,9 @@ fun PuzzleScreen(
|
||||
.fillMaxWidth()
|
||||
.height(280.dp)
|
||||
.clip(RoundedCornerShape(16.dp))
|
||||
.clickable { imagePickerLauncher.launch("image/*") },
|
||||
.clickable {
|
||||
imagePickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
|
||||
},
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = Color.White.copy(alpha = 0.05f)
|
||||
)
|
||||
@@ -423,7 +426,9 @@ fun PuzzleScreen(
|
||||
modifier = Modifier
|
||||
.clip(RoundedCornerShape(12.dp))
|
||||
.background(Color.White.copy(alpha = 0.08f))
|
||||
.clickable { imagePickerLauncher.launch("image/*") }
|
||||
.clickable {
|
||||
imagePickerLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
|
||||
}
|
||||
.padding(horizontal = 12.dp, vertical = 6.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(4.dp)
|
||||
|
||||
@@ -485,7 +485,7 @@ fun GalleryEditScreen(
|
||||
}
|
||||
|
||||
val rawDcpLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.GetMultipleContents()
|
||||
contract = ActivityResultContracts.OpenMultipleDocuments()
|
||||
) { uris ->
|
||||
if (uris.isEmpty()) return@rememberLauncherForActivityResult
|
||||
val photo = editSourcePhoto ?: return@rememberLauncherForActivityResult
|
||||
@@ -1769,7 +1769,7 @@ fun GalleryEditScreen(
|
||||
}
|
||||
},
|
||||
onImportDcp = {
|
||||
rawDcpLauncher.launch("*/*")
|
||||
rawDcpLauncher.launch(arrayOf("*/*"))
|
||||
},
|
||||
onDeleteDcp = { dcp ->
|
||||
val isDeletingSelectedDcp = editRawDcpId == dcp.id
|
||||
|
||||
@@ -2,13 +2,9 @@ package com.hinnka.mycamera.ui.gallery
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.provider.MediaStore
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import org.json.JSONArray
|
||||
import android.graphics.Rect
|
||||
import android.graphics.Bitmap
|
||||
import android.widget.Toast
|
||||
@@ -23,6 +19,7 @@ import android.widget.LinearLayout
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.IntentSenderRequest
|
||||
import androidx.activity.result.PickVisualMediaRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.fadeIn
|
||||
@@ -82,47 +79,9 @@ import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import android.os.Parcelable
|
||||
import org.json.JSONObject
|
||||
import kotlin.math.abs
|
||||
import androidx.core.net.toUri
|
||||
import com.hinnka.mycamera.ui.icons.AppIcons
|
||||
|
||||
private const val GALLERY_SCREEN_TAG = "GalleryScreen"
|
||||
|
||||
private fun buildSystemGalleryMediaPickIntent(context: Context): Intent {
|
||||
val systemGalleryIntent = buildSystemGalleryActionPickIntent()
|
||||
if (systemGalleryIntent.hasAvailableActivity(context)) {
|
||||
return systemGalleryIntent
|
||||
}
|
||||
|
||||
PLog.d(GALLERY_SCREEN_TAG, "ACTION_PICK media picker unavailable, falling back to ACTION_GET_CONTENT")
|
||||
return buildGetContentMediaPickIntent()
|
||||
}
|
||||
|
||||
private fun buildSystemGalleryActionPickIntent(): Intent {
|
||||
return Intent(Intent.ACTION_PICK).apply {
|
||||
setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "*/*")
|
||||
putExtra(Intent.EXTRA_MIME_TYPES, buildGalleryMediaMimeTypes())
|
||||
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildGetContentMediaPickIntent(): Intent {
|
||||
return Intent(Intent.ACTION_GET_CONTENT).apply {
|
||||
type = "*/*"
|
||||
putExtra(Intent.EXTRA_MIME_TYPES, buildGalleryMediaMimeTypes())
|
||||
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildGalleryMediaMimeTypes(): Array<String> {
|
||||
return arrayOf("image/*", "video/*")
|
||||
}
|
||||
|
||||
private fun Intent.hasAvailableActivity(context: Context): Boolean {
|
||||
return resolveActivity(context.packageManager) != null
|
||||
}
|
||||
|
||||
/**
|
||||
* 相册浏览界面
|
||||
*/
|
||||
@@ -167,20 +126,16 @@ fun GalleryScreen(
|
||||
}
|
||||
}
|
||||
|
||||
fun importSelectedMedia(uris: List<Uri>, videoUris: List<Uri?>? = null) {
|
||||
fun importSelectedMedia(selection: SelectedVisualMedia) {
|
||||
val uris = selection.uris
|
||||
if (uris.isEmpty()) return
|
||||
val uniqueUris = uris.distinct().also { distinct ->
|
||||
if (distinct.size != uris.size) {
|
||||
PLog.d("GalleryScreen", "Ignoring ${uris.size - distinct.size} duplicate selected media URI(s)")
|
||||
}
|
||||
}
|
||||
val alignedVideoUris = videoUris?.let { uriVideoMap ->
|
||||
uniqueUris.map { uri ->
|
||||
val originalIndex = uris.indexOf(uri)
|
||||
uriVideoMap.getOrNull(originalIndex)
|
||||
}
|
||||
}
|
||||
viewModel.importPhotos(uniqueUris, alignedVideoUris) { importedIds ->
|
||||
val videoUris = uniqueUris.map { selection.pairedVideoUris[it] }
|
||||
viewModel.importPhotos(uniqueUris, videoUris) { importedIds ->
|
||||
if (importedIds.size == 1) {
|
||||
val newPhotoId = importedIds.first()
|
||||
viewModel.setCurrentPhotoById(newPhotoId)
|
||||
@@ -191,46 +146,13 @@ fun GalleryScreen(
|
||||
}
|
||||
|
||||
val launcher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
result.data?.let { data ->
|
||||
PLog.d("GalleryScreen", "Received ACTION_PICK result. Data URI: ${data.data}, ClipData count: ${data.clipData?.itemCount ?: 0}")
|
||||
|
||||
val mediaInfosStrs = data.getStringArrayListExtra("selected_media_infos")
|
||||
val vivoLivePhotosMap = mutableMapOf<Uri, Uri>()
|
||||
if (!mediaInfosStrs.isNullOrEmpty()) {
|
||||
try {
|
||||
for (text in mediaInfosStrs) {
|
||||
val jsonObject = JSONObject(text)
|
||||
val mainUriStr = jsonObject.optString("mainUri")
|
||||
val extraUriStr = jsonObject.optString("extraUri")
|
||||
if (!mainUriStr.isNullOrBlank() && !extraUriStr.isNullOrBlank()) {
|
||||
vivoLivePhotosMap[mainUriStr.toUri()] = extraUriStr.toUri()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e("GalleryScreen", "Failed to parse selected_media_infos", e)
|
||||
}
|
||||
}
|
||||
|
||||
val uris = mutableListOf<Uri>()
|
||||
val videoUris = mutableListOf<Uri?>()
|
||||
fun addSelectedUri(uri: Uri) {
|
||||
uris.add(uri)
|
||||
videoUris.add(vivoLivePhotosMap[uri])
|
||||
}
|
||||
|
||||
data.data?.let(::addSelectedUri)
|
||||
data.clipData?.let { clipData ->
|
||||
for (i in 0 until clipData.itemCount) {
|
||||
clipData.getItemAt(i).uri?.let(::addSelectedUri)
|
||||
}
|
||||
}
|
||||
|
||||
importSelectedMedia(uris, videoUris)
|
||||
}
|
||||
}
|
||||
contract = PickMultipleVisualMediaWithLivePhoto()
|
||||
) { selection ->
|
||||
PLog.d(
|
||||
"GalleryScreen",
|
||||
"Received photo picker result: ${selection.uris.size} media URI(s), ${selection.pairedVideoUris.size} Live Photo pair(s)"
|
||||
)
|
||||
importSelectedMedia(selection)
|
||||
}
|
||||
|
||||
val permissionLauncher = rememberLauncherForActivityResult(
|
||||
@@ -368,7 +290,7 @@ fun GalleryScreen(
|
||||
}
|
||||
} else {
|
||||
IconButton(onClick = {
|
||||
launcher.launch(buildSystemGalleryMediaPickIntent(context))
|
||||
launcher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo))
|
||||
}) {
|
||||
Icon(
|
||||
imageVector = AppIcons.AddPhotoAlternate,
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.hinnka.mycamera.ui.gallery
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import androidx.activity.result.PickVisualMediaRequest
|
||||
import androidx.activity.result.contract.ActivityResultContract
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.net.toUri
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
|
||||
internal data class SelectedVisualMedia(
|
||||
val uris: List<Uri>,
|
||||
val pairedVideoUris: Map<Uri, Uri> = emptyMap(),
|
||||
)
|
||||
|
||||
/** Keeps vivo Live Photo pairs when the picker includes them in its result. */
|
||||
internal class PickMultipleVisualMediaWithLivePhoto :
|
||||
ActivityResultContract<PickVisualMediaRequest, SelectedVisualMedia>() {
|
||||
private val picker = ActivityResultContracts.PickMultipleVisualMedia()
|
||||
|
||||
override fun createIntent(context: Context, input: PickVisualMediaRequest): Intent =
|
||||
picker.createIntent(context, input)
|
||||
|
||||
override fun parseResult(resultCode: Int, intent: Intent?): SelectedVisualMedia {
|
||||
val uris = picker.parseResult(resultCode, intent)
|
||||
if (uris.isEmpty()) return SelectedVisualMedia(uris)
|
||||
|
||||
val selectedUris = uris.toSet()
|
||||
val pairedVideoUris = mutableMapOf<Uri, Uri>()
|
||||
intent?.getStringArrayListExtra("selected_media_infos")?.forEach { text ->
|
||||
try {
|
||||
val mediaInfo = JSONObject(text)
|
||||
val mainUri = mediaInfo.optString("mainUri")
|
||||
val extraUri = mediaInfo.optString("extraUri")
|
||||
if (mainUri.isNotBlank() && extraUri.isNotBlank()) {
|
||||
val imageUri = mainUri.toUri()
|
||||
if (imageUri in selectedUris) {
|
||||
pairedVideoUris[imageUri] = extraUri.toUri()
|
||||
}
|
||||
}
|
||||
} catch (e: JSONException) {
|
||||
PLog.e("GalleryMediaPicker", "Failed to parse selected_media_infos entry", e)
|
||||
}
|
||||
}
|
||||
return SelectedVisualMedia(uris, pairedVideoUris)
|
||||
}
|
||||
}
|
||||
@@ -294,7 +294,7 @@ fun FilterManagementScreen(
|
||||
|
||||
// 批量文件选择器
|
||||
val lutFilePicker = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.GetMultipleContents()
|
||||
contract = ActivityResultContracts.OpenMultipleDocuments()
|
||||
) { uris: List<Uri> ->
|
||||
if (uris.isNotEmpty()) {
|
||||
pendingImportUris = uris
|
||||
@@ -527,7 +527,7 @@ fun FilterManagementScreen(
|
||||
// 导入按钮
|
||||
IconButton(
|
||||
onClick = {
|
||||
lutFilePicker.launch("*/*")
|
||||
lutFilePicker.launch(arrayOf("*/*"))
|
||||
},
|
||||
enabled = !isImporting
|
||||
) {
|
||||
|
||||
@@ -108,7 +108,7 @@ fun PresetManagementScreen(
|
||||
val lazyListState = rememberLazyListState()
|
||||
|
||||
val presetFilePicker = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.GetMultipleContents()
|
||||
contract = ActivityResultContracts.OpenMultipleDocuments()
|
||||
) { uris: List<Uri> ->
|
||||
if (uris.isNotEmpty() && !isImporting) {
|
||||
isImporting = true
|
||||
@@ -238,7 +238,7 @@ fun PresetManagementScreen(
|
||||
)
|
||||
}
|
||||
IconButton(
|
||||
onClick = { presetFilePicker.launch("*/*") },
|
||||
onClick = { presetFilePicker.launch(arrayOf("*/*")) },
|
||||
enabled = !isImporting,
|
||||
) {
|
||||
if (isImporting) {
|
||||
|
||||
@@ -628,7 +628,7 @@ fun SettingsScreen(
|
||||
}
|
||||
|
||||
val importDcpLauncher = rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.GetMultipleContents()
|
||||
ActivityResultContracts.OpenMultipleDocuments()
|
||||
) { uris ->
|
||||
if (uris.isNotEmpty()) {
|
||||
viewModel.importRawDcps(uris) { importedDcps, failedCount ->
|
||||
@@ -667,7 +667,7 @@ fun SettingsScreen(
|
||||
}
|
||||
|
||||
val importRawNoiseProfileLauncher = rememberLauncherForActivityResult(
|
||||
ActivityResultContracts.GetMultipleContents(),
|
||||
ActivityResultContracts.OpenMultipleDocuments(),
|
||||
) { uris ->
|
||||
if (uris.isNotEmpty()) {
|
||||
viewModel.importRawNoiseProfiles(uris) { importedProfiles, failedCount ->
|
||||
@@ -2196,7 +2196,7 @@ fun SettingsScreen(
|
||||
spectralFilmPrint = rawSpectralFilmPrint ?: "kodak_portra_endura",
|
||||
onSelectDcp = { viewModel.setRawDcpId(it) },
|
||||
onRawDcpIdsByLensChange = { viewModel.setRawDcpIdsByLens(it) },
|
||||
onImportDcp = { importDcpLauncher.launch("*/*") },
|
||||
onImportDcp = { importDcpLauncher.launch(arrayOf("*/*")) },
|
||||
onDeleteDcp = { dcp ->
|
||||
viewModel.deleteRawDcp(dcp.id) { success ->
|
||||
android.widget.Toast.makeText(
|
||||
@@ -2231,7 +2231,7 @@ fun SettingsScreen(
|
||||
onSelectRawNoiseProfile = viewModel::setRawNoiseProfileId,
|
||||
onRawNoiseProfileIdsByLensChange = viewModel::setRawNoiseProfileIdsByLens,
|
||||
onImportRawNoiseProfile = {
|
||||
importRawNoiseProfileLauncher.launch("*/*")
|
||||
importRawNoiseProfileLauncher.launch(arrayOf("*/*"))
|
||||
},
|
||||
onDeleteRawNoiseProfile = { profile ->
|
||||
viewModel.deleteRawNoiseProfile(profile.id) { success ->
|
||||
|
||||
Reference in new issue
Block a user