Can I write to Android SharedPreference for a different package? - java

I've written a game which I intend to upload to the marketplace as a free demo, and I intend to offer a full version for a buck.
I'd like to make the download for the full version just a simple unlocker which writes a value to the SharedPreferences for the demo.
When the demo launches it reads its shared prefs and if the value is present then it runs in full mode, otherwise it runs in demo mode.
The reason for this is A) so that when people purchase the full version the download is close to instant instead of having to wait for the whole app to be downloaded again, and B) so that I don't have to update two market listings whenever I change the code for the app.
So, is it possible to alter the SharedPrefs for a package that is separate from the currently running package?

Android sharedpreferences for an application come in 4 modes.
MODE_PRIVATE is used the most and only internal to the app
MODE_WORLD_READABLE is used if you want your preferences to be
read by another application
MODE_WORLD_WRITEABLE is deprecated API 17 onwards.
MODE_MULTI_PROCESS can be modified by multiple processes.
When an application wants to write preferences,it is called by
Context.getSharedPreferences (String name, int mode).
So it is possible to read the preferences.But to write it that particular app should be using MODE_WORLD_WRITEABLE which is deprecated.
Moral:You cannot alter the preferences of other applications.
And you really should not want to do it either

Related

Clearing Internal Storage Upon Uninstall

I need to be able to clear the internal storage directory of my application when the user uninstalls my app. How can I do this?
I am in the testing phase of building a security app which requires an ID to be set by an admin when the application is first installed to a user's phone. The ID file cannot be in external storage because I do not want any tampering.
In the event that an administrator gives a user the incorrect ID, they will not be able to fix this by uninstalling the app right now. I have tried uninstalling the app (and clearing cache, etc) to clear the ID data. Upon reinstalling the application, the files still exist, so the ID number cannot be changed.
I am saving the file to the main internal files directory for my application, if that helps.
IDFile = new File(context.getFilesDir(),"ID_Data.txt");
It's not possible to remove the internal storage, an app doesn't get a callback when uninstalled, so there's nothing it can do to wrap things up.
But it should work for external storage: Use getExternalFilesDir() and/or getExternalCacheDir() for your files on external storage ("sdcard"). Those directories are automatically removed when your app is uninstalled.
(source)
You can consider another approach - encrypt the files that the app stores with a key stored only in your code, so decryption will be possible only by the app, and when uninstalled - the remaining files will remain encrypted.
Idea #2
Delete data of older installation, if re-installed
Upon launch of your app, check if there's a preference "first_run", and if it's empty - create one with current time.
If there is already such a preference, you need to check if it's from this installation of from an old one: check the actual install time.
If your preference is smaller than the install time: delete all the old files.
In the manifest specify AutoBackup to "false"
android:allowBackup="false"
https://developer.android.com/guide/topics/data/autobackup.html

Updating the version and dumping shared preferences into database

I developed an android app ver 1.0 in which i stored the user data in shared preferences.
Now I am developing ver 2.0 and I have the following questions about updating it
1.How to detect if version 1.0 is already installed? I mean if it is a pure install(direct 2.0 install) or update from 1.0
2.If it is detected as 1.0 I want to dump the shared preferences values into database. Will the shared preferences be overwritten during update? How to prevent this? If they are not overwritten I want to write an activity which loads the values and dumps them into db
What parameter should i set so that market gives notification that update is available. Should i set that in android manifest with same keystore?
Please kindly help me out
Thanking You,
ChinniKrishna Kothapalli
The Android Documentation has a part explaining how to update your apps.
Basically you increase the number of your android:versionCode in the manifest. You should also change the android:versionName field so your users can see it's a different version.
As for your problem with dumping preferences into a database: The preferences allow you to use a default value if a certain preference is not found (when the downloads a fresh install).
I'm not sure if there is a way of detecting wether your application has been installed or not, except if you have something like a database already in your earlier version, then you could just check if it exists or not. Might not be the best practice to solve this tough.

How to take backup for installed packages?

I'm trying to develop an application for taking backup for my device. How can I write this? Till now, I've got all the installed packages list and I'm placing 3 buttons for taking the backup(Backup, CheckAll, Cancel).
If I'm going to take backup button it contains one alertdialog it shows 3 options (now, later, cancel). How to take backup the installed packages to one new list and it'll shows by date and time. How can I do this? Any idea?
i don't know if its possible to do that. I remember that i read in an article that in order to access to other packages you need to use the same user that the app does, and from my point of view that means that all the applications that are going to be backed up need to have the same user. I'm not an android expert but i think it wouldn't be possible to gain access to the other packages if you try Java IO to read the files as you normally do.
Good luck, maybe someone knows how to do this.
I don't know what do you mean by backup installed packages.
Generally users keep backup of their database and preferences data.
If you want to take a backup of your database and preference then you can create xml of your database and preferences using xml-serialization.
More information about xml-serialization Click here
And also you can refer inbuilt facility for get backup Click here

Run SQLite commands on database in one app from another

I have an SQLite database stored in the assets resources of one application used to load UI and other stuff into the app, mainly just holding text nothing out of the ordinary. I want to be able to get a writable version of this database so I can modify it from another application.
Example:
First application is on the market with limited number of enabled features. User gets to a certain point where they need to buy extra content to do more stuff in the app. The original app has these features but they are not enabled in the app using the database. I want the user then to download a second app from the market which is just used to change one field in the database from disabled to enabled thus unlocking the new features.
I have an idea I may need to use content providers but my understanding is once created they are accessible to all applications. I need it, for piracy reasons I guess, to only be able to communicate with apps signed off by my key.
Thanks
Sam,
I understand what you intend to do, but you are going about it the wrong way. Your 'Unlock App' would not be able to modify the Database in the assets folder of your 'Free App'. That's just general android security model stuff.
You may want to look at this question: How can I use the paid version of my app as a "key" to the free version?
It describes how you can create a 'Unlock App' on the market to unlock features of your 'Free app' without needing to actually modify any of the original data in the 'Free App'.
Good luck

Automatically update jar files

I am currently working on desktop software based on java.It's quite a big code base (more than 40 jar files).
I wish to provide an automatic update functionality. The desktop software constantly checks one back end system to see if there are new versions of the jar files available.
The problem now is: How to replace the updated jar files?
If you deploy your application using Java Webstart (JNLP), you get this mechanism almost for free...
From http://mindprod.com/jgloss/javawebstart.html
The key benefit for Java Web Start is automatic update without having to download the entire program every time.
Easiest would be to check for updates on each startup, download the updates and then launch your application. I think this is the way that Java Web Start works (see aioobes answer).
More complex would be to use either the netbeans or eclipse framework for your application. Both are rather complex and you will have to rewrite your application to work with them. This solution supports live updates.
As far as I am aware there is no easy way to update a running application. It is possible to load new versions of a class with a different classloader, but not possible to unload old versions while they are still referenced.
You can make a little server and a launcher which downloads the newest version, replaces the old one, and starts the jar with:
Runtime.getRuntime().exec("java yourjar -jar");
And you terminate the launcher with:
System.exit(1)
You can also serialize down your state (keep it in memory) and then create a new ClassLoader instance pointing to the new .jar files. Then serialize up your state again using this new classloader. You have just changed the underlaying .jars within a executing product.
Please note that you do not need to change the classloader for everything only for the part that is actually using the .jar files. This can be tricky to conclude what parts that are. And you might get nasty linking errors if done wrongly. So..
.. to keep it simple, use WebStart or a preloader that updates the .jars and then starts the main app (basically what WebStart does for you).
A reason for rolling your own, is that you can use your own format for the .jars, encryption, other packing formats etc.
After reading some answers to many auto-update questions, I thought of a solution. This is how I would implement a secure auto-update for a Java/Kotlin jar application.
Assumption: the installer will contain two jars: a launcher and the main application. Any shortcuts created will point to the launcher, but still be the name of the application. The release will contain the main application and the installer.
The launcher is launched first:
First check if an update has already been downloaded as app_name_update.jar
if an update has been downloaded, rename app_name_update.jar to app_name.jar
Start app_name.jar
This part does not have to be in the launcher, but it's preferred as to not slow down the main application: at this point, the launcher should check for an update (e.g. GitHub releases API) and download it to {CWD}/unverified_app_name_update.jar.
Compare the hash of unverified_app_name_update.jar to an online location containing hashes for all published versions. hashes.txt would be an example found in the same github repository. If the software is open-source, GPG signed commits is a must and the launcher should check if the latest update is a verified commit! If its a proprietary application, keep the hashes.txt at a separate URL from the release where your company does not control the infrastructure (e.g. GitHub).
Main app launched:
No need to check for updates unless updates are mandatory, in which case check for update -> if update found, start loading animation "updating" while you can detect that the launcher is still running. Ensure that the launcher has no race condition while loops!
I found ready project to solve automatically updating.
You can update your app, in your cases you can update jars and resources of your desktop app. The idea of the this is next: wrap you app with starter which can control updating and running you app. In details you can find here.

Categories