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: crash on GrapheneOS when downloading certificate (WPB-7407) #2864

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.content.Context
import android.media.MediaRecorder
import android.os.Build
import com.wire.android.appLogger
import com.wire.android.util.audioFileDateTime
import com.wire.android.util.fileDateTime
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.asset.KaliumFileSystem
import com.wire.kalium.util.DateTimeUtil
Expand Down Expand Up @@ -118,7 +118,7 @@ class AudioMediaRecorder @Inject constructor(

private companion object {
fun getRecordingAudioFileName(): String =
"wire-audio-${DateTimeUtil.currentInstant().audioFileDateTime()}.m4a"
"wire-audio-${DateTimeUtil.currentInstant().fileDateTime()}.m4a"
const val SIZE_OF_1MB = 1024 * 1024
const val AUDIO_CHANNELS = 1
const val SAMPLING_RATE = 44100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
Expand All @@ -49,11 +48,9 @@ import com.wire.android.ui.common.topappbar.NavigationIconType
import com.wire.android.ui.common.topappbar.WireCenterAlignedTopAppBar
import com.wire.android.util.copyLinkToClipboard
import com.wire.android.util.createPemFile
import com.wire.android.util.saveFileToDownloadsFolder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okio.Path.Companion.toOkioPath

@RootNavGraph
@Destination(
Expand All @@ -67,7 +64,7 @@ fun E2eiCertificateDetailsScreen(
) {
val snackbarHostState = LocalSnackbarHostState.current
val scope = rememberCoroutineScope()
val context = LocalContext.current
val downloadedString = stringResource(id = R.string.media_gallery_on_image_downloaded)

WireScaffold(
topBar = {
Expand All @@ -92,7 +89,6 @@ fun E2eiCertificateDetailsScreen(
with(e2eiCertificateDetailsViewModel) {
val copiedToClipboardString =
stringResource(id = R.string.e2ei_certificate_details_certificate_copied_to_clipboard)
val downloadedString = stringResource(id = R.string.media_gallery_on_image_downloaded)

E2eiCertificateDetailsContent(
padding = it,
Expand All @@ -110,14 +106,10 @@ fun E2eiCertificateDetailsScreen(
onDownload = {
scope.launch {
withContext(Dispatchers.IO) {
createPemFile(CERTIFICATE_FILE_NAME, getCertificate()).also {
saveFileToDownloadsFolder(
context = context,
assetName = CERTIFICATE_FILE_NAME,
assetDataPath = it.toPath().toOkioPath(),
assetDataSize = it.length()
)
}
createPemFile(
pathname = getCertificateName(),
content = getCertificate()
)
}
state.wireModalSheetState.hide()
snackbarHostState.showSnackbar(downloadedString)
Expand Down Expand Up @@ -153,5 +145,3 @@ fun E2eiCertificateDetailsContent(
style = textStyle
)
}

const val CERTIFICATE_FILE_NAME = "certificate.txt"
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.ui.common.bottomsheet.WireModalSheetState
import com.wire.android.ui.navArgs
import com.wire.android.util.fileDateTime
import com.wire.kalium.logic.feature.user.GetSelfUserUseCase
import com.wire.kalium.util.DateTimeUtil
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class E2eiCertificateDetailsViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val observerSelfUser: GetSelfUserUseCase,
) : ViewModel() {

var state: E2eiCertificateDetailsState by mutableStateOf(E2eiCertificateDetailsState())
Expand All @@ -38,7 +45,24 @@ class E2eiCertificateDetailsViewModel @Inject constructor(
private val e2eiCertificateDetailsScreenNavArgs: E2eiCertificateDetailsScreenNavArgs =
savedStateHandle.navArgs()

private var selfUserHandle: String? = null

init {
getSelfUserId()
}

private fun getSelfUserId() {
viewModelScope.launch {
selfUserHandle = observerSelfUser().first().handle
}
}

fun getCertificate() = e2eiCertificateDetailsScreenNavArgs.certificateString

fun getCertificateName(): String {
val date = DateTimeUtil.currentInstant().fileDateTime()
return "wire-certificate-$selfUserHandle-$date.txt"
}
}

data class E2eiCertificateDetailsState(
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/com/wire/android/util/DateTimeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private val readReceiptDateTimeFormat = SimpleDateFormat(
Locale.getDefault()
).apply { timeZone = TimeZone.getDefault() }

private val audioFileDateTimeFormat = SimpleDateFormat(
private val fileDateTimeFormat = SimpleDateFormat(
"yyyy-MM-dd-hh-mm-ss",
Locale.getDefault()
).apply { timeZone = TimeZone.getDefault() }
Expand Down Expand Up @@ -96,7 +96,7 @@ fun Date.toMediumOnlyDateTime(): String = mediumOnlyDateTimeFormat.format(this)

fun Instant.uiReadReceiptDateTime(): String = readReceiptDateTimeFormat.format(Date(this.toEpochMilliseconds()))

fun Instant.audioFileDateTime(): String = audioFileDateTimeFormat
fun Instant.fileDateTime(): String = fileDateTimeFormat
.format(Date(this.toEpochMilliseconds()))

fun getCurrentParsedDateTime(): String = mediumDateTimeFormat.format(System.currentTimeMillis())
Expand Down
Loading