Home Assistant Community Add-on: ADB - Android Debug Bridge

Yes, the same here. :frowning:

I’m having issues with the Plex app not always opening. Sometimes it works, but sometimes it gets into a state where it refuses to start via the intent until I open it via the menus on the Shield itself.

This is the intent I’m currently using that seems to work more reliably:

intent: dummy: -n com.plexapp.android/com.plexapp.plex.activities.SplashActivity

The reason is that the -n package/activity syntax doesn’t get parsed quite like I thought it did originally. Looking at the logcat output, it appears the -n is getting stuck into the data_uri:

ActivityManager: START u0 {act=android.intent.action.VIEW dat=-n flg=0x10000000 cmp=com.plexapp.android/com.plexapp.plex.activities.SplashActivity}

Plex actually tries to use that and will fail for certain things. For whatever reason, anything containing a : seems to work. However, plex: is special and will cause it to attempt to load content, which will present an error message if it can’t find it. I was able to get it to launch shows and movies directly using:

plex://<server_id>/movie//library/metadata/<movie_id>

You can get the server_id and movie_id by viewing the item in the plex web app and looking at the URL. The path for shows is a tiny bit different, but you can figure it out from the same URL.

Replacing everything in the URL with hyphens also seems to work to launch the app:

plex://-/-//-

Really though, I think this would be more useful if we could just write the entire ADB command or shell command or at least am start command from scratch. Because I think what is really needed here is

am start -a android.intent.action.MAIN -c android.intent.category.LEANBACK_LAUNCHER -n com.plexapp.android/com.plexapp.plex.activities.SplashActivity

That matches the intent that gets used when launching the app from launcher and should work more reliably for starting any app.

At the very least, removing the -d from the am start command should help because trying to pass a data_uri that the app can’t handle seems to be causing issues and even passing a blank data_uri doesn’t fix it.

Edit: And finally, here is my hack around the issue:

intent: ; am start -a android.intent.action.MAIN -c android.intent.category.LEANBACK_LAUNCHER -n com.plexapp.android/com.plexapp.plex.activities.SplashActivity

The semicolon will simply end the first am start command (which will fail because it’s missing data) and then run the second command. That will let you run any arbitrary ADB shell command. I made some helper scripts to do all of this:

script:
  'androidtv_shell':
    alias: Android TV Shell
    sequence:
    - service: media_player.androidtv_intent
      data_template:
        entity_id: "{{ entity_id }}"
        intent: "; {{ command }}"
  'androidtv_app':
    alias: Android TV App
    sequence:
    - service: script.androidtv_shell
      data_template:
        entity_id: "{{ entity_id }}"
        command: "am start -a android.intent.action.MAIN -c android.intent.category.LEANBACK_LAUNCHER -n {{component}}"
  'plex':
    alias: Plex
    sequence:
    - service: script.androidtv_app
      data:
        entity_id: media_player.shield_tv
        component: com.plexapp.android/com.plexapp.plex.activities.SplashActivity
1 Like

You could register a new service, such as media_player.androidtv_adb_cmd. You could use the media_player.firetv_adb_cmd in my Fire TV custom component as a starting point. It checks if the command cmd is in a dictionary called KEYS and, if so, sends the key command adb shell input keyevent <KEYS[cmd]>. If not, it sends the command adb shell <cmd>. The main lines of interest in the code start here and here.

I actually tried that initially but I got an error that the AndroidTV object has no method adb_shell. I’m not sure why because I can see where it gets set and it’s obviously being used by the other services. I didn’t look into it too much though.

Edit: Figured it out. It was renamed in 0.0.6. For 0.0.5, it’s _adb_shell. I went ahead and updated to the custom component based on 0.0.7 and implemented it there. Works great. Thanks!

PR for anyone who wants it and my updated scripts:

script:
  'androidtv_app':
    alias: Android TV App
    sequence:
    - service: media_player.androidtv_command
      data_template:
        entity_id: "{{ entity_id }}"
        command: "am start -a android.intent.action.MAIN -c android.intent.category.LEANBACK_LAUNCHER -n {{component}}"
  'plex':
    alias: Plex
    sequence:
    - service: script.androidtv_app
      data:
        entity_id: media_player.shield_tv
        component: com.plexapp.android/com.plexapp.plex.activities.SplashActivity

I dont use Hass.io but i’m interest to understand how you have been able to launch a specific movie in Plex from the ADB intent.
Could you elaborate ?

Edit:
I figure it out by myself :

{"entity_id": "media_player.shield", "intent": "plex://<server_id>/movie//library/metadata/<movie_id>" }

1 Like

I have now my code on github for my lovelace “multi remote”
Where the “Shield” part uses this wounderful addon.

Anyone else who cannot get KEYCODE_ENTER to work?

Yes, mine does not work either.

Working fine here. Here’s an example that works for me:

{"entity_id" :"media_player.nvidia_shield", "key": "KEYCODE_ENTER"}

Make sure you aren’t using KEYCODE_DPAD_ENTER since the enter key drops the DPAD part.

1 Like

Thanx, it works!!!

And thanx to @teachingbirds for the remote code (from Github)!

It works here with this config

service: media_player.androidtv_key
  data:
    entity_id: media_player.shield_tv
    key: 'KEYCODE_ENTER'

Make sure to use media_player.androidtv_key as service.

2 Likes

I cannot get Plex to start. What intent do you use? i use

-n com.plexapp.android/com.plexapp.plex.activities.SplashActivity

i cannot get it to work.

I have this entities:
tv.emby.embyatv/.startup.StartupActivity
com.nst.iptvsmarterstvbox/.view.activity.SplashActivity
Where can I add it, can’t find where hassio stores all the other entities…

See my previous comment if you just want it to work without messing around too much. In short, the intent you want is (including the leading semicolon)

; am start -a android.intent.action.MAIN -c android.intent.category.LEANBACK_LAUNCHER -n com.plexapp.android/com.plexapp.plex.activities.SplashActivity

The better way is in my followup comment but requires a modified androidtv.py (here).

Really, you should use one of those methods for every app if you’re launching via an activity. The “-n” flag doesn’t really get handled the way I thought it did and the fact that it works for most apps is just luck.

1 Like
127|once:/ $ pm list packages | sed -e "s/package://" | while read x; do cmd package resolve-activity --brief $x | tail -n 1 | grep -v "No activity found"; done
y --brief $x | tail -n 1 | grep -v "No activity found"; done                  <
com.mxtech.videoplayer.pro/.ActivityWelcomeMX
com.plexapp.android/com.plexapp.plex.activities.SplashActivity
com.crunchyroll.crunchyroid/.ui.views.activities.SplashActivity
de.cyberdream.androidtv.notifications.google/de.cyberdream.androidtv.notifications.StartActivity
com.silicondust.view/.App
com.android.vending/com.google.android.finsky.tvmainactivity.TvMainActivity
com.google.android.tv/com.android.tv.TvActivity
com.rcreations.WebCamViewerPaid/.IpCamViewerActivity
com.android.gallery3d/.app.GalleryActivity
tv.twitch.android.app/tv.twitch.android.apps.TVLandingActivity
org.xbmc.kodi/.Splash
com.facebook.katana/.Fb4FireTVLaunchActivity
com.google.android.youtube.tv/com.google.android.apps.youtube.tv.activity.ShellActivity
com.accuweather.android/com.accuweather.accutv.core.MainActivity
com.google.android.tv.remote.service/.settings.SettingsActivity
com.netflix.ninja/.MainActivity
com.nousguide.android.rbtv/com.redbull.launch.SplashActivity
com.yodo1.crossyroad/com.unity3d.player.UnityPlayerActivity

So the HD HomeRun package is com.silicondust.view/.App But im not sure how to find out what to send to it, to go to a specific channel?

Or can I use the inbuilt android TV Channels thing and send intents to that? Im not sure how to find the intents either for that…

I’ve been looking into doing the Live Channels and honestly can’t figure it out. The best I’ve gotten is opening the app with

content://android.media.tv/channel

I can get it to go to channel 1 with

content://android.media.tv/channel/1

but no other channels seem to work.

You can also open (and close) the program guide with

content://android.media.tv/program

That’s as far as I was able to get. I think the other way would be to just have a script that opens the app, types in the channel number, and hits enter.

'live_channel_nasa':
  alias: "NASA Live Channel"
  sequence:
  - service: script.live_channels
  - delay:
      milliseconds: 250
  - service: media_player.androidtv_key
    data:
      entity_id: media_player.shield_tv
      key: 13
  - service: media_player.androidtv_key
    data:
      entity_id: media_player.shield_tv
      key: 11
  - service: media_player.androidtv_key
    data:
      entity_id: media_player.shield_tv
      key: 10
  - service: media_player.androidtv_key
    data:
      entity_id: media_player.shield_tv
      key: 66

The script.live_channels just opens up the live channels app. I use a modified custom component to send the ADB command to start com.google.android.tv/com.android.tv.MainActivity, but any way that gets the app opened should work.

Amazing job. Thank you for sharing this. I use most of your config for the shield and it works very well.

Anyone else having issues after 0.88 with loading the custom component?