Quick cleanup across the board

- Clean up imports
- Adjust method visibility
- Drop in NonNull annotations where needed
- Prevent potential NPEs from nulled getActivity calls
- Remove unused methods

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2018-04-27 22:59:24 +05:30 committed by Jason A. Donenfeld
parent 592c5cbbd6
commit ba862b166b
8 changed files with 16 additions and 19 deletions

View File

@ -5,22 +5,18 @@ import android.content.Intent;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.support.v4.util.ArraySet; import android.support.v4.util.ArraySet;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import com.wireguard.android.Application; import com.wireguard.android.Application;
import com.wireguard.android.model.Tunnel; import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State; import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.model.Tunnel.Statistics; import com.wireguard.android.model.Tunnel.Statistics;
import com.wireguard.android.model.TunnelManager;
import com.wireguard.config.Config; import com.wireguard.config.Config;
import com.wireguard.config.IPCidr; import com.wireguard.config.IPCidr;
import com.wireguard.config.Interface; import com.wireguard.config.Interface;
import com.wireguard.config.Peer; import com.wireguard.config.Peer;
import com.wireguard.crypto.KeyEncoding; import com.wireguard.crypto.KeyEncoding;
import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Collections; import java.util.Collections;
import java.util.Formatter; import java.util.Formatter;
import java.util.Set; import java.util.Set;
@ -45,7 +41,7 @@ public final class GoBackend implements Backend {
this.context = context; this.context = context;
} }
public void startVpnService() { private void startVpnService() {
context.startService(new Intent(context, VpnService.class)); context.startService(new Intent(context, VpnService.class));
} }

View File

@ -1,6 +1,7 @@
package com.wireguard.android.fragment; package com.wireguard.android.fragment;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -33,7 +34,7 @@ public class TunnelDetailFragment extends BaseFragment {
} }
@Override @Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) { final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState); super.onCreateView(inflater, container, savedInstanceState);
binding = TunnelDetailFragmentBinding.inflate(inflater, container, false); binding = TunnelDetailFragmentBinding.inflate(inflater, container, false);

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -101,7 +102,7 @@ public class TunnelEditorFragment extends BaseFragment {
} }
@Override @Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) { final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState); super.onCreateView(inflater, container, savedInstanceState);
binding = TunnelEditorFragmentBinding.inflate(inflater, container, false); binding = TunnelEditorFragmentBinding.inflate(inflater, container, false);
@ -118,6 +119,7 @@ public class TunnelEditorFragment extends BaseFragment {
private void onFinished() { private void onFinished() {
// Hide the keyboard; it rarely goes away on its own. // Hide the keyboard; it rarely goes away on its own.
final Activity activity = getActivity(); final Activity activity = getActivity();
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 Object service = activity.getSystemService(Context.INPUT_METHOD_SERVICE);
@ -174,7 +176,7 @@ public class TunnelEditorFragment extends BaseFragment {
} }
@Override @Override
public void onSaveInstanceState(final Bundle outState) { public void onSaveInstanceState(@NonNull final Bundle outState) {
outState.putParcelable(KEY_LOCAL_CONFIG, localConfig); outState.putParcelable(KEY_LOCAL_CONFIG, localConfig);
outState.putString(KEY_ORIGINAL_NAME, originalName); outState.putString(KEY_ORIGINAL_NAME, originalName);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);

View File

@ -9,6 +9,7 @@ import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
import android.view.ActionMode; import android.view.ActionMode;
@ -110,7 +111,7 @@ public class TunnelListFragment extends BaseFragment {
} }
@Override @Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) { final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState); super.onCreateView(inflater, container, savedInstanceState);
binding = TunnelListFragmentBinding.inflate(inflater, container, false); binding = TunnelListFragmentBinding.inflate(inflater, container, false);
@ -220,7 +221,8 @@ public class TunnelListFragment extends BaseFragment {
@Override @Override
public boolean onCreateActionMode(final ActionMode mode, final Menu menu) { public boolean onCreateActionMode(final ActionMode mode, final Menu menu) {
actionMode = mode; actionMode = mode;
resources = getActivity().getResources(); if (getActivity() != null)
resources = getActivity().getResources();
tunnelList = binding.tunnelList; tunnelList = binding.tunnelList;
mode.getMenuInflater().inflate(R.menu.tunnel_list_action_mode, menu); mode.getMenuInflater().inflate(R.menu.tunnel_list_action_mode, menu);
return true; return true;

View File

@ -37,8 +37,8 @@ public class Tunnel extends BaseObservable implements Keyed<String> {
this.state = state; this.state = state;
} }
public static boolean isNameValid(@NonNull final CharSequence name) { public static boolean isNameInvalid(@NonNull final CharSequence name) {
return NAME_PATTERN.matcher(name).matches(); return !NAME_PATTERN.matcher(name).matches();
} }
public CompletionStage<Void> delete() { public CompletionStage<Void> delete() {

View File

@ -66,7 +66,7 @@ public final class TunnelManager extends BaseObservable {
} }
public CompletionStage<Tunnel> create(@NonNull final String name, final Config config) { public CompletionStage<Tunnel> create(@NonNull final String name, final Config config) {
if (!Tunnel.isNameValid(name)) if (Tunnel.isNameInvalid(name))
return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name")); return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name"));
if (tunnels.containsKey(name)) { if (tunnels.containsKey(name)) {
final String message = "Tunnel " + name + " already exists"; final String message = "Tunnel " + name + " already exists";
@ -190,7 +190,7 @@ public final class TunnelManager extends BaseObservable {
} }
CompletionStage<String> setTunnelName(final Tunnel tunnel, final String name) { CompletionStage<String> setTunnelName(final Tunnel tunnel, final String name) {
if (!Tunnel.isNameValid(name)) if (Tunnel.isNameInvalid(name))
return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name")); return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name"));
if (tunnels.containsKey(name)) { if (tunnels.containsKey(name)) {
final String message = "Tunnel " + name + " already exists"; final String message = "Tunnel " + name + " already exists";

View File

@ -30,10 +30,6 @@ public class ToolsInstallerPreference extends Preference {
toolsInstaller = applicationComponent.getToolsInstaller(); toolsInstaller = applicationComponent.getToolsInstaller();
} }
public ToolsInstallerPreference(final Context context) {
this(context, null);
}
@Override @Override
public CharSequence getSummary() { public CharSequence getSummary() {
return getContext().getString(state.messageResourceId); return getContext().getString(state.messageResourceId);

View File

@ -22,7 +22,7 @@ public class AsyncWorker {
private final Handler handler; private final Handler handler;
@Inject @Inject
public AsyncWorker(final Executor executor, @ApplicationHandler final Handler handler) { AsyncWorker(final Executor executor, @ApplicationHandler final Handler handler) {
this.executor = executor; this.executor = executor;
this.handler = handler; this.handler = handler;
} }