2018-05-07 06:05:54 +10:00
|
|
|
/* Copyright 2018 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Tusky 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 Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.LinearLayout
|
2019-10-12 02:51:47 +11:00
|
|
|
import android.widget.Toast
|
2019-09-22 16:18:44 +10:00
|
|
|
import androidx.annotation.VisibleForTesting
|
|
|
|
import androidx.lifecycle.Lifecycle
|
|
|
|
import com.google.android.material.bottomsheet.BottomSheetBehavior
|
2018-05-07 06:05:54 +10:00
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
2019-02-12 00:43:57 +11:00
|
|
|
import com.keylesspalace.tusky.util.LinkHelper
|
2019-09-22 16:18:44 +10:00
|
|
|
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
|
2019-09-22 17:49:26 +10:00
|
|
|
import com.uber.autodispose.autoDispose
|
2019-09-22 16:18:44 +10:00
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
2018-05-07 06:05:54 +10:00
|
|
|
import java.net.URI
|
|
|
|
import java.net.URISyntaxException
|
2018-05-09 03:15:10 +10:00
|
|
|
import javax.inject.Inject
|
2018-05-07 06:05:54 +10:00
|
|
|
|
|
|
|
/** this is the base class for all activities that open links
|
|
|
|
* links are checked against the api if they are mastodon links so they can be openend in Tusky
|
|
|
|
* Subclasses must have a bottom sheet with Id item_status_bottom_sheet in their layout hierachy
|
|
|
|
*/
|
|
|
|
|
|
|
|
abstract class BottomSheetActivity : BaseActivity() {
|
|
|
|
|
|
|
|
lateinit var bottomSheet: BottomSheetBehavior<LinearLayout>
|
|
|
|
var searchUrl: String? = null
|
|
|
|
|
2018-05-09 03:15:10 +10:00
|
|
|
@Inject
|
|
|
|
lateinit var mastodonApi: MastodonApi
|
2018-05-07 06:05:54 +10:00
|
|
|
|
|
|
|
override fun onPostCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onPostCreate(savedInstanceState)
|
|
|
|
|
|
|
|
val bottomSheetLayout: LinearLayout = findViewById(R.id.item_status_bottom_sheet)
|
2019-09-22 16:18:44 +10:00
|
|
|
bottomSheet = BottomSheetBehavior.from(bottomSheetLayout)
|
|
|
|
bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
|
2019-10-23 06:18:20 +11:00
|
|
|
bottomSheet.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
|
2019-09-22 16:18:44 +10:00
|
|
|
override fun onStateChanged(bottomSheet: View, newState: Int) {
|
|
|
|
if (newState == BottomSheetBehavior.STATE_HIDDEN) {
|
|
|
|
cancelActiveSearch()
|
2018-05-07 06:05:54 +10:00
|
|
|
}
|
2019-09-22 16:18:44 +10:00
|
|
|
}
|
2018-05-07 06:05:54 +10:00
|
|
|
|
2019-09-22 16:18:44 +10:00
|
|
|
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
|
|
|
|
})
|
2018-05-07 06:05:54 +10:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:51:47 +11:00
|
|
|
open fun viewUrl(url: String, lookupFallbackBehavior: PostLookupFallbackBehavior = PostLookupFallbackBehavior.OPEN_IN_BROWSER) {
|
2018-05-07 06:05:54 +10:00
|
|
|
if (!looksLikeMastodonUrl(url)) {
|
|
|
|
openLink(url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-22 16:18:44 +10:00
|
|
|
mastodonApi.searchObservable(
|
|
|
|
query = url,
|
|
|
|
resolve = true
|
|
|
|
).observeOn(AndroidSchedulers.mainThread())
|
2019-09-22 17:49:26 +10:00
|
|
|
.autoDispose(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY))
|
2019-09-22 16:18:44 +10:00
|
|
|
.subscribe({ (accounts, statuses) ->
|
|
|
|
if (getCancelSearchRequested(url)) {
|
|
|
|
return@subscribe
|
2018-05-07 06:05:54 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
onEndSearch(url)
|
2019-09-22 16:18:44 +10:00
|
|
|
|
|
|
|
if (statuses.isNotEmpty()) {
|
|
|
|
viewThread(statuses[0].id, statuses[0].url)
|
|
|
|
return@subscribe
|
|
|
|
} else if (accounts.isNotEmpty()) {
|
|
|
|
viewAccount(accounts[0].id)
|
|
|
|
return@subscribe
|
|
|
|
}
|
|
|
|
|
2019-10-12 02:51:47 +11:00
|
|
|
performUrlFallbackAction(url, lookupFallbackBehavior)
|
2019-09-22 16:18:44 +10:00
|
|
|
}, {
|
|
|
|
if (!getCancelSearchRequested(url)) {
|
|
|
|
onEndSearch(url)
|
2019-10-12 02:51:47 +11:00
|
|
|
performUrlFallbackAction(url, lookupFallbackBehavior)
|
2019-09-22 16:18:44 +10:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-05-07 06:05:54 +10:00
|
|
|
onBeginSearch(url)
|
|
|
|
}
|
|
|
|
|
2019-02-13 05:22:37 +11:00
|
|
|
open fun viewThread(statusId: String, url: String?) {
|
2018-05-07 06:05:54 +10:00
|
|
|
if (!isSearching()) {
|
|
|
|
val intent = Intent(this, ViewThreadActivity::class.java)
|
2019-02-13 05:22:37 +11:00
|
|
|
intent.putExtra("id", statusId)
|
|
|
|
intent.putExtra("url", url)
|
2018-08-01 05:25:25 +10:00
|
|
|
startActivityWithSlideInAnimation(intent)
|
2018-05-07 06:05:54 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
open fun viewAccount(id: String) {
|
2018-06-18 21:26:18 +10:00
|
|
|
val intent = AccountActivity.getIntent(this, id)
|
2018-08-01 05:25:25 +10:00
|
|
|
startActivityWithSlideInAnimation(intent)
|
2018-05-07 06:05:54 +10:00
|
|
|
}
|
|
|
|
|
2019-10-12 02:51:47 +11:00
|
|
|
protected open fun performUrlFallbackAction(url: String, fallbackBehavior: PostLookupFallbackBehavior) {
|
|
|
|
when (fallbackBehavior) {
|
|
|
|
PostLookupFallbackBehavior.OPEN_IN_BROWSER -> openLink(url)
|
|
|
|
PostLookupFallbackBehavior.DISPLAY_ERROR -> Toast.makeText(this, getString(R.string.post_lookup_error_format, url), Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:05:54 +10:00
|
|
|
@VisibleForTesting
|
|
|
|
fun onBeginSearch(url: String) {
|
|
|
|
searchUrl = url
|
|
|
|
showQuerySheet()
|
|
|
|
}
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
fun getCancelSearchRequested(url: String): Boolean {
|
|
|
|
return url != searchUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
fun isSearching(): Boolean {
|
|
|
|
return searchUrl != null
|
|
|
|
}
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
fun onEndSearch(url: String?) {
|
|
|
|
if (url == searchUrl) {
|
|
|
|
// Don't clear query if there's no match,
|
|
|
|
// since we might just now be getting the response for a canceled search
|
|
|
|
searchUrl = null
|
|
|
|
hideQuerySheet()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
fun cancelActiveSearch() {
|
|
|
|
if (isSearching()) {
|
|
|
|
onEndSearch(searchUrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
open fun openLink(url: String) {
|
2019-02-12 00:43:57 +11:00
|
|
|
LinkHelper.openLink(url, this)
|
2018-05-07 06:05:54 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun showQuerySheet() {
|
2019-09-22 16:18:44 +10:00
|
|
|
bottomSheet.state = BottomSheetBehavior.STATE_COLLAPSED
|
2018-05-07 06:05:54 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun hideQuerySheet() {
|
2019-09-22 16:18:44 +10:00
|
|
|
bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
|
2018-05-07 06:05:54 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://mastodon.foo.bar/@User
|
|
|
|
// https://mastodon.foo.bar/@User/43456787654678
|
|
|
|
// https://pleroma.foo.bar/users/User
|
2020-02-26 05:59:40 +11:00
|
|
|
// https://pleroma.foo.bar/users/9qTHT2ANWUdXzENqC0
|
|
|
|
// https://pleroma.foo.bar/notice/9sBHWIlwwGZi5QGlHc
|
2018-05-07 06:05:54 +10:00
|
|
|
// https://pleroma.foo.bar/objects/d4643c42-3ae0-4b73-b8b0-c725f5819207
|
2020-02-26 05:59:40 +11:00
|
|
|
// https://friendica.foo.bar/profile/user
|
|
|
|
// https://friendica.foo.bar/display/d4643c42-3ae0-4b73-b8b0-c725f5819207
|
|
|
|
// https://misskey.foo.bar/notes/83w6r388br (always lowercase)
|
2018-05-07 06:05:54 +10:00
|
|
|
fun looksLikeMastodonUrl(urlString: String): Boolean {
|
|
|
|
val uri: URI
|
|
|
|
try {
|
|
|
|
uri = URI(urlString)
|
|
|
|
} catch (e: URISyntaxException) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uri.query != null ||
|
|
|
|
uri.fragment != null ||
|
|
|
|
uri.path == null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
val path = uri.path
|
|
|
|
return path.matches("^/@[^/]+$".toRegex()) ||
|
|
|
|
path.matches("^/@[^/]+/\\d+$".toRegex()) ||
|
2020-02-26 05:59:40 +11:00
|
|
|
path.matches("^/users/\\w+$".toRegex()) ||
|
|
|
|
path.matches("^/notice/[a-zA-Z0-9]+$".toRegex()) ||
|
|
|
|
path.matches("^/objects/[-a-f0-9]+$".toRegex()) ||
|
|
|
|
path.matches("^/notes/[a-z0-9]+$".toRegex()) ||
|
|
|
|
path.matches("^/display/[-a-f0-9]+$".toRegex()) ||
|
|
|
|
path.matches("^/profile/\\w+$".toRegex())
|
2018-05-07 06:05:54 +10:00
|
|
|
}
|
2019-10-12 02:51:47 +11:00
|
|
|
|
|
|
|
enum class PostLookupFallbackBehavior {
|
|
|
|
OPEN_IN_BROWSER,
|
|
|
|
DISPLAY_ERROR,
|
|
|
|
}
|