-
Notifications
You must be signed in to change notification settings - Fork 761
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
||
|
@@ -579,6 +582,7 @@ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, | |
// Always close the output | ||
out.close(); | ||
} | ||
broadcastNewFile(inputURL); | ||
} | ||
catch (NullPointerException e) | ||
{ | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this comparison for? I'm pretty sure that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.