Skip to content

Commit e5a1c9b

Browse files
committed
If routing goes poorly, open in browser. If already viewing project, open the right tab
1 parent 4461b16 commit e5a1c9b

File tree

8 files changed

+73
-32
lines changed

8 files changed

+73
-32
lines changed

app/src/main/java/com/commit451/gitlab/activity/ProjectActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class ProjectActivity : BaseActivity() {
8787
var project: Project? = null
8888
var ref: Ref? = null
8989

90+
private var adapter: ProjectPagerAdapter? = null
91+
9092
private val projectSelection by extraOrNull<DeepLinker.ProjectSelection>(EXTRA_PROJECT_SELECTION)
9193

9294
private val onMenuItemClickListener = Toolbar.OnMenuItemClickListener { item ->
@@ -190,6 +192,17 @@ class ProjectActivity : BaseActivity() {
190192
return true
191193
}
192194

195+
override fun onNewIntent(intent: Intent?) {
196+
super.onNewIntent(intent)
197+
val selection = intent?.getSerializableExtra(EXTRA_PROJECT_SELECTION) as? DeepLinker.ProjectSelection
198+
selection?.let {
199+
val index = adapter?.indexForSelection(it)
200+
if (index != null) {
201+
viewPager.setCurrentItem(index, false)
202+
}
203+
}
204+
}
205+
193206
private fun loadProject(projectId: String) {
194207
showProgress()
195208
loadProject(App.get().gitLab.getProject(projectId))
@@ -242,6 +255,7 @@ class ProjectActivity : BaseActivity() {
242255

243256
private fun setupTabs() {
244257
val adapter = ProjectPagerAdapter(this, supportFragmentManager)
258+
this.adapter = adapter
245259
viewPager.adapter = adapter
246260
tabLayout.setupWithViewPager(viewPager)
247261
projectSelection?.let {

app/src/main/java/com/commit451/gitlab/activity/RoutingActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class RoutingActivity : BaseActivity() {
5959
override fun onRouteUnknown(url: String?) {
6060
Timber.d("Route unknown. Opening original Uri if it exists")
6161
if (url != null) {
62-
IntentUtil.openPage(this@RoutingActivity, url.toString(), App.get().currentAccount)
62+
IntentUtil.openBrowser(this@RoutingActivity, url.toString().replaceFirst("labcoat://", "https://"))
6363
} else {
6464
Toast.makeText(this@RoutingActivity, R.string.deeplink_navigate_error, Toast.LENGTH_SHORT)
6565
.show()

app/src/main/java/com/commit451/gitlab/fragment/FeedFragment.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.commit451.gitlab.model.rss.Entry
2020
import com.commit451.gitlab.model.rss.Feed
2121
import com.commit451.gitlab.navigation.Navigator
2222
import com.commit451.gitlab.rx.CustomSingleObserver
23-
import com.commit451.gitlab.util.IntentUtil
2423
import com.novoda.simplechromecustomtabs.SimpleChromeCustomTabs
2524
import timber.log.Timber
2625

@@ -72,7 +71,7 @@ class FeedFragment : ButterKnifeFragment() {
7271
Snackbar.make(swipeRefreshLayout, R.string.not_a_valid_url, Snackbar.LENGTH_SHORT)
7372
.show()
7473
} else {
75-
IntentUtil.openBrowser(baseActivty, entry.link.href, App.get().getAccount())
74+
Navigator.navigateToUrl(baseActivty, entry.link.href, App.get().getAccount())
7675
}
7776
}
7877
})
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package com.commit451.gitlab.navigation
22

33
import android.content.Context
4-
import android.content.Intent
54
import android.net.Uri
6-
import android.widget.Toast
7-
8-
import com.commit451.gitlab.R
5+
import com.commit451.gitlab.util.IntentUtil
96
import com.novoda.simplechromecustomtabs.navigation.NavigationFallback
10-
117
import java.lang.ref.WeakReference
128

139
/**
@@ -18,15 +14,7 @@ class BrowserFallback(context: Context) : NavigationFallback {
1814
private val context: WeakReference<Context> = WeakReference(context)
1915

2016
override fun onFallbackNavigateTo(url: Uri) {
21-
val intent = Intent(Intent.ACTION_VIEW)
22-
intent.data = url
2317
val context = this.context.get() ?: return
24-
try {
25-
context.startActivity(intent)
26-
} catch (e: Exception) {
27-
Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT)
28-
.show()
29-
}
30-
18+
IntentUtil.openBrowser(context, url.toString())
3119
}
3220
}

app/src/main/java/com/commit451/gitlab/navigation/DeepLinker.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ object DeepLinker {
2626
navigator.onRouteUnknown(null)
2727
return
2828
}
29-
val link = HttpUrl.parse(url)
29+
// It doesn't like it if we have a host like this, so replace it
30+
val link = HttpUrl.parse(url.replaceFirst("labcoat://", "https://"))
3031
if (link == null) {
3132
navigator.onRouteUnknown(url)
3233
return

app/src/main/java/com/commit451/gitlab/navigation/Navigator.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,12 @@ object Navigator {
215215
fun navigateToUrl(activity: Activity, url: String, account: Account) {
216216
Timber.d("navigateToUrl: $url")
217217
val uri = Uri.parse(url.resolveUrl(account))
218-
IntentUtil.openPage(activity as BaseActivity, uri.toString())
218+
val serverUri = Uri.parse(account.serverUrl)
219+
if (serverUri.host == uri.host) {
220+
activity.startActivity(DeepLinker.generateDeeplinkIntentFromUri(activity, uri))
221+
} else {
222+
IntentUtil.openPage(activity as BaseActivity, uri.toString())
223+
}
219224
}
220225

221226
/**

app/src/main/java/com/commit451/gitlab/util/IntentUtil.kt

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.support.design.widget.Snackbar
77
import android.view.View
88
import android.widget.Toast
99
import com.commit451.addendum.themeAttrColor
10-
import com.commit451.easel.Easel
1110
import com.commit451.gitlab.R
1211
import com.commit451.gitlab.activity.BaseActivity
1312
import com.commit451.gitlab.extension.resolveUrl
@@ -34,18 +33,6 @@ object IntentUtil {
3433
.navigateTo(Uri.parse(resolvedUrl), activity)
3534
}
3635

37-
fun openBrowser(context: Context, url: String, account: Account? = null) {
38-
val intent = Intent(Intent.ACTION_VIEW)
39-
val resolvedUrl = if (account == null) url else url.resolveUrl(account)
40-
intent.data = Uri.parse(resolvedUrl)
41-
try {
42-
context.startActivity(intent)
43-
} catch (e: Exception) {
44-
Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT)
45-
.show()
46-
}
47-
}
48-
4936
fun share(root: View, url: Uri) {
5037
val shareIntent = Intent(Intent.ACTION_SEND)
5138
shareIntent.type = "text/plain"
@@ -58,4 +45,43 @@ object IntentUtil {
5845
}
5946

6047
}
48+
49+
/**
50+
* Opens the link in the browser. Will never open in the app, even if it matches the schema
51+
*/
52+
fun openBrowser(context: Context, url: String) {
53+
54+
val intent = Intent(Intent.ACTION_VIEW)
55+
val uri = Uri.parse(url)
56+
intent.data = uri
57+
val resInfos = context.packageManager.queryIntentActivities(intent, 0)
58+
val intents = mutableListOf<Intent>()
59+
resInfos.forEach {
60+
if (!it.activityInfo.packageName.contains(context.packageName)) {
61+
val intentToAdd = Intent(Intent.ACTION_VIEW)
62+
intentToAdd.data = uri
63+
intentToAdd.setPackage(it.activityInfo.packageName)
64+
intents.add(intentToAdd)
65+
}
66+
}
67+
68+
when {
69+
intents.isEmpty() -> {
70+
Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT)
71+
.show()
72+
}
73+
intents.size == 1 -> try {
74+
context.startActivity(intents.first())
75+
} catch (e: Exception) {
76+
Toast.makeText(context, R.string.error_no_browser, Toast.LENGTH_SHORT)
77+
.show()
78+
}
79+
else -> {
80+
// remove the first intent and show it as the main option
81+
val chooserIntent = Intent.createChooser(intents.removeAt(0), "Choose browser")
82+
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toTypedArray())
83+
context.startActivity(chooserIntent)
84+
}
85+
}
86+
}
6187
}

app/src/test/java/com/commit451/gitlab/DeepLinkerTests.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ class DeepLinkerTests {
4040
DeepLinker.route(link, callbacks)
4141
Assert.assertEquals(1, callbacks.build)
4242
}
43+
44+
@Test
45+
fun issuesInternalTest() {
46+
val link = "labcoat://gitlab.com/Commit451/LabCoat/issues/392"
47+
val callbacks = CounterCallbacks()
48+
DeepLinker.route(link, callbacks)
49+
Assert.assertEquals(1, callbacks.issue)
50+
}
4351
}

0 commit comments

Comments
 (0)