From 67d9f046e1e8e9780f5c7a49817208869b24e768 Mon Sep 17 00:00:00 2001 From: Sobottasgithub Date: Fri, 3 May 2024 15:16:33 +0200 Subject: [PATCH] [MMLV, MMCA] Move permissions_external_storage to mobileHelpers.py --- src/MobileCrashAnalyzer/ui/main_window.py | 21 ++----------------- src/MobileLogViewer/ui/main_window.py | 21 ++----------------- src/shared/utils/mobileHelpers.py | 25 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 38 deletions(-) create mode 100644 src/shared/utils/mobileHelpers.py diff --git a/src/MobileCrashAnalyzer/ui/main_window.py b/src/MobileCrashAnalyzer/ui/main_window.py index 20d9559..2a5a876 100644 --- a/src/MobileCrashAnalyzer/ui/main_window.py +++ b/src/MobileCrashAnalyzer/ui/main_window.py @@ -17,6 +17,7 @@ from shared.storage import CrashReport, Rawlog from shared.utils import Paths from shared.ui.mobile_about_dialog import MobileAboutDialog +import shared.utils.mobileHelpers as helpers # Just import if the os is Android to avoid Android peculiarities try: @@ -24,10 +25,6 @@ J_FileOutputStream = autoclass("java.io.FileOutputStream") J_FileUtils = autoclass("android.os.FileUtils") J_Intent = autoclass("android.content.Intent") - J_PythonActivity = autoclass('org.kivy.android.PythonActivity') - J_Environment = autoclass("android.os.Environment") - J_Settings = autoclass("android.provider.Settings") - J_Uri = autoclass("android.net.Uri") OPERATING_SYSTEM = "Android" except: OPERATING_SYSTEM = None @@ -91,7 +88,7 @@ def on_start(self, *args): # Use if the os is Android to avoid Android peculiarities if OPERATING_SYSTEM == "Android": logger.info("Asking for permission for external storage") - self.permissions_external_storage() + helpers.permissions_external_storage() context = cast('android.content.Context', mActivity.getApplicationContext()) logger.info(f"Startup application context: {context}") @@ -155,20 +152,6 @@ def on_new_intent(self, intent): """ - # See: https://stackoverflow.com/questions/64849485/why-is-filemanager-not-working-on-android-kivymd - def permissions_external_storage(self, *args): - if not J_Environment.isExternalStorageManager(): - try: - logger.debug("Ask for ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION") - context = mActivity.getApplicationContext() - uri = J_Uri.parse("package:" + context.getPackageName()) - intent = J_Intent(J_Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri) - except Exception as e: - logger.debug("ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION Failed! Open ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION") - intent = J_Intent(J_Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION) - currentActivity = cast("android.app.Activity", J_PythonActivity.mActivity) - currentActivity.startActivityForResult(intent, 101) - def openFile(self, *args): logger.debug("Create file select popup dialog...") diff --git a/src/MobileLogViewer/ui/main_window.py b/src/MobileLogViewer/ui/main_window.py index f178ac6..5bc4b3d 100644 --- a/src/MobileLogViewer/ui/main_window.py +++ b/src/MobileLogViewer/ui/main_window.py @@ -9,7 +9,6 @@ from kivy.uix.label import Label from kivy.uix.popup import Popup from kivy.factory import Factory -from kivy.utils import platform import functools import os @@ -22,10 +21,6 @@ J_FileOutputStream = autoclass("java.io.FileOutputStream") J_FileUtils = autoclass("android.os.FileUtils") J_Intent = autoclass("android.content.Intent") - J_PythonActivity = autoclass('org.kivy.android.PythonActivity') - J_Environment = autoclass("android.os.Environment") - J_Settings = autoclass("android.provider.Settings") - J_Uri = autoclass("android.net.Uri") OPERATING_SYSTEM = "Android" except: OPERATING_SYSTEM = None @@ -34,6 +29,7 @@ from shared.storage import Rawlog from shared.utils import Paths from shared.ui.mobile_about_dialog import MobileAboutDialog +import shared.utils.mobileHelpers as helpers import logging logger = logging.getLogger(__name__) @@ -104,7 +100,7 @@ def on_start(self, *args): # Use if the os is Android to avoid Android peculiarities if OPERATING_SYSTEM == "Android": logger.info("Asking for permission for external storage") - self.permissions_external_storage() + helpers.permissions_external_storage() context = cast('android.content.Context', mActivity.getApplicationContext()) logger.info(f"Startup application context: {context}") @@ -165,19 +161,6 @@ def on_new_intent(self, intent): self.openFile(cacheFile) os.remove(cacheFile) """ - # See: https://stackoverflow.com/questions/64849485/why-is-filemanager-not-working-on-android-kivymd - def permissions_external_storage(self, *args): - if not J_Environment.isExternalStorageManager(): - try: - logger.debug("Ask for ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION") - context = mActivity.getApplicationContext() - uri = J_Uri.parse("package:" + context.getPackageName()) - intent = J_Intent(J_Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri) - except Exception as e: - logger.debug("ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION Failed! Open ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION") - intent = J_Intent(J_Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION) - currentActivity = cast("android.app.Activity", J_PythonActivity.mActivity) - currentActivity.startActivityForResult(intent, 101) def selectFile(self, *args): # Create popup window to select file diff --git a/src/shared/utils/mobileHelpers.py b/src/shared/utils/mobileHelpers.py new file mode 100644 index 0000000..1e1c251 --- /dev/null +++ b/src/shared/utils/mobileHelpers.py @@ -0,0 +1,25 @@ +from jnius import autoclass, cast +from android import activity, mActivity, permissions + +J_Intent = autoclass("android.content.Intent") +J_PythonActivity = autoclass('org.kivy.android.PythonActivity') +J_Environment = autoclass("android.os.Environment") +J_Settings = autoclass("android.provider.Settings") +J_Uri = autoclass("android.net.Uri") + +import logging +logger = logging.getLogger(__name__) + +# See: https://stackoverflow.com/questions/64849485/why-is-filemanager-not-working-on-android-kivymd +def permissions_external_storage(self, *args): + if not J_Environment.isExternalStorageManager(): + try: + logger.debug("Ask for ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION") + context = mActivity.getApplicationContext() + uri = J_Uri.parse("package:" + context.getPackageName()) + intent = J_Intent(J_Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri) + except Exception as e: + logger.debug("ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION Failed! Open ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION") + intent = J_Intent(J_Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION) + currentActivity = cast("android.app.Activity", J_PythonActivity.mActivity) + currentActivity.startActivityForResult(intent, 101) \ No newline at end of file