EasyMPermission - Code generator for new Android app permission model

Introduction

EasyMPermission provides annotations to generate boilerplate code for the new Android M app permissions model.

@RuntimePermission
public class MainActivity extends Activity {

    @Permission({"android.permission.camera"})
    public void accessCamera() {
        // Obtain camera instance
        Camera camera = Camera.open();
    }
}
			  

Backgroud

The Android M release introduces a new app permissions model. With this new permissions model, user grants permissions at run-time instead of installing time. When the app requests a permission, the system shows a dialog to the user, then calls the app's callback function to notify it whether the user granted the permission. The permissions are revocable. User can revoke app's permissions at anytime. So the app needs to verify required permissions before performing any restricted actions.

This new permission model gives the user great flexibilities on controlling app's permissions. But this creates problems for app developers.

  • The app flow with restrictied actions is no longer streamlined.
  • A lot of boilerplate code needs to be added

What is EasyMPermission

EasyMPermission is based on the great project lombok. It creates the boilerplate code to

  • check required permission at run-time,
  • generate permission request code,
  • generate callback function onRequestPermissionsResult.

With EasyMPermission app developers don't need to change current app flow. By adding annotations to existing classes and methods EasyMPermission will generate all boilerplate code for you.

EasyMPermission provides two annotations, RuntimePermission and Permission, for code generation.

RuntimePermission

This annotation is used to annoate class which uses new app permissions model.

@RuntimePermission
public class MainActivity extends Activity {
}
			  

Permission

This annotation is used to annoate methods which contain restricted actions. Multiple permissions can be passed in as array.
@Permission({"android.permission.camera", "android.permission.ACCESS_FINE_LOCATION"})
public void accessCameraAndLocation() {
}
			  

Example

Here's an example to open camera which requires android.permission.camera permission

Code for Pre-M Permission Model
public class MainActivity extends Activity {
    public void accessCamera() {
        // "android.permission.camera" permission was granted at install time
        // Obtain camera instance
        Camera camera = Camera.open();
    }
}			  
Code for New Permission Model using EasyMPermission
@RuntimePermission
public class MainActivity extends Activity {

    @Permission({"android.permission.camera"})
    public void accessCamera() {
        // Obtain camera instance
        Camera camera = Camera.open();
    }
}
			  

Generated code by EasyMPermission for the above example is roughly equivalent to the following:

			  
public class MainActivity extends Activity {
    private int MY_PERMISSIONS_REQUEST_CAMERA = 1000;

    public void accessCamera() {
	    if (checkSelfPermission("android.permission.camera")
		       != PackageManager.PERMISSION_GRANTED) {
		    requestPermissions(new String[]{"android.permission.camera"},
			    MY_PERMISSIONS_REQUEST_CAMERA);

		    return;
	    } else {
		    // Obtain camera instance
		    Camera camera = Camera.open();
	    }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
	    String permissions[], int[] grantResults) {
	    if (requestCode == MY_PERMISSIONS_REQUEST_CAMERA) {
		    if (grantResults[0] ==
			    PackageManager.PERMISSION_GRANTED) {
			    Toast.makeText(this, 'Permissions were granted', Toast.LENGTH_LONG).show();
		    } else {
			    Toast.makeText(this, 'Permissions denied', Toast.LENGTH_LONG).show();
		    }
		    return;
	    }
    }
}
			  

How to use EasyMPermission

Download

The source code and sample application as well as this website is available at GitHub.

Android Studio (Gradle)

Add EasyMPermission repo url
maven {
    url  "http://dl.bintray.com/mobmead/EasyMPermission" 
}
			
Add EasyMPermission into dependencies
provided 'com.mobmead:easympermission:1.0.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
			

License

Copyright (C) 2015 Jian Chen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.