For my app I want to keep track of which files have been accessed most recently (for cache management: files used least are first to go out).
Now Android doesn't appear to have a last accessed date function for files, so I'm looking at the next best thing: file.lastModifiedDate(). That gives me the last modified date, which is effectively the creation date.
But when accessing the file I'd like to set this value to the current time. Like Linux's touch command. How can I do this, without actually modifying the file?
This should work:
http://www.java-examples.com/set-last-modified-time-file-or-directory
Related
I want to know if there is a way to include your Android bootanimation.zip inside frameworks.apk(or another place not easily accessible) when building from source so that it gets loaded instead of the one in /system/media/bootanimation.zip
So when a user replaces the bootanimation in /system/media it still loads the default one built into frameworks.apk or some other place that Android can access to stop a user from modifying the bootanimation easily.
Or another scenario, a user replaces the bootanimation but then on the next boot Android checks if there is a size, file difference on the bootanimation.zip with a predetermined value(or original file) and if it differs, then it copies a spare bootanimation.zip located somewhere(if possible in frameworks.apk, so users can't get it easily, without decompiling it) and then copies the original bootanimation over the one in /system/media.
Then on the next boot, the bootanimation will be the original one and users will be baffled why it changed again. Is there a way to write such a script to run on boot or include it in some runtime file in /system/bin perhaps?
You can change the default location of the bootanimation.zip, but where would be a good place to hide it and what to rename it too, will also need to keep it small under 5MB and without .zip extension? But this method might be easily discovered.
It is for protecting my work, so others can't take credit for it.
So I just want to make it a little harder for someone that tries to do such a thing.
You can store the boot animation somewhere with a different name. And then implement a service in init.rc for copying your boot animation to /system/media after checking the current bootanimation.zip file's checksum. If the user had replaced the bootanimation.zip then the checksum will be different compared to your bootanimation.zip.
I've got an Android app written in Kivy (Python), which stores local files that should survive an app update (adb install -r).
If the files are stored in a subdirectory of the current directory ("data/data/app_name/files"), I see that they are deleted after update.
However after some experiments I could "solve" this by storing the files in the "data/data/app_name/shared_prefs" directory, which seems to be persistent after updates. By the way, I didn't check but maybe the "data/data/app_name/databases" also is.
Is there a cleaner way of doing things ?
I need to test if I can create a new folder not called shared_prefs nor databases under "data/data/app_name", and if it is persistent.
(this seems kind of a hack because those directories have another dedicated purpose, even though my app is not using them for this dedicated purpose right now)
(NB: I don't want to keep the files outside the app private directory)
There is not a simple way (as in a build hook or similar) right now, but it's something we've specifically discussed in the last few days as the current situation has become a direct problem. I'm not sure what the resolution was, but there will probably be a change in python-for-android to fix it fairly soon.
If you want to keep up to date with this, ask on the kivy mailing list or irc. In particular, knapper_tech was making these changes.
FileUtils.copyDirectory doesn't write date modified correctly unless the Android SD card is unmounted. Using this simple bit of code from FileUtils:
try {
FileUtils.copyDirectory(srcDir2, destDir2);
} catch (IOException e) {
I can copy a directory from the internal storage on the phone to the sd card preserving the date modified information on the files in the directory which is essential for my app.
Sadly if the SD card is removed without ejecting it all the date modified information on the files in the copied dir is set to the time the files were copied. if the SD is unmounted correctly then the date modified information is preserved correctly.
I have tried the flush and close functions but they are not relevant to this kind of file. What code am I missing to finalize the directories without unmounting? I am using an android device will a full size SD slot and I cant risk loosing all the information if it gets knocked out without a proper eject
There's a version of this method that accepts a flag as third parameter to try to control the date of last modification. You can check the docs here. But looks like the method you are using also tries to preserve the date by default. In fact, reading the source code, the method you are using just calls this method with the third parameter set to true.
In the docs for your method it is said:
Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.
In the end what this library does each time it copies a file or dir is to call File.setLastModified over the destination file with the last modified date of the source file. This method has been reported to be unreliable in Android, as you can see in these other questions:
Android set last modified time for the file
file.lastModified() is never what was set with file.setLastModified()
Is it possible to reset the last modified date of an Android file?
But in your case, I think you are trying to provide a workaround for something that is just designed that way. I'm no expert, but this is managed by either the OS or the FileSystem. The unmounting mechanism serves a purpose, I think you can't do much about it as an app developer.
Do you know any way for safe copy cut operation in Android?
I want to make a file manager. I can do them with FileReader/Writer, but as is not safe (if app crashes), I want another safe way to do this.
You should probably design a fail-safe order of operations. For example (just making this up, haven't validated it thoroughly):
Record in a persistent worklist file (/shared preference, etc) what you intend to do
Copy the file
Verify the copy is complete
Only then remove the original
Cleanup the record in the worklist
You should not do the work on the UI thread. You may want to consider doing it in a service to handle the case of huge files (or large numbers of files) which may take a while.
You may also want to consider if you can in some cases use the File.renameTo() method - this would require that the source and destination be on the same partition (ie, both on external storage). Also you would have to make sure that the destination directory exists.
Also put some thought into what you are going to do if the selected object is a directory rather than a file...
I'm writing a purge function for our software that removes all image files that haven't been used in over a month. To do this, I am checking two things:
the last-modified date of the file
a query on a database that shows recently viewed files.
The query is much slower than the file check. So I would ideally like to be able to reset the last modified date on any files that fail the first check, but pass the second, so that (for example) a list of venerable but often-used files aren't gradually increasing the processing load of the management system.
Is there a way to do this without recourse to something crude (and possibly even slower), like renaming each file to a temporary label, then itself?
As Nik said the setLastModified() method on the File class may not always work depending where you are in the Android file system. If on the SD Card then some devices will return false from that call and thus not change the date.
There are more details about it here:
http://code.google.com/p/android/issues/detail?id=1992
http://code.google.com/p/android/issues/detail?id=1699
Plus some other stackoverflow thread here:
file.lastModified() is never what was set with file.setLastModified()
setLastModified() is a standard method on any Java File object that you can use to update this value.
SDK Documentation.