Monday, 28 October 2013

Custom ContentProvider Permissions and Use from other Apps


A custom content provider was discussed and implemented here earlier. Today I'll discuss how a custom content provider can be used by other applications and how content provider permissions can be defined which other applications need to have in order to use it.

Content Provider Permissions:

Lets start with the provider permissions. Below is the modified Manifest.xml file of the MyContentProvider:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sohail.aziz.mycontentprovider"
    android:versionCode="1"
    android:versionName="1.0" >

   <uses-sdk android:minSdkVersion="9" />
    
   <permission
        android:name="sohail.aziz.READ"
        android:protectionLevel="signature" />
   <permission
        android:name="sohail.aziz.WRITE"
        android:protectionLevel="signature" />

   <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

       
        <provider android:name=".MyContentProvider"
          android:authorities="sohail.aziz.mycontentprovider"

          android:enabled="true"
          android:exported="true"
          android:readPermission="sohail.aziz.READ"
          android:writePermission="sohail.aziz.WRITE" />
    </application>

</manifest> 


We have defined two custom permissions sohail.aziz.READ and sohail.aziz.WRITE and used them as content provider's readPermission and writePermission. By defining these permissions we made it mandatory for other apps to have these permission in order to READ (Query) and WRITE (insert, update,delete) to this content provider.

Accessing Content Provider from other Apps:

In order to access this custom content provider from other apps, we need to have following things:

  •     Name of the needed permissions if any.
  •     Content URI of all the tables we need to interact.
  •     Exact fields/Cols name of the particular table.

Lets see how can we perform CRUD operations on the MyContentProvider defined here. Content URI for the categories table defined in MyContentDescriptor will be:

Uri  categoriesUri=Uri.parse("content://sohail.aziz.mycontentprovider/categories");

Lets put some content values in mycontentprovider from another app, say sample.apk.

 
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


Uri  categoriesURI=Uri.parse("content://sohail.aziz.mycontentprovider/categories"); 

 ContentValues vals=new ContentValues();

    /* name and status are the field name of categories table in MyContentProvider*/

            vals.put("name","sohail");
            vals.put("status",false);
            getContentResolver().insert(categoriesURI,vals);

   }
}


Same way we can call the other CRUD operations. As discussed at the start will be required to have  sohail.aziz.READ and/or sohail.aziz.WRITE permissions.

No comments:

Post a Comment