Code Monkey home page Code Monkey logo

permissionutils's Introduction

Permission Utils

Download API GitHub issues GitHub forks GitHub stars GitHub license Android Arsenal

Logo

Check marshmallow permission easily

Import

At the moment the library is in my personal maven repo

repositories {
    maven {
        url 'http://dl.bintray.com/raphaelbussa/maven'
    }
}
dependencies {
    compile 'rebus:permission-utils:2.0.7'
}

How to use

Request a permission

First, override onRequestPermissionsResult and call PermissionManager.handleResult(requestCode, permissions, grantResults);

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionManager.handleResult(this, requestCode, permissions, grantResults);
}

Now you can ask permission :D

PermissionManager.Builder()
        .permission(PermissionEnum.WRITE_EXTERNAL_STORAGE)
        .askAgain(true)
        .askAgainCallback(new AskAgainCallback() {
            @Override
            public void showRequestPermission(UserResponse response) {
                    showDialog(response);
                }
            })
        .callback(new FullCallback() {
            @Override
            public void result(ArrayList<PermissionEnum> permissionsGranted, ArrayList<PermissionEnum> permissionsDenied, ArrayList<PermissionEnum> permissionsDeniedForever, ArrayList<PermissionEnum> permissionsAsked) {
                }
            })
        .ask(this);

or more permission

.permission(PermissionEnum.GET_ACCOUNTS, PermissionEnum.ACCESS_FINE_LOCATION, PermissionEnum.READ_SMS)

or

ArrayList<PermissionEnum> permissionEnumArrayList = new ArrayList<>();
permissionEnumArrayList.add(PermissionEnum.ACCESS_FINE_LOCATION);
permissionEnumArrayList.add(PermissionEnum.GET_ACCOUNTS);
permissionEnumArrayList.add(PermissionEnum.READ_CONTACTS);

.permissions(permissionEnumArrayList)

if you need to simply check a permission, just call utils

PermissionEnum permissionEnum = PermissionEnum.WRITE_EXTERNAL_STORAGE;
boolean granted = PermissionUtils.isGranted(MainActivity.this, PermissionEnum.WRITE_EXTERNAL_STORAGE);
Toast.makeText(MainActivity.this, permissionEnum.toString() + " isGranted [" + granted + "]", Toast.LENGTH_SHORT).show()

Callbacks

You can use three different callback, it depends of your needs.

  • FullCallback: gives you all the information on permission requested by you
  • SimpleCallback: returns a boolean that says if all permission requests were permitted
  • SmartCallback: returns a boolean that says if all permission requests were permitted and a boolean that says if some permissions are denied forever

Little extra

If user answer "Never ask again" to a request for permission, you can redirect user to app settings, with an utils

PermissionUtils.openApplicationSettings(MainActivity.this, R.class.getPackage().getName());

That's all folks!

Sample

Browse the sample code here

Javadoc

Browse Javadoc here

App using Permission Utils

If you use this lib contact me and I will add it to the list below:

Developed By

Raphaël Bussa - [email protected]

Twitter Linkedin

License

The MIT License (MIT)

Copyright (c) 2017-2019 Raphaël Bussa

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.

permissionutils's People

Contributors

alorma avatar raphaelbussa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

permissionutils's Issues

Missing Permissions in Enum List

Hi,

Thanks for a very neat library!

My application needs plenty permissions. These are found in Manifest.permission.*, but they are not in this library's PermissionsEnum.

I declare my Permission List as such:

public static ArrayList<PermissionEnum> permissionList = new ArrayList<PermissionEnum>() {

    {

        add(PermissionEnum.ACCESS_COARSE_LOCATION);
        add(PermissionEnum.ACCESS_FINE_LOCATION);
        add(PermissionEnum.ACCESS_WIFI_STATE); //not in enum list
        add(PermissionEnum.ACCESS_NETWORK_STATE); //not in enum list
        add(PermissionEnum.BLUETOOTH); //not in enum list
        add(PermissionEnum.BLUETOOTH_ADMIN); //not in enum list
        add(PermissionEnum.CAMERA);
        add(PermissionEnum.CHANGE_WIFI_STATE); //not in enum list
        add(PermissionEnum.FLASHLIGHT); //not in enum list
        add(PermissionEnum.INTERNET); //not in enum list
        add(PermissionEnum.READ_LOGS); //not in enum list
        add(PermissionEnum.READ_OWNER_DATA); //not in enum list
        add(PermissionEnum.READ_PHONE_STATE);
        add(PermissionEnum.RECORD_AUDIO);
        add(PermissionEnum.RECEIVE_SMS);
        add(PermissionEnum.READ_EXTERNAL_STORAGE);
        add(PermissionEnum.USB_PERMISSION); //not in enum list
        add(PermissionEnum.VIBRATE); //not in enum list
        add(PermissionEnum.WRITE_EXTERNAL_STORAGE);
        add(PermissionEnum.WRITE_OWNER_DATA); //not in enum list
        add(PermissionEnum.WRITE_SETTINGS); //not in enum list
        add(PermissionEnum.WAKE_LOCK); //not in enum list

    }

};

Is there a work around to include these which are not in enum list?

Regards,

Willem

FullCallback not triggered for denied and denied for ever

Below is what my FullCallback implementation looks like.

new FullCallback() {@
    Override
    public void result(ArrayList < PermissionEnum > permissionsGranted,
        ArrayList < PermissionEnum > permissionsDenied,
        ArrayList < PermissionEnum > permissionsDeniedForever,
        ArrayList < PermissionEnum > permissionsAsked) {

        Log.d("DEBUG", "call back"); // Not printed if I deny, or deny forever.

        for (PermissionEnum permissionEnum: permissionsGranted) {
            if (permissionEnum.equals(PermissionEnum.CAMERA)) {
                Log.d("DEBUG", "granted!");
            }
        }

        for (PermissionEnum permissionEnum: permissionsDenied) {
            if (permissionEnum.equals(PermissionEnum.CAMERA)) {
                Log.d("DEBUG", "denied!");
            }
        }

        for (PermissionEnum permissionEnum: permissionsDeniedForever) {
            if (permissionEnum.equals(PermissionEnum.CAMERA)) {
                Log.d("DEBUG", "denied for ever!");
            }
        }

        for (PermissionEnum permissionEnum: permissionsAsked) {
            if (permissionEnum.equals(PermissionEnum.CAMERA)) {
                Log.d("DEBUG", "asked!");
            }
        }
    }
}

Support library 26.0.2

Hi, great library i just want to reminds you to update support libraries to 26.0.2 (the latest until now :))

can init the request using Fragment(in support library)

currently the util just support using context, hence if using Fragment.getContext() as the parameter to init the permission util we still have to put PermissionManager.handleResult(requestCode, permissions, grantResults);
in the activity's onRequestPermissionsResult()

When fragment deny permissions automatic finish current fragment

Hello

I have use your library but one scenario is suppose in fragment apply permission manager and onRequestPermissionsResult handleResult this resule but after deny permission current fragment is automatic finish if you have any solution

Fragment code is-
PermissionManager.Builder()
.permission(PermissionEnum.WRITE_EXTERNAL_STORAGE)
.askAgain(true)
.callback(new FullCallback() {
public void result(ArrayList<PermissionEnum> permissionsGranted, ArrayList<PermissionEnum> permissionsDenied, ArrayList<PermissionEnum> permissionsDeniedForever, ArrayList<PermissionEnum> permissionsAsked) {
}
})
.ask(EditProfileFragment.this);

And On Result permission handle
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionManager.handleResult(EditProfileFragment.this, requestCode, permissions, grantResults);

Thanks
JD

How to Proceed

Where should i write my own code ,if Permission Accepted and where should i display my else result which is nothing but Asking again Permissions .Explanation would be helpful @Rebus007 .It was awesome !!

Dialog for permission not shown

  • Permission Utils version: 2.0.6
  • Device OS version: 6.0
  • Device Manufacturer: Alcatel
  • Device Name: Cocoa Gray B

I just copied the example code for showing dialog, but the dialog is not shown

PermissionManager.Builder()
                .permission(permissions)
                .askAgain(true)
                .askAgainCallback(new AskAgainCallback() {
                    @Override
                    public void showRequestPermission(AskAgainCallback.UserResponse response) {
                        showDialog(response);
                    }
                })
                .callback(new FullCallback() {
                    @Override
                    public void result(ArrayList<PermissionEnum> permissionsGranted, ArrayList<PermissionEnum> permissionsDenied, ArrayList<PermissionEnum> permissionsDeniedForever, ArrayList<PermissionEnum> permissionsAsked) {
                    }
                })
                .ask(this);

This is the showDialog function:

new AlertDialog.Builder(this)
                .setTitle("Permission needed")
                .setMessage("This app really need to use this permission, you wont to authorize it?")
                .setPositiveButton("OK", (dialogInterface, i) -> response.result(true))
                .setNegativeButton("NOT NOW", (dialogInterface, i) -> response.result(false))
                .show();
Expected Result

The dialog is shown.

Actual Result

Nothing happens.

Can reqeust only one set of permissions at a time

Hello, i use your lib in my project in fragment and i add permission check in util class

public static boolean checkVoicePermission(final Fragment fragment, final PermissionEnum permissionEnum) {
final boolean[] hasPermission = {false};

    PermissionManager.Builder()
            .permission(permissionEnum)
            .askAgain(true)
            .askAgainCallback(new AskAgainCallback() {
                @Override
                public void showRequestPermission(UserResponse response) {
                }

            })

            .callback(new FullCallback() {
                @Override
                public void result(ArrayList<PermissionEnum> permissionsGranted, ArrayList<PermissionEnum> permissionsDenied, ArrayList<PermissionEnum> permissionsDeniedForever, ArrayList<PermissionEnum> permissionsAsked) {

                    if (permissionsDeniedForever.contains(permissionEnum)) {
                        Snackbar snackbar = Snackbar
                                .make(fragment.getActivity().findViewById(android.R.id.content)
                                        .findViewById(android.R.id.content), fragment.getActivity().getResources().getString(R.string.permission_content), Snackbar.LENGTH_LONG)
                                .setAction(fragment.getActivity().getResources().getString(R.string.global_settings), new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Intent intent = new Intent();
                                        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                        Uri uri = Uri.fromParts("package", fragment.getActivity().getPackageName(), null);
                                        intent.setData(uri);
                                        fragment.getActivity().startActivity(intent);

                                    }
                                });

                        snackbar.show();
                    }
                    if (permissionsGranted.contains(permissionEnum)) {

                        hasPermission[0] = true;

                    }
                }
            })
            .ask(fragment);
    return hasPermission[0];
}

but when shoose permissionsDeniedForever i have this erreur
Can reqeust only one set of permissions at a time

PermissionUtls.isGranted() is not correct when you forbid permission in system settings

  • I have verified there are no duplicate active or recent bugs, questions, or requests
  • I have verified that I am using the latest version of Permission Utils.
  • I have given my issue a non-generic title.
  • I have read over the documentation (before asking questions on how to do something).
Include the following:
  • Permission Utils version: x.x.x
  • Device OS version: 7.0
  • Device Manufacturer: LG
  • Device Name: Nexus 5X

Also, please wrap Java with correct syntax highlighting.

System.out.println("Hello, world!");
Reproduction Steps
Expected Result
Actual Result

direct call to result() when starting from GooglePlay (just after install)

  • [ x] I have verified there are no duplicate active or recent bugs, questions, or requests
  • [ x] I have verified that I am using the latest version of Permission Utils.
  • [x ] I have given my issue a non-generic title.
  • [x ] I have read over the documentation (before asking questions on how to do something).
Include the following:
  • Permission Utils version: 1.0.9
  • Device OS version: 6.0
  • Device Manufacturer: ASUS
  • Device Name: Nexus 7 2013

I've implemented your library like that in my MainActivity

    private void showDialog(final AskAgainCallback.UserResponse response, String title, String msg) {
        new AlertDialog.Builder(this)
                .setTitle(getResources().getString(R.string.dialog_title_warning))
                .setMessage(getResources().getString(R.string.perm_location_explain) +
                        getResources().getString(R.string.perm_storage_explain) +
                        getResources().getString(R.string.perm_calendar_explain)
                )
                .setPositiveButton(getResources().getString(R.string.label_positiveBtnContinue), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        response.result(true);
                    }
                })
                .setNegativeButton(getResources().getString(R.string.label_negativeBtnCancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        response.result(false);
                    }
                })
                .setCancelable(false)
                .show();
    }

    @Override
    public void result(ArrayList<PermissionEnum> permissionsGranted, ArrayList<PermissionEnum> permissionsDenied, ArrayList<PermissionEnum> permissionsDeniedForever, ArrayList<PermissionEnum> permissionsAsked) {
        List<String> msg = new ArrayList<>();

        int neededPermissions = 0;

        for (PermissionEnum permissionEnum : permissionsGranted) {
            if (PermissionEnum.ACCESS_FINE_LOCATION.equals(permissionEnum)) {
                neededPermissions++;
            } else if (PermissionEnum.WRITE_EXTERNAL_STORAGE.equals(permissionEnum)) {
                neededPermissions++;
            }
        }

        if (neededPermissions >= 2) { // I really need these 2 permissions
            startApp();
        } else {
            msg.add(getResources().getString(R.string.perm_all_refused) + getResources().getString(R.string.perm_go_settings));
        }

        for (PermissionEnum permissionEnum : permissionsDenied) {

            if (PermissionEnum.ACCESS_FINE_LOCATION.equals(permissionEnum)) {
                msg.add(getResources().getString(R.string.perm_location_explain));
            } else if (PermissionEnum.WRITE_EXTERNAL_STORAGE.equals(permissionEnum)) {
                msg.add(getResources().getString(R.string.perm_storage_explain));
            } else if (PermissionEnum.WRITE_CALENDAR.equals(permissionEnum)) {
                msg.add(getResources().getString(R.string.perm_calendar_explain));
            }
        }

        if (permissionsDeniedForever.size() > 0) {
            msg.add(getResources().getString(R.string.perm_go_settings));
        }

        if (msg.size() > 0) {
            String[] items = msg.toArray(new String[msg.size()]);
            new AlertDialog.Builder(this)
                    .setTitle(getResources().getString(R.string.label_DlgTitleInfo))
                    .setItems(items, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                        }
                    })
                    .show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionManager.handleResult(requestCode, permissions, grantResults);
    }
Reproduction Steps

If I click on open from the PlayStore, it doesn't ask me any permission and directly displays the msg generated by the else in the following code:

    if (neededPermissions >= 2) {
        startApp();
    } else {
        msg.add(getResources().getString(R.string.perm_all_refused) + getResources().getString(R.string.perm_go_settings));
    }

If I quit and kill the app and restart it, it asks me for the permissions as expected.

I don't see how your result() callback can be directly called.
Any idea?

Read me error

good project~.
the document has a litter error, please modify it.
2016-08-20_115148

permissionGranted is always false if deniedForever and then enabled from settings

  • [ x] I have verified there are no duplicate active or recent bugs, questions, or requests
  • [ x] I have verified that I am using the latest version of Permission Utils.
  • [ x] I have given my issue a non-generic title.
  • [ x] I have read over the documentation (before asking questions on how to do something).
Include the following:
  • Permission Utils version: 3.0.1
  • Device OS version: 7.0
  • Device Manufacturer: Samsung
  • Device Name: A5

Also, please wrap Java with correct syntax highlighting.

PermissionManager.Builder()
                    .key(Constants.REQUEST_PERMISSION_LOCATION)
                    .permission(PermissionEnum.GROUP_LOCATION)
                    .askAgain(true)
                    .callback { permissionsGranted ->
                        //permissionsGranted is false here
                    }
                    .ask(this)
System.out.println("Hello, world!");
Reproduction Steps
  1. Click on never ask again for permission
  2. Go to settings and enable the permission
  3. in callback(permissionGranted) -> permissionGranted is still false
Expected Result

permissionsGranted should give true

Actual Result

permissionsGranted gives false

PermissionUtils.isGranted(this, PermissionEnum.GROUP_LOCATION) gives true

fragment?

sorry,This will use the exception in the Fragment.....

Leak in PermissionManager instance

  • [x ] I have verified there are no duplicate active or recent bugs, questions, or requests
  • [x ] I have verified that I am using the latest version of Permission Utils.
  • [ x] I have given my issue a non-generic title.
  • [ x] I have read over the documentation (before asking questions on how to do something).
Include the following:
  • Permission Utils version: 1.0.9
  • Device OS version: 7.0
  • Device Manufacturer: Samsung
  • Device Name: S7 Edge

Also, please wrap Java with correct syntax highlighting.

PermissionManager.with(HomeActivity.this)
						 .key(RC_CAMERA)
						 .permission(PermissionEnum.CAMERA)
						 .askAgain(true)
						 .askAgainCallback(new AskAgainCallback() {
							 @Override
							 public void showRequestPermission(UserResponse response) {
							 }
						 })
						 .callback(new FullCallback() {
							 @Override
							 public void result(ArrayList<PermissionEnum> permissionsGranted, ArrayList<PermissionEnum> permissionsDenied,
												ArrayList<PermissionEnum> permissionsDeniedForever, ArrayList<PermissionEnum> permissionsAsked) {
								 if (!permissionsGranted.isEmpty() && permissionsGranted.contains(PermissionEnum.CAMERA)) {
									 startPublishWirecastPage();
								 } else if (!permissionsDeniedForever.isEmpty()
										 || permissionsDeniedForever.contains(PermissionEnum.CAMERA)
										 ) {
								 }
							 }
						 })
						 .ask();
Reproduction Steps
  1. Use Permission Manager anywhere
Expected Result

Should not leak

Actual Result

Leakcanary shows a leak in PermissionManager instance

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.