changeset 2595:6b5057dd6b8e

Android: Experimental change for the new file browser... require it be on secondary... thread, and use condition for the UI thread to trigger execution on result.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 28 May 2021 18:28:15 +0000
parents 2c15b3d41fe4
children 60ec91d23746
files android/DWindows.kt
diffstat 1 files changed, 28 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Fri May 28 01:02:07 2021 +0000
+++ b/android/DWindows.kt	Fri May 28 18:28:15 2021 +0000
@@ -742,6 +742,8 @@
     private var menuBar: DWMenu? = null
     private var defaultItem: View? = null
     private var fileURI: Uri? = null
+    private var fileLock = ReentrantLock()
+    private var fileCond = threadLock.newCondition()
 
     // Our version of runOnUiThread that waits for execution
     fun waitOnUiThread(runnable: Runnable)
@@ -3225,9 +3227,15 @@
 
     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
         super.onActivityResult(requestCode, resultCode, data)
-        if(requestCode == 100 && resultCode == Activity.RESULT_OK) {
-            fileURI = data!!.data
-            throw java.lang.RuntimeException()
+        if(requestCode == 100) {
+            fileLock.lock()
+            if(resultCode == Activity.RESULT_OK) {
+                fileURI = data!!.data
+            } else {
+                fileURI = null
+            }
+            fileCond.signal()
+            fileLock.unlock()
         }
     }
 
@@ -3235,20 +3243,24 @@
     {
         var retval: String? = null
 
-        waitOnUiThread {
-            val fileintent = Intent(Intent.ACTION_GET_CONTENT)
-            fileintent.type = "text/plain"
-            fileintent.addCategory(Intent.CATEGORY_OPENABLE)
-            startActivityForResult(fileintent, 100)
+        // This can't be called from the main thread
+        if(Looper.getMainLooper() != Looper.myLooper()) {
+            fileLock.lock()
+            waitOnUiThread {
+                val fileintent = Intent(Intent.ACTION_GET_CONTENT)
+                fileintent.type = "text/plain"
+                fileintent.addCategory(Intent.CATEGORY_OPENABLE)
+                startActivityForResult(fileintent, 100)
+            }
+
+            // Wait until the intent finishes.
+            fileCond.await()
+            fileLock.unlock()
+
+            if(fileURI != null) {
+                retval = getUriRealPath(this, fileURI)
+            }
         }
-
-        // loop till a runtime exception is triggered.
-        try {
-            Looper.loop()
-        } catch (e2: RuntimeException) {
-        }
-
-        retval = getUriRealPath(this, fileURI)
         return retval
     }