@@ -419,26 +419,21 @@ suspend fun getWallpaperFromFolder(folderUri: String, context: Context): List<Wa
419
419
}
420
420
421
421
/* *
422
- * Get the last modified date of a folder
423
- * @param folderUri URI of the folder
424
- * @param context Android context
422
+ * Get the folder name from the folder URI
425
423
*/
426
- suspend fun getFolderLastModified (folderUri : String , context : Context ): Long = withContext(Dispatchers .IO ) {
427
- val contentResolver = context.contentResolver
428
- val projection = arrayOf(
429
- DocumentsContract .Document .COLUMN_LAST_MODIFIED ,
430
- )
431
- val childrenUri = DocumentsContract .buildChildDocumentsUriUsingTree(
432
- folderUri.toUri(),
433
- DocumentsContract .getTreeDocumentId(folderUri.toUri())
434
- )
435
- val cursor = contentResolver.query(childrenUri, projection, null , null , null )
436
- cursor?.use {
437
- if (it.moveToFirst()) {
438
- return @use it.getLong(it.getColumnIndexOrThrow(DocumentsContract .Document .COLUMN_LAST_MODIFIED ))
424
+ suspend fun getFolderMetadata (folderUri : String , context : Context ): Metadata = withContext(Dispatchers .IO ) {
425
+ return @withContext try {
426
+ val uri = folderUri.toUri()
427
+ try {
428
+ val file = DocumentFileCompat .fromTreeUri(context, uri)
429
+ Metadata (file?.name?.substringBeforeLast(' .' , file.name).toString(), file?.lastModified ? : 0 )
430
+ } catch (_: Exception ) {
431
+ val file = DocumentFile .fromTreeUri(context, uri)
432
+ Metadata (file?.name?.substringBeforeLast(' .' , file.name.toString()) ? : " " , file?.lastModified() ? : 0 )
439
433
}
434
+ } catch (_: Exception ) {
435
+ Metadata (" " , 0 )
440
436
}
441
- return @withContext 0
442
437
}
443
438
444
439
/* *
@@ -451,13 +446,13 @@ suspend fun getImageMetadata(context: Context, uriString: String): Metadata = wi
451
446
val uri = uriString.toUri()
452
447
try {
453
448
val file = DocumentFileCompat .fromSingleUri(context, uri)
454
- return @withContext Metadata (file?.name?.substringBeforeLast(' .' , file.name).toString(), file?.lastModified ? : 0 )
449
+ Metadata (file?.name?.substringBeforeLast(' .' , file.name).toString(), file?.lastModified ? : 0 )
455
450
} catch (_: Exception ) {
456
451
val file = DocumentFile .fromSingleUri(context, uri)
457
- return @withContext Metadata (file?.name?.substringBeforeLast(' .' , file.name.toString()) ? : " " , file?.lastModified() ? : 0 )
452
+ Metadata (file?.name?.substringBeforeLast(' .' , file.name.toString()) ? : " " , file?.lastModified() ? : 0 )
458
453
}
459
454
} catch (_: Exception ) {
460
- return @withContext Metadata (" " , 0 )
455
+ Metadata (" " , 0 )
461
456
}
462
457
}
463
458
@@ -482,33 +477,24 @@ suspend fun findFirstValidUri(
482
477
return @withContext null
483
478
}
484
479
485
- /* *
486
- * Get the folder name from the folder URI
487
- */
488
- suspend fun getFolderNameFromUri (folderUri : String , context : Context ): String? = withContext(Dispatchers .IO ) {
489
- return @withContext try {
490
- DocumentFileCompat .fromTreeUri(context, folderUri.toUri())?.name
491
- } catch (_: Exception ) {
492
- DocumentFile .fromTreeUri(context, folderUri.toUri())?.name
493
- }
494
- }
495
-
496
480
/* *
497
481
* Check if a URI is valid
498
482
*/
499
483
fun isValidUri (context : Context , uriString : String? ): Boolean {
500
484
val uri = uriString?.decompress(" content://com.android.externalstorage.documents/" )?.toUri()
501
- if (uri == null ) { return false }
485
+ ? : return false
502
486
return try {
503
487
DocumentFileCompat .fromSingleUri(context, uri)?.exists() ? : false
504
488
} catch (_: Exception ) {
505
489
DocumentFile .fromSingleUri(context, uri)?.exists() ? : false
506
490
}
507
491
}
508
492
493
+ /* *
494
+ * Check if a URI is a directory
495
+ */
509
496
fun isDirectory (context : Context , uriString : String? ): Boolean {
510
- val uri = uriString?.toUri()
511
- if (uri == null ) { return false }
497
+ val uri = uriString?.toUri() ? : return false
512
498
return try {
513
499
DocumentFileCompat .fromSingleUri(context, uri)?.isDirectory() ? : false
514
500
} catch (_: Exception ) {
0 commit comments