Reputation: 7995
I have an Android/Kotlin application and I would like to configure deep links for certain pages to open inside my application (to look native).
At the moment, I have intent filters that redirect the user to an activity with WebView in which I open the desired url:
<activity android:name=".activity.WebViewActivity">
<intent-filter android:label="Futurity">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="api.example.com"
android:pathPrefix="/auth/confirm"
android:scheme="https" />
</intent-filter>
</activity>
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_web_view)
val data = intent.data
// construct url
val url = if (intent.data != null ) {
"https://" + data.host + data.path + "?" + data.query
}
appWebView.webViewClient = WebViewClient()
appWebView.loadUrl(url)
}
}
This works fine, but I would like to use Chrome custom tabs instead for security reasons.
However, when I try to configure the custom tab instead of WebView, I am getting an infinite loop of redirects between the page (launched in the chrome tab) and the intent filter which immediately redirects user back to the activity:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
How can I achieve the same behavior as with webviews but without modifying the url? Is it even doable?
Upvotes: 7
Views: 3926
Reputation: 520
I did some digging and found out that you should search for packages that support the "warming services". If a package is returned then you can assign the package name to the Intent. Google has a GitHub repo with some helper classes. The one in question is CustomTabHelper#getPackageNameToUse().
So in your case:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
String packageName = CustomTabsHelper.getPackageNameToUse(getContext());
if (packageName != null) {
customTabsIntent.intent.setPackage(packageName);
}
customTabsIntent.launchUrl(this, Uri.parse(url));
If a package is not found then you'll want to have some fallback code else the infinite loop will keep happening.
Upvotes: 1