Skip to content

Commit b2210e9

Browse files
Fix: [MMCA, MMLV] Fix intent, fixes #25, fixes #23
1 parent 8eeb22c commit b2210e9

2 files changed

Lines changed: 102 additions & 94 deletions

File tree

src/MobileCrashAnalyzer/ui/main_window.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616
from jnius import autoclass, cast
1717

1818
# Just import if the os is Android to avoid Android peculiarities
19-
if platform == "android":
19+
try:
2020
from android import activity, mActivity, permissions
2121
J_FileOutputStream = autoclass("java.io.FileOutputStream")
2222
J_FileUtils = autoclass("android.os.FileUtils")
2323
#J_Intent = autoclass("android.content.Intent")
2424
#J_PythonActivity = autoclass('org.kivy.android.PythonActivity')
2525
permissions.request_permissions([permissions.Permission.READ_EXTERNAL_STORAGE, permissions.Permission.WRITE_EXTERNAL_STORAGE])
26+
OPERATING_SYSTEM = "Android"
27+
except:
28+
OPERATING_SYSTEM = None
2629

2730
from shared.storage import CrashReport, Rawlog
2831
from shared.utils import Paths
@@ -60,8 +63,8 @@ def build(self):
6063
self.layout.add_widget(self.uiActionBar)
6164
self.layout.add_widget(self.uiTextInput)
6265

63-
# Just import if the os is Android to avoid Android peculiarities
64-
if platform == "android":
66+
# Use if the os is Android to avoid Android peculiarities
67+
if OPERATING_SYSTEM == "Android":
6568
activity.bind(on_new_intent=self.on_new_intent)
6669

6770
return self.layout
@@ -70,8 +73,8 @@ def quit(self, *args):
7073
self.stop()
7174

7275
def on_start(self, *args):
73-
# Just import if the os is Android to avoid Android peculiarities
74-
if platform == "android":
76+
# Use if the os is Android to avoid Android peculiarities
77+
if OPERATING_SYSTEM == "Android":
7578
context = cast('android.content.Context', mActivity.getApplicationContext())
7679
logger.info(f"Startup application context: {context}")
7780
intent = mActivity.getIntent()
@@ -82,54 +85,55 @@ def on_start(self, *args):
8285
#see https://github.com/termux/termux-app/blob/74b23cb2096652601050d0f4951f9fb92577743c/app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java#L70
8386
@mainthread
8487
def on_new_intent(self, intent):
85-
logger.info("Got new intent with action: %s" % str(intent.getAction()))
86-
logger.debug("Raw intent: %s" % str(intent))
87-
if intent.getAction() == "android.intent.action.VIEW":
88-
logger.info("Intent scheme: %s" % intent.getScheme())
89-
logger.info("Intent type: %s" % intent.getType())
90-
logger.info("Intent data: %s" % intent.getData())
91-
logger.info("Intent path: %s" % intent.getData().getPath())
92-
93-
uri = intent.getData()
94-
context = mActivity.getApplicationContext()
95-
contentResolver = context.getContentResolver()
96-
97-
cacheFile = Paths.get_cache_filepath("intent.file")
98-
if os.path.exists(cacheFile):
99-
os.remove(cacheFile)
100-
logger.debug(f"Writing file at '{uri.getPath()}' to '{cacheFile}'...")
101-
bytecount = J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
102-
logger.debug(f"{bytecount} bytes copied...")
103-
self.openFile(cacheFile)
104-
os.remove(cacheFile)
105-
106-
"""
107-
logger.info("Intent uri: %s" % intent.getParcelableExtra(J_Intent.EXTRA_STREAM))
108-
logger.info("Intent text: %s" % intent.getStringExtra(J_Intent.EXTRA_TEXT))
109-
uri = intent.getParcelableExtra(J_Intent.EXTRA_STREAM)
110-
context = mActivity.getApplicationContext()
111-
contentResolver = context.getContentResolver()
112-
if uri != None and type(uri) != str:
113-
logger.info("Real android.net.Uri found...")
114-
uri = cast("android.net.Uri", uri)
115-
if uri.getScheme().lower() != 'content':
116-
logger.error("Uri scheme not supported: '%s'" % uri.getScheme())
117-
return
88+
if OPERATING_SYSTEM == "Android":
89+
logger.info("Got new intent with action: %s" % str(intent.getAction()))
90+
logger.debug("Raw intent: %s" % str(intent))
91+
if intent.getAction() == "android.intent.action.VIEW":
92+
logger.info("Intent scheme: %s" % intent.getScheme())
93+
logger.info("Intent type: %s" % intent.getType())
94+
logger.info("Intent data: %s" % intent.getData())
95+
logger.info("Intent path: %s" % intent.getData().getPath())
96+
97+
uri = intent.getData()
98+
context = mActivity.getApplicationContext()
99+
contentResolver = context.getContentResolver()
100+
118101
cacheFile = Paths.get_cache_filepath("intent.file")
119102
if os.path.exists(cacheFile):
120103
os.remove(cacheFile)
121-
J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
104+
logger.debug(f"Writing file at '{uri.getPath()}' to '{cacheFile}'...")
105+
bytecount = J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
106+
logger.debug(f"{bytecount} bytes copied...")
122107
self.openFile(cacheFile)
123108
os.remove(cacheFile)
124-
else:
125-
logger.info("Str based uri found...")
126-
cacheFile = Paths.get_cache_filepath("intent.file")
127-
if os.path.exists(cacheFile):
109+
110+
"""
111+
logger.info("Intent uri: %s" % intent.getParcelableExtra(J_Intent.EXTRA_STREAM))
112+
logger.info("Intent text: %s" % intent.getStringExtra(J_Intent.EXTRA_TEXT))
113+
uri = intent.getParcelableExtra(J_Intent.EXTRA_STREAM)
114+
context = mActivity.getApplicationContext()
115+
contentResolver = context.getContentResolver()
116+
if uri != None and type(uri) != str:
117+
logger.info("Real android.net.Uri found...")
118+
uri = cast("android.net.Uri", uri)
119+
if uri.getScheme().lower() != 'content':
120+
logger.error("Uri scheme not supported: '%s'" % uri.getScheme())
121+
return
122+
cacheFile = Paths.get_cache_filepath("intent.file")
123+
if os.path.exists(cacheFile):
124+
os.remove(cacheFile)
125+
J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
126+
self.openFile(cacheFile)
128127
os.remove(cacheFile)
129-
J_FileUtils.copy(contentResolver.openInputStream(intent.getData()), J_FileOutputStream(cacheFile))
130-
self.openFile(cacheFile)
131-
os.remove(cacheFile)
132-
"""
128+
else:
129+
logger.info("Str based uri found...")
130+
cacheFile = Paths.get_cache_filepath("intent.file")
131+
if os.path.exists(cacheFile):
132+
os.remove(cacheFile)
133+
J_FileUtils.copy(contentResolver.openInputStream(intent.getData()), J_FileOutputStream(cacheFile))
134+
self.openFile(cacheFile)
135+
os.remove(cacheFile)
136+
"""
133137

134138
def openFile(self, *args):
135139
logger.debug("Create file select popup dialog...")

src/MobileLogViewer/ui/main_window.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
from jnius import autoclass, cast
1818

1919
# Just import if the os is Android to avoid Android peculiarities
20-
if platform == "android":
20+
try:
2121
from android import activity, mActivity, permissions
2222
J_FileOutputStream = autoclass("java.io.FileOutputStream")
2323
J_FileUtils = autoclass("android.os.FileUtils")
2424
#J_Intent = autoclass("android.content.Intent")
2525
#J_PythonActivity = autoclass('org.kivy.android.PythonActivity')
2626
permissions.request_permissions([permissions.Permission.READ_EXTERNAL_STORAGE, permissions.Permission.WRITE_EXTERNAL_STORAGE])
27+
OPERATING_SYSTEM = "Android"
28+
except:
29+
OPERATING_SYSTEM = None
2730

2831
from shared.utils.constants import LOGLEVELS
2932
from shared.storage import Rawlog
@@ -85,8 +88,8 @@ def build(self):
8588
self.layout.add_widget(gridLayout_menueBar)
8689
self.layout.add_widget(self.uiScrollWidget_logs)
8790

88-
# Just import if the os is Android to avoid Android peculiarities
89-
if platform == "android":
91+
# Use if the os is Android to avoid Android peculiarities
92+
if OPERATING_SYSTEM == "Android":
9093
activity.bind(on_new_intent=self.on_new_intent)
9194

9295
return self.layout
@@ -95,8 +98,8 @@ def quit(self, *args):
9598
self.stop()
9699

97100
def on_start(self, *args):
98-
# Just import if the os is Android to avoid Android peculiarities
99-
if platform == "android":
101+
# Use if the os is Android to avoid Android peculiarities
102+
if OPERATING_SYSTEM == "Android":
100103
context = cast('android.content.Context', mActivity.getApplicationContext())
101104
logger.info(f"Startup application context: {context}")
102105
intent = mActivity.getIntent()
@@ -107,54 +110,55 @@ def on_start(self, *args):
107110
#see https://github.com/termux/termux-app/blob/74b23cb2096652601050d0f4951f9fb92577743c/app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java#L70
108111
@mainthread
109112
def on_new_intent(self, intent):
110-
logger.info("Got new intent with action: %s" % str(intent.getAction()))
111-
logger.debug("Raw intent: %s" % str(intent))
112-
if intent.getAction() == "android.intent.action.VIEW":
113-
logger.info("Intent scheme: %s" % intent.getScheme())
114-
logger.info("Intent type: %s" % intent.getType())
115-
logger.info("Intent data: %s" % intent.getData())
116-
logger.info("Intent path: %s" % intent.getData().getPath())
117-
118-
uri = intent.getData()
119-
context = mActivity.getApplicationContext()
120-
contentResolver = context.getContentResolver()
121-
122-
cacheFile = Paths.get_cache_filepath("intent.file")
123-
if os.path.exists(cacheFile):
124-
os.remove(cacheFile)
125-
logger.debug(f"Writing file at '{uri.getPath()}' to '{cacheFile}'...")
126-
bytecount = J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
127-
logger.debug(f"{bytecount} bytes copied...")
128-
self.openFile(cacheFile)
129-
os.remove(cacheFile)
130-
131-
"""
132-
logger.info("Intent uri: %s" % intent.getParcelableExtra(J_Intent.EXTRA_STREAM))
133-
logger.info("Intent text: %s" % intent.getStringExtra(J_Intent.EXTRA_TEXT))
134-
uri = intent.getParcelableExtra(J_Intent.EXTRA_STREAM)
135-
context = mActivity.getApplicationContext()
136-
contentResolver = context.getContentResolver()
137-
if uri != None and type(uri) != str:
138-
logger.info("Real android.net.Uri found...")
139-
uri = cast("android.net.Uri", uri)
140-
if uri.getScheme().lower() != 'content':
141-
logger.error("Uri scheme not supported: '%s'" % uri.getScheme())
142-
return
113+
if OPERATING_SYSTEM == "Android":
114+
logger.info("Got new intent with action: %s" % str(intent.getAction()))
115+
logger.debug("Raw intent: %s" % str(intent))
116+
if intent.getAction() == "android.intent.action.VIEW":
117+
logger.info("Intent scheme: %s" % intent.getScheme())
118+
logger.info("Intent type: %s" % intent.getType())
119+
logger.info("Intent data: %s" % intent.getData())
120+
logger.info("Intent path: %s" % intent.getData().getPath())
121+
122+
uri = intent.getData()
123+
context = mActivity.getApplicationContext()
124+
contentResolver = context.getContentResolver()
125+
143126
cacheFile = Paths.get_cache_filepath("intent.file")
144127
if os.path.exists(cacheFile):
145128
os.remove(cacheFile)
146-
J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
129+
logger.debug(f"Writing file at '{uri.getPath()}' to '{cacheFile}'...")
130+
bytecount = J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
131+
logger.debug(f"{bytecount} bytes copied...")
147132
self.openFile(cacheFile)
148133
os.remove(cacheFile)
149-
else:
150-
logger.info("Str based uri found...")
151-
cacheFile = Paths.get_cache_filepath("intent.file")
152-
if os.path.exists(cacheFile):
134+
135+
"""
136+
logger.info("Intent uri: %s" % intent.getParcelableExtra(J_Intent.EXTRA_STREAM))
137+
logger.info("Intent text: %s" % intent.getStringExtra(J_Intent.EXTRA_TEXT))
138+
uri = intent.getParcelableExtra(J_Intent.EXTRA_STREAM)
139+
context = mActivity.getApplicationContext()
140+
contentResolver = context.getContentResolver()
141+
if uri != None and type(uri) != str:
142+
logger.info("Real android.net.Uri found...")
143+
uri = cast("android.net.Uri", uri)
144+
if uri.getScheme().lower() != 'content':
145+
logger.error("Uri scheme not supported: '%s'" % uri.getScheme())
146+
return
147+
cacheFile = Paths.get_cache_filepath("intent.file")
148+
if os.path.exists(cacheFile):
149+
os.remove(cacheFile)
150+
J_FileUtils.copy(contentResolver.openInputStream(uri), J_FileOutputStream(cacheFile))
151+
self.openFile(cacheFile)
153152
os.remove(cacheFile)
154-
J_FileUtils.copy(contentResolver.openInputStream(intent.getData()), J_FileOutputStream(cacheFile))
155-
self.openFile(cacheFile)
156-
os.remove(cacheFile)
157-
"""
153+
else:
154+
logger.info("Str based uri found...")
155+
cacheFile = Paths.get_cache_filepath("intent.file")
156+
if os.path.exists(cacheFile):
157+
os.remove(cacheFile)
158+
J_FileUtils.copy(contentResolver.openInputStream(intent.getData()), J_FileOutputStream(cacheFile))
159+
self.openFile(cacheFile)
160+
os.remove(cacheFile)
161+
"""
158162

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

0 commit comments

Comments
 (0)