Skip to content

Commit

Permalink
[MMLV, MMCA] Move permissions_external_storage to mobileHelpers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Sobottasgithub committed May 6, 2024
1 parent efe43d0 commit 67d9f04
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
21 changes: 2 additions & 19 deletions src/MobileCrashAnalyzer/ui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
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:
from android import activity, mActivity, permissions
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
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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...")

Expand Down
21 changes: 2 additions & 19 deletions src/MobileLogViewer/ui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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__)
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/shared/utils/mobileHelpers.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 67d9f04

Please sign in to comment.