From a6e530049cfb3786cfbadf73fc9844cb7b16e4ef Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 30 Apr 2018 16:56:25 +0530 Subject: [PATCH] ZipExporterPreference: Correctly get preference activity In AppCompat based preferences, this#getContext returns an object of android.view.ContextThemeWrapper class from where we can safely extract a reference to our parent activity. Signed-off-by: Harsh Shandilya --- .../preference/ZipExporterPreference.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java b/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java index 3fd8f7db..d568fbf9 100644 --- a/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java +++ b/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java @@ -6,6 +6,7 @@ import android.content.pm.PackageManager; import android.os.Environment; import android.support.design.widget.Snackbar; import android.support.v7.preference.Preference; +import android.view.ContextThemeWrapper; import android.util.AttributeSet; import android.util.Log; @@ -24,6 +25,7 @@ import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -102,7 +104,7 @@ public class ZipExporterPreference extends Preference { final String message = getContext().getString(R.string.export_error, error); Log.e(TAG, message, throwable); Snackbar.make( - ((SettingsActivity)getContext()).findViewById(android.R.id.content), + Objects.requireNonNull(getPrefActivity(this)).findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show(); } else { exportedFilePath = filePath; @@ -111,9 +113,19 @@ public class ZipExporterPreference extends Preference { } } + private SettingsActivity getPrefActivity(Preference preference) { + Context context = preference.getContext(); + if (context instanceof ContextThemeWrapper) { + if (((ContextThemeWrapper) context).getBaseContext() instanceof SettingsActivity) { + return ((SettingsActivity) ((ContextThemeWrapper) context).getBaseContext()); + } + } + return null; + } + @Override protected void onClick() { - ((SettingsActivity)getContext()).ensurePermissions( + Objects.requireNonNull(getPrefActivity(this)).ensurePermissions( new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, (permissions, granted) -> { if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED)