diff --git a/app/src/main/java/org/thoughtcrime/securesms/linkdevice/LinkDeviceRepository.kt b/app/src/main/java/org/thoughtcrime/securesms/linkdevice/LinkDeviceRepository.kt index 7c8566c6cc..ab381a3a7d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/linkdevice/LinkDeviceRepository.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/linkdevice/LinkDeviceRepository.kt @@ -190,6 +190,8 @@ object LinkDeviceRepository { * @param token Comes from [LinkDeviceResult.Success] */ fun waitForDeviceToBeLinked(token: String, maxWaitTime: Duration): WaitForLinkedDeviceResponse? { + Log.d(TAG, "[waitForDeviceToBeLinked] Starting to wait for device.") + val startTime = System.currentTimeMillis() var timeRemaining = maxWaitTime.inWholeMilliseconds @@ -206,6 +208,7 @@ object LinkDeviceRepository { return result.result } is NetworkResult.ApplicationError -> { + Log.e(TAG, "[waitForDeviceToBeLinked] Application error!", result.throwable) throw result.throwable } is NetworkResult.NetworkError -> { @@ -220,6 +223,9 @@ object LinkDeviceRepository { 429 -> { Log.w(TAG, "[waitForDeviceToBeLinked] Hit a rate-limit. Will try to wait again.") } + else -> { + Log.w(TAG, "[waitForDeviceToBeLinked] Hit an unknown status code of ${result.code}. Will try to wait again.") + } } } } @@ -235,13 +241,16 @@ object LinkDeviceRepository { * Performs the entire process of creating and uploading an archive for a newly-linked device. */ fun createAndUploadArchive(ephemeralMessageBackupKey: MessageBackupKey, deviceId: Int, deviceCreatedAt: Long): LinkUploadArchiveResult { + Log.d(TAG, "[createAndUploadArchive] Beginning process.") val stopwatch = Stopwatch("link-archive") val tempBackupFile = BlobProvider.getInstance().forNonAutoEncryptingSingleSessionOnDisk(AppDependencies.application) val outputStream = FileOutputStream(tempBackupFile) try { + Log.d(TAG, "[createAndUploadArchive] Starting the export.") BackupRepository.export(outputStream = outputStream, append = { tempBackupFile.appendBytes(it) }, messageBackupKey = ephemeralMessageBackupKey, mediaBackupEnabled = false) } catch (e: Exception) { + Log.w(TAG, "[createAndUploadArchive] Failed to export a backup!", e) return LinkUploadArchiveResult.BackupCreationFailure(e) } Log.d(TAG, "[createAndUploadArchive] Successfully created backup.") @@ -262,6 +271,7 @@ object LinkDeviceRepository { } stopwatch.split("validate-backup") + Log.d(TAG, "[createAndUploadArchive] Fetching an upload form...") val uploadForm = when (val result = SignalNetwork.attachments.getAttachmentV4UploadForm()) { is NetworkResult.Success -> result.result.logD(TAG, "[createAndUploadArchive] Successfully retrieved upload form.") is NetworkResult.ApplicationError -> throw result.throwable @@ -277,6 +287,7 @@ object LinkDeviceRepository { } stopwatch.split("upload-backup") + Log.d(TAG, "[createAndUploadArchive] Setting the transfer archive...") val transferSetResult = SignalNetwork.linkDevice.setTransferArchive( destinationDeviceId = deviceId, destinationDeviceCreated = deviceCreatedAt, @@ -286,7 +297,7 @@ object LinkDeviceRepository { when (transferSetResult) { is NetworkResult.Success -> Log.i(TAG, "[createAndUploadArchive] Successfully set transfer archive.") - is NetworkResult.ApplicationError -> throw transferSetResult.throwable + is NetworkResult.ApplicationError -> throw transferSetResult.throwable.logW(TAG, "[createAndUploadArchive] Hit an error when setting transfer archive!", transferSetResult.throwable) is NetworkResult.NetworkError -> return LinkUploadArchiveResult.NetworkError(transferSetResult.exception).logW(TAG, "[createAndUploadArchive] Network error when setting transfer archive.", transferSetResult.exception) is NetworkResult.StatusCodeError -> { return when (transferSetResult.code) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/linkdevice/LinkDeviceViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/linkdevice/LinkDeviceViewModel.kt index 1b0905b790..1767e209c4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/linkdevice/LinkDeviceViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/linkdevice/LinkDeviceViewModel.kt @@ -198,6 +198,8 @@ class LinkDeviceViewModel : ViewModel() { } private fun addDeviceWithSync(linkUri: Uri) { + Log.d(TAG, "[addDeviceWithSync] Beginning device adding process.") + val ephemeralMessageBackupKey = MessageBackupKey(Util.getSecretBytes(32)) val result = LinkDeviceRepository.addDevice(linkUri, ephemeralMessageBackupKey) @@ -209,15 +211,17 @@ class LinkDeviceViewModel : ViewModel() { ) } + Log.d(TAG, "[addDeviceWithSync] Got result: $result") + if (result !is LinkDeviceResult.Success) { - Log.w(TAG, "Unable to link device $result") + Log.w(TAG, "[addDeviceWithSync] Unable to link device $result") return } - Log.i(TAG, "Waiting for a new linked device...") + Log.i(TAG, "[addDeviceWithSync] Waiting for a new linked device...") val waitResult: WaitForLinkedDeviceResponse? = LinkDeviceRepository.waitForDeviceToBeLinked(result.token, maxWaitTime = 60.seconds) if (waitResult == null) { - Log.i(TAG, "No linked device found!") + Log.i(TAG, "[addDeviceWithSync] No linked device found!") _state.update { it.copy( dialogState = DialogState.SyncingTimedOut @@ -226,7 +230,7 @@ class LinkDeviceViewModel : ViewModel() { return } - Log.i(TAG, "Found a linked device!") + Log.d(TAG, "[addDeviceWithSync] Found a linked device!") _state.update { it.copy( @@ -235,10 +239,13 @@ class LinkDeviceViewModel : ViewModel() { ) } - Log.i(TAG, "Beginning the archive generation process...") + Log.d(TAG, "[addDeviceWithSync] Beginning the archive generation process...") val uploadResult = LinkDeviceRepository.createAndUploadArchive(ephemeralMessageBackupKey, waitResult.id, waitResult.created) + + Log.d(TAG, "[addDeviceWithSync] Archive finished with result: $uploadResult") when (uploadResult) { LinkDeviceRepository.LinkUploadArchiveResult.Success -> { + Log.i(TAG, "[addDeviceWithSync] Successfully uploaded archive.") _state.update { it.copy( oneTimeEvent = OneTimeEvent.ToastLinked(waitResult.getPlaintextDeviceName()), @@ -250,6 +257,7 @@ class LinkDeviceViewModel : ViewModel() { is LinkDeviceRepository.LinkUploadArchiveResult.BackupCreationFailure, is LinkDeviceRepository.LinkUploadArchiveResult.BadRequest, is LinkDeviceRepository.LinkUploadArchiveResult.NetworkError -> { + Log.w(TAG, "[addDeviceWithSync] Failed to upload the archive! Result: $uploadResult") _state.update { it.copy( dialogState = DialogState.SyncingFailed(waitResult.id)