Wednesday, May 11, 2016

Android Custom Permission with example

Android Custom Permission:

Android allows mobile application developers to define custom permissions in their apps. Custom permissions are often used to protect different application components, such as Activities, Services, Content Providers, and Broadcast Receivers, from 3rd- party applications installed on the device. For example, if an organization wanted to share data between a few of its own Android applications, it could use Content Providers via custom permissions to share the data.

Declare Syntex:
<permission android:description="string resource"
            android:icon="drawable resource"
            android:label="string resource"
            android:name="string"
            android:permissionGroup="string"
            android:protectionLevel=["normal" | "dangerous" |
                                     "signature" | "signatureOrSystem"] />

Access syntax:
<uses-permission android:name=" string "/>


Here android:name and android:protectionLevel are mandatory attribute other are optional > It is a good practice to add all attribute.

Android Name:

The name of the permission. This is the name that will be used in code to refer to the permission — for example, in a <uses-permission> element and the permission attributes of application components.
The name must be unique, so it should use Java-style scoping — for example, "com.example.project.PERMITTED_ACTION".

Protection Level:

Characterizes the potential risk implied in the permission and indicates the procedure the system should follow when determining whether or not to grant the permission to an application requesting it. The value can be set to one of the following strings:
1.       Normal: Low Risk. This is the default protection level. Automatically granted to the application upon installation. The user’s approval is not required during installation.

2.       Dangerous: Higher Risk than Normal. User’s approval is required before an application is granted this permission.


3.       Signature: Only applications that have the same signature as of the application defining the permission are granted this permission. The user’s approval is not required during installation.

4.       SignatureOrSystem: Only applications in the android’s system image or applications that have the same signature as of the application defining the permission are granted this permission. The user’s approval is not required during installation.



Here I want to take an example like in my one app I have create an activity and this activity would be accessible from other client app with specific permission then we have to implement an custom permission for the same. Custom permission can be protect any android component like Activities, services etc.
Application with custom permission is mandatory to install first and then user can install second app.

             First app with declared custom permission

Here is the manifest file of my first application in which I declare permission and protect to provide access other client application.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackage.mycustompermissin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <permission android:name="com.mypackage.mypermission" android:label="my_permission" android:protectionLevel="dangerous"></permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:permission="com.mypackage.mypermission"
            android:name=".PermissionTestActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter >
                <action android:name="com.mypackage.permissiontestclient.MyAction" />
                <category android:name="android.intent.category.DEFAULT" />               
            </intent-filter>
        </activity>
    </application>

</manifest>

Activity here I am showing only blank activity for example purpose.

package com.mypackage.mycustompermissin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PermissionTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

  }
}



   Second app with uses permission:

This is the second application uses permission which we declared in first application. In this application we are opening the first application that we secured from android custom permissions.
Here is  Manifest file  

<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="com.mypackage.client "/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".PermissionTestClientActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>


Activity that calls first application activity with permissions.


package  com.mypackage.client;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PermissionTestClientActivity extends Activity {

    private static final String TAG = "PermissionTestClientActivity";
    Button btnTest;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btnTest = (Button) findViewById(R.id.btnTest);
        btnTest.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "Button click");
                Intent in = new Intent();
                in.setAction("com.mypackage.permissiontestclient.MyAction");
                in.addCategory("android.intent.category.DEFAULT");
                startActivity(in);
            }
        });
    }
}



No comments:

Post a Comment