diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2018-06-19 15:12:14 +0530 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-06-23 01:00:07 -0500 |
commit | 99cf2152c43ce0a4a0546fff1378e5b8aca2a173 (patch) | |
tree | e7d0966c75422f8e1973b107e868d7f2515c8e9a | |
parent | 53e8d425e9288b14a35461f65dccf54ab3d469e2 (diff) |
android: Consolidate getPrefActivity into FragmentUtils
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
[Samuel: Changed static to non-static import]
Signed-off-by: Samuel Holland <samuel@sholland.org>
3 files changed, 31 insertions, 28 deletions
diff --git a/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java b/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java index 36cc513e..4e4d4a4f 100644 --- a/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java +++ b/app/src/main/java/com/wireguard/android/preference/LogExporterPreference.java @@ -14,12 +14,11 @@ import android.support.design.widget.Snackbar; import android.support.v7.preference.Preference; import android.util.AttributeSet; import android.util.Log; -import android.view.ContextThemeWrapper; import com.wireguard.android.Application; import com.wireguard.android.R; -import com.wireguard.android.activity.SettingsActivity; import com.wireguard.android.util.ExceptionLoggers; +import com.wireguard.android.util.FragmentUtils; import java.io.BufferedReader; import java.io.File; @@ -40,16 +39,6 @@ public class LogExporterPreference extends Preference { super(context, attrs); } - private static SettingsActivity getPrefActivity(final Preference preference) { - final Context context = preference.getContext(); - if (context instanceof ContextThemeWrapper) { - if (((ContextThemeWrapper) context).getBaseContext() instanceof SettingsActivity) { - return ((SettingsActivity) ((ContextThemeWrapper) context).getBaseContext()); - } - } - return null; - } - private void exportLog() { Application.getAsyncWorker().supplyAsync(() -> { final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); @@ -90,7 +79,7 @@ public class LogExporterPreference extends Preference { final String message = getContext().getString(R.string.log_export_error, error); Log.e(TAG, message, throwable); Snackbar.make( - getPrefActivity(this).findViewById(android.R.id.content), + FragmentUtils.getPrefActivity(this).findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show(); setEnabled(true); } else { @@ -113,7 +102,7 @@ public class LogExporterPreference extends Preference { @Override protected void onClick() { - getPrefActivity(this).ensurePermissions( + FragmentUtils.getPrefActivity(this).ensurePermissions( new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, (permissions, granted) -> { if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED) { 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 98ab6eb3..2a326915 100644 --- a/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java +++ b/app/src/main/java/com/wireguard/android/preference/ZipExporterPreference.java @@ -14,13 +14,12 @@ import android.support.design.widget.Snackbar; import android.support.v7.preference.Preference; import android.util.AttributeSet; import android.util.Log; -import android.view.ContextThemeWrapper; import com.wireguard.android.Application; import com.wireguard.android.R; -import com.wireguard.android.activity.SettingsActivity; import com.wireguard.android.model.Tunnel; import com.wireguard.android.util.ExceptionLoggers; +import com.wireguard.android.util.FragmentUtils; import com.wireguard.config.Config; import java.io.File; @@ -47,16 +46,6 @@ public class ZipExporterPreference extends Preference { super(context, attrs); } - private static SettingsActivity getPrefActivity(final Preference preference) { - final Context context = preference.getContext(); - if (context instanceof ContextThemeWrapper) { - if (((ContextThemeWrapper) context).getBaseContext() instanceof SettingsActivity) { - return ((SettingsActivity) ((ContextThemeWrapper) context).getBaseContext()); - } - } - return null; - } - private void exportZip() { final List<Tunnel> tunnels = new ArrayList<>(Application.getTunnelManager().getTunnels()); final List<CompletableFuture<Config>> futureConfigs = new ArrayList<>(tunnels.size()); @@ -96,7 +85,7 @@ public class ZipExporterPreference extends Preference { final String message = getContext().getString(R.string.zip_export_error, error); Log.e(TAG, message, throwable); Snackbar.make( - getPrefActivity(this).findViewById(android.R.id.content), + FragmentUtils.getPrefActivity(this).findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show(); setEnabled(true); } else { @@ -119,7 +108,7 @@ public class ZipExporterPreference extends Preference { @Override protected void onClick() { - getPrefActivity(this).ensurePermissions( + FragmentUtils.getPrefActivity(this).ensurePermissions( new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, (permissions, granted) -> { if (granted.length > 0 && granted[0] == PackageManager.PERMISSION_GRANTED) { diff --git a/app/src/main/java/com/wireguard/android/util/FragmentUtils.java b/app/src/main/java/com/wireguard/android/util/FragmentUtils.java new file mode 100644 index 00000000..6e5ddee7 --- /dev/null +++ b/app/src/main/java/com/wireguard/android/util/FragmentUtils.java @@ -0,0 +1,25 @@ +/* + * Copyright © 2018 Harsh Shandilya <msfjarvis@gmail.com> + * Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. + * SPDX-License-Identifier: GPL-2.0-or-later + */ +package com.wireguard.android.util; + +import android.content.Context; +import android.support.v7.preference.Preference; +import android.view.ContextThemeWrapper; + +import com.wireguard.android.activity.SettingsActivity; + +public class FragmentUtils { + + public static SettingsActivity getPrefActivity(final Preference preference) { + final Context context = preference.getContext(); + if (context instanceof ContextThemeWrapper) { + if (((ContextThemeWrapper) context).getBaseContext() instanceof SettingsActivity) { + return ((SettingsActivity) ((ContextThemeWrapper) context).getBaseContext()); + } + } + return null; + } +} |