ContentProvider to expose app preferences - android

I want to write a content provider for an app that exposes some data sored in the app's shared preferences. Is it possible to do that?

Why do you want to to do that ? Normally Preferences are secret data which should be only available to source app.
Incase you want to do that, you can read other's app preferences by obtaining the context of source application
Context otherAppsContext = createPackageContext("other.application.package", mode);
Also you should understand that you can’t get all preferences. When you will receive results you will see that PRIVATE and WORLD_WRITABLE aren’t available.

Related

Access data from other application saved in preference

If an application has saved data using SharedPreference like this:
SharedPreferences.Editor preferencesEditor = getDefaultSharedPreferences.edit();
preferencesEditor.putInt("count", mValue);
preferencesEditor.apply();
can an other application have access to these data after reboot like this for example:
SharedPreferences sharedPref = getDefaultSharedPreferences();
int value = sharedPref.getInt("count",0);
And if it is not posssible, how is possible from an application to store variable so that other application have access to it?
Yes it is possible, but both apps need to be signed by the same certificates among other things, check this answer for details.
An application can also share and access data from a public content provider and this is usually the recommended way of sharing data among different applications.
You can also have a shared file in storage that both apps can read and write.

Application settings/properties in Xamarin Forms similar to Shared Preferences in Android

I wish to store settings/properties at the Application level as well as Page wise. In other words, I need different sets of settings which will stay even when the app is restarted. I have considered Application.Current.Properties and Settings Plugin for Xamarin Forms but they don't seem to provide the same functionality as Shared Preferences in Android, where you can simply pass context and get Preferences for that context. Any help is appreciated.
Thanks
You can use the settings plugin for Xamarin Forms if you data you want to save does not need securing.
You could create a settings object, serialise and store with a context key if you need to store different preferences per context
https://github.com/jamesmontemagno/SettingsPlugin
If it is usernames, passwords or tokens and needs securing one option is the secure storage plugin
https://github.com/sameerkapps/SecureStorage
You might consider a local SQLite database to store application settings that persist through app and phone restarts.
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/
You could use Xamarin.Auth. It uses Keychain for iOS and KeyStore for Android to persist data.
"Saved Accounts are uniquely identified using a key composed of the account's Username property and a "Service ID". The "Service ID" is any string that is used when fetching accounts from the store."
https://github.com/xamarin/Xamarin.Auth
Section 4.2 from README file

getPreference() - accessibility of a shared preference for components of the application

In the official site of Android, for getPreference() method, it is said that :
Retrieve a SharedPreferences object for accessing preferences that are
private to this activity.
And here(How do I get the SharedPreferences from a PreferenceActivity in Android?) it is said that :
These preferences can only be used within the particular activity and
can not be used by other components of the application.
However, in url http://skillgun.com/android/basics/interview-questions-and-answers/paper/25 5th question,
it is said that it is not guaranteed to be protected as it will be
stored with the name of Activity.
I am confused whether other components of an application(such as activity,service etc.) can access the shared preference created by getPreference() method. is a shared preference created by calling getPreference() method accessible only for an activity for all circumstances?
Basically if you use shared preferences you will be able to read and write the preference from any part of your app. But other apps will not be able to access this information.
The statement regarding not being protected refers to the fact that rooted users (and apps) can read this files from the phone internal storage. So avoid at any cost saving sensitive user information in Shared Preferences. Ex. Dont store userNames, Passwords, personal details, etc.
Use shared preferences for simple things you wish to store Ex. If developing a contacts app you can store whether the user likes to read his contacts firstName LastName or LastName FirstName.
This kind of data is very short and not compromising.
If you require to store sensitive information always encrypt the data first.

Android: Cannot access other app's SharedPreference

I've been searching for days to solve this, but with no success.
I want to get the shared preference settings from my old app and put it on to my new app.
but I've encounter some security issue (suspect).
my code:
Context c = createPackageContext("my.app.pkg", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sp = c.getSharedPreferences("my.app.pkg", Context.CONTEXT_IGNORE_SECURITY);
Running the code above giving me this:
Attempt to read preferences file /data/data/my.app.pkg/shared_prefs/my.app.pkg_preferences.xml without permission
even thought the object sp is not null, but it does not retrieve anything from my old app.
I tried googling around and seems like most people can run the code above with no errors.
Is there anything I've missed out?
We've done exactly this for our Android book Android in Practice. The key is to use the same process and user ID for both apps. The sample code and sample apps are on Google Code (SharedProcessApp1 and SharedProcessApp2). You can go from there.
You can indeed share preferences across applications. That's why it's called SharedPreferences.
What you need to do is make sure both application are signed with the same certificate and they both share the same SharedUserId in the AndroidManifest.xml file: read here.
This is because the SharedPreferences you get from
PreferenceManager.getDefaultSharedPreferences(context)
has always MODE_PRIVATE.
However in an application you can also get a SharedPreferences object within a Context with the following:
SharedPreferences prefs = getSharedPreferences("my_public_shared_prefs", MODE_WORLD_READABLE);
which you can freely retreive from another app with the following:
Context context = createPackageContext("my_target_app_package", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences prefs = context.getSharedPreferences("my_public_shared_prefs", MODE_WORLD_READABLE);
Make sure you don't store any private information there because it's WORLD readable, which means you and anyone else can read that data.
To finish, if you want to retrieve the SharedPreferences of your old app you will need to update the old application with a SharedUserId in the Manifest file.
Android APplications runs in their own sandbox, so one application can not access data of other activity. BUt If an application want to share some of its data, this can be achieved by ContentProvider.
See Content Provider: http://developer.android.com/guide/topics/providers/content-providers.html

Difference between Shared Preferences and Content Providers in android

I know partially about Shared preferences and intents.But i want to know what are shared preferences and content providers in android ? And also what is the basic difference between intents , shared preferences and content providers.
Please explain me this.
shared preferences are the location where you can store the secret information for your app, like setting cookies in the browser, this can be used for login credentials and other.
where as content provider stores and retrieves the data and make it available to other applications also. like suppose you want to access the contacts available in the android phone, they can be accessed by content providers
SharedPreferences
SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.
Content providers
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.
You don't need to develop your own provider if you don't intend to share your data with other applications. However, you do need your own provider to provide custom search suggestions in your own application. You also need your own provider if you want to copy and paste complex data or files from your application to other applications.
Android itself includes content providers that manage data such as audio, video, images, and personal contact information.

Categories

Resources