From dd1c9976e3413fdee955455dff1ac6061195a5b5 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 6 Oct 2020 17:13:57 +0200 Subject: [PATCH] Fix runtime issue after upgrading Moshi library --- .../session/content/ContentAttachmentData.kt | 5 ++- .../sdk/api/session/room/model/Signed.kt | 2 ++ .../crypto/IncomingSecretShareRequest.kt | 2 ++ .../android/sdk/internal/di/MoshiProvider.kt | 4 +++ .../parsing/CipherSuiteMoshiAdapter.kt | 35 +++++++++++++++++++ .../network/parsing/TlsVersionMoshiAdapter.kt | 35 +++++++++++++++++++ 6 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/parsing/CipherSuiteMoshiAdapter.kt create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/parsing/TlsVersionMoshiAdapter.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentAttachmentData.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentAttachmentData.kt index 045a9bc1a0..b89dfee77f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentAttachmentData.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentAttachmentData.kt @@ -20,9 +20,11 @@ package org.matrix.android.sdk.api.session.content import android.net.Uri import android.os.Parcelable import androidx.exifinterface.media.ExifInterface +import com.squareup.moshi.JsonClass import kotlinx.android.parcel.Parcelize @Parcelize +@JsonClass(generateAdapter = true) data class ContentAttachmentData( val size: Long = 0, val duration: Long? = 0, @@ -32,10 +34,11 @@ data class ContentAttachmentData( val exifOrientation: Int = ExifInterface.ORIENTATION_UNDEFINED, val name: String? = null, val queryUri: Uri, - private val mimeType: String?, + val mimeType: String?, val type: Type ) : Parcelable { + @JsonClass(generateAdapter = false) enum class Type { FILE, IMAGE, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/Signed.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/Signed.kt index 9c14275b3e..9f0e0c17ff 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/Signed.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/Signed.kt @@ -18,7 +18,9 @@ package org.matrix.android.sdk.api.session.room.model import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass +@JsonClass(generateAdapter = true) data class Signed( @Json(name = "token") val token: String, @Json(name = "signatures") val signatures: Any, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/IncomingSecretShareRequest.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/IncomingSecretShareRequest.kt index 4b91ed5d76..de71fd2391 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/IncomingSecretShareRequest.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/IncomingSecretShareRequest.kt @@ -17,6 +17,7 @@ package org.matrix.android.sdk.internal.crypto +import com.squareup.moshi.JsonClass import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.internal.crypto.model.rest.SecretShareRequest @@ -24,6 +25,7 @@ import org.matrix.android.sdk.internal.crypto.model.rest.SecretShareRequest /** * IncomingRoomKeyRequest class defines the incoming room keys request. */ +@JsonClass(generateAdapter = true) data class IncomingSecretShareRequest( /** * The user id diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/MoshiProvider.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/MoshiProvider.kt index 5e16d0b455..5921e28f59 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/MoshiProvider.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/di/MoshiProvider.kt @@ -32,8 +32,10 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageTextContent import org.matrix.android.sdk.api.session.room.model.message.MessageType import org.matrix.android.sdk.api.session.room.model.message.MessageVerificationRequestContent import org.matrix.android.sdk.api.session.room.model.message.MessageVideoContent +import org.matrix.android.sdk.internal.network.parsing.CipherSuiteMoshiAdapter import org.matrix.android.sdk.internal.network.parsing.ForceToBooleanJsonAdapter import org.matrix.android.sdk.internal.network.parsing.RuntimeJsonAdapterFactory +import org.matrix.android.sdk.internal.network.parsing.TlsVersionMoshiAdapter import org.matrix.android.sdk.internal.network.parsing.UriMoshiAdapter object MoshiProvider { @@ -41,6 +43,8 @@ object MoshiProvider { private val moshi: Moshi = Moshi.Builder() .add(UriMoshiAdapter()) .add(ForceToBooleanJsonAdapter()) + .add(CipherSuiteMoshiAdapter()) + .add(TlsVersionMoshiAdapter()) .add(RuntimeJsonAdapterFactory.of(MessageContent::class.java, "msgtype", MessageDefaultContent::class.java) .registerSubtype(MessageTextContent::class.java, MessageType.MSGTYPE_TEXT) .registerSubtype(MessageNoticeContent::class.java, MessageType.MSGTYPE_NOTICE) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/parsing/CipherSuiteMoshiAdapter.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/parsing/CipherSuiteMoshiAdapter.kt new file mode 100644 index 0000000000..ad5821cb0f --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/parsing/CipherSuiteMoshiAdapter.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2019 New Vector Ltd + * Copyright 2020 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.network.parsing + +import com.squareup.moshi.FromJson +import com.squareup.moshi.ToJson +import okhttp3.CipherSuite + +internal class CipherSuiteMoshiAdapter { + + @ToJson + fun toJson(cipherSuite: CipherSuite): String { + return cipherSuite.javaName + } + + @FromJson + fun fromJson(cipherSuiteString: String): CipherSuite { + return CipherSuite.forJavaName(cipherSuiteString) + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/parsing/TlsVersionMoshiAdapter.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/parsing/TlsVersionMoshiAdapter.kt new file mode 100644 index 0000000000..75301521d5 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/network/parsing/TlsVersionMoshiAdapter.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2019 New Vector Ltd + * Copyright 2020 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.network.parsing + +import com.squareup.moshi.FromJson +import com.squareup.moshi.ToJson +import okhttp3.TlsVersion + +internal class TlsVersionMoshiAdapter { + + @ToJson + fun toJson(tlsVersion: TlsVersion): String { + return tlsVersion.javaName + } + + @FromJson + fun fromJson(tlsVersionString: String): TlsVersion { + return TlsVersion.forJavaName(tlsVersionString) + } +}