aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorHarsh Shandilya <harsh@prjkt.io>2018-04-30 16:56:25 +0530
committerHarsh Shandilya <harsh@prjkt.io>2018-04-30 17:30:25 +0530
commitdca18a98aae49770afd5ee4d6140a2753183c282 (patch)
tree61ab9d40692ccfd086f1564280008bed244b4b99
parentTunnelListFragment: Use Collections methods in place of Arrays (diff)
downloadwireguard-android-dca18a98aae49770afd5ee4d6140a2753183c282.tar.xz
wireguard-android-dca18a98aae49770afd5ee4d6140a2753183c282.zip
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 <harsh@prjkt.io>
-rw-r--r--app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java16
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)