Skip to content
Snippets Groups Projects
Commit e5a1c9b7 authored by John's avatar John
Browse files

If routing goes poorly, open in browser. If already viewing project, open the right tab

parent 4461b168
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -87,6 +87,8 @@ class ProjectActivity : BaseActivity() {
var project: Project? = null
var ref: Ref? = null
 
private var adapter: ProjectPagerAdapter? = null
private val projectSelection by extraOrNull<DeepLinker.ProjectSelection>(EXTRA_PROJECT_SELECTION)
 
private val onMenuItemClickListener = Toolbar.OnMenuItemClickListener { item ->
Loading
Loading
@@ -190,6 +192,17 @@ class ProjectActivity : BaseActivity() {
return true
}
 
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
val selection = intent?.getSerializableExtra(EXTRA_PROJECT_SELECTION) as? DeepLinker.ProjectSelection
selection?.let {
val index = adapter?.indexForSelection(it)
if (index != null) {
viewPager.setCurrentItem(index, false)
}
}
}
private fun loadProject(projectId: String) {
showProgress()
loadProject(App.get().gitLab.getProject(projectId))
Loading
Loading
@@ -242,6 +255,7 @@ class ProjectActivity : BaseActivity() {
 
private fun setupTabs() {
val adapter = ProjectPagerAdapter(this, supportFragmentManager)
this.adapter = adapter
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
projectSelection?.let {
Loading
Loading
Loading
Loading
@@ -59,7 +59,7 @@ class RoutingActivity : BaseActivity() {
override fun onRouteUnknown(url: String?) {
Timber.d("Route unknown. Opening original Uri if it exists")
if (url != null) {
IntentUtil.openPage(this@RoutingActivity, url.toString(), App.get().currentAccount)
IntentUtil.openBrowser(this@RoutingActivity, url.toString().replaceFirst("labcoat://", "https://"))
} else {
Toast.makeText(this@RoutingActivity, R.string.deeplink_navigate_error, Toast.LENGTH_SHORT)
.show()
Loading
Loading
Loading
Loading
@@ -20,7 +20,6 @@ import com.commit451.gitlab.model.rss.Entry
import com.commit451.gitlab.model.rss.Feed
import com.commit451.gitlab.navigation.Navigator
import com.commit451.gitlab.rx.CustomSingleObserver
import com.commit451.gitlab.util.IntentUtil
import com.novoda.simplechromecustomtabs.SimpleChromeCustomTabs
import timber.log.Timber
 
Loading
Loading
@@ -72,7 +71,7 @@ class FeedFragment : ButterKnifeFragment() {
Snackbar.make(swipeRefreshLayout, R.string.not_a_valid_url, Snackbar.LENGTH_SHORT)
.show()
} else {
IntentUtil.openBrowser(baseActivty, entry.link.href, App.get().getAccount())
Navigator.navigateToUrl(baseActivty, entry.link.href, App.get().getAccount())
}
}
})
Loading
Loading
package com.commit451.gitlab.navigation
 
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import com.commit451.gitlab.R
import com.commit451.gitlab.util.IntentUtil
import com.novoda.simplechromecustomtabs.navigation.NavigationFallback
import java.lang.ref.WeakReference
 
/**
Loading
Loading
@@ -18,15 +14,7 @@ class BrowserFallback(context: Context) : NavigationFallback {
private val context: WeakReference<Context> = WeakReference(context)
 
override fun onFallbackNavigateTo(url: Uri) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = url
val context = this.context.get() ?: return
try {
context.startActivity(intent)
} catch (e: Exception) {
Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT)
.show()
}
IntentUtil.openBrowser(context, url.toString())
}
}
\ No newline at end of file
Loading
Loading
@@ -26,7 +26,8 @@ object DeepLinker {
navigator.onRouteUnknown(null)
return
}
val link = HttpUrl.parse(url)
// It doesn't like it if we have a host like this, so replace it
val link = HttpUrl.parse(url.replaceFirst("labcoat://", "https://"))
if (link == null) {
navigator.onRouteUnknown(url)
return
Loading
Loading
Loading
Loading
@@ -215,7 +215,12 @@ object Navigator {
fun navigateToUrl(activity: Activity, url: String, account: Account) {
Timber.d("navigateToUrl: $url")
val uri = Uri.parse(url.resolveUrl(account))
IntentUtil.openPage(activity as BaseActivity, uri.toString())
val serverUri = Uri.parse(account.serverUrl)
if (serverUri.host == uri.host) {
activity.startActivity(DeepLinker.generateDeeplinkIntentFromUri(activity, uri))
} else {
IntentUtil.openPage(activity as BaseActivity, uri.toString())
}
}
 
/**
Loading
Loading
Loading
Loading
@@ -7,7 +7,6 @@ import android.support.design.widget.Snackbar
import android.view.View
import android.widget.Toast
import com.commit451.addendum.themeAttrColor
import com.commit451.easel.Easel
import com.commit451.gitlab.R
import com.commit451.gitlab.activity.BaseActivity
import com.commit451.gitlab.extension.resolveUrl
Loading
Loading
@@ -34,18 +33,6 @@ object IntentUtil {
.navigateTo(Uri.parse(resolvedUrl), activity)
}
 
fun openBrowser(context: Context, url: String, account: Account? = null) {
val intent = Intent(Intent.ACTION_VIEW)
val resolvedUrl = if (account == null) url else url.resolveUrl(account)
intent.data = Uri.parse(resolvedUrl)
try {
context.startActivity(intent)
} catch (e: Exception) {
Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT)
.show()
}
}
fun share(root: View, url: Uri) {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "text/plain"
Loading
Loading
@@ -58,4 +45,43 @@ object IntentUtil {
}
 
}
/**
* Opens the link in the browser. Will never open in the app, even if it matches the schema
*/
fun openBrowser(context: Context, url: String) {
val intent = Intent(Intent.ACTION_VIEW)
val uri = Uri.parse(url)
intent.data = uri
val resInfos = context.packageManager.queryIntentActivities(intent, 0)
val intents = mutableListOf<Intent>()
resInfos.forEach {
if (!it.activityInfo.packageName.contains(context.packageName)) {
val intentToAdd = Intent(Intent.ACTION_VIEW)
intentToAdd.data = uri
intentToAdd.setPackage(it.activityInfo.packageName)
intents.add(intentToAdd)
}
}
when {
intents.isEmpty() -> {
Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT)
.show()
}
intents.size == 1 -> try {
context.startActivity(intents.first())
} catch (e: Exception) {
Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT)
.show()
}
else -> {
// remove the first intent and show it as the main option
val chooserIntent = Intent.createChooser(intents.removeAt(0), "Choose browser")
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toTypedArray())
context.startActivity(chooserIntent)
}
}
}
}
Loading
Loading
@@ -40,4 +40,12 @@ class DeepLinkerTests {
DeepLinker.route(link, callbacks)
Assert.assertEquals(1, callbacks.build)
}
@Test
fun issuesInternalTest() {
val link = "labcoat://gitlab.com/Commit451/LabCoat/issues/392"
val callbacks = CounterCallbacks()
DeepLinker.route(link, callbacks)
Assert.assertEquals(1, callbacks.issue)
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment