From 5ab975cc5ceaa914baa001e2bc2e196c2c2b6b29 Mon Sep 17 00:00:00 2001 From: Dominic Fischer Date: Thu, 24 Oct 2019 14:53:44 +0100 Subject: [PATCH] General kotlinification. Signed-off-by: Dominic Fischer --- .../internal/crypto/DeviceListManager.kt | 2 +- .../crypto/OutgoingRoomKeyRequestManager.kt | 2 +- .../DefaultSasVerificationService.kt | 6 ++--- .../core/linkify/VectorAutoLinkPatterns.kt | 6 ++--- .../riotx/core/linkify/VectorLinkify.kt | 2 +- .../riotx/core/pushers/PushersManager.kt | 3 ++- .../java/im/vector/riotx/core/utils/Emoji.kt | 7 ++--- .../ViewEditHistoryEpoxyController.kt | 2 +- .../riotx/features/themes/ThemeUtils.kt | 26 +++++++------------ 9 files changed, 23 insertions(+), 33 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt index 7f2a23e4c2..b2002f0916 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/DeviceListManager.kt @@ -66,7 +66,7 @@ internal class DeviceListManager @Inject constructor(private val cryptoStore: IM if (':' in userId) { try { synchronized(notReadyToRetryHS) { - res = !notReadyToRetryHS.contains(userId.substring(userId.lastIndexOf(":") + 1)) + res = !notReadyToRetryHS.contains(userId.substringAfterLast(':')) } } catch (e: Exception) { Timber.e(e, "## canRetryKeysDownload() failed") diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OutgoingRoomKeyRequestManager.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OutgoingRoomKeyRequestManager.kt index 89a27c9463..86e8a1825c 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OutgoingRoomKeyRequestManager.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/OutgoingRoomKeyRequestManager.kt @@ -216,7 +216,7 @@ internal class OutgoingRoomKeyRequestManager @Inject constructor( sendMessageToDevices(requestMessage, request.recipients, request.requestId, object : MatrixCallback { private fun onDone(state: OutgoingRoomKeyRequest.RequestState) { if (request.state !== OutgoingRoomKeyRequest.RequestState.UNSENT) { - Timber.v("## sendOutgoingRoomKeyRequest() : Cannot update room key request from UNSENT as it was already updated to " + request.state) + Timber.v("## sendOutgoingRoomKeyRequest() : Cannot update room key request from UNSENT as it was already updated to ${request.state}") } else { request.state = state cryptoStore.updateOutgoingRoomKeyRequest(request) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt index ca1157e583..3115eafc45 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/verification/DefaultSasVerificationService.kt @@ -342,10 +342,8 @@ internal class DefaultSasVerificationService @Inject constructor(private val cre private fun addTransaction(tx: VerificationTransaction) { tx.otherUserId.let { otherUserId -> synchronized(txMap) { - if (txMap[otherUserId] == null) { - txMap[otherUserId] = HashMap() - } - txMap[otherUserId]?.set(tx.transactionId, tx) + val txInnerMap = txMap.getOrPut(otherUserId) { HashMap() } + txInnerMap[tx.transactionId] = tx dispatchTxAdded(tx) tx.addListener(this) } diff --git a/vector/src/main/java/im/vector/riotx/core/linkify/VectorAutoLinkPatterns.kt b/vector/src/main/java/im/vector/riotx/core/linkify/VectorAutoLinkPatterns.kt index e6eb886e02..ae4131b5e9 100644 --- a/vector/src/main/java/im/vector/riotx/core/linkify/VectorAutoLinkPatterns.kt +++ b/vector/src/main/java/im/vector/riotx/core/linkify/VectorAutoLinkPatterns.kt @@ -15,8 +15,6 @@ */ package im.vector.riotx.core.linkify -import java.util.regex.Pattern - /** * Better support for geo URi */ @@ -26,7 +24,7 @@ object VectorAutoLinkPatterns { private const val LAT_OR_LONG_OR_ALT_NUMBER = "-?\\d+(?:\\.\\d+)?" private const val COORDINATE_SYSTEM = ";crs=[\\w-]+" - val GEO_URI: Pattern = Pattern.compile("(?:geo:)?" + + val GEO_URI: Regex = Regex("(?:geo:)?" + "(" + LAT_OR_LONG_OR_ALT_NUMBER + ")" + "," + "(" + LAT_OR_LONG_OR_ALT_NUMBER + ")" + @@ -35,5 +33,5 @@ object VectorAutoLinkPatterns { "(?:" + ";u=\\d+(?:\\.\\d+)?" + ")?" + // uncertainty in meters "(?:" + ";[\\w-]+=(?:[\\w-_.!~*'()]|%[\\da-f][\\da-f])+" + // dafuk - ")*", Pattern.CASE_INSENSITIVE) + ")*", RegexOption.IGNORE_CASE) } diff --git a/vector/src/main/java/im/vector/riotx/core/linkify/VectorLinkify.kt b/vector/src/main/java/im/vector/riotx/core/linkify/VectorLinkify.kt index 0d6a71f701..99b0316cbe 100644 --- a/vector/src/main/java/im/vector/riotx/core/linkify/VectorLinkify.kt +++ b/vector/src/main/java/im/vector/riotx/core/linkify/VectorLinkify.kt @@ -94,7 +94,7 @@ object VectorLinkify { createdSpans.add(LinkSpec(URLSpan(urlSpan.url), start, end)) } - LinkifyCompat.addLinks(spannable, VectorAutoLinkPatterns.GEO_URI, "geo:", arrayOf("geo:"), geoMatchFilter, null) + LinkifyCompat.addLinks(spannable, VectorAutoLinkPatterns.GEO_URI.toPattern(), "geo:", arrayOf("geo:"), geoMatchFilter, null) spannable.forEachSpanIndexed { _, urlSpan, start, end -> spannable.removeSpan(urlSpan) createdSpans.add(LinkSpec(URLSpan(urlSpan.url), start, end)) diff --git a/vector/src/main/java/im/vector/riotx/core/pushers/PushersManager.kt b/vector/src/main/java/im/vector/riotx/core/pushers/PushersManager.kt index ceae3a2b76..e2c08a1fe8 100644 --- a/vector/src/main/java/im/vector/riotx/core/pushers/PushersManager.kt +++ b/vector/src/main/java/im/vector/riotx/core/pushers/PushersManager.kt @@ -24,6 +24,7 @@ import im.vector.riotx.core.resources.LocaleProvider import im.vector.riotx.core.resources.StringProvider import java.util.UUID import javax.inject.Inject +import kotlin.math.abs private const val DEFAULT_PUSHER_FILE_TAG = "mobile" @@ -36,7 +37,7 @@ class PushersManager @Inject constructor( fun registerPusherWithFcmKey(pushKey: String): UUID { val currentSession = activeSessionHolder.getActiveSession() - var profileTag = DEFAULT_PUSHER_FILE_TAG + "_" + Math.abs(currentSession.myUserId.hashCode()) + val profileTag = DEFAULT_PUSHER_FILE_TAG + "_" + abs(currentSession.myUserId.hashCode()) return currentSession.addHttpPusher( pushKey, diff --git a/vector/src/main/java/im/vector/riotx/core/utils/Emoji.kt b/vector/src/main/java/im/vector/riotx/core/utils/Emoji.kt index c65fcafb16..a5babcc885 100644 --- a/vector/src/main/java/im/vector/riotx/core/utils/Emoji.kt +++ b/vector/src/main/java/im/vector/riotx/core/utils/Emoji.kt @@ -59,9 +59,10 @@ fun initKnownEmojiHashSet(context: Context, done: (() -> Unit)? = null) { val jsonAdapter = moshi.adapter(EmojiDataSource.EmojiData::class.java) val inputAsString = input.bufferedReader().use { it.readText() } val source = jsonAdapter.fromJson(inputAsString) - knownEmojiSet = HashSet() - source?.emojis?.values?.forEach { - knownEmojiSet?.add(it.emojiString()) + knownEmojiSet = HashSet().also { + source?.emojis?.mapTo(it) { (_, value) -> + value.emojiString() + } } done?.invoke() } diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/edithistory/ViewEditHistoryEpoxyController.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/edithistory/ViewEditHistoryEpoxyController.kt index 32abc0384b..4661d8f8cd 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/edithistory/ViewEditHistoryEpoxyController.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/edithistory/ViewEditHistoryEpoxyController.kt @@ -94,7 +94,7 @@ class ViewEditHistoryEpoxyController(private val context: Context, val body = cContent.second?.let { eventHtmlRenderer.render(it) } ?: cContent.first - val nextEvent = if (index + 1 <= sourceEvents.lastIndex) sourceEvents[index + 1] else null + val nextEvent = sourceEvents.getOrNull(index + 1) var spannedDiff: Spannable? = null if (nextEvent != null && cContent.second == null /*No diff for html*/) { diff --git a/vector/src/main/java/im/vector/riotx/features/themes/ThemeUtils.kt b/vector/src/main/java/im/vector/riotx/features/themes/ThemeUtils.kt index 076b5d6ea2..40a14b3e6f 100644 --- a/vector/src/main/java/im/vector/riotx/features/themes/ThemeUtils.kt +++ b/vector/src/main/java/im/vector/riotx/features/themes/ThemeUtils.kt @@ -130,24 +130,16 @@ object ThemeUtils { */ @ColorInt fun getColor(c: Context, @AttrRes colorAttribute: Int): Int { - if (mColorByAttr.containsKey(colorAttribute)) { - return mColorByAttr[colorAttribute] as Int + return mColorByAttr.getOrPut(colorAttribute) { + try { + val color = TypedValue() + c.theme.resolveAttribute(colorAttribute, color, true) + color.data + } catch (e: Exception) { + Timber.e(e, "Unable to get color") + ContextCompat.getColor(c, android.R.color.holo_red_dark) + } } - - var matchedColor: Int - - try { - val color = TypedValue() - c.theme.resolveAttribute(colorAttribute, color, true) - matchedColor = color.data - } catch (e: Exception) { - Timber.e(e, "Unable to get color") - matchedColor = ContextCompat.getColor(c, android.R.color.holo_red_dark) - } - - mColorByAttr[colorAttribute] = matchedColor - - return matchedColor } fun getAttribute(c: Context, @AttrRes attribute: Int): TypedValue? {