Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
Add OnClickListener to dialog shown when any permission is denied (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
JcMinarro authored Jun 8, 2020
1 parent 5e471c2 commit cee61bd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.karumi.dexter.listener;

public interface OnDialogButtonClickListener {

void onClick();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;

import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.listener.OnDialogButtonClickListener;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import com.karumi.dexter.MultiplePermissionsReport;

/**
* Utility listener that shows a {@link Dialog} with a minimum configuration when the user rejects
Expand All @@ -36,14 +39,17 @@ public class DialogOnAnyDeniedMultiplePermissionsListener extends BaseMultiplePe
private final String message;
private final String positiveButtonText;
private final Drawable icon;
private final OnDialogButtonClickListener onDialogButtonClickListener;

private DialogOnAnyDeniedMultiplePermissionsListener(Context context, String title,
String message, String positiveButtonText, Drawable icon) {
String message, String positiveButtonText, Drawable icon,
OnDialogButtonClickListener onDialogButtonClickListener) {
this.context = context;
this.title = title;
this.message = message;
this.positiveButtonText = positiveButtonText;
this.icon = icon;
this.onDialogButtonClickListener = onDialogButtonClickListener;
}

@Override public void onPermissionsChecked(MultiplePermissionsReport report) {
Expand All @@ -61,6 +67,7 @@ private void showDialog() {
.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onDialogButtonClickListener.onClick();
}
})
.setIcon(icon)
Expand All @@ -77,6 +84,7 @@ public static class Builder {
private String message;
private String buttonText;
private Drawable icon;
private OnDialogButtonClickListener onDialogButtonClickListener;

private Builder(Context context) {
this.context = context;
Expand Down Expand Up @@ -116,6 +124,18 @@ public Builder withButtonText(@StringRes int resId) {
return this;
}

public Builder withButtonText(String buttonText, OnDialogButtonClickListener onDialogButtonClickListener) {
this.buttonText = buttonText;
this.onDialogButtonClickListener = onDialogButtonClickListener;
return this;
}

public Builder withButtonText(@StringRes int resId, OnDialogButtonClickListener onDialogButtonClickListener) {
this.buttonText = context.getString(resId);
this.onDialogButtonClickListener = onDialogButtonClickListener;
return this;
}

public Builder withIcon(Drawable icon) {
this.icon = icon;
return this;
Expand All @@ -130,7 +150,16 @@ public DialogOnAnyDeniedMultiplePermissionsListener build() {
String title = this.title == null ? "" : this.title;
String message = this.message == null ? "" : this.message;
String buttonText = this.buttonText == null ? "" : this.buttonText;
return new DialogOnAnyDeniedMultiplePermissionsListener(context, title, message, buttonText, icon);
OnDialogButtonClickListener onDialogButtonClickListener =
this.onDialogButtonClickListener != null
? this.onDialogButtonClickListener
: new OnDialogButtonClickListener() {
@Override
public void onClick() {
}
};
return new DialogOnAnyDeniedMultiplePermissionsListener(context, title, message, buttonText, icon,
onDialogButtonClickListener);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;

import com.karumi.dexter.listener.OnDialogButtonClickListener;
import com.karumi.dexter.listener.PermissionDeniedResponse;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import com.karumi.dexter.listener.PermissionDeniedResponse;

/**
* Utility listener that shows a {@link android.app.Dialog} with a minimum configuration when the
Expand All @@ -35,14 +38,16 @@ public class DialogOnDeniedPermissionListener extends BasePermissionListener {
private final String message;
private final String positiveButtonText;
private final Drawable icon;
private final OnDialogButtonClickListener onDialogButtonClickListener;

private DialogOnDeniedPermissionListener(Context context, String title, String message,
String positiveButtonText, Drawable icon) {
String positiveButtonText, Drawable icon, OnDialogButtonClickListener onDialogButtonClickListener) {
this.context = context;
this.title = title;
this.message = message;
this.positiveButtonText = positiveButtonText;
this.icon = icon;
this.onDialogButtonClickListener = onDialogButtonClickListener;
}

@Override public void onPermissionDenied(PermissionDeniedResponse response) {
Expand All @@ -54,6 +59,7 @@ private DialogOnDeniedPermissionListener(Context context, String title, String m
.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
onDialogButtonClickListener.onClick();
}
})
.setIcon(icon)
Expand All @@ -70,6 +76,7 @@ public static class Builder {
private String message;
private String buttonText;
private Drawable icon;
private OnDialogButtonClickListener onDialogButtonClickListener;

private Builder(Context context) {
this.context = context;
Expand Down Expand Up @@ -109,6 +116,18 @@ public Builder withButtonText(@StringRes int resId) {
return this;
}

public Builder withButtonText(String buttonText, OnDialogButtonClickListener onDialogButtonClickListener) {
this.buttonText = buttonText;
this.onDialogButtonClickListener = onDialogButtonClickListener;
return this;
}

public Builder withButtonText(@StringRes int resId, OnDialogButtonClickListener onDialogButtonClickListener) {
this.buttonText = context.getString(resId);
this.onDialogButtonClickListener = onDialogButtonClickListener;
return this;
}

public Builder withIcon(Drawable icon) {
this.icon = icon;
return this;
Expand All @@ -123,7 +142,16 @@ public DialogOnDeniedPermissionListener build() {
String title = this.title == null ? "" : this.title;
String message = this.message == null ? "" : this.message;
String buttonText = this.buttonText == null ? "" : this.buttonText;
return new DialogOnDeniedPermissionListener(context, title, message, buttonText, icon);
OnDialogButtonClickListener onDialogButtonClickListener =
this.onDialogButtonClickListener != null
? this.onDialogButtonClickListener
: new OnDialogButtonClickListener() {
@Override
public void onClick() {
}
};
return new DialogOnDeniedPermissionListener(context, title, message, buttonText, icon,
onDialogButtonClickListener);
}
}
}

0 comments on commit cee61bd

Please sign in to comment.