Skip to content

Commit 46feaee

Browse files
committed
Fixed reported error which occurs during the app icon extraction for homescreen apps
1 parent 238f93c commit 46feaee

1 file changed

Lines changed: 82 additions & 78 deletions

File tree

src/tv.py

Lines changed: 82 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -656,86 +656,90 @@ async def _apply_current_app_metadata(self, current_app: str) -> dict:
656656
global HOMESCREEN_IMAGE
657657

658658
update = {}
659-
# one-time initialization
660-
if HOMESCREEN_IMAGE is None:
661-
HOMESCREEN_IMAGE = ""
662-
HOMESCREEN_IMAGE = await encode_icon_to_data_uri("config://androidtv.png")
663-
664-
# Special handling for homescreen & Android TV system apps: show pre-defined icon & clear media information
665-
homescreen_app = apps.is_homescreen_app(current_app)
666-
if homescreen_app or apps.is_standby_app(current_app):
667-
self._clear_media_information()
668-
update[MediaAttr.SOURCE] = apps.IdMappings[current_app]
669-
update[MediaAttr.MEDIA_TITLE] = ""
670-
update[MediaAttr.MEDIA_ALBUM] = ""
671-
update[MediaAttr.MEDIA_ARTIST] = ""
672-
update[MediaAttr.MEDIA_IMAGE_URL] = HOMESCREEN_IMAGE
673-
update[MediaAttr.MEDIA_POSITION] = 0
674-
update[MediaAttr.MEDIA_DURATION] = 0
675-
update[MediaAttr.STATE] = (
676-
media_player.States.ON.value if homescreen_app else media_player.States.STANDBY.value
677-
)
678-
return update
679-
680-
# Track state of data sources
681-
offline_name = None
682-
offline_match = None
683-
external_name = None
684-
external_icon = None
685-
686-
# Try offline ID mapping first
687-
if current_app in apps.IdMappings:
688-
offline_name = apps.IdMappings[current_app]
689-
self._media_app = offline_name
690-
691-
# Try fuzzy offline name matching if ID mapping failed
692-
if not offline_name:
693-
for query, name in apps.NameMatching.items():
694-
if query in current_app:
695-
offline_match = name
696-
self._media_app = name
697-
break
698659

699-
# Try external metadata
700-
metadata = (
701-
await get_app_metadata(current_app) if current_app and self._device_config.use_external_metadata else None
702-
)
703-
if metadata:
704-
if _LOG.isEnabledFor(logging.DEBUG):
705-
_LOG.debug("App metadata: %s", filter_data_img_properties(metadata))
706-
external_name = metadata.get("name")
707-
external_icon = metadata.get("icon")
708-
if external_name:
709-
self._media_app = external_name
710-
if external_icon:
711-
self._app_image_url = external_icon
712-
713-
# Determine final name/title to use
714-
name_to_use = offline_name or offline_match or external_name or current_app
715-
# TODO why set name to both source & media title fields?
716-
update[MediaAttr.SOURCE] = name_to_use
717-
if not self._media_title and not self._media_image_url:
718-
update[MediaAttr.MEDIA_TITLE] = name_to_use
719-
720-
# Determine which icon to use
721-
icon_to_use = None
722-
if self._device_config.use_external_metadata or self._use_app_url:
723-
if external_icon:
724-
icon_to_use = external_icon
725-
else:
726-
icon_to_use = ""
727-
elif self._media_image_url:
728-
# TODO what's the intended logic?
729-
# `icon_to_use` is never used because of the inverse `if not self._media_image_url:` check below!
730-
icon_to_use = self._media_image_url
731-
732-
update[MediaAttr.STATE] = media_player.States.PLAYING.value
733-
# Skip applying app icon if media image from cast is present
734-
if not self._media_image_url:
735-
if not icon_to_use:
660+
try:
661+
# one-time initialization
662+
if HOMESCREEN_IMAGE is None:
663+
HOMESCREEN_IMAGE = ""
664+
HOMESCREEN_IMAGE = await encode_icon_to_data_uri("config://androidtv.png")
665+
666+
# Special handling for homescreen & Android TV system apps: show pre-defined icon & clear media information
667+
homescreen_app = apps.is_homescreen_app(current_app)
668+
if homescreen_app or apps.is_standby_app(current_app):
669+
self._clear_media_information()
670+
update[MediaAttr.SOURCE] = apps.IdMappings.get(current_app, current_app)
671+
update[MediaAttr.MEDIA_TITLE] = ""
672+
update[MediaAttr.MEDIA_ALBUM] = ""
673+
update[MediaAttr.MEDIA_ARTIST] = ""
736674
update[MediaAttr.MEDIA_IMAGE_URL] = HOMESCREEN_IMAGE
737-
else:
738-
update[MediaAttr.MEDIA_IMAGE_URL] = icon_to_use
675+
update[MediaAttr.MEDIA_POSITION] = 0
676+
update[MediaAttr.MEDIA_DURATION] = 0
677+
update[MediaAttr.STATE] = (
678+
media_player.States.ON.value if homescreen_app else media_player.States.STANDBY.value
679+
)
680+
return update
681+
682+
# Track state of data sources
683+
offline_name = None
684+
offline_match = None
685+
external_name = None
686+
external_icon = None
687+
688+
# Try offline ID mapping first
689+
if current_app in apps.IdMappings:
690+
offline_name = apps.IdMappings[current_app]
691+
self._media_app = offline_name
692+
693+
# Try fuzzy offline name matching if ID mapping failed
694+
if not offline_name:
695+
for query, name in apps.NameMatching.items():
696+
if query in current_app:
697+
offline_match = name
698+
self._media_app = name
699+
break
700+
701+
# Try external metadata
702+
metadata = (
703+
await get_app_metadata(current_app) if current_app and self._device_config.use_external_metadata else None
704+
)
705+
if metadata:
706+
if _LOG.isEnabledFor(logging.DEBUG):
707+
_LOG.debug("App metadata: %s", filter_data_img_properties(metadata))
708+
external_name = metadata.get("name")
709+
external_icon = metadata.get("icon")
710+
if external_name:
711+
self._media_app = external_name
712+
if external_icon:
713+
self._app_image_url = external_icon
714+
715+
# Determine final name/title to use
716+
name_to_use = offline_name or offline_match or external_name or current_app
717+
# TODO why set name to both source & media title fields?
718+
update[MediaAttr.SOURCE] = name_to_use
719+
if not self._media_title and not self._media_image_url:
720+
update[MediaAttr.MEDIA_TITLE] = name_to_use
721+
722+
# Determine which icon to use
723+
icon_to_use = None
724+
if self._device_config.use_external_metadata or self._use_app_url:
725+
if external_icon:
726+
icon_to_use = external_icon
727+
else:
728+
icon_to_use = ""
729+
elif self._media_image_url:
730+
# TODO what's the intended logic?
731+
# `icon_to_use` is never used because of the inverse `if not self._media_image_url:` check below!
732+
icon_to_use = self._media_image_url
733+
734+
update[MediaAttr.STATE] = media_player.States.PLAYING.value
735+
# Skip applying app icon if media image from cast is present
736+
if not self._media_image_url:
737+
if not icon_to_use:
738+
update[MediaAttr.MEDIA_IMAGE_URL] = HOMESCREEN_IMAGE
739+
else:
740+
update[MediaAttr.MEDIA_IMAGE_URL] = icon_to_use
741+
except Exception as ex:
742+
_LOG.exception("[%s] Error during app metadata analysis : %s", self._identifier, ex)
739743

740744
return update
741745

0 commit comments

Comments
 (0)