Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Added RationaleDialogCallback (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest Saidu Kamara authored and samtstern committed Mar 15, 2018
1 parent 813e744 commit 20987dc
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 21 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
```

### Interacting with the rationale dialog

Implement the `EasyPermissions.RationaleCallbacks` if you want to interact with the rationale dialog.

```java
@Override
public void onRationaleAccepted(int requestCode) {
// Rationale accpets to request some permissions
// ...
}

@Override
public void onRationaleDenied(int requestCode) {
// Rationale denied to request some permissions
// ...
}
```

Rationale callbacks don't necessarily imply permission changes. To check for those, see the `EasyPermissions.RationaleCallbacks`.

## LICENSE

```
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks,
EasyPermissions.RationaleCallbacks{

private static final String TAG = "MainActivity";
private static final String[] LOCATION_AND_CONTACTS =
Expand Down Expand Up @@ -73,6 +74,10 @@ private boolean hasSmsPermission() {
return EasyPermissions.hasPermissions(this, Manifest.permission.READ_SMS);
}

private boolean hasStoragePermission() {
return EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}

@AfterPermissionGranted(RC_CAMERA_PERM)
public void cameraTask() {
if (hasCameraPermission()) {
Expand Down Expand Up @@ -148,4 +153,14 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
.show();
}
}

@Override
public void onRationaleAccepted(int requestCode) {
Log.d(TAG, "onRationaleAccepted:" + requestCode);
}

@Override
public void onRationaleDenied(int requestCode) {
Log.d(TAG, "onRationaleDenied:" + requestCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -49,7 +51,15 @@ public interface PermissionCallbacks extends ActivityCompat.OnRequestPermissions
void onPermissionsGranted(int requestCode, @NonNull List<String> perms);

void onPermissionsDenied(int requestCode, @NonNull List<String> perms);
}

/**
* Callback interface to receive button clicked events of the rationale dialog
*/
public interface RationaleCallbacks {
void onRationaleAccepted(int requestCode);

void onRationaleDenied(int requestCode);
}

private static final String TAG = "EasyPermissions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,27 @@ class RationaleDialogClickListener implements Dialog.OnClickListener {
private Object mHost;
private RationaleDialogConfig mConfig;
private EasyPermissions.PermissionCallbacks mCallbacks;
private EasyPermissions.RationaleCallbacks mRationaleCallbacks;

RationaleDialogClickListener(RationaleDialogFragmentCompat compatDialogFragment,
RationaleDialogConfig config,
EasyPermissions.PermissionCallbacks callbacks) {
EasyPermissions.PermissionCallbacks callbacks,
EasyPermissions.RationaleCallbacks rationaleCallbacks) {

mHost = compatDialogFragment.getParentFragment() != null
? compatDialogFragment.getParentFragment()
: compatDialogFragment.getActivity();

mConfig = config;
mCallbacks = callbacks;
mRationaleCallbacks = rationaleCallbacks;

}

RationaleDialogClickListener(RationaleDialogFragment dialogFragment,
RationaleDialogConfig config,
EasyPermissions.PermissionCallbacks callbacks) {
EasyPermissions.PermissionCallbacks callbacks,
EasyPermissions.RationaleCallbacks dialogCallback) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
mHost = dialogFragment.getParentFragment() != null ?
Expand All @@ -45,32 +50,37 @@ class RationaleDialogClickListener implements Dialog.OnClickListener {

mConfig = config;
mCallbacks = callbacks;
mRationaleCallbacks = dialogCallback;
}

@Override
public void onClick(DialogInterface dialog, int which) {
int requestCode = mConfig.requestCode;
if (which == Dialog.BUTTON_POSITIVE) {
String[] permissions = mConfig.permissions;
if (mRationaleCallbacks != null) {
mRationaleCallbacks.onRationaleAccepted(requestCode);
}
if (mHost instanceof Fragment) {
PermissionHelper.newInstance((Fragment) mHost).directRequestPermissions(
mConfig.requestCode, mConfig.permissions);
PermissionHelper.newInstance((Fragment) mHost).directRequestPermissions(requestCode, permissions);
} else if (mHost instanceof android.app.Fragment) {
PermissionHelper.newInstance((android.app.Fragment) mHost).directRequestPermissions(
mConfig.requestCode, mConfig.permissions);
PermissionHelper.newInstance((android.app.Fragment) mHost).directRequestPermissions(requestCode, permissions);
} else if (mHost instanceof Activity) {
PermissionHelper.newInstance((Activity) mHost).directRequestPermissions(
mConfig.requestCode, mConfig.permissions);
PermissionHelper.newInstance((Activity) mHost).directRequestPermissions(requestCode, permissions);
} else {
throw new RuntimeException("Host must be an Activity or Fragment!");
}
} else {
if (mRationaleCallbacks != null) {
mRationaleCallbacks.onRationaleDenied(requestCode);
}
notifyPermissionDenied();
}
}

private void notifyPermissionDenied() {
if (mCallbacks != null) {
mCallbacks.onPermissionsDenied(mConfig.requestCode,
Arrays.asList(mConfig.permissions));
mCallbacks.onPermissionsDenied(mConfig.requestCode, Arrays.asList(mConfig.permissions));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class RationaleDialogFragment extends DialogFragment {
public static final String TAG = "RationaleDialogFragment";

private EasyPermissions.PermissionCallbacks mPermissionCallbacks;
private EasyPermissions.RationaleCallbacks mRationaleCallbacks;
private boolean mStateSaved = false;

public static RationaleDialogFragment newInstance(
Expand All @@ -44,13 +45,23 @@ public static RationaleDialogFragment newInstance(
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
&& getParentFragment() != null
&& getParentFragment() instanceof EasyPermissions.PermissionCallbacks) {
mPermissionCallbacks = (EasyPermissions.PermissionCallbacks) getParentFragment();
} else if (context instanceof EasyPermissions.PermissionCallbacks) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && getParentFragment() != null) {
if (getParentFragment() instanceof EasyPermissions.PermissionCallbacks) {
mPermissionCallbacks = (EasyPermissions.PermissionCallbacks) getParentFragment();
}
if (getParentFragment() instanceof EasyPermissions.RationaleCallbacks){
mRationaleCallbacks = (EasyPermissions.RationaleCallbacks) getParentFragment();
}

}

if (context instanceof EasyPermissions.PermissionCallbacks) {
mPermissionCallbacks = (EasyPermissions.PermissionCallbacks) context;
}

if (context instanceof EasyPermissions.RationaleCallbacks) {
mRationaleCallbacks = (EasyPermissions.RationaleCallbacks) context;
}
}

@Override
Expand Down Expand Up @@ -93,7 +104,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get config from arguments, create click listener
RationaleDialogConfig config = new RationaleDialogConfig(getArguments());
RationaleDialogClickListener clickListener =
new RationaleDialogClickListener(this, config, mPermissionCallbacks);
new RationaleDialogClickListener(this, config, mPermissionCallbacks, mRationaleCallbacks);

// Create an AlertDialog
return config.createFrameworkDialog(getActivity(), clickListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class RationaleDialogFragmentCompat extends AppCompatDialogFragment {
public static final String TAG = "RationaleDialogFragmentCompat";

private EasyPermissions.PermissionCallbacks mPermissionCallbacks;
private EasyPermissions.RationaleCallbacks mRationaleCallbacks;

public static RationaleDialogFragmentCompat newInstance(
@NonNull String rationaleMsg,
Expand Down Expand Up @@ -54,17 +55,29 @@ public void showAllowingStateLoss(FragmentManager manager, String tag) {
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (getParentFragment() != null && getParentFragment() instanceof EasyPermissions.PermissionCallbacks) {
mPermissionCallbacks = (EasyPermissions.PermissionCallbacks) getParentFragment();
} else if (context instanceof EasyPermissions.PermissionCallbacks) {
if (getParentFragment() != null) {
if (getParentFragment() instanceof EasyPermissions.PermissionCallbacks) {
mPermissionCallbacks = (EasyPermissions.PermissionCallbacks) getParentFragment();
}
if (getParentFragment() instanceof EasyPermissions.RationaleCallbacks){
mRationaleCallbacks = (EasyPermissions.RationaleCallbacks) getParentFragment();
}
}

if (context instanceof EasyPermissions.PermissionCallbacks) {
mPermissionCallbacks = (EasyPermissions.PermissionCallbacks) context;
}

if (context instanceof EasyPermissions.RationaleCallbacks) {
mRationaleCallbacks = (EasyPermissions.RationaleCallbacks) context;
}
}

@Override
public void onDetach() {
super.onDetach();
mPermissionCallbacks = null;
mRationaleCallbacks = null;
}

@NonNull
Expand All @@ -76,7 +89,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get config from arguments, create click listener
RationaleDialogConfig config = new RationaleDialogConfig(getArguments());
RationaleDialogClickListener clickListener =
new RationaleDialogClickListener(this, config, mPermissionCallbacks);
new RationaleDialogClickListener(this, config, mPermissionCallbacks, mRationaleCallbacks);

// Create an AlertDialog
return config.createSupportDialog(getContext(), clickListener);
Expand Down

0 comments on commit 20987dc

Please sign in to comment.