Skip to content

Commit

Permalink
fix(android, utils): fix rare crash getting documents directory (#5118)
Browse files Browse the repository at this point in the history
* Fix: value for key KEY_DOCUMENT_DIRECTORY not exists if Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT and externalDirectory != null

* fix(android, utils): fix rare crash getting documents directory

this ensures the documents directory always exists, at the expense
of having it be a different value under rare circumstances (better
than crashing at least...)

Docs updated to reflect the rare cases it can return unexpected values

Co-authored-by: Mike Hardy <github@mikehardy.net>
  • Loading branch information
LittoCats and mikehardy authored Apr 9, 2021
1 parent d6b6d23 commit f0a2957
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,18 @@ public Map<String, Object> getConstants() {

File externalDirectory = context.getExternalFilesDir(null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if ( externalDirectory != null ) {
if (externalDirectory != null) {
constants.put(KEY_DOCUMENT_DIRECTORY, externalDirectory.getAbsolutePath());
} else {
// The external directory may be null if it is truly external *and*
// the device's external storage environment changes. We will use the regular
// Files directory as a backup and note in the documentation that the directory may
// vary under rare conditions
constants.put(KEY_DOCUMENT_DIRECTORY, context.getFilesDir().getAbsolutePath());
}
} else {
}

if (!constants.containsKey(KEY_DOCUMENT_DIRECTORY)) {
constants.put(KEY_DOCUMENT_DIRECTORY, context.getFilesDir().getAbsolutePath());
}

Expand Down
5 changes: 5 additions & 0 deletions packages/app/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ export namespace Utils {
*
* Use this directory to place documents that have been created by the user.
*
* Normally this is the external files directory on Android but if no external storage directory found,
* e.g. removable media has been ejected by the user, it will fall back to internal storage. This may
* under rare circumstances where device storage environment changes cause the directory to be different
* between runs of the application
*
* ```js
* firebase.utils.FilePath.DOCUMENT_DIRECTORY;
* ```
Expand Down

1 comment on commit f0a2957

@vercel
Copy link

@vercel vercel bot commented on f0a2957 Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.