Skip to content

Commit

Permalink
Fix: [MMCA, MMLV] Fix intent, fixes #25, fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Sobottasgithub committed Apr 26, 2024
1 parent 8eeb22c commit b2210e9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 94 deletions.
98 changes: 51 additions & 47 deletions src/MobileCrashAnalyzer/ui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
from jnius import autoclass, cast

# Just import if the os is Android to avoid Android peculiarities
if platform == "android":
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')
permissions.request_permissions([permissions.Permission.READ_EXTERNAL_STORAGE, permissions.Permission.WRITE_EXTERNAL_STORAGE])
OPERATING_SYSTEM = "Android"
except:
OPERATING_SYSTEM = None

from shared.storage import CrashReport, Rawlog
from shared.utils import Paths
Expand Down Expand Up @@ -60,8 +63,8 @@ def build(self):
self.layout.add_widget(self.uiActionBar)
self.layout.add_widget(self.uiTextInput)

# Just import if the os is Android to avoid Android peculiarities
if platform == "android":
# Use if the os is Android to avoid Android peculiarities
if OPERATING_SYSTEM == "Android":
activity.bind(on_new_intent=self.on_new_intent)

return self.layout
Expand All @@ -70,8 +73,8 @@ def quit(self, *args):
self.stop()

def on_start(self, *args):
# Just import if the os is Android to avoid Android peculiarities
if platform == "android":
# Use if the os is Android to avoid Android peculiarities
if OPERATING_SYSTEM == "Android":
context = cast('android.content.Context', mActivity.getApplicationContext())
logger.info(f"Startup application context: {context}")
intent = mActivity.getIntent()
Expand All @@ -82,54 +85,55 @@ def on_start(self, *args):
#see https://github.com/termux/termux-app/blob/74b23cb2096652601050d0f4951f9fb92577743c/app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java#L70
@mainthread
def on_new_intent(self, intent):
logger.info("Got new intent with action: %s" % str(intent.getAction()))
logger.debug("Raw intent: %s" % str(intent))
if intent.getAction() == "android.intent.action.VIEW":
logger.info("Intent scheme: %s" % intent.getScheme())
logger.info("Intent type: %s" % intent.getType())
logger.info("Intent data: %s" % intent.getData())
logger.info("Intent path: %s" % intent.getData().getPath())

uri = intent.getData()
context = mActivity.getApplicationContext()
contentResolver = context.getContentResolver()

cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):
os.remove(cacheFile)
logger.debug(f"Writing file at '{uri.getPath()}' to '{cacheFile}'...")
bytecount = J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
logger.debug(f"{bytecount} bytes copied...")
self.openFile(cacheFile)
os.remove(cacheFile)

"""
logger.info("Intent uri: %s" % intent.getParcelableExtra(J_Intent.EXTRA_STREAM))
logger.info("Intent text: %s" % intent.getStringExtra(J_Intent.EXTRA_TEXT))
uri = intent.getParcelableExtra(J_Intent.EXTRA_STREAM)
context = mActivity.getApplicationContext()
contentResolver = context.getContentResolver()
if uri != None and type(uri) != str:
logger.info("Real android.net.Uri found...")
uri = cast("android.net.Uri", uri)
if uri.getScheme().lower() != 'content':
logger.error("Uri scheme not supported: '%s'" % uri.getScheme())
return
if OPERATING_SYSTEM == "Android":
logger.info("Got new intent with action: %s" % str(intent.getAction()))
logger.debug("Raw intent: %s" % str(intent))
if intent.getAction() == "android.intent.action.VIEW":
logger.info("Intent scheme: %s" % intent.getScheme())
logger.info("Intent type: %s" % intent.getType())
logger.info("Intent data: %s" % intent.getData())
logger.info("Intent path: %s" % intent.getData().getPath())

uri = intent.getData()
context = mActivity.getApplicationContext()
contentResolver = context.getContentResolver()

cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):
os.remove(cacheFile)
J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
logger.debug(f"Writing file at '{uri.getPath()}' to '{cacheFile}'...")
bytecount = J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
logger.debug(f"{bytecount} bytes copied...")
self.openFile(cacheFile)
os.remove(cacheFile)
else:
logger.info("Str based uri found...")
cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):

"""
logger.info("Intent uri: %s" % intent.getParcelableExtra(J_Intent.EXTRA_STREAM))
logger.info("Intent text: %s" % intent.getStringExtra(J_Intent.EXTRA_TEXT))
uri = intent.getParcelableExtra(J_Intent.EXTRA_STREAM)
context = mActivity.getApplicationContext()
contentResolver = context.getContentResolver()
if uri != None and type(uri) != str:
logger.info("Real android.net.Uri found...")
uri = cast("android.net.Uri", uri)
if uri.getScheme().lower() != 'content':
logger.error("Uri scheme not supported: '%s'" % uri.getScheme())
return
cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):
os.remove(cacheFile)
J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
self.openFile(cacheFile)
os.remove(cacheFile)
J_FileUtils.copy(contentResolver.openInputStream(intent.getData()), J_FileOutputStream(cacheFile))
self.openFile(cacheFile)
os.remove(cacheFile)
"""
else:
logger.info("Str based uri found...")
cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):
os.remove(cacheFile)
J_FileUtils.copy(contentResolver.openInputStream(intent.getData()), J_FileOutputStream(cacheFile))
self.openFile(cacheFile)
os.remove(cacheFile)
"""

def openFile(self, *args):
logger.debug("Create file select popup dialog...")
Expand Down
98 changes: 51 additions & 47 deletions src/MobileLogViewer/ui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
from jnius import autoclass, cast

# Just import if the os is Android to avoid Android peculiarities
if platform == "android":
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')
permissions.request_permissions([permissions.Permission.READ_EXTERNAL_STORAGE, permissions.Permission.WRITE_EXTERNAL_STORAGE])
OPERATING_SYSTEM = "Android"
except:
OPERATING_SYSTEM = None

from shared.utils.constants import LOGLEVELS
from shared.storage import Rawlog
Expand Down Expand Up @@ -85,8 +88,8 @@ def build(self):
self.layout.add_widget(gridLayout_menueBar)
self.layout.add_widget(self.uiScrollWidget_logs)

# Just import if the os is Android to avoid Android peculiarities
if platform == "android":
# Use if the os is Android to avoid Android peculiarities
if OPERATING_SYSTEM == "Android":
activity.bind(on_new_intent=self.on_new_intent)

return self.layout
Expand All @@ -95,8 +98,8 @@ def quit(self, *args):
self.stop()

def on_start(self, *args):
# Just import if the os is Android to avoid Android peculiarities
if platform == "android":
# Use if the os is Android to avoid Android peculiarities
if OPERATING_SYSTEM == "Android":
context = cast('android.content.Context', mActivity.getApplicationContext())
logger.info(f"Startup application context: {context}")
intent = mActivity.getIntent()
Expand All @@ -107,54 +110,55 @@ def on_start(self, *args):
#see https://github.com/termux/termux-app/blob/74b23cb2096652601050d0f4951f9fb92577743c/app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java#L70
@mainthread
def on_new_intent(self, intent):
logger.info("Got new intent with action: %s" % str(intent.getAction()))
logger.debug("Raw intent: %s" % str(intent))
if intent.getAction() == "android.intent.action.VIEW":
logger.info("Intent scheme: %s" % intent.getScheme())
logger.info("Intent type: %s" % intent.getType())
logger.info("Intent data: %s" % intent.getData())
logger.info("Intent path: %s" % intent.getData().getPath())

uri = intent.getData()
context = mActivity.getApplicationContext()
contentResolver = context.getContentResolver()

cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):
os.remove(cacheFile)
logger.debug(f"Writing file at '{uri.getPath()}' to '{cacheFile}'...")
bytecount = J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
logger.debug(f"{bytecount} bytes copied...")
self.openFile(cacheFile)
os.remove(cacheFile)

"""
logger.info("Intent uri: %s" % intent.getParcelableExtra(J_Intent.EXTRA_STREAM))
logger.info("Intent text: %s" % intent.getStringExtra(J_Intent.EXTRA_TEXT))
uri = intent.getParcelableExtra(J_Intent.EXTRA_STREAM)
context = mActivity.getApplicationContext()
contentResolver = context.getContentResolver()
if uri != None and type(uri) != str:
logger.info("Real android.net.Uri found...")
uri = cast("android.net.Uri", uri)
if uri.getScheme().lower() != 'content':
logger.error("Uri scheme not supported: '%s'" % uri.getScheme())
return
if OPERATING_SYSTEM == "Android":
logger.info("Got new intent with action: %s" % str(intent.getAction()))
logger.debug("Raw intent: %s" % str(intent))
if intent.getAction() == "android.intent.action.VIEW":
logger.info("Intent scheme: %s" % intent.getScheme())
logger.info("Intent type: %s" % intent.getType())
logger.info("Intent data: %s" % intent.getData())
logger.info("Intent path: %s" % intent.getData().getPath())

uri = intent.getData()
context = mActivity.getApplicationContext()
contentResolver = context.getContentResolver()

cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):
os.remove(cacheFile)
J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
logger.debug(f"Writing file at '{uri.getPath()}' to '{cacheFile}'...")
bytecount = J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
logger.debug(f"{bytecount} bytes copied...")
self.openFile(cacheFile)
os.remove(cacheFile)
else:
logger.info("Str based uri found...")
cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):

"""
logger.info("Intent uri: %s" % intent.getParcelableExtra(J_Intent.EXTRA_STREAM))
logger.info("Intent text: %s" % intent.getStringExtra(J_Intent.EXTRA_TEXT))
uri = intent.getParcelableExtra(J_Intent.EXTRA_STREAM)
context = mActivity.getApplicationContext()
contentResolver = context.getContentResolver()
if uri != None and type(uri) != str:
logger.info("Real android.net.Uri found...")
uri = cast("android.net.Uri", uri)
if uri.getScheme().lower() != 'content':
logger.error("Uri scheme not supported: '%s'" % uri.getScheme())
return
cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):
os.remove(cacheFile)
J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
self.openFile(cacheFile)
os.remove(cacheFile)
J_FileUtils.copy(contentResolver.openInputStream(intent.getData()), J_FileOutputStream(cacheFile))
self.openFile(cacheFile)
os.remove(cacheFile)
"""
else:
logger.info("Str based uri found...")
cacheFile = Paths.get_cache_filepath("intent.file")
if os.path.exists(cacheFile):
os.remove(cacheFile)
J_FileUtils.copy(contentResolver.openInputStream(intent.getData()), J_FileOutputStream(cacheFile))
self.openFile(cacheFile)
os.remove(cacheFile)
"""

def selectFile(self, *args):
# Create popup window to select file
Expand Down

0 comments on commit b2210e9

Please sign in to comment.