ContentProvider android permission - android

I'm hoping someone smarter then me can answer this question.
By default can all android databases be accessed through the ContentProvider, or does the application in question have to explicitly give permissions to share it with the CP before another program can access its db?
If they are not shared by default, short of getting the application developer to include the change, root would be the only way around it?

By default can all android databases be accessed through the ContentProvider, or does the application in question have to explicitly give permissions to share it with the CP before another program can access its db?
By default, a ContentProvider is exported, meaning third parties can perform CRUD operations upon it. You can change this behavior either by:
Marking it as not exported (android:exported="false" on the <provider> element)
Using your own custom permissions to allow the user to conditionally allow access to the provider
If they are not shared by default, short of getting the application developer to include the change, root would be the only way around it?
Root will not help you access another applications' content provider. Please respect the wishes of the other developer.

Related

Android Studio connect with existing SQLite database

i have one application with SQLite database and then i create another application that can connect to the database that i created one, my question is, is it possible connect the existing database without copying the existing once ? if possible can any one give me a sample code to connect my existing database . the name of my database is "SEIS" and the Table is Proinfo .
By default, each app's files are private to the app.
You could tell Android that your two apps should get permission to access each other's files by setting the sharedUserId attribute for both and signing them with the same signature, but the 'official' way to give other apps access to your data is to create a content provider.
Yes it is possible through the use of content providers. According to the documentation:
A content provider manages access to a central repository of data. A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object.
I suggest you read the following articles:
http://developer.android.com/guide/topics/providers/content-providers.html
http://www.tutorialspoint.com/android/android_content_providers.htm

Soundhound - Android - How to programmatically access Favorites ?

I understand that Soundhound on Android doesn't currently store favorites on the cloud. They must therefore be on the local storage somewhere.
Does anyone know a good way to access the favorites programmatically ?
I know the user can email his favorites but I would like to access them through my code without user interaction.
Thanks!
Local app storage is generally restricted. One application can not access the local storage of another's without explicit permission of some sort. Either the application (Soundhound) has to set access to global (unlikely) or explicitly expose the data via some sort of API (also extremely unlikely). I would expect this to not be reasonably possible (without root permissions).
It's discouraging to find a -1 vote on the first question I asked here but here's the answer to my own question anyways...
As of Android 4.2.2, the Soundhound data is stored in this Sqlite3 DB:
/data/data/com.melodis.midomiMusicIdentifier.freemium/databases/bookmarks.db
There's no content provider for the bookmarks so of course using the DB isn't a good idea since its subject to change in any future release of the app.

problem to understand content provider

dear i am confused the use of content provider in android applications.i go through various sides and read it but i am still confused about content provider ..can any one explain it in some simple way ,how to use it?
Content providers store and retrieve data and make it accessible to all applications....whats that means?
can it means any package name or in same pakage name...
if i am making another app of diffrent package name then that c.p. is available also in that package?
OK, first of all, a content provider will be used if and only if you want to use the same data through several applications. You can still use it if you want to implement it in your application.
Now, by definition a ContentProvider "Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security." (According to Android Developer Guides)
So in other words, it's like a new layer of abstraction that you are adding to your data, assigning a new man-in-the-middle that will request everything you ask it to do (read, write, update your database, files, or webservices). And this man-in-the-middle is not only available for you, it's also available for other apps if you set the tag android:exported to true in the <provider> in your manifest file.
A ContentProvider needs you to implement:
A contract: Basically a class with public static variables with your tables names and the fields of info for those tables.
A data model: Normally a database in SQLite but you can implement it also with third party libraries like SugarORM, GreenDao, Retrofit, local files, etc).
Your custom ContentProvider: Extend ContentProvider and inside here set the calls to your data model.
This is a good tutorial on how to make your own ContentProvider:
http://www.grokkingandroid.com/android-tutorial-writing-your-own-content-provider/
There are also a few libraries that will help you create a ContentProvider automatically (I find it amazing because it takes you like 10 min to set up everything, instead of creating your own ContentProvider from scratch which could take you days).
My personal favorite is Schematic:
https://github.com/SimonVT/schematic
I hope it helps. Kind regards!
how to use it?
you actually don't use it directly, but via the content resolver (which will, according to the URI you give it, query the right ContentProvider) :
getContentResolver().query("content://com.myapp.myprovider/data/", ...);
will find your content provider if that one is registered to handle URIs that match "content://com.myapp.myprovider/data/"
if i am making another app of diffrent
package name then that c.p. is
available also in that package?
If you decide to publish the content provider, it is available outside of your application (this is a setting in the manifest).
what is the major benifit of using c.p.?
It is a common design pattern in Android to offer access to data. Major benefit is that you can abstract access to your data and decide whether to open it to other applications or not. For instance, without content providers, you could not access the media stored on the phone or the contacts of the phone.
Since a package is meant as a unique identifier for an application, how can you create an app with the same package name? If this was your question, I think I've answered it. If you don't understand something else - feel free to ask. Good luck!
P.S. Consider accepting some answers, your accept ratio is low.

Write a private access file to another app's files directory

The two apps have the same sharedUserId. When I use this code in app1
context.openFileOutput("/data/data/org.me.app2/files/shared-data.dat", MODE_PRIVATE)
I get an exception telling me that the file contains a path separator.
I am trying to write a file from app1 into app2's storage. (I do of course need to make sure that app2's files directory exists first)
Ideally, I would write to a user specific directory instead of an app specific directory, but I do not know if that can be done
First of all, NEVER use a full path to internal storage like /data/data. Let the operating system give you the path (for example, via Context.getFilesDir() or Environment.getExternalStorageState()). Don't make assumption on where the data is.
Secondly - you already are doing that! Unlike File, Context.openFileOutput already prepends /data/data/[package] to your path, so you don't need to specify that. Just specify the file name.
If you really feel that it's safe and necessary, and if both apps share the same user ID using android:sharedUserId in the manifest, you can get a context of the other app by using Context.createPackageContext() and use CONTEXT_RESTRICTED, then use openFileOutput with only the file name.
Open a FileOutputStream of the needed file, relative to this path:
String filePath = getPackageManager().
getPackageInfo("com.your2ndApp.package", 0).
applicationInfo.dataDir;
Since this is months old I assume you've already solved your problem, but I'll contribute anyway.
Sharing data between apps is what ContentProviders are for. Assuming that you know how to write a ContentProvider and access it, you can access files via ParcelFileDescriptor, which includes constants for the mode in which you create the files.
What you need now is to limit access so that not everybody can read the files through the content provider, and you do that via android permissions. In the manifest of one your apps, the one that will host the files and the content provider, write something like this:
<permission android:name="com.example.android.provider.ACCESS" android:protectionLevel="signature"/>
and in both apps add this:
<uses-permission android:name="com.example.android.provider.ACCESS" />
by using protectionLevel="signature", only apps signed by you can access your content provider, and thus your files.
You should not be overwriting other applications files. That said you have two solutions
Use public external storage (like the SD card) to share the file between the apps.
If the other app is not yours then you can't write to its /data directory, without root that is. Anything is possible with root, just don't expect your users to all have root access.
Edit: Developer owns both applications
Thanks for Roman Kurik for pointing this out. A link to his post on SO
From the android docs
android:sharedUserId
The name of a Linux user ID that will
be shared with other applications. By
default, Android assigns each
application its own unique user ID.
However, if this attribute is set to
the same value for two or more
applications, they will all share the
same ID — provided that they are also
signed by the same certificate.
Application with the same user ID can
access each other's data and, if
desired, run in the same process.
So this is exactly the way user id's work in linux, essentially you are the owner of both and have read/write access to both.

Android Content Provider inside the same application

I've more than one activity (inside the same Application) that needs to have access to the database. What's the best pattern to implement this ? Do I need a content provider even if all activities belong to the same application?
Which activity should have the responsibility for opening and closing the database ?
Your two options are Content Provider or just using your own database abstraction layer. The content provider is a better way to go as pointed out, if you need other apps to share your data or if you need to hook into some other part of Android (like the Quick Search framework). It should not be tied into an Activity - should just be a separate class that you import and use.
The OReilly Android programming book has a chapter which illustrates both approaches, its a good read.
Not necessary. You just have to create a Content Provider if you want some external application to access your data.
Content providers offer a structured storage mechanism that can be limited to your own application or exported to allow access by other applications. If you do not intend to provide other applications with access to your ContentProvider, mark them as android:exported=false in the application manifest. Otherwise, set the android:exported attribute to true to allow other apps to access the stored data.
https://developer.android.com/training/articles/security-tips

Categories

Resources