Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions androidtv/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ class DeviceEnum(IntEnum):
CMD_SUCCESS1_FAILURE0 = r" && echo -e '1\c' || echo -e '0\c'"

#: Get the audio state
CMD_AUDIO_STATE = r"dumpsys audio | grep paused | grep -qv 'Buffer Queue' && echo -e '1\c' || (dumpsys audio | grep started | grep -qv 'Buffer Queue' && echo '2\c' || echo '0\c')"
CMD_AUDIO_STATE = r"dumpsys audio 2>/dev/null | grep paused | grep -qv 'Buffer Queue' && echo -e '1\c' || (dumpsys audio 2>/dev/null | grep started | grep -qv 'Buffer Queue' && echo '2\c' || echo '0\c')"

#: Get the audio state for an Android 11+ device
CMD_AUDIO_STATE11 = (
"CURRENT_AUDIO_STATE=$(dumpsys audio | sed -r -n '/[0-9]{2}-[0-9]{2}.*player piid:.*(state|event):(started|paused|stopped).*$/h; ${x;p;}') && "
"CURRENT_AUDIO_STATE=$(dumpsys audio 2>/dev/null | sed -r -n '/[0-9]{2}-[0-9]{2}.*player piid:.*(state|event):(started|paused|stopped).*$/h; ${x;p;}') && "
+ r"echo $CURRENT_AUDIO_STATE | grep -q paused && echo -e '1\c' || { echo $CURRENT_AUDIO_STATE | grep -q started && echo '2\c' || echo '0\c' ; }"
)

#: Determine whether the device is awake
CMD_AWAKE = "dumpsys power | grep mWakefulness | grep -q Awake"
CMD_AWAKE = "dumpsys power 2>/dev/null | grep mWakefulness | grep -q Awake"

#: Parse current application identifier from dumpsys output and assign it to ``CURRENT_APP`` variable (assumes dumpsys output is momentarily set to ``CURRENT_APP`` variable)
CMD_PARSE_CURRENT_APP = "CURRENT_APP=${CURRENT_APP#*ActivityRecord{* * } && CURRENT_APP=${CURRENT_APP#*{* * } && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP%\\}*}"
Expand All @@ -90,22 +90,24 @@ class DeviceEnum(IntEnum):
CMD_PARSE_CURRENT_APP11 = "CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* }"
#: Assign focused application identifier to ``CURRENT_APP`` variable
CMD_DEFINE_CURRENT_APP_VARIABLE = (
"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp') && " + CMD_PARSE_CURRENT_APP
"CURRENT_APP=$(dumpsys window windows 2>/dev/null | grep -E 'mCurrentFocus|mFocusedApp') && "
+ CMD_PARSE_CURRENT_APP
)
#: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 11 device
CMD_DEFINE_CURRENT_APP_VARIABLE11 = (
"CURRENT_APP=$(dumpsys window windows | grep -E 'mInputMethod(Input)?Target') && " + CMD_PARSE_CURRENT_APP11
"CURRENT_APP=$(dumpsys window windows 2>/dev/null | grep -E 'mInputMethod(Input)?Target') && "
+ CMD_PARSE_CURRENT_APP11
)

#: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 12 device
CMD_DEFINE_CURRENT_APP_VARIABLE12 = (
"CURRENT_APP=$(dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp|mObscuringWindow') && "
"CURRENT_APP=$(dumpsys window windows 2>/dev/null | grep -E 'mCurrentFocus|mFocusedApp|mObscuringWindow') && "
+ CMD_PARSE_CURRENT_APP11
)

#: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 13+ device
CMD_DEFINE_CURRENT_APP_VARIABLE13 = (
"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && "
"CURRENT_APP=$(dumpsys window windows 2>/dev/null | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && "
+ CMD_PARSE_CURRENT_APP11
)

Expand All @@ -125,7 +127,7 @@ class DeviceEnum(IntEnum):
#: Assign focused application identifier to ``CURRENT_APP`` variable (for a Askey STI6130 )
#: Tested on Bell Streamer which is sti6130. May also work the same on Walmart Onn android stick, which is the same hardware, but labelled Askey STI6140
CMD_DEFINE_CURRENT_APP_VARIABLE_ASKEY_STI6130 = (
"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && "
"CURRENT_APP=$(dumpsys window windows 2>/dev/null | grep -E -m 1 'imeLayeringTarget|imeInputTarget|imeControlTarget') && "
+ CMD_PARSE_CURRENT_APP11
)

Expand All @@ -135,7 +137,7 @@ class DeviceEnum(IntEnum):

#: Assign focused application identifier to ``CURRENT_APP`` variable (for a Google TV device)
CMD_DEFINE_CURRENT_APP_VARIABLE_GOOGLE_TV = (
"CURRENT_APP=$(dumpsys activity a . | grep mResumedActivity) && " + CMD_PARSE_CURRENT_APP
"CURRENT_APP=$(dumpsys activity a . 2>/dev/null | grep mResumedActivity) && " + CMD_PARSE_CURRENT_APP
)

#: Output identifier for current/focused application (for a Google TV device)
Expand All @@ -149,12 +151,12 @@ class DeviceEnum(IntEnum):

#: Get the HDMI input
CMD_HDMI_INPUT = (
"dumpsys activity starter | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'"
"dumpsys activity starter 2>/dev/null | grep -E -o '(ExternalTv|HDMI)InputService/HW[0-9]' -m 1 | grep -o 'HW[0-9]'"
)

#: Get the HDMI input for an Android 11 device
CMD_HDMI_INPUT11 = (
"(HDMI=$(dumpsys tv_input | grep 'ResourceClientProfile {.*}' | grep -o -E '(hdmi_port=[0-9]|TV)') && { echo ${HDMI/hdmi_port=/HW} | cut -d' ' -f1 ; }) || "
"(HDMI=$(dumpsys tv_input 2>/dev/null | grep 'ResourceClientProfile {.*}' | grep -o -E '(hdmi_port=[0-9]|TV)') && { echo ${HDMI/hdmi_port=/HW} | cut -d' ' -f1 ; }) || "
+ CMD_HDMI_INPUT
)

Expand Down Expand Up @@ -199,7 +201,7 @@ class DeviceEnum(IntEnum):
)

#: 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 {'"
CMD_MEDIA_SESSION_STATE = "dumpsys media_session 2>/dev/null | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'"

#: Determine the current app and get the state from ``dumpsys media_session``
CMD_CURRENT_APP_MEDIA_SESSION_STATE = CMD_CURRENT_APP + " && " + CMD_MEDIA_SESSION_STATE
Expand All @@ -226,10 +228,10 @@ class DeviceEnum(IntEnum):
CMD_INSTALLED_APPS = "pm list packages"

#: Determine if the device is on
CMD_SCREEN_ON = "(dumpsys power | grep 'Display Power' | grep -q 'state=ON' || dumpsys power | grep -q 'mScreenOn=true' || dumpsys display | grep -q 'mScreenState=ON')"
CMD_SCREEN_ON = "(dumpsys power 2>/dev/null | grep 'Display Power' | grep -q 'state=ON' || dumpsys power 2>/dev/null | grep -q 'mScreenOn=true' || dumpsys display 2>/dev/null | grep -q 'mScreenState=ON')"

#: Get the "STREAM_MUSIC" block from ``dumpsys audio``
CMD_STREAM_MUSIC = r"dumpsys audio | grep '\- STREAM_MUSIC:' -A 11"
CMD_STREAM_MUSIC = r"dumpsys audio 2>/dev/null | grep '\- STREAM_MUSIC:' -A 11"

#: Turn off an Android TV device (note: `KEY_POWER = 26` is defined below)
CMD_TURN_OFF_ANDROIDTV = CMD_SCREEN_ON + " && input keyevent 26"
Expand All @@ -244,7 +246,7 @@ class DeviceEnum(IntEnum):
CMD_TURN_ON_FIRETV = CMD_SCREEN_ON + " || (input keyevent 26 && input keyevent 3)"

#: Get the wake lock size
CMD_WAKE_LOCK_SIZE = "dumpsys power | grep Locks | grep 'size='"
CMD_WAKE_LOCK_SIZE = "dumpsys power 2>/dev/null | grep Locks | grep 'size='"

#: Determine if the device is on, the screen is on, and get the wake lock size
CMD_SCREEN_ON_AWAKE_WAKE_LOCK_SIZE = (
Expand Down
Loading
Loading