Mercurial > dwindows
comparison android/DWindows.kt @ 2795:5c61aba17b69
Android: Change dw_file_browse() to return URIs or paths on Android.
Paths will be returned if either DW_DIRECTORY_OPEN or DW_FILE_PATH flags are
specified. Otherwise a URI may be returned. The double string method of
returning both path and URI has been removed. DW_FILE_PATH and DW_FILE_MASK
have been added, but are really only used on Android.
__DW_MOBILE__ will be defined on Mobile platforms such as iOS and Android.
__DW_DESKTOP__ will be defined on most other desktop operating systems.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Fri, 15 Jul 2022 11:50:09 +0000 |
parents | 20d39af27aa4 |
children | 0c534743b7a9 |
comparison
equal
deleted
inserted
replaced
2794:7ce51a7e8009 | 2795:5c61aba17b69 |
---|---|
6124 | 6124 |
6125 // 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 |
6126 fun fileBrowseNew(title: String, defpath: String?, ext: String?, flags: Int): String? | 6126 fun fileBrowseNew(title: String, defpath: String?, ext: String?, flags: Int): String? |
6127 { | 6127 { |
6128 var retval: String? = null | 6128 var retval: String? = null |
6129 var uristr: String? = null | |
6130 var permission = Manifest.permission.WRITE_EXTERNAL_STORAGE | 6129 var permission = Manifest.permission.WRITE_EXTERNAL_STORAGE |
6131 var permissions: Int = -1 | 6130 var permissions: Int = -1 |
6132 | 6131 |
6133 // Handle requesting permissions if necessary | 6132 // Handle requesting permissions if necessary |
6134 permissions = ContextCompat.checkSelfPermission(this, permission) | 6133 permissions = ContextCompat.checkSelfPermission(this, permission) |
6159 // Wait until the intent finishes. | 6158 // Wait until the intent finishes. |
6160 fileCond.await() | 6159 fileCond.await() |
6161 fileLock.unlock() | 6160 fileLock.unlock() |
6162 | 6161 |
6163 // Save the URI string for later use | 6162 // Save the URI string for later use |
6164 uristr = fileURI.toString() | 6163 retval = fileURI.toString() |
6165 | 6164 |
6166 if (DocumentsContract.isDocumentUri(this, fileURI)) { | 6165 // If DW_DIRECTORY_OPEN or DW_FILE_PATH ... use the path not URI |
6167 // ExternalStorageProvider | 6166 if((flags and 65535) == 2 || ((flags shr 16) and 1) == 1) { |
6168 if (fileURI?.authority == "com.android.externalstorage.documents") { | 6167 if (DocumentsContract.isDocumentUri(this, fileURI)) { |
6169 val docId = DocumentsContract.getDocumentId(fileURI) | 6168 // ExternalStorageProvider |
6170 val split = docId.split(":").toTypedArray() | 6169 if (fileURI?.authority == "com.android.externalstorage.documents") { |
6171 retval = Environment.getExternalStorageDirectory().toString() + "/" + split[1] | 6170 val docId = DocumentsContract.getDocumentId(fileURI) |
6172 } else if (fileURI?.authority == "com.android.providers.downloads.documents") { | 6171 val split = docId.split(":").toTypedArray() |
6173 val id = DocumentsContract.getDocumentId(fileURI) | 6172 retval = Environment.getExternalStorageDirectory() |
6174 val contentUri = ContentUris.withAppendedId( | 6173 .toString() + "/" + split[1] |
6175 Uri.parse("content://downloads/public_downloads"), | 6174 } else if (fileURI?.authority == "com.android.providers.downloads.documents") { |
6176 java.lang.Long.valueOf(id) | 6175 val id = DocumentsContract.getDocumentId(fileURI) |
6177 ) | 6176 val contentUri = ContentUris.withAppendedId( |
6178 retval = getDataColumn(this, contentUri, null, null) | 6177 Uri.parse("content://downloads/public_downloads"), |
6179 } else if (fileURI?.authority == "com.android.providers.media.documents") { | 6178 java.lang.Long.valueOf(id) |
6180 val docId = DocumentsContract.getDocumentId(fileURI) | 6179 ) |
6181 val split = docId.split(":").toTypedArray() | 6180 retval = getDataColumn(this, contentUri, null, null) |
6182 val type = split[0] | 6181 } else if (fileURI?.authority == "com.android.providers.media.documents") { |
6183 var contentUri: Uri? = null | 6182 val docId = DocumentsContract.getDocumentId(fileURI) |
6184 if ("image" == type) { | 6183 val split = docId.split(":").toTypedArray() |
6185 contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI | 6184 val type = split[0] |
6186 } else if ("video" == type) { | 6185 var contentUri: Uri? = null |
6187 contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI | 6186 if ("image" == type) { |
6188 } else if ("audio" == type) { | 6187 contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI |
6189 contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | 6188 } else if ("video" == type) { |
6189 contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI | |
6190 } else if ("audio" == type) { | |
6191 contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | |
6192 } | |
6193 val selection = "_id=?" | |
6194 val selectionArgs = arrayOf<String?>( | |
6195 split[1] | |
6196 ) | |
6197 retval = getDataColumn(this, contentUri, selection, selectionArgs) | |
6190 } | 6198 } |
6191 val selection = "_id=?" | 6199 } else if (fileURI?.scheme == "content") { |
6192 val selectionArgs = arrayOf<String?>( | 6200 retval = getDataColumn(this, fileURI, null, null) |
6193 split[1] | 6201 } |
6194 ) | 6202 // File |
6195 retval = getDataColumn(this, contentUri, selection, selectionArgs) | 6203 else if (fileURI?.scheme == "file") { |
6196 } | 6204 retval = fileURI?.path |
6197 } else if (fileURI?.scheme == "content") { | 6205 } |
6198 retval = getDataColumn(this, fileURI, null, null) | 6206 |
6199 } | 6207 // If we are opening a directory DW_DIRECTORY_OPEN |
6200 // File | 6208 if (retval != null && (flags and 65535) == 2) { |
6201 else if (fileURI?.scheme == "file") { | 6209 val split = retval.split("/") |
6202 retval = fileURI?.path | 6210 val filename = split.last() |
6203 } | 6211 |
6204 | 6212 if (filename != null) { |
6205 // If we are opening a directory DW_DIRECTORY_OPEN | 6213 val pathlen = retval.length |
6206 if(retval != null && flags == 2) { | 6214 val filelen = filename.length |
6207 val split = retval.split("/") | 6215 |
6208 val filename = split.last() | 6216 retval = retval.substring(0, pathlen - filelen - 1) |
6209 | 6217 } |
6210 if(filename != null) { | |
6211 val pathlen = retval.length | |
6212 val filelen = filename.length | |
6213 | |
6214 retval = retval.substring(0, pathlen - filelen - 1) | |
6215 } | 6218 } |
6216 } | 6219 } |
6217 } else { | 6220 } else { |
6218 // If we failed to start the intent... use old dialog | 6221 // If we failed to start the intent... use old dialog |
6219 fileLock.unlock() | 6222 fileLock.unlock() |
6220 retval = fileBrowse(title, defpath, ext, flags) | 6223 retval = fileBrowse(title, defpath, ext, flags) |
6221 } | 6224 } |
6222 } | |
6223 if(retval != null && uristr != null) { | |
6224 return retval + "\n\n" + uristr | |
6225 } | 6225 } |
6226 return retval | 6226 return retval |
6227 } | 6227 } |
6228 | 6228 |
6229 fun fileBrowse(title: String, defpath: String?, ext: String?, flags: Int): String? | 6229 fun fileBrowse(title: String, defpath: String?, ext: String?, flags: Int): String? |