diff options
Diffstat (limited to 'app/src/main/java/com')
5 files changed, 45 insertions, 20 deletions
diff --git a/app/src/main/java/com/wireguard/android/Application.java b/app/src/main/java/com/wireguard/android/Application.java index d0dc22d9..3ba9814c 100644 --- a/app/src/main/java/com/wireguard/android/Application.java +++ b/app/src/main/java/com/wireguard/android/Application.java @@ -46,6 +46,10 @@ public class Application extends android.app.Application { return get().asyncWorker; } + public static Backend getBackend() { + return get().backend; + } + public static Class getBackendType() { return get().backend.getClass(); } diff --git a/app/src/main/java/com/wireguard/android/backend/Backend.java b/app/src/main/java/com/wireguard/android/backend/Backend.java index 9a88550c..875cbd78 100644 --- a/app/src/main/java/com/wireguard/android/backend/Backend.java +++ b/app/src/main/java/com/wireguard/android/backend/Backend.java @@ -62,4 +62,19 @@ public interface Backend { * @return The updated state of the tunnel. */ State setState(Tunnel tunnel, State state) throws Exception; + + /** + * Determine version of underlying backend. + * + * @return The version of the backend. + * @throws Exception + */ + String getVersion() throws Exception; + + /** + * Determine type name of underlying backend. + * + * @return Type name + */ + String getTypeName(); } diff --git a/app/src/main/java/com/wireguard/android/backend/GoBackend.java b/app/src/main/java/com/wireguard/android/backend/GoBackend.java index 8c8d593a..10d8d911 100644 --- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -58,7 +58,11 @@ public final class GoBackend implements Backend { private static native String wgVersion(); - public static String getVersion() { return wgVersion(); } + @Override + public String getVersion() { return wgVersion(); } + + @Override + public String getTypeName() { return "Go userspace"; } @Override public Config applyConfig(final Tunnel tunnel, final Config config) throws Exception { diff --git a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java index 56d62a1f..2068e5f4 100644 --- a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java @@ -13,8 +13,6 @@ import com.wireguard.android.Application; import com.wireguard.android.model.Tunnel; import com.wireguard.android.model.Tunnel.State; import com.wireguard.android.model.Tunnel.Statistics; -import com.wireguard.android.util.RootShell; -import com.wireguard.android.util.ToolsInstaller; import com.wireguard.config.Config; import java.io.File; @@ -42,6 +40,18 @@ public final class WgQuickBackend implements Backend { } @Override + public String getVersion() throws Exception { + final List<String> output = new ArrayList<>(); + if (Application.getRootShell() + .run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty()) + throw new Exception("Unable to determine kernel module version"); + return output.get(0); + } + + @Override + public String getTypeName() { return "Kernel module"; } + + @Override public Config applyConfig(final Tunnel tunnel, final Config config) throws Exception { if (tunnel.getState() == State.UP) { // Restart the tunnel to apply the new config. diff --git a/app/src/main/java/com/wireguard/android/preference/VersionPreference.java b/app/src/main/java/com/wireguard/android/preference/VersionPreference.java index c42fe32a..ab51644f 100644 --- a/app/src/main/java/com/wireguard/android/preference/VersionPreference.java +++ b/app/src/main/java/com/wireguard/android/preference/VersionPreference.java @@ -14,6 +14,7 @@ import android.util.AttributeSet; import com.wireguard.android.Application; import com.wireguard.android.BuildConfig; import com.wireguard.android.R; +import com.wireguard.android.backend.Backend; import com.wireguard.android.backend.GoBackend; import com.wireguard.android.backend.WgQuickBackend; @@ -26,23 +27,14 @@ public class VersionPreference extends Preference { public VersionPreference(final Context context, final AttributeSet attrs) { super(context, attrs); - if (Application.getBackendType() == GoBackend.class) { - versionSummary = getContext().getString(R.string.version_userspace_summary, GoBackend.getVersion()); - } else if (Application.getBackendType() == WgQuickBackend.class) { - versionSummary = getContext().getString(R.string.version_kernel_summary_checking); - Application.getAsyncWorker().supplyAsync(() -> { - final List<String> output = new ArrayList<>(); - if (Application.getRootShell() - .run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty()) - throw new RuntimeException("Unable to determine kernel module version"); - return output.get(0); - }).whenComplete((version, exception) -> { - versionSummary = exception == null - ? getContext().getString(R.string.version_kernel_summary, version) - : getContext().getString(R.string.version_kernel_summary_unknown); - notifyChanged(); - }); - } + final Backend backend = Application.getBackend(); + versionSummary = getContext().getString(R.string.version_summary_checking, backend.getTypeName().toLowerCase()); + Application.getAsyncWorker().supplyAsync(backend::getVersion).whenComplete((version, exception) -> { + versionSummary = exception == null + ? getContext().getString(R.string.version_summary, backend.getTypeName(), version) + : getContext().getString(R.string.version_summary_unknown, backend.getTypeName().toLowerCase()); + notifyChanged(); + }); } @Override |