diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2018-04-30 16:56:25 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2018-04-30 17:30:25 +0530 |
commit | a6e530049cfb3786cfbadf73fc9844cb7b16e4ef (patch) | |
tree | 61ab9d40692ccfd086f1564280008bed244b4b99 /app/src/main/java/com/wireguard | |
parent | 77c0d4dfa6d46c13632832b8f882e6121e7bef0b (diff) |
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 <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/wireguard')
-rw-r--r-- | app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java | 16 |
1 files 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) |