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();
final File files[] = new File[EXECUTABLES.length];
final File tempFiles[] = new File[EXECUTABLES.length];
final File[] files = new File[EXECUTABLES.length];
final File[] tempFiles = new File[EXECUTABLES.length];
boolean allExist = true;
for (int i = 0; i < files.length; ++i) {
files[i] = new File(localBinaryDir, EXECUTABLES[i]);

View File

@ -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) {

View File

@ -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:

View File

@ -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")

View File

@ -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);

View File

@ -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) {

View File

@ -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)

View File

@ -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

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

View File

@ -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;