Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A utility module has been created to work with android external storage files #2910

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 44 additions & 28 deletions android/src/toga_android/dialogs.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from android import R
from android.app import AlertDialog
from android.content import DialogInterface
from android.content import DialogInterface, Intent
from java import dynamic_proxy

import toga

from .libs import utilfile as uf


class OnClickListener(dynamic_proxy(DialogInterface.OnClickListener)):
def __init__(self, fn=None, value=None):
Expand All @@ -31,12 +33,12 @@ def show(self, host_window, future):

class TextDialog(BaseDialog):
def __init__(
self,
title,
message,
positive_text,
negative_text=None,
icon=None,
self,
title,
message,
positive_text,
negative_text=None,
icon=None,
):
super().__init__()

Expand Down Expand Up @@ -104,10 +106,10 @@ def __init__(self, title, message):

class StackTraceDialog(BaseDialog):
def __init__(
self,
title,
message,
**kwargs,
self,
title,
message,
**kwargs,
):
super().__init__()

Expand All @@ -117,11 +119,11 @@ def __init__(

class SaveFileDialog(BaseDialog):
def __init__(
self,
title,
filename,
initial_directory,
file_types=None,
self,
title,
filename,
initial_directory,
file_types=None,
):
super().__init__()

Expand All @@ -130,25 +132,39 @@ def __init__(


class OpenFileDialog(BaseDialog):
class HandlerOpenDialog(uf.HandlerFileDialog):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless there's a reason for nesting classes like this, we generally keep classes at the module level.

def show(self):
intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.setType("*/*")
self.app.start_activity(intent, on_complete=self.parent.handler)

def __init__(
self,
title,
initial_directory,
file_types,
multiple_select,
self,
title,
initial_directory,
file_types,
multiple_select,
):
super().__init__()
self.native = OpenFileDialog.HandlerOpenDialog(self, toga.App.app.current_window)
self.mActive = self.native.mActive

toga.App.app.factory.not_implemented("dialogs.OpenFileDialog()")
self.native = None
def handler(self, code, indent):
self._handler(indent.getData())

def _handler(self, uri):
ist = self.mActive.getContentResolver().openInputStream(uri)
isr = uf.InputStreamReader(uf.Objects.requireNonNull(ist))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InputStreamReader is a java class; it should be imported from the Java namespace, not implicitly from the utilfile namespace.

reader = uf.VFile(uf.BufferedReader(isr))
self.future.set_result(reader)


class SelectFolderDialog(BaseDialog):
def __init__(
self,
title,
initial_directory,
multiple_select,
self,
title,
initial_directory,
multiple_select,
):
super().__init__()

Expand Down
58 changes: 58 additions & 0 deletions android/src/toga_android/libs/utilfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import abc
import io

import java

BufferedReader = java.jclass("java.io.BufferedReader")
InputStreamReader = java.jclass("java.io.InputStreamReader")
Objects = java.jclass("java.util.Objects")
Comment on lines +6 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use from java.io import BufferedReader etc here.



class HandlerFileDialog(abc.ABC):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me why this is in utilfile.py, rather than part of the dialogs class.

"""Абстрактный класс вызова файлового менеджера"""

def __init__(self, parent, app_toga):
self.parent = parent
self.app = app_toga._impl
self.mActive = app_toga._impl.native

@abc.abstractmethod
def show(self):
"""Запуск менеджера"""
pass


class VFile(io.TextIOBase):
"""Файл для работы с внешнем хранилищем андроида"""

def __init__(self, bufferedReader):
self._bfr = bufferedReader

def __del__(self):
self.close()

def close(self):
self._bfr.close()

def readline(self, size: int = 0) -> str:
"""Функция для чтения строки
:param size: количество строк, которые нужно считать.
Если он равен 0, то будет прочтен весь файл
:return: Строка"""
return self._bfr.readLine()

def read(self, size: int = 0) -> str:
"""Функция для чтения строки
:param size: количество символов, которые нужно считать.
Если он равен 0, то будет прочтен весь файл
:return: Строка"""
res = ""
counter = size if size else "+"
while counter:
resp = self._bfr.read()
if resp == -1:
break
res += chr(resp)
if isinstance(counter, int):
counter -= 1
return res
1 change: 1 addition & 0 deletions changes/2910.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Partial OpenFileDialog implementation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog message should be written in the form of a potential release note for the new feature. In this case, it should be a .feature (this isn't a small miscellaneous fix - it's a major new feature), and should read something like:

Suggested change
Partial OpenFileDialog implementation
Android now has support for OpenFileDialog.

Loading