android: command_webview notification does not change current view on the companion app

Home Assistant Android app version(s): 2023.1.1-full

Android version(s): 10

Device model(s): Teclast P20HD

Home Assistant version: Home Assistant 2023.2.2 Supervisor 2023.01.1 Operating System 9.5 Frontend 20230202.0

Last working Home Assistant release (if known): N/A

Description of problem, include YAML if issue is related to notifications: According to documentation, the script below should navigate to the specified by path view on a remote Android companion application. In fact, this works only if the companion app is not yet running. So running the script below, it launches and navigates to the requested view. Next, after the current view is changed manually, or another path is specified in the script, and script started again, navigation does not happen, and view is not changed.

- service: notify.mobile_app_android_tablet
    data:
      message: command_webview
      data:
        command: /dashboard-main/resources

Steps to reproduce:

  • close the companion app
  • start the script above (using existing view path)
  • see the app is open and the view is set as requested
  • switch to a different view by hand on the Android app OR change the view path in the script
  • start the script again

Expected result: view changes accordingly. Actual result: view is not changed anymore.

Companion App Logs: In logs below: 19:46:xx - no app running, script launched, requested view opened 19:47:xx - view changed manually on the android app 19:48:xx - scipt launched again, no view changed (expected: change back to requested one)

02-17 19:46:25.993 23130 23584 W FirebaseMessaging: Unable to log event: analytics library is missing
02-17 19:46:25.997 23130 23584 D FCMService: From: 331041709873
02-17 19:46:25.999 23130 23584 D MessagingService: Processing device command
02-17 19:46:26.281 23130 23584 W FirebaseMessaging: Unable to log event: analytics library is missing
02-17 19:46:26.283 23130 23584 D FCMService: From: 331041709873
02-17 19:46:26.285 23130 23584 D MessagingService: Processing device command
02-17 19:46:26.514 23130 23584 W FirebaseMessaging: Unable to log event: analytics library is missing
02-17 19:46:26.515 23130 23584 D FCMService: From: 331041709873
02-17 19:46:26.517 23130 23584 D MessagingService: Processing device command
02-17 19:46:26.551 23130 23130 W ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@5df5a59
02-17 19:46:26.675 23130 23130 D IntegrationRepository: isAppLocked(): false. (LockEnabled: false, appActive: false, expireMillis: 1676652332305, currentMillis: 1676652386675)
02-17 19:46:26.679 23130 23130 W cr_SupportWebSettings: setForceDarkBehavior() is a no-op in an app with targetSdkVersion>=T
02-17 19:46:26.679 23130 23130 W cr_SupportWebSettings: setForceDark() is a no-op in an app with targetSdkVersion>=T
02-17 19:46:26.680 23130 23130 D WebviewActivity: Current webview package com.google.android.webview and version 109.0.5414.117
02-17 19:46:26.691 23130 23130 D WebviewActivity: Matter commissioning status changed to NOT_STARTED
02-17 19:46:26.693 23130 23130 D IntegrationRepository: isAppLocked(): false. (LockEnabled: false, appActive: false, expireMillis: 1676652332305, currentMillis: 1676652386693)
02-17 19:46:27.360 23130 23130 I Choreographer: Skipped 38 frames!  The application may be doing too much work on its main thread.
02-17 19:46:27.412 23130 23195 D         : Gralloc Register  w:1200, h:1920, f:0x1101, usage:0xb00, ui64Stamp:114125, sSize:9236480, line = 2270
02-17 19:46:27.447 23130 23248 W chromium: [WARNING:display_webview.cc(31)] WebView overlays are enabled!
02-17 19:46:27.451 23130 23195 I OpenGLRenderer: Davey! duration=728ms; Flags=1, IntendedVsync=78204966805102, Vsync=78205610872878, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=78205613330485, AnimationStart=78205613358139, PerformTraversalsStart=78205613639139, DrawStart=78205654267600, SyncQueued=78205656851331, SyncStart=78205665487754, IssueDrawCommandsStart=78205665678408, SwapBuffers=78205701307754, FrameCompleted=78205703624600, DequeueBufferDuration=0, QueueBufferDuration=1702000, 
02-17 19:46:27.489 23130 23195 D         : Gralloc Register  w:1200, h:1920, f:0x1101, usage:0xb00, ui64Stamp:114136, sSize:9236480, line = 2270
02-17 19:46:27.489 23130 23130 D IntegrationRepository: isAppLocked(): false. (LockEnabled: false, appActive: false, expireMillis: 1676652332305, currentMillis: 1676652387489)
02-17 19:46:27.514 23130 23163 D UrlRepository: localUrl is: false, usesInternalSsid is: false, usesWifi is: true
02-17 19:46:27.518 23130 23172 I WM-Processor: Moving WorkSpec (882299a0-344b-4c9a-a843-7b21259ae659) to the foreground
02-17 19:46:27.522 23130 23165 D UrlRepository: localUrl is: false, usesInternalSsid is: false, usesWifi is: true
02-17 19:46:27.525 23130 23130 D UrlRepository: localUrl is: false, usesInternalSsid is: false, usesWifi is: true
02-17 19:46:27.525 23130 23130 D UrlRepository: Using external URL
02-17 19:46:27.528 23130 23195 D         : Gralloc Register  w:1200, h:1920, f:0x1101, usage:0xb00, ui64Stamp:114138, sSize:9236480, line = 2270
02-17 19:46:27.536 23130 23165 D SensorWorker: Updating all Sensors in foreground.
02-17 19:46:27.544 23130 23165 D UrlRepository: localUrl is: false, usesInternalSsid is: false, usesWifi is: true
02-17 19:46:27.551 23130 23172 I WM-WorkerWrapper: Worker result SUCCESS for Work [ id=e8cada00-541c-4565-b046-86eb6e4905f1, tags={ io.homeassistant.companion.android.websocket.WebsocketManager } ]
02-17 19:46:27.554 23130 23165 D UrlRepository: localUrl is: false, usesInternalSsid is: false, usesWifi is: true
02-17 19:46:27.560 23130 23130 I WM-SystemFgDispatcher: Started foreground service Intent { act=ACTION_START_FOREGROUND cmp=io.homeassistant.companion.android/androidx.work.impl.foreground.SystemForegroundService (has extras) }
02-17 19:46:27.881 23130 23142 I mpanion.androi: Background concurrent copying GC freed 38208(2093KB) AllocSpace objects, 6(184KB) LOS objects, 49% free, 6347KB/12MB, paused 201us total 186.557ms
02-17 19:46:28.712 23130 23315 D WebviewActivity: External bus {"type":"config/get","id":1}
02-17 19:46:28.713 23130 23130 D WebviewActivity: externalBus({"id":1,"type":"result","success":true,"result":{"hasSettingsScreen":true,"canWriteTag":false,"hasExoPlayer":true,"canCommissionMatter":true}});
02-17 19:46:28.785 23130 23130 D UrlRepository: localUrl is: false, usesInternalSsid is: false, usesWifi is: true
02-17 19:46:28.785 23130 23130 D UrlRepository: Using external URL
02-17 19:46:28.788 23130 23130 D WebviewActivity: Callback null
02-17 19:46:28.789 23130 23130 D WebviewActivity: Color from webview is "#101e24-SPACER-#111111"
02-17 19:46:28.789 23130 23130 D WebviewActivity: Color from webview is "#101e24-SPACER-#111111"
02-17 19:46:28.790 23130 23163 D WebViewPresenterImpl: Try getting color from webview color "#101e24".
02-17 19:46:28.790 23130 23163 I WebViewPresenterImpl: Found color -15720924.
02-17 19:46:28.792 23130 23164 D WebViewPresenterImpl: Try getting color from webview color "#111111".
02-17 19:46:28.793 23130 23164 I WebViewPresenterImpl: Found color -15658735.
02-17 19:46:28.920 23130 23165 D UrlRepository: localUrl is: false, usesInternalSsid is: false, usesWifi is: true
02-17 19:46:28.956 23130 23169 I WM-WorkerWrapper: Worker result SUCCESS for Work [ id=882299a0-344b-4c9a-a843-7b21259ae659, tags={ io.homeassistant.companion.android.sensors.SensorWorker } ]
02-17 19:46:28.974 23130 23130 I WM-SystemFgDispatcher: Stopping foreground service
02-17 19:46:30.119 23130 23315 D WebviewActivity: External bus {"type":"connection-status","payload":{"event":"connected"},"id":2}
02-17 19:46:30.121 23130 23130 D UrlRepository: localUrl is: false, usesInternalSsid is: false, usesWifi is: true
02-17 19:46:30.279 23130 23315 D WebviewActivity: External bus {"type":"theme-update","id":3}
02-17 19:46:30.302 23130 23130 D WebviewActivity: Color from webview is "#101e24-SPACER-#111111"
02-17 19:46:30.302 23130 23130 D WebviewActivity: Color from webview is "#101e24-SPACER-#111111"
02-17 19:46:30.302 23130 23164 D WebViewPresenterImpl: Try getting color from webview color "#101e24".
02-17 19:46:30.303 23130 23164 I WebViewPresenterImpl: Found color -15720924.
02-17 19:46:30.304 23130 23164 D WebViewPresenterImpl: Try getting color from webview color "#111111".
02-17 19:46:30.304 23130 23164 I WebViewPresenterImpl: Found color -15658735.
02-17 19:47:09.840 23130 23716 E IMGSRV  : :0: BufPoolFreeBuffer: fragment buffer for render surface still in use
02-17 19:47:09.840 23130 23716 I chatty  : uid=10147(io.homeassistant.companion.android) identical 29 lines
02-17 19:47:09.840 23130 23716 E IMGSRV  : :0: BufPoolFreeBuffer: fragment buffer for render surface still in use
02-17 19:47:09.853 23130 23716 E IMGSRV  : :0: BufPoolFreeBuffer: fragment buffer for render surface still in use
02-17 19:47:09.853 23130 23716 E IMGSRV  : :0: BufPoolFreeBuffer: fragment buffer for render surface still in use
02-17 19:47:15.778 23130 23716 E IMGSRV  : :0: BufPoolFreeBuffer: fragment buffer for render surface still in use
02-17 19:47:15.862 23130 23716 E IMGSRV  : :0: BufPoolFreeBuffer: fragment buffer for render surface still in use
02-17 19:48:10.030 23130 23741 W FirebaseMessaging: Unable to log event: analytics library is missing
02-17 19:48:10.033 23130 23741 D FCMService: From: 331041709873
02-17 19:48:10.035 23130 23741 D MessagingService: Processing device command
02-17 19:48:10.230 23130 23741 W FirebaseMessaging: Unable to log event: analytics library is missing
02-17 19:48:10.233 23130 23741 D FCMService: From: 331041709873
02-17 19:48:10.235 23130 23741 D MessagingService: Processing device command
02-17 19:48:10.465 23130 23741 W FirebaseMessaging: Unable to log event: analytics library is missing
02-17 19:48:10.466 23130 23741 D FCMService: From: 331041709873
02-17 19:48:10.468 23130 23741 D MessagingService: Processing device command

Screenshot or video of problem:

Additional information:

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 1
  • Comments: 26 (14 by maintainers)

Most upvoted comments

I had a chance to take a look at the code. In my opinion, intent should be fired adding flags:

            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)

below the existing intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

This prevent the webviewactivity to be reopened if already started, but still create a new task if activity is not running. Doing so, methods onCreate and onStart of WebViewActivity.kt are not fired, but onResume is.

In the onResume method, desired path should be retrieved using val path = intent.getStringExtra(EXTRA_PATH) and javascript should be injected (webView.evaluateJavascript("<JS CODE>") in order to check if desired path is the current one, otherwise change the view with something like

anchor = 'a[href="/' + path+ '"]';
document.querySelector('body > home-assistant').shadowRoot.querySelector('home-assistant-main')
                                                                   .shadowRoot.querySelector('ha-sidebar')
                                                                   .shadowRoot.querySelector('paper-listbox > ' + anchor).click();
window.scrollTo(0, 0);

Some work must be done in the last part ( 1. as I said, check if the desired path is not the current one 2. target specific tabs in a dashboard 3. handle the entityId:domain.entity special path to open entity popup).

That said…

I simply gave up and used Browser Mod (https://github.com/thomasloven/hass-browser_mod) to navigate to view just after sending a notification with the command_webview command to open the app.

image

There are enough people here who seem to be affected by it, so even if it doesn’t work as it should, the issue could be opened if someone is looking at alternatives to make the command work on more devices as mentioned above. Reopening.

I’m an android app developer myself, I’ll look into it. Worse case, the app should check if the webview is already active and in this case just inject javascript to switch tab or dashboard. As a bonus, this would avoid the webview reload. That said, development would be easier having an open issue to discuss solutions and progress. The problem is there, solutions (or workaround) are surely possible, I really don’t see why close the issue, but I’m not the maintainer so it’s up to you.

If my contribution with my tests can be useful: With a stock Motorola Android 12 = same problem, lovelace doesn’t change. With a Huawey Android 9 tablet = it works perfectly

obviously Home Assistant 2023.3.0

Samsung A40 with Android 11 lovelace doesn’t change, always start at main view.

service: notify.mobile_app_sm_a405fn
data:
  message: command_webview
  data:
    command: /lovelace/1