diff --git a/src/bitmessagekivy/get_platform.py b/src/bitmessagekivy/get_platform.py new file mode 100644 index 00000000..72333aff --- /dev/null +++ b/src/bitmessagekivy/get_platform.py @@ -0,0 +1,49 @@ +from sys import platform as _sys_platform +from os import environ + +""" +We need to check platform and set environ for KIVY_CAMERA, if requires, before importing kivy. + +We cannot use sys.platform directly because it returns 'linux' on android devices as well. +We cannot use kivy.util.platform beacuse it imports kivy beforehand and thus setting environ +after that doesn't make any sense. + +So we needed to copy the `_get_platform` function from kivy.utils +""" + + +def _get_platform(): + # On Android sys.platform returns 'linux2', so prefer to check the + # existence of environ variables set during Python initialization + kivy_build = environ.get("KIVY_BUILD", "") + if kivy_build in {"android", "ios"}: + return kivy_build + elif "P4A_BOOTSTRAP" in environ: + return "android" + elif "ANDROID_ARGUMENT" in environ: + # We used to use this method to detect android platform, + # leaving it here to be backwards compatible with `pydroid3` + # and similar tools outside kivy's ecosystem + return "android" + elif _sys_platform in ("win32", "cygwin"): + return "win" + elif _sys_platform == "darwin": + return "macosx" + elif _sys_platform.startswith("linux"): + return "linux" + elif _sys_platform.startswith("freebsd"): + return "linux" + return "unknown" + + +platform = _get_platform() + +if platform not in ("android", "unknown"): + """ + After tweaking a little bit with opencv camera, it's possible to make camera + go on and off as required while the app is still running. + + Other camera provider such as `gi` has some issue upon closing the camera. + by setting KIVY_CAMERA environment variable before importing kivy, we are forcing it to use opencv camera provider. + """ + environ["KIVY_CAMERA"] = "opencv" \ No newline at end of file diff --git a/src/bitmessagekivy/main.kv b/src/bitmessagekivy/main.kv index 70bd9fe8..c14ebe04 100644 --- a/src/bitmessagekivy/main.kv +++ b/src/bitmessagekivy/main.kv @@ -16,7 +16,7 @@ #:import MDScrollViewRefreshLayout kivymd.uix.refreshlayout.MDScrollViewRefreshLayout #:import MDSpinner kivymd.uix.spinner.MDSpinner #:import MDTabsBase kivymd.uix.tab.MDTabsBase -##:import ZBarSymbol pyzbar.pyzbar.ZBarSymbol +#:import ZBarSymbol pyzbar.pyzbar.ZBarSymbol #:set color_button (0.784, 0.443, 0.216, 1) # brown diff --git a/src/bitmessagekivy/mpybit.py b/src/bitmessagekivy/mpybit.py index afd5c0b1..439acf05 100644 --- a/src/bitmessagekivy/mpybit.py +++ b/src/bitmessagekivy/mpybit.py @@ -1,76 +1,16 @@ -""" -Bitmessage android(mobile) interface -""" + # pylint: disable=too-many-lines,import-error,no-name-in-module,unused-argument # pylint: disable=too-many-ancestors,too-many-locals,useless-super-delegation # pylint: disable=protected-access -from sys import platform as _sys_platform -from os import environ +# pylint: disable=too-many-return-statements """ -We need to check platform and set environ for KIVY_CAMERA, if requires, before importing kivy. - -We cannot use sys.platform directly because it returns 'linux' on android devices as well. -We cannot use kivy.util.platform beacuse it imports kivy beforehand and thus setting environ -after that doesn't make any sense. - -So we needed to copy the `_get_platform` function from kivy.utils +Bitmessage android(mobile) interface """ -def _get_platform(): - # On Android sys.platform returns 'linux2', so prefer to check the - # existence of environ variables set during Python initialization - kivy_build = environ.get("KIVY_BUILD", "") - if kivy_build in {"android", "ios"}: - return kivy_build - elif "P4A_BOOTSTRAP" in environ: - return "android" - elif "ANDROID_ARGUMENT" in environ: - # We used to use this method to detect android platform, - # leaving it here to be backwards compatible with `pydroid3` - # and similar tools outside kivy's ecosystem - return "android" - elif _sys_platform in ("win32", "cygwin"): - return "win" - elif _sys_platform == "darwin": - return "macosx" - elif _sys_platform.startswith("linux"): - return "linux" - elif _sys_platform.startswith("freebsd"): - return "linux" - return "unknown" - - -platform = _get_platform() - -if platform == "android": - from jnius import autoclass, cast - from android.runnable import run_on_ui_thread - from android import python_act as PythonActivity - - Toast = autoclass("android.widget.Toast") - String = autoclass("java.lang.String") - CharSequence = autoclass("java.lang.CharSequence") - context = PythonActivity.mActivity - - @run_on_ui_thread - def show_toast(text, length): - t = Toast.makeText(context, text, length) - t.show() - - -else: - """ - After tweaking a little bit with opencv camera, it's possible to make camera - go on and off as required while the app is still running. - - Other camera provider such as `gi` has some issue upon closing the camera. - by setting KIVY_CAMERA environment variable before importing kivy, we are forcing it to use opencv camera provider. - """ - environ["KIVY_CAMERA"] = "opencv" - +from bitmessagekivy.get_platform import platform import os import time from bitmessagekivy import identiconGeneration @@ -106,7 +46,6 @@ from kivy.uix.recycleview.views import RecycleDataViewBehavior from kivy.uix.screenmanager import Screen from kivy.uix.spinner import Spinner from kivy.uix.textinput import TextInput -from kivy.utils import platform from kivymd.uix.button import MDIconButton from kivymd.uix.dialog import MDDialog from kivymd.uix.label import MDLabel @@ -140,11 +79,28 @@ from kivymd.uix.bottomsheet import MDCustomBottomSheet from kivy.effects.dampedscroll import DampedScrollEffect from kivymd.uix.menu import MDDropdownMenu +from kivy_garden.zbarcam import ZBarCam +from pyzbar.pyzbar import ZBarSymbol if platform != "android": from kivy.config import Config Config.set("input", "mouse", "mouse, multitouch_on_demand") +elif platform == "android": + from jnius import autoclass, cast + from android.runnable import run_on_ui_thread + from android import python_act as PythonActivity + + Toast = autoclass("android.widget.Toast") + String = autoclass("java.lang.String") + CharSequence = autoclass("java.lang.CharSequence") + context = PythonActivity.mActivity + + @run_on_ui_thread + def show_toast(text, length): + t = Toast.makeText(context, text, length) + t.show() + # pylint: disable=too-few-public-methods,too-many-arguments,attribute-defined-outside-init KVFILES = [