Skip to content

Commit

Permalink
Merge pull request #624 from kivy/kengoon-master
Browse files Browse the repository at this point in the history
#611 add filters for file chooser on android
  • Loading branch information
akshayaurora committed Jun 2, 2021
2 parents e7a0647 + 8bf6a99 commit c773bcd
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions plyer/platforms/android/filechooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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':
Expand Down

0 comments on commit c773bcd

Please sign in to comment.