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>
This commit is contained in:
Harsh Shandilya 2018-04-30 16:56:25 +05:30
parent 77c0d4dfa6
commit a6e530049c

View File

@ -6,6 +6,7 @@ import android.content.pm.PackageManager;
import android.os.Environment; import android.os.Environment;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.view.ContextThemeWrapper;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
@ -24,6 +25,7 @@ import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@ -102,7 +104,7 @@ public class ZipExporterPreference extends Preference {
final String message = getContext().getString(R.string.export_error, error); final String message = getContext().getString(R.string.export_error, error);
Log.e(TAG, message, throwable); Log.e(TAG, message, throwable);
Snackbar.make( Snackbar.make(
((SettingsActivity)getContext()).findViewById(android.R.id.content), Objects.requireNonNull(getPrefActivity(this)).findViewById(android.R.id.content),
message, Snackbar.LENGTH_LONG).show(); message, Snackbar.LENGTH_LONG).show();
} else { } else {
exportedFilePath = filePath; 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 @Override
protected void onClick() { protected void onClick() {
((SettingsActivity)getContext()).ensurePermissions( Objects.requireNonNull(getPrefActivity(this)).ensurePermissions(
new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
(permissions, granted) -> { (permissions, granted) -> {
if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED) if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED)