Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CustomTabs instead of WebView for login #4973

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
use CustomTabs instead of WebView for login
johnvan7 committed Jan 13, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 2804f366c92a43347385c9e4a70ae273a04a0bc5
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -118,6 +118,7 @@ android {

dependencies {
implementation(project(":common"))
implementation(libs.androidx.browser)

coreLibraryDesugaring(libs.tools.desugar.jdk)

14 changes: 13 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -306,7 +306,19 @@
<activity
android:name=".onboarding.OnboardingActivity"
android:configChanges="orientation|screenSize|keyboard|keyboardHidden|navigation"
android:theme="@style/Theme.HomeAssistant.Config" />
android:theme="@style/Theme.HomeAssistant.Config"
android:exported="true">

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:scheme="homeassistant"
android:host="auth-callback" />
</intent-filter>
</activity>

<activity
android:name=".settings.wear.SettingsWearActivity"
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
package io.homeassistant.companion.android.onboarding

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.activity.viewModels
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.app.NotificationManagerCompat
import androidx.fragment.app.commit
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.BaseActivity
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.onboarding.authentication.AuthenticationFragment
import io.homeassistant.companion.android.common.data.authentication.impl.AuthenticationService
import io.homeassistant.companion.android.onboarding.discovery.DiscoveryFragment
import io.homeassistant.companion.android.onboarding.integration.MobileAppIntegrationFragment
import io.homeassistant.companion.android.onboarding.manual.ManualSetupFragment
import io.homeassistant.companion.android.onboarding.welcome.WelcomeFragment
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl

@AndroidEntryPoint
class OnboardingActivity : BaseActivity() {

companion object {
private const val AUTHENTICATION_FRAGMENT = "authentication_fragment"
const val AUTH_CALLBACK = "homeassistant://auth-callback"
private const val TAG = "OnboardingActivity"

fun buildAuthUrl(context: Context, base: String): String {
return try {
val url = base.toHttpUrl()
val builder = if (url.host.endsWith("ui.nabu.casa", true)) {
HttpUrl.Builder()
.scheme(url.scheme)
.host(url.host)
.port(url.port)
} else {
url.newBuilder()
}
builder
.addPathSegments("auth/authorize")
.addEncodedQueryParameter("response_type", "code")
.addEncodedQueryParameter("client_id", AuthenticationService.CLIENT_ID)
.addEncodedQueryParameter("redirect_uri", AUTH_CALLBACK)
.build()
.toString()
} catch (e: Exception) {
Log.e(TAG, "Unable to build authentication URL", e)
Toast.makeText(context, io.homeassistant.companion.android.common.R.string.error_connection_failed, Toast.LENGTH_LONG).show()
""
}
}
}

private val viewModel by viewModels<OnboardingViewModel>()
@@ -66,15 +102,22 @@ class OnboardingActivity : BaseActivity() {
}
}
if (viewModel.manualContinueEnabled) {
supportFragmentManager.commit {
replace(R.id.content, AuthenticationFragment::class.java, null)
addToBackStack(null)
}
val uri = buildAuthUrl(baseContext, input.url)
val builder = CustomTabsIntent.Builder()
val customTabsIntent = builder.build()
customTabsIntent.launchUrl(baseContext, Uri.parse(uri))
}
}
}
}

if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
val uri = intent.data
if (uri != null && uri.scheme == "homeassistant") {
handleAuthCallback(uri.toString())
}
}

val onBackPressed = object : OnBackPressedCallback(supportFragmentManager.backStackEntryCount > 0) {
override fun handleOnBackPressed() {
supportFragmentManager.popBackStack()
@@ -86,6 +129,18 @@ class OnboardingActivity : BaseActivity() {
}
}

private fun handleAuthCallback(url: String) {
val code = Uri.parse(url).getQueryParameter("code")
if (url.startsWith(AUTH_CALLBACK) && !code.isNullOrBlank()) {
viewModel.registerAuthCode(code)
supportFragmentManager
.beginTransaction()
.replace(R.id.content, MobileAppIntegrationFragment::class.java, null)
.addToBackStack(null)
.commit()
}
}

override fun dispatchKeyEvent(event: KeyEvent): Boolean {
// Workaround to sideload on Android TV and use a remote for basic navigation in WebView
val fragmentManager = supportFragmentManager.findFragmentByTag(AUTHENTICATION_FRAGMENT)

This file was deleted.

Loading