2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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.
|
2019-01-18 10:12:08 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-18 17:29:49 +00:00
|
|
|
package im.vector.matrix.android.api.permalinks
|
|
|
|
|
|
|
|
import android.text.Spannable
|
2018-12-20 13:13:23 +00:00
|
|
|
import im.vector.matrix.android.api.MatrixPatterns
|
2018-12-18 17:29:49 +00:00
|
|
|
|
2019-01-18 15:26:17 +00:00
|
|
|
/**
|
|
|
|
* MatrixLinkify take a piece of text and turns all of the
|
|
|
|
* matrix patterns matches in the text into clickable links.
|
|
|
|
*/
|
2018-12-19 18:08:30 +00:00
|
|
|
object MatrixLinkify {
|
2018-12-18 17:29:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the matrix spans i.e matrix id , user id ... to display them as URL.
|
|
|
|
*
|
2018-12-19 10:50:44 +00:00
|
|
|
* @param spannable the text in which the matrix items has to be clickable.
|
2018-12-18 17:29:49 +00:00
|
|
|
*/
|
2018-12-19 18:08:30 +00:00
|
|
|
fun addLinks(spannable: Spannable?, callback: MatrixPermalinkSpan.Callback?): Boolean {
|
2018-12-18 17:29:49 +00:00
|
|
|
// sanity checks
|
2018-12-19 10:50:44 +00:00
|
|
|
if (spannable.isNullOrEmpty()) {
|
|
|
|
return false
|
2018-12-18 17:29:49 +00:00
|
|
|
}
|
2018-12-19 10:50:44 +00:00
|
|
|
val text = spannable.toString()
|
|
|
|
var hasMatch = false
|
2019-07-05 11:17:26 +00:00
|
|
|
for (pattern in MatrixPatterns.MATRIX_PATTERNS) {
|
|
|
|
for (match in pattern.findAll(spannable)) {
|
2018-12-19 10:50:44 +00:00
|
|
|
hasMatch = true
|
2019-07-05 11:17:26 +00:00
|
|
|
val startPos = match.range.first
|
2018-12-18 17:29:49 +00:00
|
|
|
if (startPos == 0 || text[startPos - 1] != '/') {
|
2019-07-05 11:17:26 +00:00
|
|
|
val endPos = match.range.last
|
|
|
|
val url = text.substring(match.range)
|
2018-12-19 18:08:30 +00:00
|
|
|
val span = MatrixPermalinkSpan(url, callback)
|
2018-12-19 10:50:44 +00:00
|
|
|
spannable.setSpan(span, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
2018-12-18 17:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 10:50:44 +00:00
|
|
|
return hasMatch
|
|
|
|
}
|
2019-06-07 11:38:58 +00:00
|
|
|
|
2018-12-18 17:29:49 +00:00
|
|
|
}
|