diff --git a/app/src/main/java/io/homeassistant/companion/android/HomeAssistantApplication.kt b/app/src/main/java/io/homeassistant/companion/android/HomeAssistantApplication.kt
index ab9cae96c..1ba723e37 100644
--- a/app/src/main/java/io/homeassistant/companion/android/HomeAssistantApplication.kt
+++ b/app/src/main/java/io/homeassistant/companion/android/HomeAssistantApplication.kt
@@ -18,6 +18,7 @@ import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.settings.SensorUpdateFrequencySetting
import io.homeassistant.companion.android.sensors.SensorReceiver
import io.homeassistant.companion.android.settings.language.LanguagesManager
+import io.homeassistant.companion.android.util.LifecycleHandler
import io.homeassistant.companion.android.websocket.WebsocketBroadcastReceiver
import io.homeassistant.companion.android.widgets.button.ButtonWidget
import io.homeassistant.companion.android.widgets.entity.EntityWidget
@@ -46,6 +47,8 @@ open class HomeAssistantApplication : Application() {
override fun onCreate() {
super.onCreate()
+ registerActivityLifecycleCallbacks(LifecycleHandler)
+
ioScope.launch {
initCrashReporting(
applicationContext,
diff --git a/app/src/main/java/io/homeassistant/companion/android/settings/SettingsPresenterImpl.kt b/app/src/main/java/io/homeassistant/companion/android/settings/SettingsPresenterImpl.kt
index 95c81f2f7..7c392c70d 100644
--- a/app/src/main/java/io/homeassistant/companion/android/settings/SettingsPresenterImpl.kt
+++ b/app/src/main/java/io/homeassistant/companion/android/settings/SettingsPresenterImpl.kt
@@ -52,6 +52,7 @@ class SettingsPresenterImpl @Inject constructor(
"app_lock_home_bypass" -> authenticationUseCase.isLockHomeBypassEnabled()
"crash_reporting" -> prefsRepository.isCrashReporting()
"autoplay_video" -> integrationUseCase.isAutoPlayVideoEnabled()
+ "always_show_first_view_on_app_start" -> integrationUseCase.isAlwaysShowFirstViewOnAppStartEnabled()
"webview_debug" -> integrationUseCase.isWebViewDebugEnabled()
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
@@ -68,6 +69,7 @@ class SettingsPresenterImpl @Inject constructor(
"app_lock_home_bypass" -> authenticationUseCase.setLockHomeBypassEnabled(value)
"crash_reporting" -> prefsRepository.setCrashReporting(value)
"autoplay_video" -> integrationUseCase.setAutoPlayVideo(value)
+ "always_show_first_view_on_app_start" -> integrationUseCase.setAlwaysShowFirstViewOnAppStart(value)
"webview_debug" -> integrationUseCase.setWebViewDebugEnabled(value)
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
diff --git a/app/src/main/java/io/homeassistant/companion/android/util/LifecycleHandler.kt b/app/src/main/java/io/homeassistant/companion/android/util/LifecycleHandler.kt
new file mode 100644
index 000000000..b3f0936c6
--- /dev/null
+++ b/app/src/main/java/io/homeassistant/companion/android/util/LifecycleHandler.kt
@@ -0,0 +1,44 @@
+package io.homeassistant.companion.android.util
+
+import android.app.Activity
+import android.app.Application
+import android.os.Bundle
+
+object LifecycleHandler : Application.ActivityLifecycleCallbacks {
+ private var activityReferences = 0
+ private var isActivityChangingConfigurations = false
+ override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
+ // Not implemented
+ }
+
+ override fun onActivityStarted(activity: Activity) {
+ isActivityChangingConfigurations = activity.isChangingConfigurations
+ activityReferences++
+ }
+
+ override fun onActivityResumed(activity: Activity) {
+ // Not implemented
+ }
+
+ override fun onActivityPaused(activity: Activity) {
+ // Not implemented
+ }
+
+ override fun onActivityStopped(activity: Activity) {
+ isActivityChangingConfigurations = activity.isChangingConfigurations
+ activityReferences--
+ }
+
+ override fun onActivitySaveInstanceState(activity: Activity, bunle: Bundle) {
+ // Not implemented
+ }
+
+ override fun onActivityDestroyed(activity: Activity) {
+ // Not implemented
+ }
+
+ fun isAppInBackground(): Boolean {
+ // No activities left and activity is not changing configuration (ex. change the orientation)
+ return activityReferences == 0 && !isActivityChangingConfigurations
+ }
+}
diff --git a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt
index f1d6682f3..1aaf0ebea 100644
--- a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt
+++ b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewActivity.kt
@@ -94,6 +94,7 @@ import io.homeassistant.companion.android.settings.SettingsActivity
import io.homeassistant.companion.android.themes.ThemesManager
import io.homeassistant.companion.android.util.ChangeLog
import io.homeassistant.companion.android.util.DataUriDownloadManager
+import io.homeassistant.companion.android.util.LifecycleHandler
import io.homeassistant.companion.android.util.OnSwipeListener
import io.homeassistant.companion.android.util.TLSWebViewClient
import io.homeassistant.companion.android.util.isStarted
@@ -746,6 +747,11 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
changeLog.showChangeLog(this, false)
}
+ override fun onStop() {
+ super.onStop()
+ openFirstViewOnDashboardIfNeeded()
+ }
+
override fun onPause() {
super.onPause()
SensorReceiver.updateAllSensors(this)
@@ -1433,4 +1439,30 @@ class WebViewActivity : BaseActivity(), io.homeassistant.companion.android.webvi
"""
) {}
}
+
+ private fun openFirstViewOnDashboardIfNeeded() {
+ if (presenter.isAlwaysShowFirstViewOnAppStartEnabled() &&
+ LifecycleHandler.isAppInBackground()
+ ) {
+ // Pattern matches urls which are NOT allowed to show the first view after app is started
+ // This is
+ // /config/* as these are the settings of HA but NOT /config/dashboard. This is just the overview of the HA settings
+ // /hassio/* as these are the addons section of HA settings.
+ if (webView.url?.matches(".*://.*/(config/(?!\\bdashboard\\b)|hassio)/*.*".toRegex()) == false) {
+ Log.d(TAG, "Show first view of default dashboard.")
+ webView.evaluateJavascript(
+ """
+ var anchor = 'a:nth-child(1)';
+ var defaultPanel = window.localStorage.getItem('defaultPanel')?.replaceAll('"',"");
+ if(defaultPanel) anchor = 'a[href="/' + defaultPanel + '"]';
+ document.querySelector('body > home-assistant').shadowRoot.querySelector('home-assistant-main')
+ .shadowRoot.querySelector('#drawer > ha-sidebar')
+ .shadowRoot.querySelector('paper-listbox > ' + anchor).click();
+ window.scrollTo(0, 0);
+ """,
+ null
+ )
+ } else Log.d(TAG, "User is in the Home Assistant config. Will not show first view of the default dashboard.")
+ }
+ }
}
diff --git a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenter.kt b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenter.kt
index 2acd8f98d..9e04f708d 100644
--- a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenter.kt
+++ b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenter.kt
@@ -29,6 +29,7 @@ interface WebViewPresenter {
fun isLockEnabled(): Boolean
fun isAutoPlayVideoEnabled(): Boolean
+ fun isAlwaysShowFirstViewOnAppStartEnabled(): Boolean
fun sessionTimeOut(): Int
diff --git a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt
index 7e9b4056c..5b60e97ef 100644
--- a/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt
+++ b/app/src/main/java/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt
@@ -192,6 +192,12 @@ class WebViewPresenterImpl @Inject constructor(
}
}
+ override fun isAlwaysShowFirstViewOnAppStartEnabled(): Boolean {
+ return runBlocking {
+ integrationUseCase.isAlwaysShowFirstViewOnAppStartEnabled()
+ }
+ }
+
override fun sessionTimeOut(): Int {
return runBlocking {
integrationUseCase.getSessionTimeOut()
diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml
index 1057099f8..97b298dc2 100644
--- a/app/src/main/res/xml/preferences.xml
+++ b/app/src/main/res/xml/preferences.xml
@@ -91,6 +91,11 @@
android:icon="@drawable/ic_baseline_video_settings_24"
android:title="@string/autoplay_video"
android:summary="@string/autoplay_video_summary" />
+
Add widget
All entities
Allow
+ Always show first view on app start
+ The first view of the default dashboard is shown as soon as the app is opened
Home Assistant
App Version Info
Application Version