Skip to content

Commit aca4da1

Browse files
committed
Testing the basic post hoc FileProvider interface class II
1 parent 75f06df commit aca4da1

1 file changed

Lines changed: 59 additions & 2 deletions

File tree

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,72 @@ fcConfig.filterByMimeTypes(List<String> fileTypesList, boolean includeExcludeInL
404404
fcConfig.filterByRegex(String fileFilterPattern, boolean includeExcludeInList);
405405
```
406406

407-
### Misc other useful utilities and customizations bundled with the main library
407+
### BasicFileProvider (DocumentsProvider) post hoc interface
408+
409+
In the use case I have for this library, I expect users to select default folder paths and initial
410+
script files from disk that will be run much later on after selection. In this context, it makes little
411+
sense to queue up to much backend resources devoted to holding on to a pivot in the documents provider
412+
that should service out the file contents until it is ready to be called. Thus, we will require a
413+
mechanism to recall folder position and state from within the local ``DocumentsProvider`` class,
414+
and then read (later more advanced functionality) out the file contents as a ``String`` or ``byte[]`` buffer,
415+
either in small chunks, or all at once for sufficiently small sized text files (like small shell scripts).
416+
417+
With this in mind, there is a class in
418+
[BasicFileProvider.java](https://github.com/maxieds/AndroidFilePickerLight/blob/master/AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/BasicFileProvider.java#L846) called
419+
``DocumentPointer`` that allows for this sort of long after the fact post hoc recovery of file I/O
420+
access once a user has selected the document path on disk (the ``Activity`` may restart, in fact,
421+
before we need to recover the read access to the file by its path). It seems silly that Android 11 now
422+
invokes having to go through this abstraction hoop when we are already returned a fully qualified path on disk.
423+
Nonetheless, the permissions specs in upcoming API releases dictate getting used to this procedure now, and
424+
better at this point than after a forced system upgrade that breaks my application in a year.
425+
426+
Here is an example that determines whether a file path (a priori known to point to a normal file, not directory)
427+
is text based based on its mime type. Then, if it is, the second function below fetches its contents in ``String`` form:
428+
```java
429+
public static boolean isFileContentsTextBased(String filePath) {
430+
try {
431+
BasicFileProvider.DocumentPointer docRef = new BasicFileProvider.DocumentPointer(CONFIG_DEFAULT_STORAGE_TYPE, FileUtils.getFileBasePath(filePath));
432+
if (!docRef.isValid()) {
433+
return false;
434+
} else if (!docRef.locateDocument(filePath)) {
435+
return false;
436+
}
437+
String fileMimeType = docRef.getDocumentType();
438+
return fileMimeType.toLowerCase(Locale.getDefault()).startsWith("text");
439+
} catch(Exception ex) {
440+
ex.printStackTrace();
441+
return false;
442+
}
443+
}
444+
445+
public static String getFileContentsAsString(String filePath) {
446+
if(!isFileContentsTextBased(filePath)) {
447+
return null;
448+
}
449+
try {
450+
BasicFileProvider.DocumentPointer docRef = new BasicFileProvider.DocumentPointer(CONFIG_DEFAULT_STORAGE_TYPE, FileUtils.getFileBasePath(filePath));
451+
if (!docRef.isValid()) {
452+
return null;
453+
} else if (!docRef.locateDocument(filePath)) {
454+
return null;
455+
}
456+
return docRef.readFileContentsAsString().toString();
457+
} catch(Exception ex) {
458+
ex.printStackTrace();
459+
return null;
460+
}
461+
}
462+
```
463+
464+
### Other useful utilities and customizations bundled with the main library
408465

409466
#### Displaying a visual linear bar style progress bar for slow directory loads
410467

411468
This functionality may be useful at some point for those willing to extend this code with
412469
custom external file providers, e.g., to read and recurse into directories on Dropbox or GitHub.
413470
I have a simple visual Toast-like display that can be updated and/or canceled in real time to
414471
let the user know that the directory is loading and that the client application is just "thinking"
415-
(as opposed to freezing with an inexplicable runtime error).
472+
(as opposed to unexpectedly freezing with an inexplicable to the user runtime error).
416473

417474
To invoke this progress bar display in realtime, consider calling the following
418475
[code examples](https://github.com/maxieds/AndroidFileChooserLight/blob/master/AndroidFilePickerLightLibrary/src/main/java/com/maxieds/androidfilepickerlightlibrary/DisplayUtils.java#L159):

0 commit comments

Comments
 (0)