From d115e7af56c5298a1d841643cf0a6264c57fa4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Thu, 24 Aug 2023 20:44:08 +0200 Subject: [PATCH] Update deeplink support with multiserver: ask which to use (#3811) - Ask the user which server to use, instead of assuming the most recently used, when using deeplinks with an app that has multiple servers configured --- .../android/launch/LaunchActivity.kt | 15 +++++++++ .../android/launch/LaunchPresenter.kt | 2 ++ .../android/launch/LaunchPresenterBase.kt | 2 ++ .../companion/android/launch/my/MyActivity.kt | 33 +++++++++++++++++-- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/launch/LaunchActivity.kt b/app/src/main/java/io/homeassistant/companion/android/launch/LaunchActivity.kt index cc335567b13..68b1c8382ed 100644 --- a/app/src/main/java/io/homeassistant/companion/android/launch/LaunchActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/launch/LaunchActivity.kt @@ -29,6 +29,7 @@ import io.homeassistant.companion.android.onboarding.OnboardApp import io.homeassistant.companion.android.onboarding.getMessagingToken import io.homeassistant.companion.android.sensors.LocationSensorManager import io.homeassistant.companion.android.settings.SettingViewModel +import io.homeassistant.companion.android.settings.server.ServerChooserFragment import io.homeassistant.companion.android.util.UrlUtil import io.homeassistant.companion.android.webview.WebViewActivity import kotlinx.coroutines.CoroutineScope @@ -89,6 +90,20 @@ class LaunchActivity : AppCompatActivity(), LaunchView { Class.forName("androidx.car.app.activity.CarAppActivity") ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) startActivity(carIntent) + } else if (presenter.hasMultipleServers() && intent.data?.path?.isNotBlank() == true) { + supportFragmentManager.setFragmentResultListener(ServerChooserFragment.RESULT_KEY, this) { _, bundle -> + val serverId = if (bundle.containsKey(ServerChooserFragment.RESULT_SERVER)) { + bundle.getInt(ServerChooserFragment.RESULT_SERVER) + } else { + null + } + supportFragmentManager.clearFragmentResultListener(ServerChooserFragment.RESULT_KEY) + startActivity(WebViewActivity.newInstance(this, intent.data?.path, serverId)) + finish() + overridePendingTransition(0, 0) // Disable activity start/stop animation + } + ServerChooserFragment().show(supportFragmentManager, ServerChooserFragment.TAG) + return } else { startActivity(WebViewActivity.newInstance(this, intent.data?.path)) } diff --git a/app/src/main/java/io/homeassistant/companion/android/launch/LaunchPresenter.kt b/app/src/main/java/io/homeassistant/companion/android/launch/LaunchPresenter.kt index 7931e013ddc..a6b95a2828b 100644 --- a/app/src/main/java/io/homeassistant/companion/android/launch/LaunchPresenter.kt +++ b/app/src/main/java/io/homeassistant/companion/android/launch/LaunchPresenter.kt @@ -6,5 +6,7 @@ interface LaunchPresenter { fun setSessionExpireMillis(value: Long) + fun hasMultipleServers(): Boolean + fun onFinish() } diff --git a/app/src/main/java/io/homeassistant/companion/android/launch/LaunchPresenterBase.kt b/app/src/main/java/io/homeassistant/companion/android/launch/LaunchPresenterBase.kt index 63ef764f31d..0e913b8e312 100644 --- a/app/src/main/java/io/homeassistant/companion/android/launch/LaunchPresenterBase.kt +++ b/app/src/main/java/io/homeassistant/companion/android/launch/LaunchPresenterBase.kt @@ -49,6 +49,8 @@ abstract class LaunchPresenterBase( } } + override fun hasMultipleServers(): Boolean = serverManager.defaultServers.size > 1 + override fun onFinish() { mainScope.cancel() } diff --git a/app/src/main/java/io/homeassistant/companion/android/launch/my/MyActivity.kt b/app/src/main/java/io/homeassistant/companion/android/launch/my/MyActivity.kt index ac2e47a2686..b07b4d8a0d3 100644 --- a/app/src/main/java/io/homeassistant/companion/android/launch/my/MyActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/launch/my/MyActivity.kt @@ -8,11 +8,16 @@ import android.os.Bundle import android.webkit.WebResourceRequest import android.webkit.WebView import android.webkit.WebViewClient +import dagger.hilt.android.AndroidEntryPoint import io.homeassistant.companion.android.BaseActivity import io.homeassistant.companion.android.BuildConfig +import io.homeassistant.companion.android.common.data.servers.ServerManager import io.homeassistant.companion.android.databinding.ActivityMyBinding +import io.homeassistant.companion.android.settings.server.ServerChooserFragment import io.homeassistant.companion.android.webview.WebViewActivity +import javax.inject.Inject +@AndroidEntryPoint class MyActivity : BaseActivity() { companion object { @@ -25,6 +30,9 @@ class MyActivity : BaseActivity() { } } + @Inject + lateinit var serverManager: ServerManager + @SuppressLint("SetJavaScriptEnabled") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -52,8 +60,7 @@ class MyActivity : BaseActivity() { ): Boolean { val url = request?.url.toString() if (url.startsWith("homeassistant://navigate/")) { - startActivity(WebViewActivity.newInstance(context, url.removePrefix("homeassistant://navigate/"))) - finish() + navigateTo(url.removePrefix("homeassistant://navigate/")) return true } return false @@ -63,4 +70,26 @@ class MyActivity : BaseActivity() { binding.webview.loadUrl(newUri.toString()) } } + + private fun navigateTo(path: String) { + if (serverManager.defaultServers.size > 1) { + supportFragmentManager.setFragmentResultListener(ServerChooserFragment.RESULT_KEY, this) { _, bundle -> + if (bundle.containsKey(ServerChooserFragment.RESULT_SERVER)) { + startActivity( + WebViewActivity.newInstance( + context = this, + path = path, + serverId = bundle.getInt(ServerChooserFragment.RESULT_SERVER) + ) + ) + finish() + } + supportFragmentManager.clearFragmentResultListener(ServerChooserFragment.RESULT_KEY) + } + ServerChooserFragment().show(supportFragmentManager, ServerChooserFragment.TAG) + } else { + startActivity(WebViewActivity.newInstance(context = this, path = path)) + finish() + } + } }