diff options
Diffstat (limited to 'ui/src/main/java/com/wireguard')
9 files changed, 62 insertions, 60 deletions
diff --git a/ui/src/main/java/com/wireguard/android/Application.java b/ui/src/main/java/com/wireguard/android/Application.java index c0c89fcd..62cf25a1 100644 --- a/ui/src/main/java/com/wireguard/android/Application.java +++ b/ui/src/main/java/com/wireguard/android/Application.java @@ -79,7 +79,7 @@ public class Application extends android.app.Application implements SharedPrefer try { if (!didStartRootShell) app.rootShell.start(); - WgQuickBackend wgQuickBackend = new WgQuickBackend(app.getApplicationContext(), app.rootShell, app.toolsInstaller); + final WgQuickBackend wgQuickBackend = new WgQuickBackend(app.getApplicationContext(), app.rootShell, app.toolsInstaller); wgQuickBackend.setMultipleTunnels(app.sharedPreferences.getBoolean("multiple_tunnels", false)); backend = wgQuickBackend; } catch (final Exception ignored) { diff --git a/ui/src/main/java/com/wireguard/android/activity/MainActivity.java b/ui/src/main/java/com/wireguard/android/activity/MainActivity.java index d9bbee20..61ae63e3 100644 --- a/ui/src/main/java/com/wireguard/android/activity/MainActivity.java +++ b/ui/src/main/java/com/wireguard/android/activity/MainActivity.java @@ -10,7 +10,6 @@ import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; -import android.view.View.OnApplyWindowInsetsListener; import android.widget.LinearLayout; import com.wireguard.android.R; @@ -78,7 +77,7 @@ public class MainActivity extends BaseActivity onBackStackChanged(); // Dispatch insets on back stack change // This is required to ensure replaced fragments are also able to consume insets - findViewById(R.id.master_detail_wrapper).setOnApplyWindowInsetsListener((OnApplyWindowInsetsListener) (v, insets) -> { + findViewById(R.id.master_detail_wrapper).setOnApplyWindowInsetsListener((v, insets) -> { final FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.addOnBackStackChangedListener(() -> { final List<Fragment> fragments = fragmentManager.getFragments(); @@ -97,6 +96,7 @@ public class MainActivity extends BaseActivity } @Override + @SuppressWarnings("UnnecessaryFullyQualifiedName") public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { case android.R.id.home: diff --git a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java index 5d8e91f9..a0726d55 100644 --- a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java +++ b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.java @@ -67,14 +67,13 @@ public class SettingsActivity extends ThemeChangeAwareActivity { } @Override + @SuppressWarnings("UnnecessaryFullyQualifiedName") public boolean onOptionsItemSelected(final MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - return true; - default: - return super.onOptionsItemSelected(item); + if (item.getItemId() == android.R.id.home) { + finish(); + return true; } + return super.onOptionsItemSelected(item); } @Override @@ -100,7 +99,7 @@ public class SettingsActivity extends ThemeChangeAwareActivity { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) screen.removePreference(getPreferenceManager().findPreference("dark_theme")); - final Preference wgQuickOnlyPrefs[] = { + final Preference[] wgQuickOnlyPrefs = { getPreferenceManager().findPreference("tools_installer"), getPreferenceManager().findPreference("restore_on_boot"), getPreferenceManager().findPreference("multiple_tunnels") diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java b/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java index e6ac7092..eb76a220 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java @@ -37,6 +37,7 @@ public class TunnelDetailFragment extends BaseFragment { @Nullable private State lastState = State.TOGGLE; @Nullable private Timer timer; + @SuppressWarnings("MagicNumber") private String formatBytes(final long bytes) { if (bytes < 1024) return requireContext().getString(R.string.transfer_bytes, bytes); diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java index 811157cc..cf3ee34b 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java @@ -21,6 +21,7 @@ import android.widget.Toast; import com.google.android.material.snackbar.Snackbar; import com.wireguard.android.Application; import com.wireguard.android.R; +import com.wireguard.android.backend.Tunnel; import com.wireguard.android.databinding.TunnelEditorFragmentBinding; import com.wireguard.android.fragment.AppListDialogFragment.AppExclusionListener; import com.wireguard.android.model.ObservableTunnel; @@ -57,8 +58,7 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi } } - private void onConfigSaved(final ObservableTunnel savedTunnel, - @Nullable final Throwable throwable) { + private void onConfigSaved(final Tunnel savedTunnel, @Nullable final Throwable throwable) { final String message; if (throwable == null) { message = getString(R.string.config_save_success, savedTunnel.getName()); @@ -118,14 +118,14 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi if (activity == null) return; final View focusedView = activity.getCurrentFocus(); if (focusedView != null) { - final Object service = activity.getSystemService(Context.INPUT_METHOD_SERVICE); - final InputMethodManager inputManager = (InputMethodManager) service; + final InputMethodManager inputManager = (InputMethodManager) + activity.getSystemService(Context.INPUT_METHOD_SERVICE); if (inputManager != null) inputManager.hideSoftInputFromWindow(focusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } // Tell the activity to finish itself or go back to the detail view. - getActivity().runOnUiThread(() -> { + requireActivity().runOnUiThread(() -> { // TODO(smaeul): Remove this hack when fixing the Config ViewModel // The selected tunnel has to actually change, but we have to remember this one. final ObservableTunnel savedTunnel = tunnel; @@ -137,39 +137,37 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi @Override public boolean onOptionsItemSelected(final MenuItem item) { - switch (item.getItemId()) { - case R.id.menu_action_save: - if (binding == null) - return false; - final Config newConfig; - try { - newConfig = binding.getConfig().resolve(); - } catch (final Exception e) { - final String error = ErrorMessages.get(e); - final String tunnelName = tunnel == null ? binding.getName() : tunnel.getName(); - final String message = getString(R.string.config_save_error, tunnelName, error); - Log.e(TAG, message, e); - Snackbar.make(binding.mainContainer, error, Snackbar.LENGTH_LONG).show(); - return false; - } - if (tunnel == null) { - Log.d(TAG, "Attempting to create new tunnel " + binding.getName()); - final TunnelManager manager = Application.getTunnelManager(); - manager.create(binding.getName(), newConfig) - .whenComplete(this::onTunnelCreated); - } else if (!tunnel.getName().equals(binding.getName())) { - Log.d(TAG, "Attempting to rename tunnel to " + binding.getName()); - tunnel.setName(binding.getName()) - .whenComplete((a, b) -> onTunnelRenamed(tunnel, newConfig, b)); - } else { - Log.d(TAG, "Attempting to save config of " + tunnel.getName()); - tunnel.setConfig(newConfig) - .whenComplete((a, b) -> onConfigSaved(tunnel, b)); - } - return true; - default: - return super.onOptionsItemSelected(item); + if (item.getItemId() == R.id.menu_action_save) { + if (binding == null) + return false; + final Config newConfig; + try { + newConfig = binding.getConfig().resolve(); + } catch (final Exception e) { + final String error = ErrorMessages.get(e); + final String tunnelName = tunnel == null ? binding.getName() : tunnel.getName(); + final String message = getString(R.string.config_save_error, tunnelName, error); + Log.e(TAG, message, e); + Snackbar.make(binding.mainContainer, error, Snackbar.LENGTH_LONG).show(); + return false; + } + if (tunnel == null) { + Log.d(TAG, "Attempting to create new tunnel " + binding.getName()); + final TunnelManager manager = Application.getTunnelManager(); + manager.create(binding.getName(), newConfig) + .whenComplete(this::onTunnelCreated); + } else if (!tunnel.getName().equals(binding.getName())) { + Log.d(TAG, "Attempting to rename tunnel to " + binding.getName()); + tunnel.setName(binding.getName()) + .whenComplete((a, b) -> onTunnelRenamed(tunnel, newConfig, b)); + } else { + Log.d(TAG, "Attempting to save config of " + tunnel.getName()); + tunnel.setConfig(newConfig) + .whenComplete((a, b) -> onConfigSaved(tunnel, b)); + } + return true; } + return super.onOptionsItemSelected(item); } public void onRequestSetExcludedApplications(@SuppressWarnings("unused") final View view) { diff --git a/ui/src/main/java/com/wireguard/android/model/TunnelManager.java b/ui/src/main/java/com/wireguard/android/model/TunnelManager.java index e370f8de..28dcd18e 100644 --- a/ui/src/main/java/com/wireguard/android/model/TunnelManager.java +++ b/ui/src/main/java/com/wireguard/android/model/TunnelManager.java @@ -161,7 +161,7 @@ public final class TunnelManager extends BaseObservable { completableTunnels.complete(tunnels); } - public void refreshTunnelStates() { + private void refreshTunnelStates() { Application.getAsyncWorker().supplyAsync(() -> Application.getBackend().getRunningTunnelNames()) .thenAccept(running -> { for (final ObservableTunnel tunnel : tunnels) diff --git a/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.java b/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.java index 86c943b3..750cb05e 100644 --- a/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.java +++ b/ui/src/main/java/com/wireguard/android/preference/ModuleDownloaderPreference.java @@ -53,12 +53,12 @@ public class ModuleDownloaderPreference extends Preference { setState(State.SUCCESS); Application.getAsyncWorker().runAsync(() -> { Thread.sleep(1000 * 5); - Intent i = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName()); - if (i == null) + final Intent restartIntent = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName()); + if (restartIntent == null) return; - i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - Application.get().startActivity(i); + restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + Application.get().startActivity(restartIntent); System.exit(0); }); } else diff --git a/ui/src/main/java/com/wireguard/android/preference/VersionPreference.java b/ui/src/main/java/com/wireguard/android/preference/VersionPreference.java index 9caec06a..b5c7c5e8 100644 --- a/ui/src/main/java/com/wireguard/android/preference/VersionPreference.java +++ b/ui/src/main/java/com/wireguard/android/preference/VersionPreference.java @@ -42,7 +42,7 @@ public class VersionPreference extends Preference { }); } - private String getBackendPrettyName(final Context context, final Backend backend) { + private static String getBackendPrettyName(final Context context, final Backend backend) { if (backend instanceof WgQuickBackend) return context.getString(R.string.type_name_kernel_module); if (backend instanceof GoBackend) diff --git a/ui/src/main/java/com/wireguard/android/util/DownloadsFileSaver.java b/ui/src/main/java/com/wireguard/android/util/DownloadsFileSaver.java index c1c667f5..14895b64 100644 --- a/ui/src/main/java/com/wireguard/android/util/DownloadsFileSaver.java +++ b/ui/src/main/java/com/wireguard/android/util/DownloadsFileSaver.java @@ -24,7 +24,11 @@ import java.io.IOException; import java.io.OutputStream; @NonNullForAll -public class DownloadsFileSaver { +public final class DownloadsFileSaver { + + private DownloadsFileSaver() { + // Prevent instantiation + } public static DownloadsFile save(final Context context, final String name, final String mimeType, final boolean overwriteExisting) throws Exception { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { @@ -73,11 +77,11 @@ public class DownloadsFileSaver { } } - public static class DownloadsFile { - private Context context; - private String fileName; - private OutputStream outputStream; - private Uri uri; + public static final class DownloadsFile { + private final Context context; + private final String fileName; + private final OutputStream outputStream; + private final Uri uri; private DownloadsFile(final Context context, final OutputStream outputStream, final String fileName, final Uri uri) { this.context = context; |