From 38064beeb7b98e20d46234ee7dff2160d5d6edf8 Mon Sep 17 00:00:00 2001 From: Sergio Gutierrez Date: Tue, 17 Nov 2015 14:51:17 +0100 Subject: [PATCH] Fix snackbarlistener to avoid inflating a new view --- .../SnackbarOnDeniedPermissionListener.java | 33 +++++++------------ dexter/src/main/res/layout/snackbar.xml | 24 -------------- .../karumi/dexter/sample/SampleActivity.java | 4 +-- 3 files changed, 14 insertions(+), 47 deletions(-) delete mode 100644 dexter/src/main/res/layout/snackbar.xml diff --git a/dexter/src/main/java/com/karumi/dexter/listener/SnackbarOnDeniedPermissionListener.java b/dexter/src/main/java/com/karumi/dexter/listener/SnackbarOnDeniedPermissionListener.java index f31a62ba..bd53f732 100644 --- a/dexter/src/main/java/com/karumi/dexter/listener/SnackbarOnDeniedPermissionListener.java +++ b/dexter/src/main/java/com/karumi/dexter/listener/SnackbarOnDeniedPermissionListener.java @@ -22,10 +22,8 @@ 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 @@ -33,22 +31,19 @@ */ 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; @@ -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); } @@ -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)); } /** @@ -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); } /** @@ -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); @@ -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); } } } diff --git a/dexter/src/main/res/layout/snackbar.xml b/dexter/src/main/res/layout/snackbar.xml deleted file mode 100644 index 23a16cb8..00000000 --- a/dexter/src/main/res/layout/snackbar.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - \ No newline at end of file diff --git a/sample/src/main/java/com/karumi/dexter/sample/SampleActivity.java b/sample/src/main/java/com/karumi/dexter/sample/SampleActivity.java index 4ce5f491..dd0e4dc3 100644 --- a/sample/src/main/java/com/karumi/dexter/sample/SampleActivity.java +++ b/sample/src/main/java/com/karumi/dexter/sample/SampleActivity.java @@ -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 =