@@ -33,13 +33,17 @@ This program (the AndroidFilePickerLight library) is free software written by
3333
3434import androidx .core .content .FileProvider ;
3535
36+ import org .apache .commons .lang3 .ArrayUtils ;
37+
3638import java .io .File ;
3739import java .io .FileNotFoundException ;
3840import java .io .IOException ;
41+ import java .util .ArrayList ;
3942import java .util .Collections ;
4043import java .util .Comparator ;
4144import java .util .HashSet ;
4245import java .util .LinkedList ;
46+ import java .util .List ;
4347import java .util .Locale ;
4448import java .util .PriorityQueue ;
4549import java .util .Set ;
@@ -188,6 +192,13 @@ public boolean onCreate() {
188192 return true ;
189193 }
190194
195+ public static final String ROOT_COLUMN_NAME_ABSPATH = "Root.CustomColumn.AbsolutePath" ;
196+ public static final String DOCUMENT_COLUMN_NAME_ABSPATH = "Document.CustomColumn.AbsolutePath" ;
197+ public static final String DOCUMENT_COLUMN_NAME_FILE_SIZE_LABEL = "Document.CustomColumn.FileSizeLabel" ;
198+ public static final String DOCUMENT_COLUMN_NAME_POSIX_PERMS = "Document.CustomColumn.PosixPerms" ;
199+ public static final String DOCUMENT_COLUMN_NAME_ISDIR = "Document.CustomColumn.IsDir" ;
200+ public static final String DOCUMENT_COLUMN_NAME_ISHIDDEN = "Document.CustomColumn.IsHidden" ;
201+
191202 public static final String [] DEFAULT_ROOT_PROJECTION = new String [] {
192203 DocumentsContract .Root .COLUMN_ROOT_ID ,
193204 DocumentsContract .Root .COLUMN_MIME_TYPES ,
@@ -196,7 +207,8 @@ public boolean onCreate() {
196207 DocumentsContract .Root .COLUMN_TITLE ,
197208 DocumentsContract .Root .COLUMN_SUMMARY ,
198209 DocumentsContract .Root .COLUMN_DOCUMENT_ID ,
199- DocumentsContract .Root .COLUMN_AVAILABLE_BYTES
210+ DocumentsContract .Root .COLUMN_AVAILABLE_BYTES ,
211+ ROOT_COLUMN_NAME_ABSPATH
200212 };
201213
202214 public static final String [] DEFAULT_DOCUMENT_PROJECTION = new String [] {
@@ -205,12 +217,16 @@ public boolean onCreate() {
205217 DocumentsContract .Document .COLUMN_DISPLAY_NAME ,
206218 DocumentsContract .Document .COLUMN_LAST_MODIFIED ,
207219 DocumentsContract .Document .COLUMN_FLAGS ,
208- DocumentsContract .Document .COLUMN_SIZE
220+ DocumentsContract .Document .COLUMN_SIZE ,
221+ DOCUMENT_COLUMN_NAME_ABSPATH ,
222+ DOCUMENT_COLUMN_NAME_FILE_SIZE_LABEL ,
223+ DOCUMENT_COLUMN_NAME_POSIX_PERMS ,
224+ DOCUMENT_COLUMN_NAME_ISDIR ,
225+ DOCUMENT_COLUMN_NAME_ISHIDDEN
209226 };
210227
211228 public static final int ROOT_PROJ_ROOTID_COLUMN_INDEX = 6 ;
212- public static final int ROOT_PROJ_DOCID_COLUMN_INDEX = 6 ;
213- public static final int DOCS_PROJ_PARENTID_COLUMN_INDEX = 0 ;
229+ public static final int DOCS_PROJ_DOCID_COLUMN_INDEX = 0 ;
214230
215231 @ Override
216232 public Cursor queryRoots (String [] projection ) throws FileNotFoundException {
@@ -231,7 +247,8 @@ public Cursor queryRoots(String[] projection) throws FileNotFoundException {
231247 row .add (DocumentsContract .Root .COLUMN_MIME_TYPES , getChildMimeTypes (baseDirPath ));
232248 row .add (DocumentsContract .Root .COLUMN_AVAILABLE_BYTES , baseDirPath != null ? baseDirPath .getFreeSpace () : 0 );
233249 row .add (DocumentsContract .Root .COLUMN_ICON , R .drawable .library_profile_icon_round_background );
234-
250+ // Custom columns:
251+ row .add (ROOT_COLUMN_NAME_ABSPATH , baseDirPath .getAbsolutePath ());
235252 return mcResult ;
236253
237254 }
@@ -527,6 +544,17 @@ private void includeFile(MatrixCursor result, String docId, File file) throws Fi
527544 row .add (DocumentsContract .Document .COLUMN_LAST_MODIFIED , file .lastModified ());
528545 row .add (DocumentsContract .Document .COLUMN_FLAGS , flags );
529546 row .add (DocumentsContract .Document .COLUMN_ICON , R .drawable .library_profile_icon_round_background );
547+ // Custom columns:
548+ row .add (DOCUMENT_COLUMN_NAME_ABSPATH , file .getAbsolutePath ());
549+ row .add (DOCUMENT_COLUMN_NAME_FILE_SIZE_LABEL , FileUtils .getFileSizeString (file ));
550+ try {
551+ row .add (DOCUMENT_COLUMN_NAME_POSIX_PERMS , FileUtils .getFilePosixPermissionsString (file .toPath ()));
552+ } catch (Exception ex ) {
553+ row .add (DOCUMENT_COLUMN_NAME_POSIX_PERMS , "---------" );
554+ }
555+ row .add (DOCUMENT_COLUMN_NAME_ISDIR , file .isDirectory () ? "true" : "false" );
556+ row .add (DOCUMENT_COLUMN_NAME_ISHIDDEN , file .isHidden () ? "true" : "false" );
557+
530558 }
531559
532560 private File getFileForDocId (String docId ) throws FileNotFoundException {
@@ -554,27 +582,18 @@ public static String getDocumentIdForCursorType(MatrixCursor mcResult, boolean c
554582 if (mcResult .getCount () == 0 ) {
555583 return null ;
556584 }
557- String docId = "" ;
585+ String columnName = "" ;
558586 if (cursorType == CURSOR_TYPE_IS_ROOT ) {
559- docId = mcResult . getString ( ROOT_PROJ_ROOTID_COLUMN_INDEX ) ;
587+ columnName = DocumentsContract . Root . COLUMN_DOCUMENT_ID ;
560588 }
561589 else {
562- docId = mcResult .getString (DOCS_PROJ_PARENTID_COLUMN_INDEX );
563- }
564- return docId ;
565- }
566-
567- public File getFileAtCurrentRow (MatrixCursor mcResult , boolean cursorType ) {
568- if (mcResult .getCount () == 0 ) {
569- return null ;
590+ columnName = DocumentsContract .Document .COLUMN_DOCUMENT_ID ;
570591 }
571- String docId = getDocumentIdForCursorType (mcResult , cursorType );
572- try {
573- return getFileForDocId (docId );
574- } catch (IOException ioe ) {
575- ioe .printStackTrace ();
576- return null ;
592+ int docIdColumnIndex = ArrayUtils .indexOf (mcResult .getColumnNames (), columnName );
593+ if (docIdColumnIndex >= 0 ) {
594+ return mcResult .getString (docIdColumnIndex );
577595 }
596+ return null ;
578597 }
579598
580599 public String getAbsPathAtCurrentRow (MatrixCursor mcResult , boolean cursorType ) {
@@ -621,15 +640,28 @@ public String[] getPropertiesOfCurrentRow(MatrixCursor mcResult, boolean cursorT
621640 File curWorkingFile = getFileForDocId (docId );
622641 return new String [] {
623642 curWorkingFile .getAbsolutePath (),
624- mcResult .getString (DOCS_PROJ_PARENTID_COLUMN_INDEX ),
643+ mcResult .getString (DOCS_PROJ_DOCID_COLUMN_INDEX ),
625644 FileUtils .getFileSizeString (curWorkingFile ),
626- FileUtils .getFilePosixPermissionsString (curWorkingFile ),
645+ FileUtils .getFilePosixPermissionsString (curWorkingFile . toPath () ),
627646 String .format (Locale .getDefault (), "%s" , curWorkingFile .isDirectory () ? "true" : "false" ),
628647 String .format (Locale .getDefault (), "%s" , curWorkingFile .isHidden () ? "true" : "false" )
629648 };
630649 } catch (IOException ioe ) {
631- ioe .printStackTrace ();
632- return null ;
650+ //ioe.printStackTrace();
651+ try {
652+ String [] mcColumnNames = mcResult .getColumnNames ();
653+ return new String [] {
654+ mcResult .getString (ArrayUtils .indexOf (mcColumnNames , DOCUMENT_COLUMN_NAME_ABSPATH )),
655+ getDocumentIdForCursorType (mcResult , !CURSOR_TYPE_IS_ROOT ),
656+ mcResult .getString (ArrayUtils .indexOf (mcColumnNames , DOCUMENT_COLUMN_NAME_FILE_SIZE_LABEL )),
657+ mcResult .getString (ArrayUtils .indexOf (mcColumnNames , DOCUMENT_COLUMN_NAME_POSIX_PERMS )),
658+ mcResult .getString (ArrayUtils .indexOf (mcColumnNames , DOCUMENT_COLUMN_NAME_ISDIR )),
659+ mcResult .getString (ArrayUtils .indexOf (mcColumnNames , DOCUMENT_COLUMN_NAME_ISHIDDEN ))
660+ };
661+ } catch (Exception ex ) {
662+ ex .printStackTrace ();
663+ return null ;
664+ }
633665 }
634666 }
635667
0 commit comments