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

Commit

Permalink
Fix snackbarlistener to avoid inflating a new view
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Gutierrez committed Nov 17, 2015
1 parent f3c398d commit 38064be
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,28 @@
import android.provider.Settings;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.karumi.dexter.R;

/**
* Utility listener that shows a {@link Snackbar} with a custom text whenever a permission has been
* denied
*/
public class SnackbarOnDeniedPermissionListener extends EmptyPermissionListener {

private final Context context;
private final ViewGroup rootView;
private final String text;
private final String buttonText;
private final View.OnClickListener onButtonClickListener;

/**
* @param context Context to inflate the snackbar
* @param rootView Parent view to show the snackbar
* @param text Message displayed in the snackbar
* @param buttonText Message displayed in the snackbar button
* @param onButtonClickListener Action performed when the user clicks the snackbar button
*/
private SnackbarOnDeniedPermissionListener(Context context, ViewGroup rootView, String text,
String buttonText, View.OnClickListener onButtonClickListener) {
this.context = context;
private SnackbarOnDeniedPermissionListener(ViewGroup rootView, String text, String buttonText,
View.OnClickListener onButtonClickListener) {
this.rootView = rootView;
this.text = text;
this.buttonText = buttonText;
Expand All @@ -58,9 +53,7 @@ private SnackbarOnDeniedPermissionListener(Context context, ViewGroup rootView,
@Override public void onPermissionDenied(String permission) {
super.onPermissionDenied(permission);

LayoutInflater inflater = LayoutInflater.from(context);
View snackbarView = inflater.inflate(R.layout.snackbar, rootView);
Snackbar snackbar = Snackbar.make(snackbarView, text, Snackbar.LENGTH_LONG);
Snackbar snackbar = Snackbar.make(rootView, text, Snackbar.LENGTH_LONG);
if (buttonText != null && onButtonClickListener != null) {
snackbar.setAction(buttonText, onButtonClickListener);
}
Expand All @@ -72,24 +65,22 @@ private SnackbarOnDeniedPermissionListener(Context context, ViewGroup rootView,
* Non set fields will not be shown
*/
public static class Builder {
private final Context context;
private final ViewGroup rootView;
private final String text;
private String buttonText;
private View.OnClickListener onClickListener;

private Builder(Context context, ViewGroup rootView, String text) {
this.context = context;
private Builder(ViewGroup rootView, String text) {
this.rootView = rootView;
this.text = text;
}

public static Builder with(Context context, ViewGroup rootView, String text) {
return new Builder(context, rootView, text);
public static Builder with(ViewGroup rootView, String text) {
return new Builder(rootView, text);
}

public static Builder with(Context context, ViewGroup rootView, @StringRes int textResourceId) {
return Builder.with(context, rootView, context.getString(textResourceId));
public static Builder with(ViewGroup rootView, @StringRes int textResourceId) {
return Builder.with(rootView, rootView.getContext().getString(textResourceId));
}

/**
Expand All @@ -106,7 +97,7 @@ public Builder withButton(String buttonText, View.OnClickListener onClickListene
*/
public Builder withButton(@StringRes int buttonTextResourceId,
View.OnClickListener onClickListener) {
return withButton(context.getString(buttonTextResourceId), onClickListener);
return withButton(rootView.getContext().getString(buttonTextResourceId), onClickListener);
}

/**
Expand All @@ -116,6 +107,7 @@ public Builder withOpenSettingsButton(String buttonText) {
this.buttonText = buttonText;
this.onClickListener = new View.OnClickListener() {
@Override public void onClick(View v) {
Context context = rootView.getContext();
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + context.getPackageName()));
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);
Expand All @@ -130,15 +122,14 @@ public Builder withOpenSettingsButton(String buttonText) {
* Adds a button that opens the application settings when clicked
*/
public Builder withOpenSettingsButton(@StringRes int buttonTextResourceId) {
return withOpenSettingsButton(context.getString(buttonTextResourceId));
return withOpenSettingsButton(rootView.getContext().getString(buttonTextResourceId));
}

/**
* Builds a new instance of {@link SnackbarOnDeniedPermissionListener}
*/
public SnackbarOnDeniedPermissionListener build() {
return new SnackbarOnDeniedPermissionListener(context, rootView, text, buttonText,
onClickListener);
return new SnackbarOnDeniedPermissionListener(rootView, text, buttonText, onClickListener);
}
}
}
24 changes: 0 additions & 24 deletions dexter/src/main/res/layout/snackbar.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ private void showPermissionDenied(TextView feedbackView) {
private void createPermissionListeners() {
cameraPermissionListener = new MultiPermissionListener(this,
SnackbarOnDeniedPermissionListener.Builder
.with(this, rootView, R.string.camera_permission_denied_feedback)
.with(rootView, R.string.camera_permission_denied_feedback)
.withOpenSettingsButton(R.string.permission_rationale_settings_button_text)
.build());
contactsPermissionListener = new MultiPermissionListener(this,
SnackbarOnDeniedPermissionListener.Builder
.with(this, rootView, R.string.contacts_permission_denied_feedback)
.with(rootView, R.string.contacts_permission_denied_feedback)
.withOpenSettingsButton(R.string.permission_rationale_settings_button_text)
.build());
PermissionListener dialogOnDeniedPermissionListener =
Expand Down

0 comments on commit 38064be

Please sign in to comment.