ui: Codestyle cleanups

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-03-10 13:08:28 +05:30
parent 021e16959f
commit a3b9c3b884
10 changed files with 65 additions and 63 deletions

View File

@ -107,10 +107,10 @@ public final class ToolsInstaller {
} }
} }
public boolean extract() throws IOException { private boolean extract() throws IOException {
localBinaryDir.mkdirs(); localBinaryDir.mkdirs();
final File files[] = new File[EXECUTABLES.length]; final File[] files = new File[EXECUTABLES.length];
final File tempFiles[] = new File[EXECUTABLES.length]; final File[] tempFiles = new File[EXECUTABLES.length];
boolean allExist = true; boolean allExist = true;
for (int i = 0; i < files.length; ++i) { for (int i = 0; i < files.length; ++i) {
files[i] = new File(localBinaryDir, EXECUTABLES[i]); files[i] = new File(localBinaryDir, EXECUTABLES[i]);

View File

@ -79,7 +79,7 @@ public class Application extends android.app.Application implements SharedPrefer
try { try {
if (!didStartRootShell) if (!didStartRootShell)
app.rootShell.start(); 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)); wgQuickBackend.setMultipleTunnels(app.sharedPreferences.getBoolean("multiple_tunnels", false));
backend = wgQuickBackend; backend = wgQuickBackend;
} catch (final Exception ignored) { } catch (final Exception ignored) {

View File

@ -10,7 +10,6 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View.OnApplyWindowInsetsListener;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import com.wireguard.android.R; import com.wireguard.android.R;
@ -78,7 +77,7 @@ public class MainActivity extends BaseActivity
onBackStackChanged(); onBackStackChanged();
// Dispatch insets on back stack change // Dispatch insets on back stack change
// This is required to ensure replaced fragments are also able to consume insets // 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(); final FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.addOnBackStackChangedListener(() -> { fragmentManager.addOnBackStackChangedListener(() -> {
final List<Fragment> fragments = fragmentManager.getFragments(); final List<Fragment> fragments = fragmentManager.getFragments();
@ -97,6 +96,7 @@ public class MainActivity extends BaseActivity
} }
@Override @Override
@SuppressWarnings("UnnecessaryFullyQualifiedName")
public boolean onOptionsItemSelected(final MenuItem item) { public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:

View File

@ -67,14 +67,13 @@ public class SettingsActivity extends ThemeChangeAwareActivity {
} }
@Override @Override
@SuppressWarnings("UnnecessaryFullyQualifiedName")
public boolean onOptionsItemSelected(final MenuItem item) { public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) { if (item.getItemId() == android.R.id.home) {
case android.R.id.home: finish();
finish(); return true;
return true;
default:
return super.onOptionsItemSelected(item);
} }
return super.onOptionsItemSelected(item);
} }
@Override @Override
@ -100,7 +99,7 @@ public class SettingsActivity extends ThemeChangeAwareActivity {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
screen.removePreference(getPreferenceManager().findPreference("dark_theme")); screen.removePreference(getPreferenceManager().findPreference("dark_theme"));
final Preference wgQuickOnlyPrefs[] = { final Preference[] wgQuickOnlyPrefs = {
getPreferenceManager().findPreference("tools_installer"), getPreferenceManager().findPreference("tools_installer"),
getPreferenceManager().findPreference("restore_on_boot"), getPreferenceManager().findPreference("restore_on_boot"),
getPreferenceManager().findPreference("multiple_tunnels") getPreferenceManager().findPreference("multiple_tunnels")

View File

@ -37,6 +37,7 @@ public class TunnelDetailFragment extends BaseFragment {
@Nullable private State lastState = State.TOGGLE; @Nullable private State lastState = State.TOGGLE;
@Nullable private Timer timer; @Nullable private Timer timer;
@SuppressWarnings("MagicNumber")
private String formatBytes(final long bytes) { private String formatBytes(final long bytes) {
if (bytes < 1024) if (bytes < 1024)
return requireContext().getString(R.string.transfer_bytes, bytes); return requireContext().getString(R.string.transfer_bytes, bytes);

View File

@ -21,6 +21,7 @@ import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.R; import com.wireguard.android.R;
import com.wireguard.android.backend.Tunnel;
import com.wireguard.android.databinding.TunnelEditorFragmentBinding; import com.wireguard.android.databinding.TunnelEditorFragmentBinding;
import com.wireguard.android.fragment.AppListDialogFragment.AppExclusionListener; import com.wireguard.android.fragment.AppListDialogFragment.AppExclusionListener;
import com.wireguard.android.model.ObservableTunnel; import com.wireguard.android.model.ObservableTunnel;
@ -57,8 +58,7 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi
} }
} }
private void onConfigSaved(final ObservableTunnel savedTunnel, private void onConfigSaved(final Tunnel savedTunnel, @Nullable final Throwable throwable) {
@Nullable final Throwable throwable) {
final String message; final String message;
if (throwable == null) { if (throwable == null) {
message = getString(R.string.config_save_success, savedTunnel.getName()); message = getString(R.string.config_save_success, savedTunnel.getName());
@ -118,14 +118,14 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi
if (activity == null) return; if (activity == null) return;
final View focusedView = activity.getCurrentFocus(); final View focusedView = activity.getCurrentFocus();
if (focusedView != null) { if (focusedView != null) {
final Object service = activity.getSystemService(Context.INPUT_METHOD_SERVICE); final InputMethodManager inputManager = (InputMethodManager)
final InputMethodManager inputManager = (InputMethodManager) service; activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null) if (inputManager != null)
inputManager.hideSoftInputFromWindow(focusedView.getWindowToken(), inputManager.hideSoftInputFromWindow(focusedView.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS); InputMethodManager.HIDE_NOT_ALWAYS);
} }
// Tell the activity to finish itself or go back to the detail view. // 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 // TODO(smaeul): Remove this hack when fixing the Config ViewModel
// The selected tunnel has to actually change, but we have to remember this one. // The selected tunnel has to actually change, but we have to remember this one.
final ObservableTunnel savedTunnel = tunnel; final ObservableTunnel savedTunnel = tunnel;
@ -137,39 +137,37 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi
@Override @Override
public boolean onOptionsItemSelected(final MenuItem item) { public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) { if (item.getItemId() == R.id.menu_action_save) {
case R.id.menu_action_save: if (binding == null)
if (binding == null) return false;
return false; final Config newConfig;
final Config newConfig; try {
try { newConfig = binding.getConfig().resolve();
newConfig = binding.getConfig().resolve(); } catch (final Exception e) {
} catch (final Exception e) { final String error = ErrorMessages.get(e);
final String error = ErrorMessages.get(e); final String tunnelName = tunnel == null ? binding.getName() : tunnel.getName();
final String tunnelName = tunnel == null ? binding.getName() : tunnel.getName(); final String message = getString(R.string.config_save_error, tunnelName, error);
final String message = getString(R.string.config_save_error, tunnelName, error); Log.e(TAG, message, e);
Log.e(TAG, message, e); Snackbar.make(binding.mainContainer, error, Snackbar.LENGTH_LONG).show();
Snackbar.make(binding.mainContainer, error, Snackbar.LENGTH_LONG).show(); return false;
return false; }
} if (tunnel == null) {
if (tunnel == null) { Log.d(TAG, "Attempting to create new tunnel " + binding.getName());
Log.d(TAG, "Attempting to create new tunnel " + binding.getName()); final TunnelManager manager = Application.getTunnelManager();
final TunnelManager manager = Application.getTunnelManager(); manager.create(binding.getName(), newConfig)
manager.create(binding.getName(), newConfig) .whenComplete(this::onTunnelCreated);
.whenComplete(this::onTunnelCreated); } else if (!tunnel.getName().equals(binding.getName())) {
} else if (!tunnel.getName().equals(binding.getName())) { Log.d(TAG, "Attempting to rename tunnel to " + binding.getName());
Log.d(TAG, "Attempting to rename tunnel to " + binding.getName()); tunnel.setName(binding.getName())
tunnel.setName(binding.getName()) .whenComplete((a, b) -> onTunnelRenamed(tunnel, newConfig, b));
.whenComplete((a, b) -> onTunnelRenamed(tunnel, newConfig, b)); } else {
} else { Log.d(TAG, "Attempting to save config of " + tunnel.getName());
Log.d(TAG, "Attempting to save config of " + tunnel.getName()); tunnel.setConfig(newConfig)
tunnel.setConfig(newConfig) .whenComplete((a, b) -> onConfigSaved(tunnel, b));
.whenComplete((a, b) -> onConfigSaved(tunnel, b)); }
} return true;
return true;
default:
return super.onOptionsItemSelected(item);
} }
return super.onOptionsItemSelected(item);
} }
public void onRequestSetExcludedApplications(@SuppressWarnings("unused") final View view) { public void onRequestSetExcludedApplications(@SuppressWarnings("unused") final View view) {

View File

@ -161,7 +161,7 @@ public final class TunnelManager extends BaseObservable {
completableTunnels.complete(tunnels); completableTunnels.complete(tunnels);
} }
public void refreshTunnelStates() { private void refreshTunnelStates() {
Application.getAsyncWorker().supplyAsync(() -> Application.getBackend().getRunningTunnelNames()) Application.getAsyncWorker().supplyAsync(() -> Application.getBackend().getRunningTunnelNames())
.thenAccept(running -> { .thenAccept(running -> {
for (final ObservableTunnel tunnel : tunnels) for (final ObservableTunnel tunnel : tunnels)

View File

@ -53,12 +53,12 @@ public class ModuleDownloaderPreference extends Preference {
setState(State.SUCCESS); setState(State.SUCCESS);
Application.getAsyncWorker().runAsync(() -> { Application.getAsyncWorker().runAsync(() -> {
Thread.sleep(1000 * 5); Thread.sleep(1000 * 5);
Intent i = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName()); final Intent restartIntent = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName());
if (i == null) if (restartIntent == null)
return; return;
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Application.get().startActivity(i); Application.get().startActivity(restartIntent);
System.exit(0); System.exit(0);
}); });
} else } else

View File

@ -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) if (backend instanceof WgQuickBackend)
return context.getString(R.string.type_name_kernel_module); return context.getString(R.string.type_name_kernel_module);
if (backend instanceof GoBackend) if (backend instanceof GoBackend)

View File

@ -24,7 +24,11 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@NonNullForAll @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 { 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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
@ -73,11 +77,11 @@ public class DownloadsFileSaver {
} }
} }
public static class DownloadsFile { public static final class DownloadsFile {
private Context context; private final Context context;
private String fileName; private final String fileName;
private OutputStream outputStream; private final OutputStream outputStream;
private Uri uri; private final Uri uri;
private DownloadsFile(final Context context, final OutputStream outputStream, final String fileName, final Uri uri) { private DownloadsFile(final Context context, final OutputStream outputStream, final String fileName, final Uri uri) {
this.context = context; this.context = context;