comparison android/DWindows.kt @ 2790:20d39af27aa4

Android: Add a new function for Android dw_file_open() which will open the URI that dw_file_browse() now saves at the end of the returned path. This is a hacky method of letting things function on Android... since Android seems to not let you open files with a pure path. Even if the file exists it will fail to open, you need to use the ugly Android URIs. Not sure if I will keep this solution or not, but committing it so Dynamic Windows Interface Builder will function on Android in the meantime. dw_file_open() just calls the system open() on non-Android platforms.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 13 Jul 2022 14:57:38 +0000
parents 025b4e3e7e75
children 5c61aba17b69
comparison
equal deleted inserted replaced
2789:025b4e3e7e75 2790:20d39af27aa4
26 import android.os.* 26 import android.os.*
27 import android.print.* 27 import android.print.*
28 import android.print.pdf.PrintedPdfDocument 28 import android.print.pdf.PrintedPdfDocument
29 import android.provider.DocumentsContract 29 import android.provider.DocumentsContract
30 import android.provider.MediaStore 30 import android.provider.MediaStore
31 import android.system.OsConstants
31 import android.text.InputFilter 32 import android.text.InputFilter
32 import android.text.InputFilter.LengthFilter 33 import android.text.InputFilter.LengthFilter
33 import android.text.InputType 34 import android.text.InputType
34 import android.text.method.PasswordTransformationMethod 35 import android.text.method.PasswordTransformationMethod
35 import android.util.* 36 import android.util.*
58 import androidx.recyclerview.widget.RecyclerView 59 import androidx.recyclerview.widget.RecyclerView
59 import androidx.viewpager2.widget.ViewPager2 60 import androidx.viewpager2.widget.ViewPager2
60 import com.google.android.material.tabs.TabLayout 61 import com.google.android.material.tabs.TabLayout
61 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener 62 import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
62 import com.google.android.material.tabs.TabLayoutMediator 63 import com.google.android.material.tabs.TabLayoutMediator
63 import java.io.BufferedInputStream 64 import java.io.*
64 import java.io.File
65 import java.io.FileOutputStream
66 import java.io.IOException
67 import java.util.* 65 import java.util.*
68 import java.util.concurrent.locks.ReentrantLock 66 import java.util.concurrent.locks.ReentrantLock
69 import java.util.zip.ZipEntry 67 import java.util.zip.ZipEntry
70 import java.util.zip.ZipFile 68 import java.util.zip.ZipFile
71 import kotlin.math.* 69 import kotlin.math.*
2855 windowDefault[index] = default 2853 windowDefault[index] = default
2856 } 2854 }
2857 } 2855 }
2858 } 2856 }
2859 2857
2860 fun windowSetStyle(window: View, style: Int, mask: Int) 2858 fun windowSetStyle(window: Any, style: Int, mask: Int)
2861 { 2859 {
2860 // TODO: Need to handle menu items and others
2862 waitOnUiThread { 2861 waitOnUiThread {
2863 if (window is TextView && window !is EditText) { 2862 if (window is TextView && window !is EditText) {
2864 val ourmask = (Gravity.HORIZONTAL_GRAVITY_MASK or Gravity.VERTICAL_GRAVITY_MASK) and mask 2863 val ourmask = (Gravity.HORIZONTAL_GRAVITY_MASK or Gravity.VERTICAL_GRAVITY_MASK) and mask
2865 2864
2866 if (ourmask != 0) { 2865 if (ourmask != 0) {
6098 cursor?.close() 6097 cursor?.close()
6099 } 6098 }
6100 return null 6099 return null
6101 } 6100 }
6102 6101
6102 fun fileOpen(filename: String, mode: Int): Int
6103 {
6104 var retval: Int = -1
6105 var uri = Uri.parse(filename)
6106 var smode: String = "r"
6107 var fd: ParcelFileDescriptor? = null
6108
6109 if((mode and OsConstants.O_WRONLY) == OsConstants.O_WRONLY) {
6110 smode = "w"
6111 } else if((mode and OsConstants.O_RDWR) == OsConstants.O_RDWR) {
6112 smode = "rw"
6113 }
6114 try {
6115 fd = contentResolver.openFileDescriptor(uri, smode)
6116 } catch (e: FileNotFoundException) {
6117 fd = null
6118 }
6119 if (fd != null) {
6120 retval = fd.fd
6121 }
6122 return retval
6123 }
6124
6103 // Defpath does not seem to be supported on Android using the ACTION_GET_CONTENT Intent 6125 // Defpath does not seem to be supported on Android using the ACTION_GET_CONTENT Intent
6104 fun fileBrowseNew(title: String, defpath: String?, ext: String?, flags: Int): String? 6126 fun fileBrowseNew(title: String, defpath: String?, ext: String?, flags: Int): String?
6105 { 6127 {
6106 var retval: String? = null 6128 var retval: String? = null
6129 var uristr: String? = null
6107 var permission = Manifest.permission.WRITE_EXTERNAL_STORAGE 6130 var permission = Manifest.permission.WRITE_EXTERNAL_STORAGE
6108 var permissions: Int = -1 6131 var permissions: Int = -1
6109 6132
6110 // Handle requesting permissions if necessary 6133 // Handle requesting permissions if necessary
6111 permissions = ContextCompat.checkSelfPermission(this, permission) 6134 permissions = ContextCompat.checkSelfPermission(this, permission)
6134 6157
6135 if(success) { 6158 if(success) {
6136 // Wait until the intent finishes. 6159 // Wait until the intent finishes.
6137 fileCond.await() 6160 fileCond.await()
6138 fileLock.unlock() 6161 fileLock.unlock()
6162
6163 // Save the URI string for later use
6164 uristr = fileURI.toString()
6139 6165
6140 if (DocumentsContract.isDocumentUri(this, fileURI)) { 6166 if (DocumentsContract.isDocumentUri(this, fileURI)) {
6141 // ExternalStorageProvider 6167 // ExternalStorageProvider
6142 if (fileURI?.authority == "com.android.externalstorage.documents") { 6168 if (fileURI?.authority == "com.android.externalstorage.documents") {
6143 val docId = DocumentsContract.getDocumentId(fileURI) 6169 val docId = DocumentsContract.getDocumentId(fileURI)
6191 } else { 6217 } else {
6192 // If we failed to start the intent... use old dialog 6218 // If we failed to start the intent... use old dialog
6193 fileLock.unlock() 6219 fileLock.unlock()
6194 retval = fileBrowse(title, defpath, ext, flags) 6220 retval = fileBrowse(title, defpath, ext, flags)
6195 } 6221 }
6222 }
6223 if(retval != null && uristr != null) {
6224 return retval + "\n\n" + uristr
6196 } 6225 }
6197 return retval 6226 return retval
6198 } 6227 }
6199 6228
6200 fun fileBrowse(title: String, defpath: String?, ext: String?, flags: Int): String? 6229 fun fileBrowse(title: String, defpath: String?, ext: String?, flags: Int): String?