GithubHelp home page GithubHelp logo

dcompanysys / runtime-permissions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from karthyks/runtime-permissions

0.0 0.0 0.0 146 KB

A simplified integration of android's runtime permissions.

Java 100.00%

runtime-permissions's Introduction

Wercker Wercker Wercker

Runtime-Permissions

Usage

Add the following lines in the application's gradle file.

    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.android.support:design:25.1.1'
    compile 'com.github.karthyks:runtime-permissions:1.8'

To ask the permission from the user, use the following code.

Permission permission = new Permission.PermissionBuilder(Permission.REQUEST_LOCATION)
        .usingActivity(AppCompatActivity).withRationale("Some rationale message!")
        .build();
permission.requestPermission(PermissionActivity.REQUEST_PERMISSION_CODE);

or

Permission permission = new Permission.PermissionBuilder(Permission.REQUEST_LOCATION)
        .usingFragment(Fragment).withRationale("Some rationale message!")
        .build();
permission.requestPermission(PermissionActivity.REQUEST_PERMISSION_CODE);

The user's response to the permission request will be reflected onActivityResult, as shown below.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PermissionActivity.REQUEST_PERMISSION_CODE) {
      switch (resultCode) {
        case PermissionActivity.PERMISSION_GRANTED:
          Toast.makeText(this, "Granted", Toast.LENGTH_SHORT).show();
          break;
        case PermissionActivity.PERMISSION_DENIED:
          Toast.makeText(this, "Denied", Toast.LENGTH_SHORT).show();
          break;
        case PermissionActivity.PERMISSION_PERMANENTLY_DENIED:
          Toast.makeText(this, "Permanently denied", Toast.LENGTH_SHORT).show();
          PermissionUtil.openAppSettings(this);
          break;
        default:
      }
    } else {
      super.onActivityResult(requestCode, resultCode, data);
    }
}

#Simple Location Settings API implementation: To check the user's location priority which uses SettingsAPI is simplified by the following method.

LocationRequest locationRequest = new LocationRequest()
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  LocationSettingsHelper settingsApi = new LocationSettingsHelper(MainActivity.this,
      locationRequest, true, false);
  settingsApi.checkLocationRequest();

And capture the result on onActivityResult.

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == LocationSettingsActivity.REQUEST_LOCATION_SETTINGS) {
     switch (resultCode) {
       case Activity.RESULT_OK:
         Toast.makeText(this, "Allowed Location Settings", Toast.LENGTH_SHORT).show();
         break;
       case Activity.RESULT_CANCELED:
         Toast.makeText(this, "Location Settings canceled", Toast.LENGTH_SHORT).show();
         break;
       default:
     }
   } else {
     super.onActivityResult(requestCode, resultCode, data);
   }
 }

#Note If it throws "Permission Denied" even after allowing it, check whether all the permission is added in the manifest for the permission group. For example, for location permission add both COARSE and FINE_LOCATION permission in the manifest.

For ease of use, the permissions to be added in the manifest, for the particular dangerous permission groups are mentioned below.

    <!--
     PERMISSION_GROUP PHONE
    -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    <uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
    <uses-permission android:name="android.permission.USE_SIP"/>
    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

    <!--
     PERMISSION_GROUP CONTACTS
    -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>

    <!--
     PERMISSION_GROUP STORAGE
    -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <!--
     PERMISSION_GROUP SENSORS
    -->
    <uses-permission android:name="android.permission.BODY_SENSORS"/>
    <uses-permission android:name="android.permission.USE_FINGERPRINT"/>

    <!--
     PERMISSION_GROUP AUDIO
    -->
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

    <!--
     PERMISSION_GROUP CALENDAR
    -->
    <uses-permission android:name="android.permission.READ_CALENDAR"/>
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>

    <!--
     PERMISSION_GROUP CAMERA
    -->
    <uses-permission android:name="android.permission.CAMERA"/>

    <!--
     PERMISSION_GROUP LOCATION
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <!--
     PERMISSION_GROUP SMS
    -->
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_WAP_PUSH"/>
    <uses-permission android:name="android.permission.RECEIVE_MMS"/>

runtime-permissions's People

Contributors

karthik-mtap avatar karthyks avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.