Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package pub.devrel.easypermissions.sample;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
Expand All @@ -39,12 +40,13 @@ public class MainActivity extends AppCompatActivity implements EasyPermissions.P

private static final int RC_CAMERA_PERM = 123;
private static final int RC_LOCATION_CONTACTS_PERM = 124;
private Context mContext;

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

mContext = this;
// Button click listener that will request one permission.
findViewById(R.id.button_camera).setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -80,17 +82,18 @@ private boolean hasStoragePermission() {

@AfterPermissionGranted(RC_CAMERA_PERM)
public void cameraTask() {
if (hasCameraPermission()) {
// Have permission, do the thing!
Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show();
} else {
// Ask for one permission
EasyPermissions.requestPermissions(
this,
getString(R.string.rationale_camera),
RC_CAMERA_PERM,
Manifest.permission.CAMERA);
}
EasyPermissions.autoRequestPermission(
this,
new EasyPermissions.AutoRequestPermissionCallbacks() {
@Override
public void onAutoRequestPermissionGranted() {
// Have permission, do the thing!
Toast.makeText(mContext, "TODO: Camera things", Toast.LENGTH_LONG).show();
}
},
getString(R.string.rationale_camera),
RC_CAMERA_PERM,
Manifest.permission.CAMERA);
}

@AfterPermissionGranted(RC_LOCATION_CONTACTS_PERM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,57 @@ public interface RationaleCallbacks {

private static final String TAG = "EasyPermissions";


/**
* @see #autoRequestPermission( Fragment, AutoRequestPermissionCallbacks,
* String,int,String...)
*/
public static void autoRequestPermission(
@NonNull Activity host,
@NonNull AutoRequestPermissionCallbacks autoRequestPermissionCallbacks,
@NonNull String rationale,
int requestCode,
@Size(min = 1) @NonNull String... perms) {
if (EasyPermissions.hasPermissions(host, perms)) {
autoRequestPermissionCallbacks.onAutoRequestPermissionGranted();
} else {
// Ask for permission you need
EasyPermissions.requestPermissions(host, rationale, requestCode, perms);
}
}

/**
* auto request permission if app don't have the permission you need.
*
* @param host requesting context.
* @param autoRequestPermissionCallbacks the call back receive the result
* @param rationale a message explaining why the application needs this set of permissions, will
* be displayed if the user rejects the request the first time.
* @param requestCode request code to track this request, must be < 256.
* @param perms one ore more permissions, such as {@link Manifest.permission#CAMERA}.
*/
public static void autoRequestPermission(
@NonNull Fragment host,
@NonNull AutoRequestPermissionCallbacks autoRequestPermissionCallbacks,
@NonNull String rationale,
int requestCode,
@Size(min = 1) @NonNull String... perms) {
if (EasyPermissions.hasPermissions(host.getContext(), perms)) {
autoRequestPermissionCallbacks.onAutoRequestPermissionGranted();
} else {
// Ask for permission you need
EasyPermissions.requestPermissions(host, rationale, requestCode, perms);
}
}

/**
* Callback interface to receive granted event of {@code EasyPermissions.autoRequestPermission()}
* calls.
*/
public interface AutoRequestPermissionCallbacks {
void onAutoRequestPermissionGranted();
}

/**
* Check if the calling context has a set of permissions.
*
Expand Down