diff --git a/plyer/platforms/android/email.py b/plyer/platforms/android/email.py index 95dfa5eea..4560bdd2c 100644 --- a/plyer/platforms/android/email.py +++ b/plyer/platforms/android/email.py @@ -48,7 +48,8 @@ def _send(self, **kwargs): Intent.createChooser(intent, chooser_title) ) else: - activity.startActivity(intent) + intent.setClassName('com.google.android.gm', 'com.google.android.gm.ComposeActivityGmailExternal') + def instance(): diff --git a/plyer/platforms/android/filechooser.py b/plyer/platforms/android/filechooser.py index 5a8364fdd..faf81acbf 100644 --- a/plyer/platforms/android/filechooser.py +++ b/plyer/platforms/android/filechooser.py @@ -51,7 +51,6 @@ from plyer.facades import FileChooser from plyer import storagepath - String = autoclass('java.lang.String') Intent = autoclass('android.content.Intent') Activity = autoclass('android.app.Activity') @@ -78,6 +77,25 @@ class AndroidFileChooser(FileChooser): # default selection value selection = None + # mime types + mime_type = { + "doc": "application/msword", + "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "ppt": "application/vnd.ms-powerpoint", + "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", + "xls": "application/vnd.ms-excel", + "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "text": "text/*", + "pdf": "application/pdf", + "zip": "application/zip", + "image": "image/*", + "video": "video/*", + "audio": "audio/*", + "application": "application/*" + } + + selected_mime_type = None + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.select_code = randint(123456, 654321) @@ -110,10 +128,15 @@ def _open_file(self, **kwargs): self._handle_selection = kwargs.pop( 'on_selection', self._handle_selection ) + self.selected_mime_type = kwargs.pop("filters")[0] if "filters" in kwargs else "" # create Intent for opening file_intent = Intent(Intent.ACTION_GET_CONTENT) - file_intent.setType('*/*') + if not self.selected_mime_type or type(self.selected_mime_type) != str or\ + self.selected_mime_type not in self.mime_type: + file_intent.setType("*/*") + else: + file_intent.setType(self.mime_type[self.selected_mime_type]) file_intent.addCategory( Intent.CATEGORY_OPENABLE ) @@ -168,7 +191,10 @@ def _handle_external_documents(uri): # external (removable) SD card i.e. microSD external = storagepath.get_sdcard_dir() - external_base = basename(external) + try: + external_base = basename(external) + except TypeError: + external_base = basename(internal) # resolve sdcard path sdcard = internal @@ -178,8 +204,7 @@ def _handle_external_documents(uri): if file_type in external_base or external_base in file_type: sdcard = external - path = join(sdcard, file_name) - return path + return join(sdcard, file_name) @staticmethod def _handle_media_documents(uri): @@ -307,9 +332,11 @@ def _resolve_uri(self, uri): selection = None downloads = None + # This does not allow file selected from google photos or gallery + # or even any other file explorer to work # not a document URI, nothing to convert from - if not DocumentsContract.isDocumentUri(mActivity, uri): - return path + # if not DocumentsContract.isDocumentUri(mActivity, uri): + # return path if uri_authority == 'com.android.externalstorage.documents': return self._handle_external_documents(uri) @@ -325,10 +352,16 @@ def _resolve_uri(self, uri): # parse content:// scheme to path if uri_scheme == 'content' and not downloads: - path = self._parse_content( - uri=uri, projection=['_data'], selection=selection, - selection_args=[file_name], sort_order=None - ) + try: + path = self._parse_content( + uri=uri, projection=['_data'], selection=selection, + selection_args=file_name, sort_order=None + ) + except JavaException: # handles array error for selection_args + path = self._parse_content( + uri=uri, projection=['_data'], selection=selection, + selection_args=[file_name], sort_order=None + ) # nothing to parse, file:// will return a proper path elif uri_scheme == 'file':