1616from 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
2730from shared .storage import CrashReport , Rawlog
2831from 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..." )
0 commit comments