Giter Club home page Giter Club logo

effortlesspermissions's Introduction

EffortlessPermissions

Icon

An Android permission library extending Google's EasyPermissions with convenient additions.

Don't say very easy, say effortless. โ€”โ€” 128 Words to Use Instead of "Very"

Google Play

Sample APK

Why EffortlessPermissions?

  • Used as a drop-in replacement for Google's EasyPermissions and based on its battle-tested implementation.
  • Added an @AfterPermissionDenied annotation for methods to run automatically after denial.
  • Included consumer ProGuard rules missing in EasyPermissions which fixes your release build.
  • Added more method overloads which make coding easier.
  • Added another DialogFragment to open app detail settings which you have more control upon, e.g. dialog title can be hidden now.

In a word, just start with EffortlessPermissions instead of EasyPermissions.

Integration

Gradle:

compile 'me.zhanghai.android.effortlesspermissions:library:1.1.0'

Usage

Just use EffortlessPermission wherever you would use EasyPermissions (documentation), and explore the improvements listed above!

And here is a fully-working sample implementation, handling permission requesting both normally and after permanent denial:

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_CODE_SAVE_FILE_PERMISSION = 1;
    private static final String[] PERMISSIONS_SAVE_FILE = {
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    ...

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

        // Dispatch to our library.
        EffortlessPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults,
                this);
    }

    // Call back to the same method so that we'll check and proceed.
    @AfterPermissionGranted(REQUEST_CODE_SAVE_FILE_PERMISSION)
    private void saveFile() {
        if (EffortlessPermissions.hasPermissions(this, PERMISSIONS_SAVE_FILE)) {
            // We've got the permission.
            saveFileWithPermission();
        } else {
            // Request the permissions.
            EffortlessPermissions.requestPermissions(this,
                    R.string.save_file_permission_request_message,
                    REQUEST_CODE_SAVE_FILE_PERMISSION, PERMISSIONS_SAVE_FILE);
        }
    }

    @AfterPermissionDenied(REQUEST_CODE_SAVE_FILE_PERMISSION)
    private void onSaveFilePermissionDenied() {
        if (EffortlessPermissions.somePermissionPermanentlyDenied(this, PERMISSIONS_SAVE_FILE)) {
            // Some permission is permanently denied so we cannot request them normally.
            OpenAppDetailsDialogFragment.show(
                    R.string.save_file_permission_permanently_denied_message,
                    R.string.open_settings, this);
        } else {
            // User denied at least some of the required permissions, report the error.
            Toast.makeText(this, R.string.save_file_permission_denied, Toast.LENGTH_SHORT).show();
        }
    }

    private void saveFileWithPermission() {
        // It's show time!
        Toast.makeText(this, R.string.save_file_show_time, Toast.LENGTH_SHORT).show();
    }
}

Without EffortlessPermissions, you would have to make your activity implement PermissionCallbacks, check request code and call permission denied callback manually. You would also need to remember writing the ProGuard rules for every project or you'll end up debugging your release build to find it out. But now, only the truly necessary code is written. Cheers!

ProGuard

The AAR of this library has already included a consumer ProGuard file to retain the annotations and annotated methods.

License

Copyright 2017 Zhang Hai

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.

effortlesspermissions's People

Contributors

zhanghai 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

Watchers

 avatar  avatar  avatar

effortlesspermissions's Issues

Annotations not being called

I tried using this library and I discovered that the effortless AfterPermissionGranted annotation is never called. This is true for the provided example. When I step the code with a debugger I am perplexed how this ever works.

Note the following code:

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

    // Dispatches to annotations
    EffortlessPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

at

https://github.com/DreaminginCodeZH/EffortlessPermissions/blob/master/library/src/main/java/me/zhanghai/android/effortlesspermissions/EffortlessPermissions.java#L131

the receivers parameter is an Array of 1 receiver. This method then delegates to EasyPermissions

https://github.com/googlesamples/easypermissions/blob/master/easypermissions/src/main/java/pub/devrel/easypermissions/EasyPermissions.java#L206

In this method the receivers is an Array of 2 the first element of the Array is another array of 1 receiver. Then this line fails:

https://github.com/googlesamples/easypermissions/blob/master/easypermissions/src/main/java/pub/devrel/easypermissions/EasyPermissions.java#L362

The clazz.getDeclaredMethods() is operating on an Array so it returns no methods.

The fix

    public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                                  @NonNull int[] grantResults,
                                                  @NonNull final Object... receivers) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, receivers);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults,
                new EasyPermissions.PermissionCallbacks() {
                    @Override
                    public void onPermissionsGranted(int requestCode,
                                                     List<String> grantedPermissions) {}
                    @Override
                    public void onPermissionsDenied(int requestCode,
                                                    List<String> deniedPermissions) {
                        for (Object object : receivers) {
                            runAfterPermissionDenied(object, requestCode, deniedPermissions);
                        }
                    }
                    @Override
                    public void onRequestPermissionsResult(int requestCode,
                                                           @NonNull String[] permissions,
                                                           @NonNull int[] grantResults) {}
                });
    }

NOTE: I am using the latest v26 of the Android SDK.

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.