Add support for link generated by matrix.to website.

This commit is contained in:
Benoit Marty
2024-05-03 11:57:22 +02:00
parent 0d294ff171
commit b156097e1a
5 changed files with 63 additions and 2 deletions

View File

@@ -87,7 +87,7 @@
<data android:host="*.element.io" />
</intent-filter>
<!--
matrix.to links
matrix.to links
Note: On Android 12 and higher clicking a web link (that is not an Android App Link) always shows content in a web browser
https://developer.android.com/training/app-links#web-links
-->
@@ -100,6 +100,17 @@
<data android:scheme="https" />
<data android:host="matrix.to" />
</intent-filter>
<!--
links from matrix.to website
-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="element" />
<data android:host="user" />
<data android:host="room" />
</intent-filter>
</activity>
<provider

View File

@@ -58,3 +58,15 @@ fun String.ellipsize(length: Int): String {
return "${this.take(length)}"
}
/**
* Replace the old prefix with the new prefix.
* If the string does not start with the old prefix, the string is returned as is.
*/
fun String.replacePrefix(oldPrefix: String, newPrefix: String): String {
return if (startsWith(oldPrefix)) {
newPrefix + substring(oldPrefix.length)
} else {
this
}
}

View File

@@ -19,6 +19,7 @@ package io.element.android.libraries.matrix.impl.permalink
import android.net.Uri
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.appconfig.MatrixConfiguration
import io.element.android.libraries.core.extensions.replacePrefix
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.matrix.api.permalink.MatrixToConverter
import javax.inject.Inject
@@ -35,9 +36,14 @@ class DefaultMatrixToConverter @Inject constructor() : MatrixToConverter {
* - https://riot.im/develop/#/room/#element-android:matrix.org -> https://matrix.to/#/#element-android:matrix.org
* - https://app.element.io/#/room/#element-android:matrix.org -> https://matrix.to/#/#element-android:matrix.org
* - https://www.example.org/#/room/#element-android:matrix.org -> https://matrix.to/#/#element-android:matrix.org
* Also convert links coming from the matrix.to website:
* - element://room/#element-android:matrix.org -> https://matrix.to/#/#element-android:matrix.org
* - element://user/@alice:matrix.org -> https://matrix.to/#/@alice:matrix.org
*/
override fun convert(uri: Uri): Uri? {
val uriString = uri.toString()
// Handle links coming from the matrix.to website.
.replacePrefix(MATRIX_TO_CUSTOM_SCHEME_BASE_URL, "https://app.element.io/#/")
val baseUrl = MatrixConfiguration.MATRIX_TO_PERMALINK_BASE_URL
return when {
@@ -54,7 +60,8 @@ class DefaultMatrixToConverter @Inject constructor() : MatrixToConverter {
}
companion object {
val SUPPORTED_PATHS = listOf(
private const val MATRIX_TO_CUSTOM_SCHEME_BASE_URL = "element://"
private val SUPPORTED_PATHS = listOf(
"/#/room/",
"/#/user/",
"/#/group/"

View File

@@ -53,4 +53,16 @@ class DefaultMatrixToConverterTest {
val url = Uri.parse("https://element.io/")
assertThat(DefaultMatrixToConverter().convert(url)).isNull()
}
@Test
fun `converting url coming from the matrix-to website returns a matrix-to url for room case`() {
val url = Uri.parse("element://room/#element-android:matrix.org")
assertThat(DefaultMatrixToConverter().convert(url)).isEqualTo(Uri.parse("https://matrix.to/#/#element-android:matrix.org"))
}
@Test
fun `converting url coming from the matrix-to website returns a matrix-to url for user case`() {
val url = Uri.parse("element://user/@alice:matrix.org")
assertThat(DefaultMatrixToConverter().convert(url)).isEqualTo(Uri.parse("https://matrix.to/#/@alice:matrix.org"))
}
}

19
tools/adb/deeplink_matrixto.sh Executable file
View File

@@ -0,0 +1,19 @@
#! /bin/bash
#
# Copyright (c) 2024 New Vector Ltd
#
# 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.
#
adb shell am start -a android.intent.action.VIEW \
-d "element://room/%23element-android%3Amatrix.org"