-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve support for Sony TV devices #233
Changes from all commits
5c81ffc
0abd708
dc515b4
66840c2
7b67bd3
38dab6f
5d1a394
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
INTENT_LAUNCH = "android.intent.category.LAUNCHER" | ||
INTENT_HOME = "android.intent.category.HOME" | ||
|
||
# Specify application identifier as CURRENT_APP from dumpsys output previously assigned to CURRENT_APP | ||
VAR_CURRENT_APP_EXTRACTION = 'CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*}' | ||
|
||
# echo '1' if the previous shell command was successful | ||
CMD_SUCCESS1 = r" && echo -e '1\c'" | ||
|
@@ -28,20 +30,29 @@ | |
#: Determine whether the device is awake | ||
CMD_AWAKE = "dumpsys power | grep mWakefulness | grep -q Awake" | ||
|
||
#: Define current app | ||
CMD_DEFINE_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + VAR_CURRENT_APP_EXTRACTION | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constant seems unnecessary. Same goes for the other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In which way do you see this as potentially unnecessary? Besides the main objective to deduplicate the code, as the majority of the complexity is already defined in The other use case I had in mind, using this module from a Python script and being able to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My comments from earlier were based on looking at this pull request on my phone, and the "Find in page" functionality wasn't working right. So I do see that these constants are used and that they make the code a bit more DRY. My main concerns are that
# I need to check what `VAR_CURRENT_APP_EXTRACTION` does
CMD_DEFINE_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + VAR_CURRENT_APP_EXTRACTION
# Here I know exactly what the ADB shell command is doing
CMD_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP"
The way I see it, anyone doing that is a power user. I think such a person would be more likely to look through these constants and piece together what they need in order to achieve their desired outcome OR simply formulate their ADB shell command from scratch rather than to pick out constants from this module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To assist with the review of the code, below is the diff of fully written commands by changed constant: CMD_CURRENT_APP -CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP
+CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*} && echo $CURRENT_APP CMD_CURRENT_APP_GOOGLE_TV -CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP
+CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*} && echo $CURRENT_APP CMD_HDMI_INPUT -dumpsys activity starter | grep -o 'HDMIInputService\\/HW[0-9]' -m 1 | grep -o 'HW[0-9]'
+dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' | grep -o 'HW[0-9]' CMD_LAUNCH_APP -CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi
+CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && CURRENT_APP=${{CURRENT_APP#*ActivityRecord{{* * }} CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP%\\}}*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + ' --pct-syskeys 0 1; fi CMD_LAUNCH_APP_GOOGLE_TV -CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi
+CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && CURRENT_APP=${{CURRENT_APP#*ActivityRecord{{* * }} CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP%\\}}*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + ' --pct-syskeys 0 1; fi Note: If you plan to compare these commands via android.adb_command service in Home Assistant, be sure to remove any escaped characters (e.g. In regard to having more constants/variables, by simply looking at the above diffs you can see there is a lot of duplicate complexity, this should simplify any future changes to |
||
|
||
#: Get the current app | ||
CMD_CURRENT_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP" | ||
CMD_CURRENT_APP = CMD_DEFINE_CURRENT_APP + ' && echo $CURRENT_APP' | ||
|
||
#: Define current app for a Google TV device | ||
CMD_DEFINE_CURRENT_APP_GOOGLE_TV = 'CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && ' + VAR_CURRENT_APP_EXTRACTION | ||
|
||
#: Get the current app for a Google TV device | ||
CMD_CURRENT_APP_GOOGLE_TV = "CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${CURRENT_APP%%/*} && echo $CURRENT_APP" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you changed the command here (old first, new second):
Why the change? I don't own a Google TV device, so I can't test whether this works. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this particular change I looked at this Home Assistant Community topic (reference from #207). Indicating that both Then I checked the alternative command (
Rather than using the existing solution which relies on space (indentation) to extract the application activity details, I decided to change this approach to be more generic by looking for the ActivityRecord instead. |
||
CMD_CURRENT_APP_GOOGLE_TV = CMD_DEFINE_CURRENT_APP_GOOGLE_TV + ' && echo $CURRENT_APP' | ||
|
||
#: Get the HDMI input | ||
CMD_HDMI_INPUT = "dumpsys activity starter | grep -o 'HDMIInputService\\/HW[0-9]' -m 1 | grep -o 'HW[0-9]'" | ||
CMD_HDMI_INPUT = "dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' | grep -o 'HW[0-9]'" | ||
|
||
#: Launch an app if it is not already the current app | ||
CMD_LAUNCH_APP = "CURRENT_APP=$(dumpsys window windows | grep mCurrentFocus) && CURRENT_APP=${{CURRENT_APP#*{{* * }} && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi" | ||
CMD_LAUNCH_APP_CONDITION = "if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + ' --pct-syskeys 0 1; fi' | ||
|
||
#: Launch an app | ||
CMD_LAUNCH_APP = CMD_DEFINE_CURRENT_APP.replace('{', '{{').replace('}', '}}') + ' && ' + CMD_LAUNCH_APP_CONDITION | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know it's not DRY to copy & paste, but given that this module is simply a bunch of constants, I think it's acceptable here. Also, I think it's much more readable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I certainly agree in regard to readability, also slightly consider this to be a despicable solution, but it does escape all curly braces in the referenced constant to accompany the formatting done with this constant definition. Although the complexity of extracting the This is the main reason I took the DRY approach, by introducing the |
||
|
||
#: Launch an app if it is not already the current app (for Google TV devices) | ||
CMD_LAUNCH_APP_GOOGLE_TV = "CURRENT_APP=$(dumpsys activity a . | grep -E 'mResumedActivity' | cut -d ' ' -f 8) && CURRENT_APP=${{CURRENT_APP%%/*}} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c " + INTENT_LAUNCH + " --pct-syskeys 0 1; fi" | ||
#: Launch an app on a Google TV device | ||
CMD_LAUNCH_APP_GOOGLE_TV = CMD_DEFINE_CURRENT_APP_GOOGLE_TV.replace('{', '{{').replace('}', '}}') + ' && ' + CMD_LAUNCH_APP_CONDITION | ||
|
||
#: Get the state from ``dumpsys media_session``; this assumes that the variable ``CURRENT_APP`` has been defined | ||
CMD_MEDIA_SESSION_STATE = "dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I guess it is used. Chrome on my phone only found it once on this page. So maybe it's OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this is the core "logic" in regard to extracting the application identifier from the dumpsys output, either it being from
dumpsys window windows
ordumpsys activity a .
.It does the following operations:
${CURRENT_APP#*ActivityRecord{* * }
Attempt to extract application activity from
mResumedActivity
ormFocusedApp
${CURRENT_APP#*{* * }
Fallback to extract application activity from
mCurrentFocus
, in casemFocusedApp
is not defined${CURRENT_APP%%/*}
Remove extraneous activity details for extracted application, leaving only identifier
${CURRENT_APP%\\}*}
Delete trailing curly brace from identifier of extracted application, in case application has no activity