From b0e5612bdc984214d79301cdb94146826ad65456 Mon Sep 17 00:00:00 2001 From: Dominic Fischer Date: Fri, 5 Jul 2019 12:17:26 +0100 Subject: [PATCH] Convert java-esque code to Kotlin Signed-off-by: Dominic Fischer --- .../matrix/android/api/MatrixPatterns.kt | 37 +++++++++---------- .../android/api/permalinks/MatrixLinkify.kt | 12 +++--- .../api/util/MatrixCallbackDelegate.kt | 11 +----- .../android/internal/util/StringUtils.kt | 36 ++++++------------ .../vector/riotx/EmojiCompatFontProvider.kt | 2 +- .../riotx/core/dialogs/DialogAdapter.kt | 12 +++--- .../vector/riotx/features/home/UserColor.kt | 3 +- 7 files changed, 46 insertions(+), 67 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/MatrixPatterns.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/MatrixPatterns.kt index eaaeb73075..5cb7f4ca0c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/MatrixPatterns.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/MatrixPatterns.kt @@ -16,7 +16,6 @@ package im.vector.matrix.android.api -import java.util.regex.Pattern /** * This class contains pattern to match the different Matrix ids @@ -29,31 +28,31 @@ object MatrixPatterns { // regex pattern to find matrix user ids in a string. // See https://matrix.org/speculator/spec/HEAD/appendices.html#historical-user-ids private const val MATRIX_USER_IDENTIFIER_REGEX = "@[A-Z0-9\\x21-\\x39\\x3B-\\x7F]+$DOMAIN_REGEX" - private val PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER = Pattern.compile(MATRIX_USER_IDENTIFIER_REGEX, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER = MATRIX_USER_IDENTIFIER_REGEX.toRegex(RegexOption.IGNORE_CASE) // regex pattern to find room ids in a string. private const val MATRIX_ROOM_IDENTIFIER_REGEX = "![A-Z0-9]+$DOMAIN_REGEX" - private val PATTERN_CONTAIN_MATRIX_ROOM_IDENTIFIER = Pattern.compile(MATRIX_ROOM_IDENTIFIER_REGEX, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_ROOM_IDENTIFIER = MATRIX_ROOM_IDENTIFIER_REGEX.toRegex(RegexOption.IGNORE_CASE) // regex pattern to find room aliases in a string. private const val MATRIX_ROOM_ALIAS_REGEX = "#[A-Z0-9._%#@=+-]+$DOMAIN_REGEX" - private val PATTERN_CONTAIN_MATRIX_ALIAS = Pattern.compile(MATRIX_ROOM_ALIAS_REGEX, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_ALIAS = MATRIX_ROOM_ALIAS_REGEX.toRegex(RegexOption.IGNORE_CASE) // regex pattern to find message ids in a string. private const val MATRIX_EVENT_IDENTIFIER_REGEX = "\\$[A-Z0-9]+$DOMAIN_REGEX" - private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER = Pattern.compile(MATRIX_EVENT_IDENTIFIER_REGEX, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER = MATRIX_EVENT_IDENTIFIER_REGEX.toRegex(RegexOption.IGNORE_CASE) // regex pattern to find message ids in a string. private const val MATRIX_EVENT_IDENTIFIER_V3_REGEX = "\\$[A-Z0-9/+]+" - private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3 = Pattern.compile(MATRIX_EVENT_IDENTIFIER_V3_REGEX, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3 = MATRIX_EVENT_IDENTIFIER_V3_REGEX.toRegex(RegexOption.IGNORE_CASE) // Ref: https://matrix.org/docs/spec/rooms/v4#event-ids private const val MATRIX_EVENT_IDENTIFIER_V4_REGEX = "\\$[A-Z0-9\\-_]+" - private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4 = Pattern.compile(MATRIX_EVENT_IDENTIFIER_V4_REGEX, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4 = MATRIX_EVENT_IDENTIFIER_V4_REGEX.toRegex(RegexOption.IGNORE_CASE) // regex pattern to find group ids in a string. private const val MATRIX_GROUP_IDENTIFIER_REGEX = "\\+[A-Z0-9=_\\-./]+$DOMAIN_REGEX" - private val PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER = Pattern.compile(MATRIX_GROUP_IDENTIFIER_REGEX, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER = MATRIX_GROUP_IDENTIFIER_REGEX.toRegex(RegexOption.IGNORE_CASE) // regex pattern to find permalink with message id. // Android does not support in URL so extract it. @@ -62,16 +61,16 @@ object MatrixPatterns { const val SEP_REGEX = "/" private const val LINK_TO_ROOM_ID_REGEXP = PERMALINK_BASE_REGEX + MATRIX_ROOM_IDENTIFIER_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX - private val PATTERN_CONTAIN_MATRIX_TO_PERMALINK_ROOM_ID = Pattern.compile(LINK_TO_ROOM_ID_REGEXP, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_TO_PERMALINK_ROOM_ID = LINK_TO_ROOM_ID_REGEXP.toRegex(RegexOption.IGNORE_CASE) private const val LINK_TO_ROOM_ALIAS_REGEXP = PERMALINK_BASE_REGEX + MATRIX_ROOM_ALIAS_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX - private val PATTERN_CONTAIN_MATRIX_TO_PERMALINK_ROOM_ALIAS = Pattern.compile(LINK_TO_ROOM_ALIAS_REGEXP, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_MATRIX_TO_PERMALINK_ROOM_ALIAS = LINK_TO_ROOM_ALIAS_REGEXP.toRegex(RegexOption.IGNORE_CASE) private const val LINK_TO_APP_ROOM_ID_REGEXP = APP_BASE_REGEX + MATRIX_ROOM_IDENTIFIER_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX - private val PATTERN_CONTAIN_APP_LINK_PERMALINK_ROOM_ID = Pattern.compile(LINK_TO_APP_ROOM_ID_REGEXP, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_APP_LINK_PERMALINK_ROOM_ID = LINK_TO_APP_ROOM_ID_REGEXP.toRegex(RegexOption.IGNORE_CASE) private const val LINK_TO_APP_ROOM_ALIAS_REGEXP = APP_BASE_REGEX + MATRIX_ROOM_ALIAS_REGEX + SEP_REGEX + MATRIX_EVENT_IDENTIFIER_REGEX - private val PATTERN_CONTAIN_APP_LINK_PERMALINK_ROOM_ALIAS = Pattern.compile(LINK_TO_APP_ROOM_ALIAS_REGEXP, Pattern.CASE_INSENSITIVE) + private val PATTERN_CONTAIN_APP_LINK_PERMALINK_ROOM_ALIAS = LINK_TO_APP_ROOM_ALIAS_REGEXP.toRegex(RegexOption.IGNORE_CASE) // list of patterns to find some matrix item. val MATRIX_PATTERNS = listOf( @@ -93,7 +92,7 @@ object MatrixPatterns { * @return true if the string is a valid user id */ fun isUserId(str: String?): Boolean { - return str != null && PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER.matcher(str).matches() + return str != null && str matches PATTERN_CONTAIN_MATRIX_USER_IDENTIFIER } /** @@ -103,7 +102,7 @@ object MatrixPatterns { * @return true if the string is a valid room Id */ fun isRoomId(str: String?): Boolean { - return str != null && PATTERN_CONTAIN_MATRIX_ROOM_IDENTIFIER.matcher(str).matches() + return str != null && str matches PATTERN_CONTAIN_MATRIX_ROOM_IDENTIFIER } /** @@ -113,7 +112,7 @@ object MatrixPatterns { * @return true if the string is a valid room alias. */ fun isRoomAlias(str: String?): Boolean { - return str != null && PATTERN_CONTAIN_MATRIX_ALIAS.matcher(str).matches() + return str != null && str matches PATTERN_CONTAIN_MATRIX_ALIAS } /** @@ -124,9 +123,9 @@ object MatrixPatterns { */ fun isEventId(str: String?): Boolean { return str != null - && (PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER.matcher(str).matches() - || PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3.matcher(str).matches() - || PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4.matcher(str).matches()) + && (str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER + || str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V3 + || str matches PATTERN_CONTAIN_MATRIX_EVENT_IDENTIFIER_V4) } /** @@ -136,6 +135,6 @@ object MatrixPatterns { * @return true if the string is a valid group id. */ fun isGroupId(str: String?): Boolean { - return str != null && PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER.matcher(str).matches() + return str != null && str matches PATTERN_CONTAIN_MATRIX_GROUP_IDENTIFIER } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/permalinks/MatrixLinkify.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/permalinks/MatrixLinkify.kt index eed9b7b6d2..cfe5a051e7 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/permalinks/MatrixLinkify.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/permalinks/MatrixLinkify.kt @@ -37,15 +37,13 @@ object MatrixLinkify { } val text = spannable.toString() var hasMatch = false - for (index in MatrixPatterns.MATRIX_PATTERNS.indices) { - val pattern = MatrixPatterns.MATRIX_PATTERNS[index] - val matcher = pattern.matcher(spannable) - while (matcher.find()) { + for (pattern in MatrixPatterns.MATRIX_PATTERNS) { + for (match in pattern.findAll(spannable)) { hasMatch = true - val startPos = matcher.start(0) + val startPos = match.range.first if (startPos == 0 || text[startPos - 1] != '/') { - val endPos = matcher.end(0) - val url = text.substring(matcher.start(0), matcher.end(0)) + val endPos = match.range.last + val url = text.substring(match.range) val span = MatrixPermalinkSpan(url, callback) spannable.setSpan(span, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/MatrixCallbackDelegate.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/MatrixCallbackDelegate.kt index f98cf5a3a5..2b4e30a1e6 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/MatrixCallbackDelegate.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/MatrixCallbackDelegate.kt @@ -21,13 +21,4 @@ import im.vector.matrix.android.api.MatrixCallback /** * Simple MatrixCallback implementation which delegate its calls to another callback */ -open class MatrixCallbackDelegate(private val callback: MatrixCallback) : MatrixCallback { - - override fun onSuccess(data: T) { - callback.onSuccess(data) - } - - override fun onFailure(failure: Throwable) { - callback.onFailure(failure) - } -} \ No newline at end of file +open class MatrixCallbackDelegate(private val callback: MatrixCallback) : MatrixCallback by callback \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/StringUtils.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/StringUtils.kt index 4e7f0c7adf..a83ab0134c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/StringUtils.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/StringUtils.kt @@ -25,19 +25,13 @@ import timber.log.Timber * @return the utf-8 string */ fun convertToUTF8(s: String): String? { - var out: String? = s - - if (null != out) { - try { - val bytes = out.toByteArray(charset("UTF-8")) - out = String(bytes) - } catch (e: Exception) { - Timber.e(e, "## convertToUTF8() failed") - } - + return try { + val bytes = s.toByteArray(Charsets.UTF_8) + String(bytes) + } catch (e: Exception) { + Timber.e(e, "## convertToUTF8() failed") + null } - - return out } /** @@ -47,17 +41,11 @@ fun convertToUTF8(s: String): String? { * @return the utf-16 string */ fun convertFromUTF8(s: String): String? { - var out: String? = s - - if (null != out) { - try { - val bytes = out.toByteArray() - out = String(bytes, charset("UTF-8")) - } catch (e: Exception) { - Timber.e(e, "## convertFromUTF8() failed") - } - + return try { + val bytes = s.toByteArray() + String(bytes, Charsets.UTF_8) + } catch (e: Exception) { + Timber.e(e, "## convertFromUTF8() failed") + null } - - return out } diff --git a/vector/src/main/java/im/vector/riotx/EmojiCompatFontProvider.kt b/vector/src/main/java/im/vector/riotx/EmojiCompatFontProvider.kt index 994cb13664..24e7e6d287 100644 --- a/vector/src/main/java/im/vector/riotx/EmojiCompatFontProvider.kt +++ b/vector/src/main/java/im/vector/riotx/EmojiCompatFontProvider.kt @@ -50,7 +50,7 @@ class EmojiCompatFontProvider @Inject constructor(): FontsContractCompat.FontReq } fun addListener(listener: FontProviderListener) { - if (!listeners.contains(listener)) { + if (listener !in listeners) { listeners.add(listener) } } diff --git a/vector/src/main/java/im/vector/riotx/core/dialogs/DialogAdapter.kt b/vector/src/main/java/im/vector/riotx/core/dialogs/DialogAdapter.kt index 0cc8479921..49d865e8cf 100644 --- a/vector/src/main/java/im/vector/riotx/core/dialogs/DialogAdapter.kt +++ b/vector/src/main/java/im/vector/riotx/core/dialogs/DialogAdapter.kt @@ -26,14 +26,16 @@ import im.vector.riotx.R internal abstract class DialogAdapter(context: Context) : ArrayAdapter(context, R.layout.item_dialog) { override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { - var view = convertView - if (view == null) { + val view: View + if (convertView == null) { view = LayoutInflater.from(context).inflate(R.layout.item_dialog, parent, false) view.tag = DialogListItemHolder(view) + } else { + view = convertView } - (view!!.tag as DialogListItemHolder).let { - it.icon.setImageResource(getItem(position).iconRes) - it.text.setText(getItem(position).titleRes) + with(view.tag as DialogListItemHolder) { + icon.setImageResource(getItem(position).iconRes) + text.setText(getItem(position).titleRes) } return view } diff --git a/vector/src/main/java/im/vector/riotx/features/home/UserColor.kt b/vector/src/main/java/im/vector/riotx/features/home/UserColor.kt index ff05d98b01..b960583374 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/UserColor.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/UserColor.kt @@ -18,6 +18,7 @@ package im.vector.riotx.features.home import androidx.annotation.ColorRes import im.vector.riotx.R +import kotlin.math.abs @ColorRes @@ -36,7 +37,7 @@ fun getColorFromUserId(userId: String?): Int { i++ } - return when (Math.abs(hash) % 8 + 1) { + return when (abs(hash) % 8 + 1) { 1 -> R.color.riotx_username_1 2 -> R.color.riotx_username_2 3 -> R.color.riotx_username_3