KernelModuleDisabler: allow disabling the kernel module backend

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-03-09 15:19:02 -06:00
parent d61f17dbd3
commit 492fcce053
5 changed files with 93 additions and 1 deletions

View File

@ -75,7 +75,7 @@ public class Application extends android.app.Application implements SharedPrefer
} catch (final Exception ignored) {
}
}
if (ModuleLoader.isModuleLoaded()) {
if (!app.sharedPreferences.getBoolean("disable_kernel_module", false) && ModuleLoader.isModuleLoaded()) {
try {
if (!didStartRootShell)
app.rootShell.start();

View File

@ -117,10 +117,12 @@ public class SettingsActivity extends ThemeChangeAwareActivity {
});
final Preference moduleInstaller = getPreferenceManager().findPreference("module_downloader");
final Preference kernelModuleDisabler = getPreferenceManager().findPreference("kernel_module_disabler");
moduleInstaller.setVisible(false);
if (ModuleLoader.isModuleLoaded()) {
screen.removePreference(moduleInstaller);
} else {
screen.removePreference(kernelModuleDisabler);
Application.getAsyncWorker().runAsync(Application.getRootShell()::start).whenComplete((v, e) -> {
if (e == null)
moduleInstaller.setVisible(true);

View File

@ -0,0 +1,84 @@
/*
* Copyright © 2020 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.preference;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import com.wireguard.android.Application;
import com.wireguard.android.R;
import com.wireguard.android.backend.WgQuickBackend;
import com.wireguard.util.NonNullForAll;
import androidx.preference.Preference;
@NonNullForAll
public class KernelModuleDisablerPreference extends Preference {
private State state;
public KernelModuleDisablerPreference(final Context context, final AttributeSet attrs) {
super(context, attrs);
state = Application.getBackend() instanceof WgQuickBackend ? State.ENABLED : State.DISABLED;
}
@Override
public CharSequence getSummary() {
return getContext().getString(state.summaryResourceId);
}
@Override
public CharSequence getTitle() {
return getContext().getString(state.titleResourceId);
}
@Override
protected void onClick() {
if (state == State.DISABLED) {
setState(State.ENABLING);
Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", false).apply();
} else if (state == State.ENABLED) {
setState(State.DISABLING);
Application.getSharedPreferences().edit().putBoolean("disable_kernel_module", true).apply();
}
Application.getAsyncWorker().runAsync(() -> {
Thread.sleep(1000 * 5);
Intent i = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName());
if (i == null)
return;
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Application.get().startActivity(i);
System.exit(0);
});
}
private void setState(final State state) {
if (this.state == state)
return;
this.state = state;
if (isEnabled() != state.shouldEnableView)
setEnabled(state.shouldEnableView);
notifyChanged();
}
private enum State {
ENABLED(R.string.module_disabler_enabled_title, R.string.module_disabler_enabled_summary, true),
DISABLED(R.string.module_disabler_disabled_title, R.string.module_disabler_disabled_summary, true),
ENABLING(R.string.module_disabler_disabled_title, R.string.module_disabler_working, false),
DISABLING(R.string.module_disabler_enabled_title, R.string.module_disabler_working, false);
private final boolean shouldEnableView;
private final int summaryResourceId;
private final int titleResourceId;
State(final int titleResourceId, final int summaryResourceId, final boolean shouldEnableView) {
this.summaryResourceId = summaryResourceId;
this.titleResourceId = titleResourceId;
this.shouldEnableView = shouldEnableView;
}
}
}

View File

@ -103,6 +103,11 @@
<string name="module_installer_title">Download and install kernel module</string>
<string name="module_installer_working">Downloading and installing…</string>
<string name="module_installer_error">Something went wrong. Please try again</string>
<string name="module_disabler_disabled_title">Enable kernel module backend</string>
<string name="module_disabler_disabled_summary">The experimental kernel module can improve performance</string>
<string name="module_disabler_enabled_title">Disable kernel module backend</string>
<string name="module_disabler_enabled_summary">The slower userspace backend may improve stability</string>
<string name="module_disabler_working">The application will restart in 5 seconds</string>
<string name="mtu">MTU</string>
<string name="multiple_tunnels_title">Allow multiple simultaneous tunnels</string>
<string name="multiple_tunnels_summary_on">Multiple tunnels may be turned on simultaneously</string>

View File

@ -23,4 +23,5 @@
android:summaryOff="@string/dark_theme_summary_off"
android:summaryOn="@string/dark_theme_summary_on"
android:title="@string/dark_theme_title" />
<com.wireguard.android.preference.KernelModuleDisablerPreference android:key="kernel_module_disabler" />
</androidx.preference.PreferenceScreen>