Mercurial > libervia-desktop-kivy
diff cagou/core/platform_/android.py @ 457:ec11a35dcf14
platform (android): open_url now tries to open the right activity for any URL:
media type is guessed (by Python standard mimetypes lib then Android API) to try to open
the right intent, before defaulting to generic open_url.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 01 Apr 2020 23:59:00 +0200 |
parents | 85fc3da9560c |
children | 3c9ba4a694ef |
line wrap: on
line diff
--- a/cagou/core/platform_/android.py Wed Apr 01 23:59:00 2020 +0200 +++ b/cagou/core/platform_/android.py Wed Apr 01 23:59:00 2020 +0200 @@ -25,7 +25,7 @@ from pathlib import Path import shutil import mimetypes -from jnius import autoclass, cast +from jnius import autoclass, cast, JavaException from android import activity from android.permissions import request_permissions, Permission from kivy.clock import Clock @@ -57,6 +57,7 @@ ImagesMedia = autoclass('android.provider.MediaStore$Images$Media') AudioMedia = autoclass('android.provider.MediaStore$Audio$Media') VideoMedia = autoclass('android.provider.MediaStore$Video$Media') +URLConnection = autoclass('java.net.URLConnection') DISPLAY_NAME = '_display_name' DATA = '_data' @@ -454,11 +455,33 @@ def open_url(self, url, wid=None): parsed_url = urlparse(url) - if parsed_url.scheme == "geo": + if parsed_url.scheme == "aesgcm": + return super().open_url(url, wid) + else: + media_type = mimetypes.guess_type(url, strict=False)[0] + if media_type is None: + log.debug( + f"media_type for {url!r} not found with python mimetypes, trying " + f"guessContentTypeFromName") + media_type = URLConnection.guessContentTypeFromName(url) intent = Intent(Intent.ACTION_VIEW) - intent.setData(Uri.parse(url)) + if media_type is not None: + log.debug(f"file {url!r} is of type {media_type}") + intent.setDataAndType(Uri.parse(url), media_type) + else: + log.debug(f"can't guess media type for {url!r}") + intent.setData(Uri.parse(url)) if mActivity.getPackageManager() is not None: activity = cast('android.app.Activity', mActivity) - activity.startActivity(intent) - else: - super().open_url(url, wid) + try: + activity.startActivity(intent) + except JavaException as e: + if e.classname != "android.content.ActivityNotFoundException": + raise e + log.debug( + f"activity not found for url {url!r}, we'll try generic opener") + else: + return + + # if nothing else worked, we default to base open_url + super().open_url(url, wid)