To get application name accessing a file - java

I am building an application where in i want to know which all applications present on the android device have opened/accessed a given file. Can any one help me on this. I have used FileObservor, but it just tells me which all files are opened or accessed. I want to know which application has accessed it.

First, there is no logging, so you would have to catch things as they are happening, before they are finished.
On an ordinary linux you would be able to use the file observer like (inotify) mechanism to find out when a file is accessed, and then go trolling through through /proc/whatever/fd to try to find who is accessing it, if you can do that while they still have it open. But android runs each application as its own userid (excepting allied packages which share), meaning that you lack permission to access this information from another app.
You might be able to accomplish something by holding the data in a content provider rather than putting it in a simple file, as then any access to it would be mediated by your code.

Related

Android: Save variable across all user profiles

I have an application that can have multiple users on the device. I have a counter that needs to be read and updated across all users. Is there a way that this variable can persist across all user profiles?
We want to avoid using Settings.Global since all applications would have access to the variable and we would like to make it so it is accessible only for our app.
SharedPreference is user specific so that won't be an option.
We could save to a file but then again another team who has access to the source code may be able to access and edit the variable. Maybe there is a way for only our application to have write access to the file?
Is SqlDelight user-specific? I haven't found a concrete answer to that online.
If I understand you correctly, internal storage is what you're looking for. Only your app can access the directory, other apps can't. No need for permission and all data removes if app removes.
More info in official docs about differece of storages:
https://developer.android.com/training/data-storage
and here is for internal storage specifically:
https://developer.android.com/training/data-storage/app-specific

Java, Codename One: Exporting and -most importantly- importing files between users

Here the problem: my app will generate some files, and I want to give to the users the opportunity to exchange these files between them.
This requires 3 steps:
Saving the data: easily done in Storage implementing the
functions required by the Externalizable class;
Sharing the data: done (probably, right now it's impossible to check if the
result is correct because the missing step 3) with the sharing
methods offered by the framework, as soon as I understood I needed
to use as mimetype "application/octect-stream";
importing the downloaded data (shared by another user): this one I can't manage to
find a way to make it work. Loading the files from the app's Storage
is easy, but accessing to the folders out of the app's Storage is
something I can't manage to do.
I used FileSystemStorage in the hope of gaining access at least to the Download folder that (mostly) every phone has, but apparently I can't manage to accomplish the task
Using the FileSystemStorage on Android, for example, I have access to
/storage/emulated/0
/storage/emulated/legacy
file:///system
The first two being related to the Storage of the app.
Acceding to file:/// I obtain a long list of folders, a partial list including
media
logs
sdcard
Removable
...
root
...
But when I try to access some of these, they all appear to be empty. Either I make some mistake or the app can't see their content.
So I wonder if there is a way to accomplish the task, namely to have access to the files (probably in the Download folder) the user has downloaded, to import them.
Phone apps live in isolation within the phone. Android literally creates a separate Linux user for every app so they don't step on each other and damage the phone. iOS does similar tricks.
As a result apps can't just write a file to downloads and can't just list all the files there. This would violate the users privacy. So the native API to share a file is usually separate from the files API. We do have a share API in the CN class which lets you share images etc. to an arbitrary OS app. See isNativeShareSupported and share.
Ok, maybe I found a solution for reading the files from the Download folder in an extension of CodenameOne called FileChooser.
According to this blog post it should give access to, between the others, the Download folder (at least in Android).
I'm going to try it and, when everything is ready and tested, edit this reply to say how it worked out for me.

Share a piece of data between several Android apps

I am writing an SDK for Android that will be used by many different apps. Each app needs to know if there is a previous installation of another app that uses the SDK; the first one will create a random id (a cookie) and store it, all later apps have to read it and reuse it. Note that the cookie will be created anew for every device.
I have searched for a long time for the answer; please read thoroughly before answering because I have read lots of different StackOverflow answers and have scoured the internet reading random blogs; I have tried a lot of things but none worked (I will save you the links).
A ContentProvider is definitely overkill. Also it needs to intrude an app's AndroidManifest.xml, something I am trying to avoid.
Likewise for a service: needs to be defined in the AndroidManifest.xml, which I do not control and do not want to require changes to.
Using external storage would perhaps be an option, but I don't want to require additional permissions.
Using SharedPreferences directly with getSharedPreferences() does not work because apps can only access their own preferences.
I can get a common preferences object using createPackageContext(package, MODE).getSharedPreferences(). This would work beautifully if I had a main package and many clients of the data, but in fact I don't know the package of the app that will be installed first -- it can be any of them. I don't even have a list of package names to search. Passing a package name which has not been installed fails.
Following the approach above, I have tried to piggyback on some standard Android app which I can count on, and store my preferences there -- say:
createPackageContext(
"com.android.browser",
Context.CONTEXT_RESTRICTED)
.getSharedPreferences(
"GLOBAL_PREFS",
Context.MODE_WORLD_READABLE);
but it does not work: the error reads
Couldn't create directory for SharedPreferences file
/data/data/com.android.browser/shared_prefs/GLOBAL_PREFS.xml
So, to recap: I need to store a piece of data (a short string) in some standard location, so that any other app can go there and read it if present, and it should work if at all possible without doing any magic in the AndroidManifest.xml or requesting any permissions.
There probably isn't any perfect answer, so perhaps the best solution is to write to external storage; then so be it. To put things into context, apparently it is trivial to do this on iOS using a keychain, designed to store secure data.
Unfortunately there really isn't a great answer for this that I know of. You've come up with a pretty good outline of your options and the best way may well be with external storage.
Just to throw something out there, I suppose it's possible you could use a flat file with a fixed name and world readable (and possibly writable) permissions. You'd have to then iterate through all applications' directories and check for this known-named file in each folder and attempt to open it.
While this might work theoretically, consider the case where the app that contains the "cookie" is uninstalled. Then you're left cookie-less. You might want to create the cookie in every app, copying over the value of the previous cookies to new cookies.
I haven't actually tried this, but I imagine it should work.

Android - configuration file

I have one question about Android. I need to run one of my activities only once - at the beggining. So, usually the best solution is to create file which contains flag isFirstRun and check the value after application's start.
But in my application it is very important to protect this file before deleting by user. Even if user has rooted phone he should not be able to change the value or delete this file.
So, is it possible to write this information to any Android system registry or somewhere else where user can't change this value?
No, it is not possible, for a simple reason, a root user have access to everything by definition. It won't make sense to have a program that has more rights than root.
The user can delete all the data your application saves. Consider saving this information on some server.

Holding Information in Android App

I've only just started to write in Java on Android, so please bear with me.
I have some settings I want to hold in my app, normally I would have used an xml file. Trouble is i'm not sure how to load it into the xml parser to read it.
I thought I might be able to drop it into /res/values/Info.xml and open it from there but it does'nt find the file.
I have also read that people are starting to use a SQLite database to hold information in, is this more the standard way to go?
thanks a lot
Luke
It sounds like what you want are Shared Preferences. Its a simple way of storing key value pairs, along with a UI for letting the user change them.
You could try storing to External Files (that way they can save settings on an SD card if they have 2.2). You wouldn't be able to modify the files in the res folder like you tried because these files get compiled into the app package. You could also try Internal Files found on the same page. SQLite might be a bit much for config settings.

Categories

Resources