0

I have an app that downloads a single html file and various images and sometime, mp4 videos.

After an initial download, repeat downloads are done every 15 minutes by a background service.

The service checks if there are any new files and if any files have been modified (in this case, it is typically the html file that would be modified and some new images will be downloaded)

I have a webview that is displaying the html file and after the background service successfully downloads some new assets, the webview get refreshed.

However, whilst the service is downloading, the app would be using the files, displaying them in the webview (the videos are handled with a videoview, using a javascript bridge, which flips the two views around)

So, to my question.

After seeing some possible issues with the current app, I want the background service to download the assets to a temporary folder, instead of the main folder used (I use a sub-folder created in Downloads)

Then, once the downloads are complete, I would "stop" the webview, copy the files form "tmp" to the real folder, and restart the webview.

Logic sounds OK, but I am worried about speed. The files to copy could weight in at 100mb potentially (maybe more, hopefully less, due to bandwidth issues obv.) so how fast could Android copy those over?

Ideally the transition needs to happen in under a couple of seconds.

Can anyone advise on this?

Is there possibly a better way to handle this situation?

Paul Canning
  • 73
  • 1
  • 7

2 Answers2

0

Put the temporary directory below the main data directory, then use an atomic rename(2) call to move the new file into place. (This only works atomically when the source and destination directories are within the same filesystem, hence the recommendation about tempfile placement. You can also put them as FILENAME.EXTENSION.new into the same directory then rename them. Be wary of tempfile races, as usual, when designing filenames; use something like mkstemp(3) to create them if you can.)

mirabilos
  • 5,123
  • 2
  • 46
  • 72
  • I'm writing this in Java, for Android. Does this still apply? – Paul Canning Dec 09 '13 at 18:26
  • Just use the Java™ equivalents if you can’t call the native functions, e.g. by using the Android NDK. It does still apply because this is syscall-level, that is, directly Linux Kernel relevant, independent of the OS layer in between (Bionic/Android vs. regular libc). – mirabilos Dec 09 '13 at 18:29
0

Could you point the WebView at the "temporary" directory instead, and then in the background delete the original directory? (then you'd always use the "temporary" directory going forward).

ksasq
  • 4,424
  • 2
  • 18
  • 10
  • Not really. I mean, I *could* but this is not how I need it to work. The webview needs to look in the main folder I create and nowhere else. Changing the directory used would just be a waste of time sadly. I am implementing logic to download the files with .tmp added to them, then rename and refresh the webview :) – Paul Canning Dec 10 '13 at 10:04
  • If you are worried about speed, anything that is going to manipulate (e.g. rename, move) files on disk before refreshing the WebView is going to be a huge bottleneck on mobile devices where flash is really slow. If you used the files that you've just downloaded right away and then removed the old files in a background thread, you'd be good to go immediately. – ksasq Dec 10 '13 at 10:39
  • I'm not using Flash... So far my initial test shows that the renaming method is pretty fast. – Paul Canning Dec 10 '13 at 13:58
  • I was meaning a flash disk, not the Browser plugin ;-) It may be "fast" on the device that you are using, but that may vary widely across Android devices depending on what disk operations you are performing, how big those operations are and the quality of the drive in the device... or maybe it won't; there are lots of variables to consider. But I think that's the point of your question, right - to understand what might make this a slow operation, and alternatives that might be faster. – ksasq Dec 10 '13 at 14:09