diff --git a/app/src/full/java/io/homeassistant/companion/android/vehicle/HaCarAppService.kt b/app/src/full/java/io/homeassistant/companion/android/vehicle/HaCarAppService.kt index 879d6b14db5..614f8086088 100644 --- a/app/src/full/java/io/homeassistant/companion/android/vehicle/HaCarAppService.kt +++ b/app/src/full/java/io/homeassistant/companion/android/vehicle/HaCarAppService.kt @@ -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 @@ -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 + ) + } } } } diff --git a/app/src/full/java/io/homeassistant/companion/android/vehicle/LoginScreen.kt b/app/src/full/java/io/homeassistant/companion/android/vehicle/LoginScreen.kt new file mode 100644 index 00000000000..b6cc5c8e274 --- /dev/null +++ b/app/src/full/java/io/homeassistant/companion/android/vehicle/LoginScreen.kt @@ -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() + } + } + } +} diff --git a/app/src/full/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt b/app/src/full/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt index 668b4779eb8..d8bd7c51ef2 100644 --- a/app/src/full/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt +++ b/app/src/full/java/io/homeassistant/companion/android/vehicle/MainVehicleScreen.kt @@ -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 @@ -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 = @@ -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) { diff --git a/app/src/full/java/io/homeassistant/companion/android/vehicle/SwitchToDrivingOptimizedScreen.kt b/app/src/full/java/io/homeassistant/companion/android/vehicle/SwitchToDrivingOptimizedScreen.kt new file mode 100644 index 00000000000..b85a8881b82 --- /dev/null +++ b/app/src/full/java/io/homeassistant/companion/android/vehicle/SwitchToDrivingOptimizedScreen.kt @@ -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" + } +} diff --git a/app/src/main/java/io/homeassistant/companion/android/BaseActivity.kt b/app/src/main/java/io/homeassistant/companion/android/BaseActivity.kt index dd53d8805fd..a991d2bbbe4 100644 --- a/app/src/main/java/io/homeassistant/companion/android/BaseActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/BaseActivity.kt @@ -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, diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index fe77911c86a..93d5b3b5cb7 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -2,6 +2,7 @@ Not logged in Native mode + Use Home Assistant in drive mode? Navigation No entities with locations found. Change server