Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/util/OnLinkClickHandler.kt

223 lines
9.0 KiB
Kotlin
Raw Normal View History

2016-07-04 03:31:17 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.util
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
2016-07-04 03:31:17 +02:00
import android.net.Uri
import android.os.BadParcelableException
2016-11-27 07:23:07 +01:00
import android.support.customtabs.CustomTabsIntent
2016-07-04 03:31:17 +02:00
import edu.tsinghua.hotmobi.HotMobiLogger
import edu.tsinghua.hotmobi.model.LinkEvent
import org.mariotaku.chameleon.Chameleon
import org.mariotaku.chameleon.ChameleonUtils
import org.mariotaku.kpreferences.get
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.activity.WebLinkHandlerActivity
import org.mariotaku.twidere.annotation.Referral
import org.mariotaku.twidere.app.TwidereApplication
import org.mariotaku.twidere.constant.IntentConstants.EXTRA_ACCOUNT_KEY
import org.mariotaku.twidere.constant.chromeCustomTabKey
2017-01-20 15:08:42 +01:00
import org.mariotaku.twidere.constant.displaySensitiveContentsKey
import org.mariotaku.twidere.constant.newDocumentApiKey
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.model.util.ParcelableMediaUtils
import org.mariotaku.twidere.util.TwidereLinkify.OnLinkClickListener
2017-02-04 07:13:08 +01:00
import org.mariotaku.twidere.util.TwidereLinkify.USER_TYPE_FANFOU_COM
2016-07-04 03:31:17 +02:00
import org.mariotaku.twidere.util.media.preview.PreviewMediaExtractor
open class OnLinkClickHandler(
protected val context: Context,
protected val manager: MultiSelectManager?,
protected val preferences: SharedPreferences
2016-07-04 03:31:17 +02:00
) : OnLinkClickListener {
2016-07-16 09:27:46 +02:00
override fun onLinkClick(link: String, orig: String?, accountKey: UserKey?,
2017-04-05 07:12:01 +02:00
extraId: Long, type: Int, sensitive: Boolean,
start: Int, end: Int): Boolean {
2016-07-04 03:31:17 +02:00
if (manager != null && manager.isActive) return false
if (!isPrivateData) {
// BEGIN HotMobi
val event = LinkEvent.create(context, link, type)
HotMobiLogger.getInstance(context).log(accountKey, event)
// END HotMobi
}
when (type) {
TwidereLinkify.LINK_TYPE_MENTION -> {
2017-01-20 15:08:42 +01:00
IntentUtils.openUserProfile(context, accountKey, null, link, preferences[newDocumentApiKey],
Referral.USER_MENTION, null)
2016-07-04 03:31:17 +02:00
return true
}
TwidereLinkify.LINK_TYPE_HASHTAG -> {
IntentUtils.openTweetSearch(context, accountKey, "#" + link)
return true
}
TwidereLinkify.LINK_TYPE_LINK_IN_TEXT -> {
2017-02-04 07:13:08 +01:00
if (accountKey != null && isMedia(link, extraId)) {
openMedia(accountKey, extraId, sensitive, link, start, end)
2016-07-04 03:31:17 +02:00
} else {
openLink(link)
}
return true
}
TwidereLinkify.LINK_TYPE_ENTITY_URL -> {
2017-02-04 07:13:08 +01:00
if (accountKey != null && isMedia(link, extraId)) {
openMedia(accountKey, extraId, sensitive, link, start, end)
2016-07-04 03:31:17 +02:00
} else {
val authority = UriUtils.getAuthority(link)
if (authority == null) {
openLink(link)
return true
}
when (authority) {
2017-02-04 07:13:08 +01:00
"fanfou.com" -> if (accountKey != null && handleFanfouLink(link, orig, accountKey)) {
return true
2016-07-04 03:31:17 +02:00
}
2017-02-04 07:13:08 +01:00
else -> if (IntentUtils.isWebLinkHandled(context, Uri.parse(link))) {
openTwitterLink(link, accountKey!!)
return true
2016-07-04 03:31:17 +02:00
}
}
openLink(link)
}
return true
}
TwidereLinkify.LINK_TYPE_LIST -> {
2017-04-07 06:31:30 +02:00
val mentionList = link.split("/")
2016-07-04 03:31:17 +02:00
if (mentionList.size != 2) {
return false
}
IntentUtils.openUserListDetails(context, accountKey, null, null, mentionList[0],
mentionList[1])
return true
}
TwidereLinkify.LINK_TYPE_CASHTAG -> {
IntentUtils.openTweetSearch(context, accountKey, link)
return true
}
TwidereLinkify.LINK_TYPE_USER_ID -> {
2017-01-20 15:08:42 +01:00
IntentUtils.openUserProfile(context, accountKey, UserKey.valueOf(link), null, preferences[newDocumentApiKey],
Referral.USER_MENTION,
null)
2016-07-04 03:31:17 +02:00
return true
}
}
return false
}
protected open val isPrivateData: Boolean
get() = false
protected open fun isMedia(link: String, extraId: Long): Boolean {
return PreviewMediaExtractor.isSupported(link)
}
protected open fun openMedia(accountKey: UserKey, extraId: Long, sensitive: Boolean, link: String, start: Int, end: Int) {
val media = arrayOf(ParcelableMediaUtils.image(link))
2017-01-20 15:08:42 +01:00
IntentUtils.openMedia(context, accountKey, media, null, sensitive, preferences[newDocumentApiKey],
preferences[displaySensitiveContentsKey])
2016-07-04 03:31:17 +02:00
}
protected open fun openLink(link: String) {
if (manager != null && manager.isActive) return
2017-04-05 07:12:01 +02:00
openLink(context, preferences, link)
2016-07-04 03:31:17 +02:00
}
protected fun openTwitterLink(link: String, accountKey: UserKey) {
if (manager != null && manager.isActive) return
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(link))
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.setClass(context, WebLinkHandlerActivity::class.java)
intent.putExtra(EXTRA_ACCOUNT_KEY, accountKey)
intent.setExtrasClassLoader(TwidereApplication::class.java.classLoader)
if (intent.resolveActivity(context.packageManager) != null) {
try {
context.startActivity(intent)
} catch (e: BadParcelableException) {
// Ignore
}
}
}
2017-02-04 07:13:08 +01:00
private fun handleFanfouLink(link: String, orig: String?, accountKey: UserKey): Boolean {
if (orig == null) return false
// Process special case for fanfou
val ch = orig[0]
// Extend selection
val length = orig.length
if (TwidereLinkify.isAtSymbol(ch)) {
var id = UriUtils.getPath(link)
if (id != null) {
val idxOfSlash = id.indexOf('/')
if (idxOfSlash == 0) {
id = id.substring(1)
}
val screenName = orig.substring(1, length)
IntentUtils.openUserProfile(context, accountKey, UserKey.valueOf(id),
screenName, preferences[newDocumentApiKey], Referral.USER_MENTION,
null)
return true
}
} else if (TwidereLinkify.isHashSymbol(ch) && TwidereLinkify.isHashSymbol(orig[length - 1])) {
IntentUtils.openSearch(context, accountKey, orig.substring(1, length - 1))
return true
}
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(link))
intent.setClass(context, WebLinkHandlerActivity::class.java)
if (accountKey.host == USER_TYPE_FANFOU_COM) {
intent.putExtra(EXTRA_ACCOUNT_KEY, accountKey)
}
context.startActivity(intent)
return true
}
2017-04-05 07:12:01 +02:00
companion object {
fun openLink(context: Context, preferences: SharedPreferences, link: String) {
val uri = Uri.parse(link)
if (!preferences[chromeCustomTabKey]) {
val viewIntent = Intent(Intent.ACTION_VIEW, uri)
viewIntent.addCategory(Intent.CATEGORY_BROWSABLE)
try {
return context.startActivity(viewIntent)
} catch (e: ActivityNotFoundException) {
// Ignore
}
return
}
val builder = CustomTabsIntent.Builder()
builder.addDefaultShareMenuItem()
(ChameleonUtils.getActivity(context) as? Chameleon.Themeable)?.overrideTheme?.let { theme ->
builder.setToolbarColor(theme.colorToolbar)
}
val intent = builder.build()
try {
intent.launchUrl(context, uri)
} catch (e: ActivityNotFoundException) {
// Ignore
}
}
}
2016-07-04 03:31:17 +02:00
}