callRecord
This commit is contained in:
@@ -14,6 +14,7 @@ import com.example.initiateaphonecallapp.data.repository.UploadRepository
|
||||
import com.example.initiateaphonecallapp.enums.CallStatus
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
@@ -148,6 +149,44 @@ object CallRecordManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动查找通话录音(带重试)
|
||||
*/
|
||||
private suspend fun findCallRecordingWithRetry(
|
||||
context: Context,
|
||||
phoneNumber: String,
|
||||
maxRetry: Int = 20, // 查询 6 次
|
||||
delayMs: Long = 1000L // 每次间隔 1.5 秒
|
||||
): AudioFile? {
|
||||
|
||||
var attempt = 0
|
||||
|
||||
while (attempt < maxRetry) {
|
||||
println("🔍 第 ${attempt + 1} 次扫描通话录音...")
|
||||
|
||||
val files = queryAudioInTargetDirs(context)
|
||||
if (files.isNotEmpty()) {
|
||||
|
||||
// 最新文件
|
||||
val latest = files.maxByOrNull { it.dateModified }
|
||||
if (latest != null) {
|
||||
val fix = latest.displayName.filter { it.isDigit() }
|
||||
println("✅ 最新录音文件: ${latest.displayName}, fix: $fix")
|
||||
if (fix.contains(phoneNumber)) {
|
||||
println("✅ 录音文件匹配成功: ${latest.displayName}")
|
||||
return latest
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attempt++
|
||||
delay(delayMs)
|
||||
}
|
||||
|
||||
println("❌ 重试 $maxRetry 次后仍未找到录音文件")
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动查找并关联通话录音文件(仅在通话接通时调用)
|
||||
*/
|
||||
@@ -164,56 +203,21 @@ object CallRecordManager {
|
||||
return
|
||||
}
|
||||
|
||||
// 使用 AudioQueryManager 的查询逻辑
|
||||
val audioFiles = queryAudioInTargetDirs(context)
|
||||
val audio = findCallRecordingWithRetry(context = context, phoneNumber = callRecord.phoneNumber)
|
||||
|
||||
if (audioFiles.isNotEmpty()) {
|
||||
// 按修改时间倒序排序,选择最新的文件
|
||||
val sortedFiles = audioFiles.sortedByDescending { it.dateModified }
|
||||
val latestFile = sortedFiles.first()
|
||||
|
||||
println("✅ CallRecordManager: 找到 ${audioFiles.size} 个音频文件,选择最新的:")
|
||||
println(" 🎵 文件名: ${latestFile.displayName}")
|
||||
println(" 📁 路径: ${latestFile.filePath}")
|
||||
println(" 🕐 修改时间: ${latestFile.dateModified}")
|
||||
println(" 📊 文件大小: ${latestFile.size} 字节")
|
||||
|
||||
// 判断latestFile文件名是否匹配callRecord.phoneNumber
|
||||
val fileNameLower = latestFile.displayName.lowercase()
|
||||
val phoneLower = callRecord.phoneNumber.lowercase()
|
||||
|
||||
if (!fileNameLower.contains(phoneLower)) {
|
||||
println("⚠️ 最新录音文件与当前手机号不匹配,忽略此文件")
|
||||
println(" ❌ 文件名: ${latestFile.displayName}")
|
||||
println(" ❌ 不包含号码: ${callRecord.phoneNumber}")
|
||||
|
||||
// 上传没有录音的通话记录
|
||||
_recentCall.value = callRecord.copy(
|
||||
audioFileName = null,
|
||||
audioFileUri = null
|
||||
)
|
||||
uploadCallRecord(callRecord)
|
||||
return
|
||||
}
|
||||
|
||||
// 创建 SelectedAudioFile
|
||||
val selectedAudioFile = SelectedAudioFile(
|
||||
uri = latestFile.contentUri,
|
||||
fileName = latestFile.displayName
|
||||
)
|
||||
|
||||
// 设置选择的音频文件(这会触发上传)
|
||||
withContext(Dispatchers.Main) {
|
||||
setSelectedAudioFile(selectedAudioFile)
|
||||
}
|
||||
} else {
|
||||
println("❌ CallRecordManager: 目标目录下没有任何音频文件")
|
||||
// 即使没有找到录音文件,也要上传通话记录
|
||||
_recentCall.value = callRecord.copy(
|
||||
audioFileName = null,
|
||||
audioFileUri = null
|
||||
)
|
||||
if (audio == null) {
|
||||
println("⚠️ 未找到录音,直接上传通话记录")
|
||||
uploadCallRecord(callRecord)
|
||||
return
|
||||
}
|
||||
|
||||
val selected = SelectedAudioFile(
|
||||
uri = audio.contentUri,
|
||||
fileName = audio.displayName
|
||||
)
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
setSelectedAudioFile(selected)
|
||||
}
|
||||
|
||||
} catch (e: Exception) {
|
||||
@@ -485,7 +489,6 @@ object CallRecordManager {
|
||||
val mimeType = cursor.getString(mimeTypeColumn) ?: "unknown"
|
||||
|
||||
if (filePath != null && isInTargetDirectory(filePath, targetDirectories)) {
|
||||
Log.d(TAG, "✅ MediaStore 找到文件: $filePath")
|
||||
|
||||
val contentUri = ContentUris.withAppendedId(
|
||||
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
|
||||
|
||||
Reference in New Issue
Block a user