Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add audio asset metadata (WPB-3334) #2080

Merged
merged 6 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.wire.android.ui.navArgs
import com.wire.android.util.FileManager
import com.wire.android.util.ImageUtil
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.getAudioLengthInMs
import com.wire.kalium.logic.configuration.FileSharingStatus
import com.wire.kalium.logic.data.asset.AttachmentType
import com.wire.kalium.logic.data.asset.KaliumFileSystem
Expand Down Expand Up @@ -305,7 +306,8 @@ class MessageComposerViewModel @Inject constructor(
assetWidth = imgWidth,
assetHeight = imgHeight,
assetDataSize = dataSize,
assetMimeType = mimeType
assetMimeType = mimeType,
audioLengthInMs = 0L
)
}

Expand All @@ -320,7 +322,11 @@ class MessageComposerViewModel @Inject constructor(
assetMimeType = mimeType,
assetDataSize = dataSize,
assetHeight = null,
assetWidth = null
assetWidth = null,
audioLengthInMs = getAudioLengthInMs(
dataPath = dataPath,
mimeType = mimeType
)
)
} catch (e: OutOfMemoryError) {
appLogger.e("There was an OutOfMemory error while uploading the asset")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@
import com.wire.android.media.audiomessage.AudioState
import com.wire.android.media.audiomessage.RecordAudioMessagePlayer
import com.wire.android.ui.home.conversations.model.UriAsset
import com.wire.android.util.AUDIO_MIME_TYPE
import com.wire.android.util.CurrentScreen
import com.wire.android.util.CurrentScreenManager
import com.wire.android.util.getAudioLengthInMs
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import okio.Path.Companion.toPath
import java.io.File
import javax.inject.Inject
import kotlin.io.path.deleteIfExists
Expand Down Expand Up @@ -143,10 +146,21 @@

fun stopRecording() {
if (state.buttonState == RecordAudioButtonState.RECORDING) {
audioMediaRecorder.stop()

state = state.copy(
buttonState = RecordAudioButtonState.READY_TO_SEND
buttonState = RecordAudioButtonState.READY_TO_SEND,
audioState = AudioState.DEFAULT.copy(
totalTimeInMs = AudioState.TotalTimeInMs.Known(
state.outputFile?.let {
getAudioLengthInMs(
dataPath = it.path.toPath(),
mimeType = AUDIO_MIME_TYPE
).toInt()
} ?: 0

Check warning on line 160 in app/src/main/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModel.kt#L160

Added line #L160 was not covered by tests
)
)
)
audioMediaRecorder.stop()
}
audioMediaRecorder.release()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.wire.android.util.FileManager
import com.wire.android.util.ImageUtil
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.android.util.getAudioLengthInMs
import com.wire.android.util.getMetadataFromUri
import com.wire.android.util.getMimeType
import com.wire.android.util.isImageFile
Expand Down Expand Up @@ -321,6 +322,10 @@
assetMimeType = importedAsset.mimeType,
assetWidth = if (isImage) (importedAsset as ImportedMediaAsset.Image).width else 0,
assetHeight = if (isImage) (importedAsset as ImportedMediaAsset.Image).height else 0,
audioLengthInMs = getAudioLengthInMs(
dataPath = importedAsset.dataPath,
mimeType = importedAsset.mimeType

Check warning on line 327 in app/src/main/kotlin/com/wire/android/ui/sharing/ImportMediaAuthenticatedViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/com/wire/android/ui/sharing/ImportMediaAuthenticatedViewModel.kt#L325-L327

Added lines #L325 - L327 were not covered by tests
)
).also {
if (it is ScheduleNewAssetMessageResult.Failure) {
appLogger.e("Failed to import asset message to conversationId=${conversation.conversationId.toLogString()} ")
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/kotlin/com/wire/android/util/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import android.content.Context
import android.content.Intent
import android.database.Cursor
import android.graphics.drawable.Drawable
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.os.Build
import android.os.Environment
Expand All @@ -54,6 +55,7 @@ import com.wire.android.util.ImageUtil.ImageSizeClass
import com.wire.android.util.ImageUtil.ImageSizeClass.Medium
import com.wire.android.util.dispatchers.DefaultDispatcherProvider
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.asset.isAudioMimeType
import com.wire.kalium.logic.util.buildFileName
import com.wire.kalium.logic.util.splitFileExtensionAndCopyCounter
import kotlinx.coroutines.withContext
Expand All @@ -62,6 +64,7 @@ import java.io.File
import java.io.FileNotFoundException
import java.io.InputStream
import java.util.Locale
import kotlin.time.Duration.Companion.milliseconds

/**
* Gets the uri of any drawable or given resource
Expand Down Expand Up @@ -414,6 +417,18 @@ fun findFirstUniqueName(dir: File, desiredName: String): String {
return currentName
}

fun getAudioLengthInMs(dataPath: Path, mimeType: String): Long =
if (isAudioMimeType(mimeType)) {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(dataPath.toFile().absolutePath)
val rawDuration = retriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
?.toLong() ?: 0L
rawDuration.milliseconds.inWholeMilliseconds
} else {
0L
}

private const val ATTACHMENT_FILENAME = "attachment"
private const val DATA_COPY_BUFFER_SIZE = 2048
const val SDK_VERSION = 33
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ internal class MessageComposerViewModelArrangement {
any(),
any(),
any(),
any(),
any()
)
} returns ScheduleNewAssetMessageResult.Success("some-message-id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand Down Expand Up @@ -228,6 +229,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand All @@ -254,6 +256,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand Down Expand Up @@ -293,6 +296,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand Down Expand Up @@ -333,6 +337,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand Down Expand Up @@ -374,6 +379,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand Down Expand Up @@ -418,6 +424,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand Down Expand Up @@ -455,6 +462,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand Down Expand Up @@ -559,6 +567,7 @@ class MessageComposerViewModelTest {
any(),
any(),
any(),
any(),
any()
)
}
Expand Down
2 changes: 1 addition & 1 deletion kalium
Submodule kalium updated 38 files
+6 −4 gradle/libs.versions.toml
+1 −1 logic/src/commonMain/kotlin/com/wire/kalium/logic/data/asset/Asset.kt
+7 −0 logic/src/commonMain/kotlin/com/wire/kalium/logic/data/asset/AssetMapper.kt
+16 −2 logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCase.kt
+93 −0 logic/src/commonTest/kotlin/com/wire/kalium/logic/data/asset/AssetMapperTest.kt
+20 −10 logic/src/commonTest/kotlin/com/wire/kalium/logic/feature/asset/ScheduleNewAssetMessageUseCaseTest.kt
+15 −0 logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/dao/MemberDAOArrangement.kt
+47 −0 .../src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/repository/ConnectionRepositoryArrangement.kt
+31 −0 ...rc/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/repository/ConversationRepositoryArrangement.kt
+31 −0 logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/repository/UserRepositoryArrangement.kt
+43 −0 logic/src/commonTest/kotlin/com/wire/kalium/logic/util/arrangement/usecase/PersistMessageUseCaseArrangement.kt
+12 −1 monkeys/README.md
+16 −0 monkeys/build.gradle.kts
+94 −75 monkeys/example.json
+34 −9 monkeys/schema.json
+14 −9 monkeys/src/main/kotlin/com/wire/kalium/monkeys/ActionScheduler.kt
+14 −0 monkeys/src/main/kotlin/com/wire/kalium/monkeys/MonkeyApplication.kt
+8 −8 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/Action.kt
+8 −3 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/AddUserToConversationAction.kt
+7 −3 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/CreateConversationAction.kt
+4 −3 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/DestroyConversationAction.kt
+10 −3 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/LeaveConversationAction.kt
+11 −4 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/LoginAction.kt
+9 −3 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/ReconnectAction.kt
+51 −2 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/SendMessageAction.kt
+14 −3 monkeys/src/main/kotlin/com/wire/kalium/monkeys/actions/SendRequestAction.kt
+75 −14 monkeys/src/main/kotlin/com/wire/kalium/monkeys/conversation/Monkey.kt
+25 −11 monkeys/src/main/kotlin/com/wire/kalium/monkeys/conversation/MonkeyConversation.kt
+21 −5 monkeys/src/main/kotlin/com/wire/kalium/monkeys/importer/TestData.kt
+71 −5 monkeys/src/main/kotlin/com/wire/kalium/monkeys/pool/ConversationPool.kt
+50 −26 monkeys/src/main/kotlin/com/wire/kalium/monkeys/pool/MonkeyPool.kt
+39 −0 monkeys/src/test/kotlin/com/wire/kalium/monkeys/actions/AddUserToConversationActionTest.kt
+54 −0 monkeys/src/test/kotlin/com/wire/kalium/monkeys/actions/LeaveConversationActionTest.kt
+41 −0 monkeys/src/test/kotlin/com/wire/kalium/monkeys/actions/LoginActionTest.kt
+100 −0 monkeys/src/test/kotlin/com/wire/kalium/monkeys/actions/SendMessageActionTest.kt
+49 −0 monkeys/src/test/kotlin/com/wire/kalium/monkeys/actions/SendRequestActionTest.kt
+0 −9 persistence/src/commonMain/kotlin/com/wire/kalium/persistence/dao/member/MemberDAO.kt
+2 −0 testservice/src/main/kotlin/com/wire/kalium/testservice/managed/ConversationRepository.kt
Loading