From 8634db69605aa5af2e73b04452660b0e7ab0da73 Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Fri, 20 Sep 2019 11:01:28 -0700 Subject: [PATCH] Put in defense for issue 278 Change-Id: If0e5dbc415baa13bba95935e2516a961e42c69d9 --- .../easypermissions/AppSettingsDialog.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/easypermissions/src/main/java/pub/devrel/easypermissions/AppSettingsDialog.java b/easypermissions/src/main/java/pub/devrel/easypermissions/AppSettingsDialog.java index 5fe4ea8..4407e4f 100644 --- a/easypermissions/src/main/java/pub/devrel/easypermissions/AppSettingsDialog.java +++ b/easypermissions/src/main/java/pub/devrel/easypermissions/AppSettingsDialog.java @@ -7,6 +7,7 @@ import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; +import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -19,11 +20,14 @@ /** * Dialog to prompt the user to go to the app's settings screen and enable permissions. If the user * clicks 'OK' on the dialog, they are sent to the settings screen. The result is returned to the - * Activity via {@link Activity#onActivityResult(int, int, Intent)}. + * Activity via {@see Activity#onActivityResult(int, int, Intent)}. *

* Use the {@link Builder} to create and display a dialog. */ public class AppSettingsDialog implements Parcelable { + + private static final String TAG = "EasyPermissions"; + public static final int DEFAULT_SETTINGS_REQ_CODE = 16061; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @@ -83,6 +87,19 @@ private AppSettingsDialog(@NonNull final Object activityOrFragment, static AppSettingsDialog fromIntent(Intent intent, Activity activity) { AppSettingsDialog dialog = intent.getParcelableExtra(AppSettingsDialog.EXTRA_APP_SETTINGS); + + // It's not clear how this could happen, but in the case that it does we should try + // to avoid a runtime crash and just use the default dialog. + // https://github.com/googlesamples/easypermissions/issues/278 + if (dialog == null) { + Log.e(TAG, "Intent contains null value for EXTRA_APP_SETTINGS: " + + "intent=" + intent + + ", " + + "extras=" + intent.getExtras()); + + dialog = new AppSettingsDialog.Builder(activity).build(); + } + dialog.setActivityOrFragment(activity); return dialog; } @@ -261,7 +278,7 @@ public Builder setPositiveButton(@StringRes int textId) { * Set the negative button text, default is {@link android.R.string#cancel}. *

* To know if a user cancelled the request, check if your permissions were given with {@link - * EasyPermissions#hasPermissions(Context, String...)} in {@link + * EasyPermissions#hasPermissions(Context, String...)} in {@see * Activity#onActivityResult(int, int, Intent)}. If you still don't have the right * permissions, then the request was cancelled. */ @@ -282,7 +299,7 @@ public Builder setNegativeButton(@StringRes int textId) { /** * Set the request code use when launching the Settings screen for result, can be retrieved - * in the calling Activity's {@link Activity#onActivityResult(int, int, Intent)} method. + * in the calling Activity's {@see Activity#onActivityResult(int, int, Intent)} method. * Default is {@link #DEFAULT_SETTINGS_REQ_CODE}. */ @NonNull