This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Snackbar on permanently denied permission Listener (#260)
* Extract Listener behavior * Extract show Snackbar behavior * Create SnackbarOnPermanentlyDeniedPermissionListener * Create SnackbarOnAnyPermanentlyDeniedMultiplePermissionsListener
- Loading branch information
Showing
6 changed files
with
350 additions
and
53 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
dexter/src/main/java/com/karumi/dexter/listener/SettingsClickListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.karumi.dexter.listener; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
import android.provider.Settings; | ||
import android.view.View; | ||
|
||
public class SettingsClickListener implements View.OnClickListener { | ||
@Override | ||
public void onClick(View view) { | ||
Context context = view.getContext(); | ||
Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, | ||
Uri.parse("package:" + context.getPackageName())); | ||
myAppSettings.addCategory(Intent.CATEGORY_DEFAULT); | ||
myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
context.startActivity(myAppSettings); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
dexter/src/main/java/com/karumi/dexter/listener/SnackbarUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.karumi.dexter.listener; | ||
|
||
import android.view.View; | ||
|
||
import com.google.android.material.snackbar.BaseTransientBottomBar; | ||
import com.google.android.material.snackbar.Snackbar; | ||
|
||
public class SnackbarUtils { | ||
|
||
public static void show(View view, String text, int duration, String buttonText, | ||
View.OnClickListener onButtonClickListener, | ||
BaseTransientBottomBar.BaseCallback<Snackbar> snackbarCallback) { | ||
Snackbar snackbar = Snackbar.make(view, text, duration); | ||
if (buttonText != null && onButtonClickListener != null) { | ||
snackbar.setAction(buttonText, onButtonClickListener); | ||
} | ||
if (snackbarCallback != null) { | ||
snackbar.addCallback(snackbarCallback); | ||
} | ||
snackbar.show(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...rumi/dexter/listener/multi/SnackbarOnAnyPermanentlyDeniedMultiplePermissionsListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright (C) 2015 Karumi. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.karumi.dexter.listener.multi; | ||
|
||
import android.view.View; | ||
|
||
import com.google.android.material.snackbar.Snackbar; | ||
import com.karumi.dexter.MultiplePermissionsReport; | ||
import com.karumi.dexter.listener.SettingsClickListener; | ||
import com.karumi.dexter.listener.SnackbarUtils; | ||
|
||
import androidx.annotation.StringRes; | ||
|
||
/** | ||
* Utility listener that shows a {@link Snackbar} with a custom text whenever a permission has been | ||
* permanently denied | ||
*/ | ||
public class SnackbarOnAnyPermanentlyDeniedMultiplePermissionsListener | ||
extends BaseMultiplePermissionsListener { | ||
|
||
private final View view; | ||
private final String text; | ||
private final String buttonText; | ||
private final View.OnClickListener onButtonClickListener; | ||
private final Snackbar.Callback snackbarCallback; | ||
private final int duration; | ||
|
||
/** | ||
* @param view The view to find a parent from | ||
* @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 SnackbarOnAnyPermanentlyDeniedMultiplePermissionsListener(View view, String text, | ||
String buttonText, View.OnClickListener onButtonClickListener, | ||
Snackbar.Callback snackbarCallback, int duration) { | ||
this.view = view; | ||
this.text = text; | ||
this.buttonText = buttonText; | ||
this.onButtonClickListener = onButtonClickListener; | ||
this.snackbarCallback = snackbarCallback; | ||
this.duration = duration; | ||
} | ||
|
||
@Override public void onPermissionsChecked(MultiplePermissionsReport report) { | ||
super.onPermissionsChecked(report); | ||
|
||
if (!report.isAnyPermissionPermanentlyDenied()) { | ||
SnackbarUtils.show(view, text, duration, buttonText, onButtonClickListener, snackbarCallback); | ||
} | ||
} | ||
|
||
/** | ||
* Builder class to configure the displayed snackbar | ||
* Non set fields will not be shown | ||
*/ | ||
public static class Builder { | ||
private final View view; | ||
private final String text; | ||
private String buttonText; | ||
private View.OnClickListener onClickListener; | ||
private Snackbar.Callback snackbarCallback; | ||
private int duration = Snackbar.LENGTH_LONG; | ||
|
||
private Builder(View view, String text) { | ||
this.view = view; | ||
this.text = text; | ||
} | ||
|
||
public static Builder with(View view, String text) { | ||
return new Builder(view, text); | ||
} | ||
|
||
public static Builder with(View view, @StringRes int textResourceId) { | ||
return Builder.with(view, view.getContext().getString(textResourceId)); | ||
} | ||
|
||
/** | ||
* Adds a text button with the provided click listener | ||
*/ | ||
public Builder withButton(String buttonText, View.OnClickListener onClickListener) { | ||
this.buttonText = buttonText; | ||
this.onClickListener = onClickListener; | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds a text button with the provided click listener | ||
*/ | ||
public Builder withButton(@StringRes int buttonTextResourceId, | ||
View.OnClickListener onClickListener) { | ||
return withButton(view.getContext().getString(buttonTextResourceId), onClickListener); | ||
} | ||
|
||
/** | ||
* Adds a button that opens the application settings when clicked | ||
*/ | ||
public Builder withOpenSettingsButton(String buttonText) { | ||
this.buttonText = buttonText; | ||
this.onClickListener = new SettingsClickListener(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds a button that opens the application settings when clicked | ||
*/ | ||
public Builder withOpenSettingsButton(@StringRes int buttonTextResourceId) { | ||
return withOpenSettingsButton(view.getContext().getString(buttonTextResourceId)); | ||
} | ||
|
||
/** | ||
* Adds a callback to handle the snackbar {@code onDismissed} and {@code onShown} events. | ||
*/ | ||
public Builder withCallback(Snackbar.Callback callback) { | ||
this.snackbarCallback = callback; | ||
return this; | ||
} | ||
|
||
public Builder withDuration(int duration) { | ||
this.duration = duration; | ||
return this; | ||
} | ||
|
||
/** | ||
* Builds a new instance of {@link SnackbarOnAnyPermanentlyDeniedMultiplePermissionsListener} | ||
*/ | ||
public SnackbarOnAnyPermanentlyDeniedMultiplePermissionsListener build() { | ||
return new SnackbarOnAnyPermanentlyDeniedMultiplePermissionsListener(view, text, buttonText, | ||
onClickListener, snackbarCallback, duration); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.