JeffLIrion / python-androidtv

Communicate with an Android TV or Fire TV device via ADB over a network.
MIT License
165 stars 57 forks source link

Why does the firetv platform allow you to get a list of apps and launch an app but the androidtv platform doesn't? #125

Closed raman325 closed 4 years ago

raman325 commented 4 years ago

Hi,

Curious as to why the FireTV class supports getting a list of apps as well as launching an app, but the AndroidTV class does not? Per some googling, ps -A | grep u0_a will get you a list of running apps on any Android device (similar to what you are using for the FireTV platform), and pm list packages -f | sed -e 's/.*=//' | sed 's/\r//g' | sort (found here: https://gist.github.com/davidnunez/1404789#gistcomment-1220924) will get you a list of all apps, whether running or not. Both of these lists could be filtered using your static app list to determine a list of available media apps to launch. I also tested the monkey adb shell command monkey -c android.intent.category.LAUNCHER -p com.netflix.ninja 1 on my Shield and that successfully launched Netflix.

Also, can you explain why you are looking at running apps instead of all installed apps? Just trying to understand the motivation behind it.

Thanks in advance!

JeffLIrion commented 4 years ago

Android TV devices in HA don't show the current and running apps in the sources list because the source attribute is the current audio device (I think). Unlike Fire TV devices, Android TV devices allow you to change the volume, so that partly motivates the rationale for sticking with the audio device as the source. But really, I think the answer is that that's how it's always been and I think you're the first person to even mention it.

As for running apps vs. all apps, I don't think that listing all the apps in the source menu is all that informative. I think it's more useful to see what's currently running. Furthermore, the select_source service doesn't work for everyone -- there's an issue in this repo about that, but I have no universal solution. Fortunately, it's easy to launch and stop apps via the androidtv.adb_command service. I think that's another reason why not much attention has been paid to the sources list and the select_source service.

raman325 commented 4 years ago

I'm seeing the source listed as HDMI but I am using HDMI ARC to connect to my soundbar so you could be right.

Are you open to functional PRs? I had a couple of ideas:

JeffLIrion commented 4 years ago

Are you open to functional PRs? I had a couple of ideas:

Yeah, that would be great!

  • Explore adding source lists and select source support to the androidtv platform (I have a FireTV as well so I will experiment with the select_source service and see if I can help). From reading through the various links you've posted here and some Googling, I see that it can be finnicky so this may be a fool's errand but a fresh set of eyes never hurts.

That should be pretty easy to do: tac on the running apps command to the update commands in constants.py, process the results in AndroidTV.update with the _get_properties() method, and add/update tests.

Another possible feature is to add more ADB functionality. I'm working on implementing the FileSync protocol, which would allow users to transfer files between their device and their HA instance. Here's the work-in-progress feature: https://github.com/JeffLIrion/adb_shell/blob/8659177b6be252ab92a76e4422e572e605bc3080/adb_shell/adb_device.py#L377

JeffLIrion commented 4 years ago

In https://github.com/JeffLIrion/python-androidtv/pull/126, I made it so that the AndroidTV.update() method will get the list of running apps. If you try it out and confirm that it works, I'll merge it in.

raman325 commented 4 years ago

Thanks for the quick turnaround on this, I had some friends in town this weekend and was unable to spend any time myself beyond just reading through the code and trying to understand it. I will test out your PR and get back to you

JeffLIrion commented 4 years ago

You may find it helpful to use this custom component for testing: https://github.com/JeffLIrion/ha-androidtv

Modify the update.sh script so that it pulls from the androidtv-running-apps branch of this repo, modify the HA component, and you should be good to go.

raman325 commented 4 years ago

yes that is super helpful, thought I was going to have to build your PR as a custom component myself 😄 I should be able to get back to you by tomorrow

JeffLIrion commented 4 years ago

Assuming this works (which it should), we may want to move the AndroidTV.start_intent, FireTV._send_intent, FireTV.launch_app, and FireTV.stop_app methods to the BaseTV class.

And the _send_intent method probably doesn't need to be hidden, although it could be confusing having both send_intent and start_intent methods.

raman325 commented 4 years ago

It looks like you can only see running apps if you include -A in the shell command to return the running processes list, as I can only see the active application in the source list. Assuming you are comfortable with adding that parameter for both device types, this should be a relatively simple change and then we could proceed with the other changes (moving device specific functions into the base class to be shared)

JeffLIrion commented 4 years ago

You mean change the running apps command to ps -A | grep u0_a? I tried that on one of my Fire TV sticks and it didn't work.

raman325 commented 4 years ago

Yup that's what I was suggesting, and I confirmed that it doesn't work on my FireTV either. Looks like the later versions of Android use an updated version of ps that supports that flag. What if we echo'd the results from both commands and then grepped the full list with some sort of dedupe in case both versions are supported?

I also discovered that switching to ps -A in CMD_RUNNING_APPS didn't fix the issue. I noticed you didn't have a break between CMD_STREAM_MUSIC and CMD_RUNNING_APPS like you do between other commands, but when I tried adding " && " in between I got the following:

ERROR (SyncWorker_5) [custom_components.androidtv.media_player] Failed to execute an ADB command. ADB connection re-establishing attempt in the next update. Error: can only concatenate str (not "list") to str
JeffLIrion commented 4 years ago

I pushed a change that will use the -A flag for Android TV devices but not Fire TV devices and I added the missing " && ". I don't know where that error you got came from...

JeffLIrion commented 4 years ago

Do you have your custom component code online?

raman325 commented 4 years ago

No all changes are local right now but I will push a fork out after this weekend, about to take off for a flight through Sunday. It looks the same though so I wonder what I am doing differently. For what it's worth, I am testing this on an Nvidia Shield and a FireTV (gen2 I think)

JeffLIrion commented 4 years ago

Here's a quick attempt at it: https://github.com/JeffLIrion/ha-androidtv/tree/androidtv-running-apps

I didn't implement a method for selecting the source for Android TV devices, but it should display the running apps in the sources menu.

JeffLIrion commented 4 years ago

Any updates?

raman325 commented 4 years ago

Sorry for the delay, just checked it and it is now showing a full list of running apps, including background services. Is that expected? If so, then the only thing to do is move the methods you mentioned here: https://github.com/JeffLIrion/python-androidtv/issues/125#issuecomment-558680369 to the BaseTV class so that launching and closing apps will work as expected.

JeffLIrion commented 4 years ago

Background services like the Bluetooth manager, for example? If so, that's expected.

raman325 commented 4 years ago

OK great, so then all it's going to take is to move the methods over as you had suggested. I can take a crack at this hopefully tonight if not tomorrow.

JeffLIrion commented 4 years ago

I did that here: https://github.com/JeffLIrion/python-androidtv/tree/androidtv-running-apps

Can you update the custom component and test it out.

raman325 commented 4 years ago

That did the trick!

I deleted the text that was in here previously because I didn't realize you updated the component code too. Added a comment to your PR.

JeffLIrion commented 4 years ago

Good to hear! I merged the pull request and I'll create a new release.

Would you mind updating the HA component? I think the custom component's media_player.py is correct, aside from the imports needing to be adjusted.

raman325 commented 4 years ago

you mean within the home-assistant/home-assistant repo itself? Happy to!

JeffLIrion commented 4 years ago

Thanks!