Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-7487 [Android] Broadcast file write #81

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/android/LocalFilesystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Licensed to the Apache Software Foundation (ASF) under one

import android.util.Base64;
import android.net.Uri;
import android.content.Context;
import android.content.Intent;
import android.app.Activity;

public class LocalFilesystem extends Filesystem {

Expand Down Expand Up @@ -579,6 +582,7 @@ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
// Always close the output
out.close();
}
broadcastNewFile(inputURL);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this happen on every file write? If so, should we try to ensure that it happens only when new files are created, or does it need to be called every time a file changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with what you are saying here. By the time we are in writeToFileAtURL it seems the file already exists, so I tried moving this logic to getFileForLocalURL. In here I was checking if the file existed prior to creation, then broadcasting if it was created.

The results however were strange. Though the files would appear, they would sometimes be 0 bytes in size. Trying to debug the javascript portion always resulted in 0 byte files.

I then tried re-testing the broadcast after the write (where it is in this commit) and it seems to work as expected. I can't really explain why this occurring, perhaps you have some insight on this? If not, I think it's probably best to leave the broadcast after every write to be safe.

}
catch (NullPointerException e)
{
Expand All @@ -589,6 +593,32 @@ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,

return rawData.length;
}

/**
* Send broadcast of new file so files appear over MTP
*
* @param inputURL
*/
private void broadcastNewFile(LocalFilesystemURL inputURL) {
//Get the activity
Activity activity = this.cordova.getActivity();

//Get the context
Context context = activity.getApplicationContext();

//Get file
File file = new File(this.filesystemPathForURL(inputURL));
if (file != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this comparison for? I'm pretty sure that new can't ever return null. It also doesn't check for file existence; you'd want file.exists() for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I will update this.

//Create the URI
Uri uri = Uri.fromFile(file);

//Create the intent
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);

//Send broadcast of new file
context.sendBroadcast(intent);
}
}

@Override
public long truncateFileAtURL(LocalFilesystemURL inputURL, long size) throws IOException {
Expand Down