Skip to content

Commit

Permalink
Update deeplink support with multiserver: ask which to use (#3811)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jpelgrom authored Aug 24, 2023
1 parent 1bdbd3f commit d115e7a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ interface LaunchPresenter {

fun setSessionExpireMillis(value: Long)

fun hasMultipleServers(): Boolean

fun onFinish()
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ abstract class LaunchPresenterBase(
}
}

override fun hasMultipleServers(): Boolean = serverManager.defaultServers.size > 1

override fun onFinish() {
mainScope.cancel()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,6 +30,9 @@ class MyActivity : BaseActivity() {
}
}

@Inject
lateinit var serverManager: ServerManager

@SuppressLint("SetJavaScriptEnabled")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -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
Expand All @@ -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()
}
}
}

0 comments on commit d115e7a

Please sign in to comment.