Skip to content

Instantly share code, notes, and snippets.

@lbalmaceda
Last active July 11, 2016 14:52
Show Gist options
  • Save lbalmaceda/7bb8a55d8ad34a2a51fb to your computer and use it in GitHub Desktop.
Save lbalmaceda/7bb8a55d8ad34a2a51fb to your computer and use it in GitHub Desktop.
Permissions on Android M
/*
* PermissionHandlerActivity.java
*
* Copyright (c) 2016 Auth0 (http://auth0.com)
*
* 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.
*/
package com.auth0.lock.util;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.PermissionChecker;
public abstract class PermissionHandlerActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback {
private static final int PERMISSION_REQ_CODE = 324;
/**
* Checks if the given permission has been granted by the user to this application before.
*
* @param permission to check availability for
* @return true if the permission is currently granted, false otherwise.
*/
protected boolean isPermissionGranted(@NonNull String permission) {
int result = ContextCompat.checkSelfPermission(this, permission);
return result == PermissionChecker.PERMISSION_GRANTED;
}
/**
* Starts the async request of the given permission.
*
* @param permission to request to the user
*/
protected void requestPermission(@NonNull String permission) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
//Show explanation dialog.
//Optionally, call requestPermission again on positive button click
} else {
ActivityCompat.requestPermissions(this,
new String[]{permission},
PERMISSION_REQ_CODE);
}
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode != PERMISSION_REQ_CODE || (permissions.length == 0 && grantResults.length == 0)) {
return;
}
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PermissionChecker.PERMISSION_GRANTED) {
onPermissionGranted(permissions[i]);
} else {
onPermissionDenied(permissions[i]);
}
}
}
/**
* Called when a permission request is accepted by the user.
*
* @param permission the user just accepted.
*/
protected abstract void onPermissionGranted(String permission);
/**
* Called when a permission request is declined by the user.
*
* @param permission the user just declined.
*/
protected abstract void onPermissionDenied(String permission);
}
/*
* PermissionHelper.java
*
* Copyright (c) 2016 Auth0 (http://auth0.com)
*
* 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.
*/
package com.auth0.lock.util;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.PermissionChecker;
public class PermissionHelper implements ActivityCompat.OnRequestPermissionsResultCallback {
private static final int PERMISSION_REQ_CODE = 324;
private final Activity context;
private final PermissionCallback callback;
public PermissionHandler(@NonNull Activity context, @NonNull PermissionCallback callback) {
this.context = context;
this.callback = callback;
}
/**
* Checks if the given permission has been granted by the user to this application before.
*
* @param permission to check availability for
* @return true if the permission is currently granted, false otherwise.
*/
public boolean isPermissionGranted(@NonNull String permission) {
int result = ContextCompat.checkSelfPermission(context, permission);
return result == PermissionChecker.PERMISSION_GRANTED;
}
/**
* Starts the async request of the given permission.
*
* @param permission to request to the user
*/
public void requestPermission(@NonNull String permission) {
if (ActivityCompat.shouldShowRequestPermissionRationale(context, permission)) {
//Show explanation dialog.
//Optionally, call requestPermission again on positive button click
} else {
ActivityCompat.requestPermissions(context,
new String[]{permission},
PERMISSION_REQ_CODE);
}
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode != PERMISSION_REQ_CODE || (permissions.length == 0 && grantResults.length == 0)) {
return;
}
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PermissionChecker.PERMISSION_GRANTED) {
callback.onPermissionGranted(permissions[i]);
} else {
callback.onPermissionDenied(permissions[i]);
}
}
}
public interface PermissionCallback {
/**
* Called when a permission request is accepted by the user.
*
* @param permission the user just accepted.
*/
void onPermissionGranted(String permission);
/**
* Called when a permission request is declined by the user.
*
* @param permission the user just declined.
*/
void onPermissionDenied(String permission);
}
}
@cliffgr
Copy link

cliffgr commented Apr 26, 2016

this methos never called when i accept Permission!!! Why?
public void onRequestPermissionsResult(int requestCode, @nonnull String[] permissions, @nonnull int[] grantResults)

@jbc25
Copy link

jbc25 commented Jul 11, 2016

@cliffgr You have to do it manually overriding onRequestPermissionsResult(...) in your Activity. Like this:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment