All Projects → googlesamples → Easypermissions

googlesamples / Easypermissions

Licence: apache-2.0
Simplify Android M system permissions

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Easypermissions

Perm
Simple authorization/permission management in Ruby
Stars: ✭ 8 (-99.91%)
Mutual labels:  permissions
Easyandroid
一系列简单、轻量、方便的Android开发工具集合(持续更新中),包括Android动态权限、SharedPreferences、反射、日志、Toast、Bundle、MVP、线程池、Html、图文混排、蒙层引导、拍照、图库选择等
Stars: ✭ 1,039 (-88.88%)
Mutual labels:  permissions
Laravel Nova Permission
A Laravel Nova tool for the Spatie Permission package
Stars: ✭ 59 (-99.37%)
Mutual labels:  permissions
Django Oml
Object Moderation Layer
Stars: ✭ 12 (-99.87%)
Mutual labels:  permissions
Cordova Plugin Ios Camera Permissions
Cordova / PhoneGap Plugin Permission Settings for NSCameraUsageDescription and NSPhotoLibraryUsageDescription in iOS 11 by adding a declaration to the Info.plist file, see:
Stars: ✭ 34 (-99.64%)
Mutual labels:  permissions
Androidpermissionx
Android RunTime Permission
Stars: ✭ 47 (-99.5%)
Mutual labels:  permissions
Xtoolkit.whitelabel
Modular MVVM framework for fast creating powerful cross-platform applications with Xamarin.
Stars: ✭ 22 (-99.76%)
Mutual labels:  permissions
Brandenburg
Laravel Authentication Package
Stars: ✭ 79 (-99.15%)
Mutual labels:  permissions
Gcp Iam Role Permissions
Exports primitive and predefined GCP IAM Roles and their permissions
Stars: ✭ 43 (-99.54%)
Mutual labels:  permissions
Luckperms
A permissions plugin for Minecraft servers.
Stars: ✭ 1,100 (-88.23%)
Mutual labels:  permissions
Deepstream.io
deepstream.io server
Stars: ✭ 6,947 (-25.67%)
Mutual labels:  permissions
Authr
🔑 a flexible and expressive approach to access-control
Stars: ✭ 33 (-99.65%)
Mutual labels:  permissions
Permissionsflow
A simple library to make it easy requesting permissions in Android using Kotlin Coroutines.
Stars: ✭ 49 (-99.48%)
Mutual labels:  permissions
Rbac
Hierarchical Role Based Access Control for NodeJS
Stars: ✭ 857 (-90.83%)
Mutual labels:  permissions
Ziggurat foundations
Framework agnostic set of sqlalchemy classes that make building applications that require permissions an easy task.
Stars: ✭ 67 (-99.28%)
Mutual labels:  permissions
Settingscompat
特殊权限(Special Permissions)兼容库,悬浮窗权限(SYSTEM_ALERT_WINDOW)与系统设置修改权限(WRITE_SETTINGS)
Stars: ✭ 942 (-89.92%)
Mutual labels:  permissions
Django Access
Django-Access - the application introducing dynamic evaluation-based instance-level (row-level) access rights control for Django
Stars: ✭ 47 (-99.5%)
Mutual labels:  permissions
Django Rules
Awesome Django authorization, without the database
Stars: ✭ 1,255 (-86.57%)
Mutual labels:  permissions
Mrbutler
Reactive Android App Permissions API with delegates and logging
Stars: ✭ 77 (-99.18%)
Mutual labels:  permissions
Node Mac Permissions
A native node module to manage system permissions on macOS.
Stars: ✭ 51 (-99.45%)
Mutual labels:  permissions

EasyPermissions Build Status Android Weekly

EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting Android M or higher.

Note: If your app is written in Kotlin consider the easypermissions-ktx library which adds Kotlin extensions to the core EasyPermissions library.

Installation

EasyPermissions is installed by adding the following dependency to your build.gradle file:

dependencies {
    // For developers using AndroidX in their applications
    implementation 'pub.devrel:easypermissions:3.0.0'
 
    // For developers using the Android Support Library
    implementation 'pub.devrel:easypermissions:2.0.1'
}

Usage

Basic

To begin using EasyPermissions, have your Activity (or Fragment) override the onRequestPermissionsResult method:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}

Request Permissions

The example below shows how to request permissions for a method that requires both CAMERA and ACCESS_FINE_LOCATION permissions. There are a few things to note:

  • Using EasyPermissions#hasPermissions(...) to check if the app already has the required permissions. This method can take any number of permissions as its final argument.
  • Requesting permissions with EasyPermissions#requestPermissions. This method will request the system permissions and show the rationale string provided if necessary. The request code provided should be unique to this request, and the method can take any number of permissions as its final argument.
  • Use of the AfterPermissionGranted annotation. This is optional, but provided for convenience. If all of the permissions in a given request are granted, all methods annotated with the proper request code will be executed(be sure to have an unique request code). The annotated method needs to be void and without input parameters (instead, you can use onSaveInstanceState in order to keep the state of your suppressed parameters). This is to simplify the common flow of needing to run the requesting method after all of its permissions have been granted. This can also be achieved by adding logic on the onPermissionsGranted callback.
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
private void methodRequiresTwoPermission() {
    String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
    if (EasyPermissions.hasPermissions(this, perms)) {
        // Already have permission, do the thing
        // ...
    } else {
        // Do not have permissions, request them now
        EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),
                RC_CAMERA_AND_LOCATION, perms);
    }
}

Or for finer control over the rationale dialog, use a PermissionRequest:

EasyPermissions.requestPermissions(
        new PermissionRequest.Builder(this, RC_CAMERA_AND_LOCATION, perms)
                .setRationale(R.string.camera_and_location_rationale)
                .setPositiveButtonText(R.string.rationale_ask_ok)
                .setNegativeButtonText(R.string.rationale_ask_cancel)
                .setTheme(R.style.my_fancy_style)
                .build());

Optionally, for a finer control, you can have your Activity / Fragment implement the PermissionCallbacks interface.

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

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

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> list) {
        // Some permissions have been granted
        // ...
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> list) {
        // Some permissions have been denied
        // ...
    }
}

Required Permissions

In some cases your app will not function properly without certain permissions. If the user denies these permissions with the "Never Ask Again" option, you will be unable to request these permissions from the user and they must be changed in app settings. You can use the method EasyPermissions.somePermissionPermanentlyDenied(...) to display a dialog to the user in this situation and direct them to the system setting screen for your app:

Note: Due to a limitation in the information provided by the Android framework permissions API, the somePermissionPermanentlyDenied method only works after the permission has been denied and your app has received the onPermissionsDenied callback. Otherwise the library cannot distinguish permanent denial from the "not yet denied" case.

@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
    Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());

    // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
    // This will display a dialog directing them to enable the permission in app settings.
    if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
        new AppSettingsDialog.Builder(this).build().show();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
        // Do something after user returned from app settings screen, like showing a Toast.
        Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)
                .show();
    }
}

Interacting with the rationale dialog

Implement the EasyPermissions.RationaleCallbacks if you want to interact with the rationale dialog.

@Override
public void onRationaleAccepted(int requestCode) {
    // Rationale accepted to request some permissions
    // ...
}

@Override
public void onRationaleDenied(int requestCode) {
    // Rationale denied to request some permissions
    // ...
}

Rationale callbacks don't necessarily imply permission changes. To check for those, see the EasyPermissions.PermissionCallbacks.

LICENSE

	Copyright 2017 Google

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].