diff --git a/app/src/androidTest/java/com/nextcloud/talk/utils/UriUtilsIT.kt b/app/src/androidTest/java/com/nextcloud/talk/utils/UriUtilsIT.kt new file mode 100644 index 0000000000..ef4b7be2a0 --- /dev/null +++ b/app/src/androidTest/java/com/nextcloud/talk/utils/UriUtilsIT.kt @@ -0,0 +1,155 @@ +/* + * Nextcloud Talk application + * + * @author Samanwith KSN + * Copyright (C) 2023 Samanwith KSN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.utils + +import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertFalse +import junit.framework.TestCase.assertTrue +import org.junit.Test + +class UriUtilsIT { + + @Test + fun testHasHttpProtocolPrefixed() { + val uriHttp = "http://www.example.com" + val resultHttp = UriUtils.hasHttpProtocolPrefixed(uriHttp) + assertTrue(resultHttp) + + val uriHttps = "https://www.example.com" + val resultHttps = UriUtils.hasHttpProtocolPrefixed(uriHttps) + assertTrue(resultHttps) + + val uriWithoutPrefix = "www.example.com" + val resultWithoutPrefix = UriUtils.hasHttpProtocolPrefixed(uriWithoutPrefix) + assertFalse(resultWithoutPrefix) + } + + @Test + fun testExtractInstanceInternalFileFileId() { + assertEquals( + "42", + UriUtils.extractInstanceInternalFileFileId( + "https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=42" + ) + ) + } + + @Test + fun testExtractInstanceInternalFileShareFileId() { + assertEquals( + "42", + UriUtils.extractInstanceInternalFileShareFileId("https://cloud.nextcloud.com/f/42") + ) + } + + @Test + fun testIsInstanceInternalFileShareUrl() { + assertTrue( + UriUtils.isInstanceInternalFileShareUrl( + "https://cloud.nextcloud.com", + "https://cloud.nextcloud.com/f/42" + ) + ) + + assertFalse( + UriUtils.isInstanceInternalFileShareUrl( + "https://nextcloud.nextcloud.com", + "https://cloud.nextcloud.com/f/42" + ) + ) + + assertFalse( + UriUtils.isInstanceInternalFileShareUrl( + "https://nextcloud.nextcloud.com", + "https://cloud.nextcloud.com/f/" + ) + ) + + assertFalse( + UriUtils.isInstanceInternalFileShareUrl( + "https://nextcloud.nextcloud.com", + "https://cloud.nextcloud.com/f/test123" + ) + ) + } + + @Test + fun testIsInstanceInternalFileUrl() { + assertTrue( + UriUtils.isInstanceInternalFileUrl( + "https://cloud.nextcloud.com", + "https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=41" + ) + ) + + assertFalse( + UriUtils.isInstanceInternalFileUrl( + "https://nextcloud.nextcloud.com", + "https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=41" + ) + ) + + assertFalse( + UriUtils.isInstanceInternalFileUrl( + "https://nextcloud.nextcloud.com", + "https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=test123" + ) + ) + + assertFalse( + UriUtils.isInstanceInternalFileUrl( + "https://nextcloud.nextcloud.com", + "https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=" + ) + ) + + assertFalse( + UriUtils.isInstanceInternalFileUrl( + "https://cloud.nextcloud.com", + "https://cloud.nextcloud.com/apps/files/?dir=/Engineering" + ) + ) + } + + @Test + fun testIsInstanceInternalFileUrlNew() { + assertTrue( + UriUtils.isInstanceInternalFileUrlNew( + "https://cloud.nextcloud.com", + "https://cloud.nextcloud.com/apps/files/files/41?dir=/" + ) + ) + + assertFalse( + UriUtils.isInstanceInternalFileUrlNew( + "https://nextcloud.nextcloud.com", + "https://cloud.nextcloud.com/apps/files/files/41?dir=/" + ) + ) + } + + @Test + fun testExtractInstanceInternalFileFileIdNew() { + assertEquals( + "42", + UriUtils.extractInstanceInternalFileFileIdNew("https://cloud.nextcloud.com/apps/files/files/42?dir=/") + ) + } +} diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLocationMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLocationMessageViewHolder.kt index 65af408a8c..bef8ae673d 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLocationMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLocationMessageViewHolder.kt @@ -221,7 +221,7 @@ class IncomingLocationMessageViewHolder(incomingView: View, payload: Any) : binding.webview.webViewClient = object : WebViewClient() { @Deprecated("Use shouldOverrideUrlLoading(WebView view, WebResourceRequest request)") override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { - return if (url != null && UriUtils.hasHttpProtocollPrefixed(url) + return if (url != null && UriUtils.hasHttpProtocolPrefixed(url) ) { view?.context?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) true diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt index 72c32d348d..bfea357d40 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt @@ -37,6 +37,7 @@ import autodagger.AutoInjector import coil.load import com.google.android.flexbox.FlexboxLayout import com.google.android.material.snackbar.Snackbar +import com.nextcloud.android.common.ui.theme.utils.ColorRole import com.nextcloud.talk.R import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication @@ -44,7 +45,6 @@ import com.nextcloud.talk.databinding.ItemCustomOutcomingLocationMessageBinding import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.models.json.chat.ReadStatus import com.nextcloud.talk.ui.theme.ViewThemeUtils -import com.nextcloud.android.common.ui.theme.utils.ColorRole import com.nextcloud.talk.utils.ApiUtils import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.UriUtils @@ -164,7 +164,7 @@ class OutcomingLocationMessageViewHolder(incomingView: View) : binding.webview.webViewClient = object : WebViewClient() { @Deprecated("Use shouldOverrideUrlLoading(WebView view, WebResourceRequest request)") override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { - return if (url != null && UriUtils.hasHttpProtocollPrefixed(url) + return if (url != null && UriUtils.hasHttpProtocolPrefixed(url) ) { view?.context?.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) true diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 98fa652df6..88dbb53bdf 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -204,6 +204,7 @@ import com.nextcloud.talk.utils.MagicCharPolicy import com.nextcloud.talk.utils.Mimetype import com.nextcloud.talk.utils.NotificationUtils import com.nextcloud.talk.utils.ParticipantPermissions +import com.nextcloud.talk.utils.UriUtils import com.nextcloud.talk.utils.VibrationUtils import com.nextcloud.talk.utils.bundle.BundleKeys import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_CALL_VOICE_ONLY @@ -2660,58 +2661,23 @@ class ChatActivity : ) } - fun isInstanceFileUrl(user: User, url: String): Boolean { - return url.startsWith(user.baseUrl!!) && - (isInstanceInternalFileShareUrl(user.baseUrl!!, url) || isInstanceInternalFileUrl(user.baseUrl!!, url)) - } - - fun isInstanceInternalFileShareUrl(baseUrl: String, url: String): Boolean { - // https://cloud.nextcloud.com/f/41 - return url.startsWith("$baseUrl/f/") || url.startsWith("$baseUrl/index.php/f/") - } - - fun extractInstanceInternalFileShareFileId(url: String): String { - // https://cloud.nextcloud.com/f/41 - return "41" - } - - fun isInstanceInternalFileUrl(baseUrl: String, url: String): Boolean { - //https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=41 - return url.startsWith("$baseUrl/apps/files/") || url.startsWith("$baseUrl/index.php/apps/files/") - } - - fun extractInstanceInternalFileFileId(url: String): String { - // https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=41 - return "41" - } - - fun isInstanceInternalFileUrlNew(baseUrl: String, url: String): Boolean { - //https://cloud.nextcloud.com/apps/files/files/41?dir=/ - return url.startsWith("$baseUrl/apps/files/files/") || url.startsWith("$baseUrl/index.php/apps/files/files/") - } - - fun extractInstanceInternalFileFileIdNew(url: String): String { - // https://cloud.nextcloud.com/apps/files/files/41?dir=/ - return "41" - } - override fun startActivity(intent: Intent) { val user = currentUserProvider.currentUser.blockingGet() if (intent.data != null && TextUtils.equals(intent.action, Intent.ACTION_VIEW)) { val uri = intent.data.toString() if (uri.startsWith(user.baseUrl!!)) { - if (isInstanceInternalFileShareUrl(user.baseUrl!!, uri)) { + if (UriUtils.isInstanceInternalFileShareUrl(user.baseUrl!!, uri)) { // https://cloud.nextcloud.com/f/41 val fileViewerUtils = FileViewerUtils(this, user) - fileViewerUtils.openFileInFilesApp(uri, extractInstanceInternalFileShareFileId(uri)) - } else if (isInstanceInternalFileUrl(user.baseUrl!!, uri)) { + fileViewerUtils.openFileInFilesApp(uri, UriUtils.extractInstanceInternalFileShareFileId(uri)) + } else if (UriUtils.isInstanceInternalFileUrl(user.baseUrl!!, uri)) { // https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=41 val fileViewerUtils = FileViewerUtils(this, user) - fileViewerUtils.openFileInFilesApp(uri, extractInstanceInternalFileFileId(uri)) - } else if (isInstanceInternalFileUrlNew(user.baseUrl!!, uri)) { + fileViewerUtils.openFileInFilesApp(uri, UriUtils.extractInstanceInternalFileFileId(uri)) + } else if (UriUtils.isInstanceInternalFileUrlNew(user.baseUrl!!, uri)) { // https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=41 val fileViewerUtils = FileViewerUtils(this, user) - fileViewerUtils.openFileInFilesApp(uri, extractInstanceInternalFileFileIdNew(uri)) + fileViewerUtils.openFileInFilesApp(uri, UriUtils.extractInstanceInternalFileFileIdNew(uri)) } else { super.startActivity(intent) } diff --git a/app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.kt b/app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.kt index 941b74ffce..d6ecd5a812 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.kt @@ -124,7 +124,7 @@ class AccountVerificationController(args: Bundle? = null) : BaseController( if ( isAccountImport && - !UriUtils.hasHttpProtocollPrefixed(baseUrl!!) || + !UriUtils.hasHttpProtocolPrefixed(baseUrl!!) || isSameProtocol(baseUrl!!, originalProtocol!!) ) { determineBaseUrlProtocol(true) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt index 1b7f27b931..a05899b52a 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt @@ -216,7 +216,7 @@ class ServerSelectionController : url = url.substring(0, url.length - 1) } - if (UriUtils.hasHttpProtocollPrefixed(url)) { + if (UriUtils.hasHttpProtocolPrefixed(url)) { checkServer(url, false) } else { checkServer("https://$url", true) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.kt b/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.kt index 1ab25d4855..391e0a2f9c 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.kt +++ b/app/src/main/java/com/nextcloud/talk/controllers/bottomsheet/EntryMenuController.kt @@ -272,7 +272,7 @@ class EntryMenuController(args: Bundle) : } binding?.textInputLayout?.isErrorEnabled = false } else if ( - UriUtils.hasHttpProtocollPrefixed(binding?.textEdit?.text.toString()) && + UriUtils.hasHttpProtocolPrefixed(binding?.textEdit?.text.toString()) && binding?.textEdit?.text.toString().contains("/call/") ) { if (!binding?.okButton?.isEnabled!!) { diff --git a/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.kt index 10bffe4e4a..80cc199dca 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/AccountUtils.kt @@ -69,7 +69,7 @@ object AccountUtils { private fun matchAccounts(importAccount: ImportAccount, user: User): Boolean { var accountFound = false if (importAccount.token != null) { - if (UriUtils.hasHttpProtocollPrefixed(importAccount.baseUrl)) { + if (UriUtils.hasHttpProtocolPrefixed(importAccount.baseUrl)) { if ( user.username == importAccount.username && user.baseUrl == importAccount.baseUrl diff --git a/app/src/main/java/com/nextcloud/talk/utils/UriUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/UriUtils.kt index 10471261bf..19cb39a1ac 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/UriUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/UriUtils.kt @@ -22,11 +22,44 @@ package com.nextcloud.talk.utils +import android.net.Uri + class UriUtils { companion object { - - fun hasHttpProtocollPrefixed(uri: String): Boolean { + fun hasHttpProtocolPrefixed(uri: String): Boolean { return uri.startsWith("http://") || uri.startsWith("https://") } + + fun extractInstanceInternalFileFileId(url: String): String { + // https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=41 + return Uri.parse(url).getQueryParameter("fileid").toString() + } + + fun isInstanceInternalFileShareUrl(baseUrl: String, url: String): Boolean { + // https://cloud.nextcloud.com/f/41 + return url.startsWith("$baseUrl/f/") || url.startsWith("$baseUrl/index.php/f/") && + Regex(".*/f/d*").matches(url) + } + + fun extractInstanceInternalFileShareFileId(url: String): String { + // https://cloud.nextcloud.com/f/41 + return Uri.parse(url).lastPathSegment ?: "" + } + + fun isInstanceInternalFileUrl(baseUrl: String, url: String): Boolean { + // https://cloud.nextcloud.com/apps/files/?dir=/Engineering&fileid=41 + return (url.startsWith("$baseUrl/apps/files/") || url.startsWith("$baseUrl/index.php/apps/files/")) && + Uri.parse(url).queryParameterNames.contains("fileid") && Regex(""".*fileid=\d*""").matches(url) + } + + fun isInstanceInternalFileUrlNew(baseUrl: String, url: String): Boolean { + // https://cloud.nextcloud.com/apps/files/files/41?dir=/ + return url.startsWith("$baseUrl/apps/files/files/") || url.startsWith("$baseUrl/index.php/apps/files/files/") + } + + fun extractInstanceInternalFileFileIdNew(url: String): String { + // https://cloud.nextcloud.com/apps/files/files/41?dir=/ + return Uri.parse(url).lastPathSegment ?: "" + } } } diff --git a/app/src/test/java/com/nextcloud/talk/utils/UriUtilsTest.kt b/app/src/test/java/com/nextcloud/talk/utils/UriUtilsTest.kt deleted file mode 100644 index 6737246deb..0000000000 --- a/app/src/test/java/com/nextcloud/talk/utils/UriUtilsTest.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Nextcloud Talk application - * - * @author Samanwith KSN - * Copyright (C) 2023 Samanwith KSN - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.nextcloud.talk.utils - -import org.junit.Assert -import org.junit.Test - -class UriUtilsTest { - - @Test - fun testHasHttpProtocolPrefixed() { - val uriHttp = "http://www.example.com" - val resultHttp = UriUtils.hasHttpProtocollPrefixed(uriHttp) - Assert.assertTrue(resultHttp) - - val uriHttps = "https://www.example.com" - val resultHttps = UriUtils.hasHttpProtocollPrefixed(uriHttps) - Assert.assertTrue(resultHttps) - - val uriWithoutPrefix = "www.example.com" - val resultWithoutPrefix = UriUtils.hasHttpProtocollPrefixed(uriWithoutPrefix) - Assert.assertFalse(resultWithoutPrefix) - } -}