From 99cf2152c43ce0a4a0546fff1378e5b8aca2a173 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 19 Jun 2018 15:12:14 +0530 Subject: [PATCH] android: Consolidate getPrefActivity into FragmentUtils Signed-off-by: Harsh Shandilya [Samuel: Changed static to non-static import] Signed-off-by: Samuel Holland --- .../preference/LogExporterPreference.java | 17 +++---------- .../preference/ZipExporterPreference.java | 17 +++---------- .../wireguard/android/util/FragmentUtils.java | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/com/wireguard/android/util/FragmentUtils.java 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 tunnels = new ArrayList<>(Application.getTunnelManager().getTunnels()); final List> 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 + * Copyright © 2018 Jason A. Donenfeld . 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; + } +}