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

Adding Transition from Park to Drive #3551

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.Log
import androidx.annotation.RequiresApi
import androidx.car.app.CarAppService
import androidx.car.app.Screen
import androidx.car.app.ScreenManager
import androidx.car.app.Session
import androidx.car.app.SessionInfo
import androidx.car.app.validation.HostValidator
Expand Down Expand Up @@ -66,12 +67,43 @@ class HaCarAppService : CarAppService() {
)

override fun onCreateScreen(intent: Intent): Screen {
return MainVehicleScreen(
carContext,
serverManager,
serverIdFlow,
entityFlow
) { loadEntities(lifecycleScope, it) }
if (intent.getBooleanExtra("TRANSITION_LAUNCH", false)) {
carContext
.getCarService(ScreenManager::class.java).run {
push(
MainVehicleScreen(
carContext,
serverManager,
serverIdFlow,
entityFlow
) { loadEntities(lifecycleScope, it) }
)

push(
LoginScreen(
carContext,
serverManager
)
)
}
return SwitchToDrivingOptimizedScreen(carContext)
} else {
carContext
.getCarService(ScreenManager::class.java).run {
push(
MainVehicleScreen(
carContext,
serverManager,
serverIdFlow,
entityFlow
) { loadEntities(lifecycleScope, it) }
)
}
return LoginScreen(
carContext,
serverManager
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.homeassistant.companion.android.vehicle

import android.content.Intent
import android.content.pm.PackageManager
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.MessageTemplate
import androidx.car.app.model.ParkedOnlyOnClickListener
import androidx.car.app.model.Template
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import io.homeassistant.companion.android.common.R
import io.homeassistant.companion.android.common.data.authentication.SessionState
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.launch.LaunchActivity
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LoginScreen(context: CarContext, val serverManager: ServerManager) : Screen(context) {
private var isLoggedIn: Boolean? = null

init {
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
isLoggedIn = serverManager.isRegistered() &&
serverManager.authenticationRepository()
.getSessionState() == SessionState.CONNECTED
invalidate()
while (isLoggedIn != true) {
delay(1000)
isLoggedIn = serverManager.isRegistered() &&
serverManager.authenticationRepository()
.getSessionState() == SessionState.CONNECTED
}
if (isLoggedIn == true) {
screenManager.pop()
}
}
}
}

override fun onGetTemplate(): Template {
return MessageTemplate.Builder(carContext.getString(R.string.aa_app_not_logged_in))
.setTitle(carContext.getString(R.string.app_name))
.setHeaderAction(Action.APP_ICON)
.addAction(
Action.Builder()
.setTitle(carContext.getString(R.string.login))
.setOnClickListener(
ParkedOnlyOnClickListener.create {
startNativeActivity()
}
)
.build()
)
.build()
}

private val isAutomotive get() = carContext.packageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)

private fun startNativeActivity() {
with(carContext) {
startActivity(
Intent(
carContext,
LaunchActivity::class.java
).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
)
if (isAutomotive) {
finishCarApp()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import androidx.car.app.model.CarColor
import androidx.car.app.model.CarIcon
import androidx.car.app.model.ItemList
import androidx.car.app.model.ListTemplate
import androidx.car.app.model.MessageTemplate
import androidx.car.app.model.ParkedOnlyOnClickListener
import androidx.car.app.model.Row
import androidx.car.app.model.Template
import androidx.lifecycle.DefaultLifecycleObserver
Expand Down Expand Up @@ -130,23 +128,6 @@ class MainVehicleScreen(
}

override fun onGetTemplate(): Template {
if (isLoggedIn == false) {
return MessageTemplate.Builder(carContext.getString(commonR.string.aa_app_not_logged_in))
.setTitle(carContext.getString(commonR.string.app_name))
.setHeaderAction(Action.APP_ICON)
.addAction(
Action.Builder()
.setTitle(carContext.getString(commonR.string.login))
.setOnClickListener(
ParkedOnlyOnClickListener.create {
startNativeActivity()
}
)
.build()
)
.build()
}

val listBuilder = ItemList.Builder()
domains.forEach { domain ->
val friendlyDomain =
Expand Down Expand Up @@ -287,13 +268,14 @@ class MainVehicleScreen(
car = Car.createCar(carContext)
carRestrictionManager =
car?.getCarManager(Car.CAR_UX_RESTRICTION_SERVICE) as CarUxRestrictionsManager
var listener =
val listener =
CarUxRestrictionsManager.OnUxRestrictionsChangedListener { restrictions ->
invalidate()
}
carRestrictionManager?.registerListener(listener)
}
}

private fun startNativeActivity() {
Log.i(TAG, "Starting login activity")
with(carContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.homeassistant.companion.android.vehicle

import android.util.Log
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.CarIcon
import androidx.car.app.model.MessageTemplate
import androidx.car.app.model.Template
import io.homeassistant.companion.android.common.R

class SwitchToDrivingOptimizedScreen(carContext: CarContext) : Screen(carContext) {

override fun onGetTemplate(): Template {
Log.i(TAG, "onGetTemplate")
return MessageTemplate.Builder(carContext.getString(R.string.aa_driving_optimized_change))
.setIcon(CarIcon.APP_ICON)
.addAction(
Action.Builder()
.setFlags(Action.FLAG_DEFAULT)
.setTitle(carContext.getString(R.string.continue_connect))
.setOnClickListener {
screenManager.pop()
}
.build()
).build()
}

companion object {
private const val TAG = "DrvOptScreen"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ open class BaseActivity : AppCompatActivity() {
Intent(
this,
Class.forName("androidx.car.app.activity.CarAppActivity")
).addFlags(FLAG_ACTIVITY_NEW_TASK)
).putExtra("TRANSITION_LAUNCH", true).addFlags(FLAG_ACTIVITY_NEW_TASK)
)
overridePendingTransition(
androidx.appcompat.R.anim.abc_slide_in_bottom,
Expand Down
1 change: 1 addition & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>
<string name="aa_app_not_logged_in">Not logged in</string>
<string name="aa_launch_native">Native mode</string>
<string name="aa_driving_optimized_change">Use Home Assistant in drive mode?</string>
<string name="aa_navigation">Navigation</string>
<string name="aa_no_entities_with_locations">No entities with locations found.</string>
<string name="aa_change_server">Change server</string>
Expand Down