Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Last active March 21, 2016 07:19
Show Gist options
  • Save vxhviet/c02f8dc0239259a65ac9 to your computer and use it in GitHub Desktop.
Save vxhviet/c02f8dc0239259a65ac9 to your computer and use it in GitHub Desktop.
Android M new runtime Permission

Source: StackOverflow, StackOverflow, inTheCheeseFactory

Library: could use hotchemi's PermissionsDispatcher

Answer:

// Storage Permissions
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private static final int REQUEST_PERMISSION_SETTING = 2;
    
    /**
     * Checks if the app has permission to read device storage
     * If the app does not has permission then the user will be prompted to grant permissions
     * @param activity
     */
    public void verifyStoragePermissions(Activity activity) {
        // Check if we have read external storage permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            if (! ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                showMessageOKCancel("You need to allow access to External Storage",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(
                                        MainActivity.this,
                                        PERMISSIONS_STORAGE,
                                        REQUEST_EXTERNAL_STORAGE
                                );
                            }
                        });
                return;
            }
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }else{
            selectVideo();
        }
    }
    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(MainActivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .create()
                .show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_EXTERNAL_STORAGE:
            {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, do YOUR THING.
                    Log.d(TAG, "permission granted");
                    selectVideo(); //do YOUR THING
                } else {
                    // permission denied. Prompt explanation and open settings so that the user can enable the required permissions
                    Toast.makeText(this, "Some Permission is Denied", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "permission denied");

                    showMessageOKCancel("This app need to access the External Storage to retrieve videos. Please enable Storage in Permissions!",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                                    intent.setData(uri);
                                    startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
                                }
                            });
                }
            }
            break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            if(requestCode == REQUEST_PERMISSION_SETTING){
                selectVideo(); //successfully retrieve the neccessary permission, do YOUR THING
            }
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment