comparison 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
comparison
equal deleted inserted replaced
456:2cfb9f7a6b62 457:ec11a35dcf14
23 from functools import partial 23 from functools import partial
24 from urllib.parse import urlparse 24 from urllib.parse import urlparse
25 from pathlib import Path 25 from pathlib import Path
26 import shutil 26 import shutil
27 import mimetypes 27 import mimetypes
28 from jnius import autoclass, cast 28 from jnius import autoclass, cast, JavaException
29 from android import activity 29 from android import activity
30 from android.permissions import request_permissions, Permission 30 from android.permissions import request_permissions, Permission
31 from kivy.clock import Clock 31 from kivy.clock import Clock
32 from kivy.uix.label import Label 32 from kivy.uix.label import Label
33 from sat.core.i18n import _ 33 from sat.core.i18n import _
55 AndroidString = autoclass('java.lang.String') 55 AndroidString = autoclass('java.lang.String')
56 Uri = autoclass('android.net.Uri') 56 Uri = autoclass('android.net.Uri')
57 ImagesMedia = autoclass('android.provider.MediaStore$Images$Media') 57 ImagesMedia = autoclass('android.provider.MediaStore$Images$Media')
58 AudioMedia = autoclass('android.provider.MediaStore$Audio$Media') 58 AudioMedia = autoclass('android.provider.MediaStore$Audio$Media')
59 VideoMedia = autoclass('android.provider.MediaStore$Video$Media') 59 VideoMedia = autoclass('android.provider.MediaStore$Video$Media')
60 URLConnection = autoclass('java.net.URLConnection')
60 61
61 DISPLAY_NAME = '_display_name' 62 DISPLAY_NAME = '_display_name'
62 DATA = '_data' 63 DATA = '_data'
63 64
64 65
452 453
453 request_permissions(perms, callback=request_permissions_cb) 454 request_permissions(perms, callback=request_permissions_cb)
454 455
455 def open_url(self, url, wid=None): 456 def open_url(self, url, wid=None):
456 parsed_url = urlparse(url) 457 parsed_url = urlparse(url)
457 if parsed_url.scheme == "geo": 458 if parsed_url.scheme == "aesgcm":
459 return super().open_url(url, wid)
460 else:
461 media_type = mimetypes.guess_type(url, strict=False)[0]
462 if media_type is None:
463 log.debug(
464 f"media_type for {url!r} not found with python mimetypes, trying "
465 f"guessContentTypeFromName")
466 media_type = URLConnection.guessContentTypeFromName(url)
458 intent = Intent(Intent.ACTION_VIEW) 467 intent = Intent(Intent.ACTION_VIEW)
459 intent.setData(Uri.parse(url)) 468 if media_type is not None:
469 log.debug(f"file {url!r} is of type {media_type}")
470 intent.setDataAndType(Uri.parse(url), media_type)
471 else:
472 log.debug(f"can't guess media type for {url!r}")
473 intent.setData(Uri.parse(url))
460 if mActivity.getPackageManager() is not None: 474 if mActivity.getPackageManager() is not None:
461 activity = cast('android.app.Activity', mActivity) 475 activity = cast('android.app.Activity', mActivity)
462 activity.startActivity(intent) 476 try:
463 else: 477 activity.startActivity(intent)
464 super().open_url(url, wid) 478 except JavaException as e:
479 if e.classname != "android.content.ActivityNotFoundException":
480 raise e
481 log.debug(
482 f"activity not found for url {url!r}, we'll try generic opener")
483 else:
484 return
485
486 # if nothing else worked, we default to base open_url
487 super().open_url(url, wid)