From ba862b166bd30de8c15593ed3b8dfd457eb39d36 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 27 Apr 2018 22:59:24 +0530 Subject: [PATCH] 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 --- .../main/java/com/wireguard/android/backend/GoBackend.java | 6 +----- .../wireguard/android/fragment/TunnelDetailFragment.java | 3 ++- .../wireguard/android/fragment/TunnelEditorFragment.java | 6 ++++-- .../com/wireguard/android/fragment/TunnelListFragment.java | 6 ++++-- app/src/main/java/com/wireguard/android/model/Tunnel.java | 4 ++-- .../java/com/wireguard/android/model/TunnelManager.java | 4 ++-- .../android/preference/ToolsInstallerPreference.java | 4 ---- .../main/java/com/wireguard/android/util/AsyncWorker.java | 2 +- 8 files changed, 16 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/wireguard/android/backend/GoBackend.java b/app/src/main/java/com/wireguard/android/backend/GoBackend.java index 1ebab857..8201b116 100644 --- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -5,22 +5,18 @@ import android.content.Intent; import android.os.ParcelFileDescriptor; import android.support.v4.util.ArraySet; import android.util.Log; -import android.util.Pair; import com.wireguard.android.Application; import com.wireguard.android.model.Tunnel; import com.wireguard.android.model.Tunnel.State; import com.wireguard.android.model.Tunnel.Statistics; -import com.wireguard.android.model.TunnelManager; import com.wireguard.config.Config; import com.wireguard.config.IPCidr; import com.wireguard.config.Interface; import com.wireguard.config.Peer; import com.wireguard.crypto.KeyEncoding; -import java.net.Inet4Address; import java.net.InetAddress; -import java.net.InetSocketAddress; import java.util.Collections; import java.util.Formatter; import java.util.Set; @@ -45,7 +41,7 @@ public final class GoBackend implements Backend { this.context = context; } - public void startVpnService() { + private void startVpnService() { context.startService(new Intent(context, VpnService.class)); } diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java index 5592c28a..08f08298 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java @@ -1,6 +1,7 @@ package com.wireguard.android.fragment; import android.os.Bundle; +import android.support.annotation.NonNull; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -33,7 +34,7 @@ public class TunnelDetailFragment extends BaseFragment { } @Override - public View onCreateView(final LayoutInflater inflater, final ViewGroup container, + public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); binding = TunnelDetailFragmentBinding.inflate(inflater, container, false); diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java index f3316490..0b4d74d8 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java @@ -5,6 +5,7 @@ import android.content.Context; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; +import android.support.annotation.NonNull; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -101,7 +102,7 @@ public class TunnelEditorFragment extends BaseFragment { } @Override - public View onCreateView(final LayoutInflater inflater, final ViewGroup container, + public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); binding = TunnelEditorFragmentBinding.inflate(inflater, container, false); @@ -118,6 +119,7 @@ public class TunnelEditorFragment extends BaseFragment { private void onFinished() { // Hide the keyboard; it rarely goes away on its own. final Activity activity = getActivity(); + if (activity == null) return; final View focusedView = activity.getCurrentFocus(); if (focusedView != null) { final Object service = activity.getSystemService(Context.INPUT_METHOD_SERVICE); @@ -174,7 +176,7 @@ public class TunnelEditorFragment extends BaseFragment { } @Override - public void onSaveInstanceState(final Bundle outState) { + public void onSaveInstanceState(@NonNull final Bundle outState) { outState.putParcelable(KEY_LOCAL_CONFIG, localConfig); outState.putString(KEY_ORIGINAL_NAME, originalName); super.onSaveInstanceState(outState); diff --git a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java index 352fbfbd..f1c76ab4 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelListFragment.java @@ -9,6 +9,7 @@ import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.OpenableColumns; +import android.support.annotation.NonNull; import android.util.Log; import android.util.SparseBooleanArray; import android.view.ActionMode; @@ -110,7 +111,7 @@ public class TunnelListFragment extends BaseFragment { } @Override - public View onCreateView(final LayoutInflater inflater, final ViewGroup container, + public View onCreateView(@NonNull final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); binding = TunnelListFragmentBinding.inflate(inflater, container, false); @@ -220,7 +221,8 @@ public class TunnelListFragment extends BaseFragment { @Override public boolean onCreateActionMode(final ActionMode mode, final Menu menu) { actionMode = mode; - resources = getActivity().getResources(); + if (getActivity() != null) + resources = getActivity().getResources(); tunnelList = binding.tunnelList; mode.getMenuInflater().inflate(R.menu.tunnel_list_action_mode, menu); return true; diff --git a/app/src/main/java/com/wireguard/android/model/Tunnel.java b/app/src/main/java/com/wireguard/android/model/Tunnel.java index 09efc70c..037b380f 100644 --- a/app/src/main/java/com/wireguard/android/model/Tunnel.java +++ b/app/src/main/java/com/wireguard/android/model/Tunnel.java @@ -37,8 +37,8 @@ public class Tunnel extends BaseObservable implements Keyed { this.state = state; } - public static boolean isNameValid(@NonNull final CharSequence name) { - return NAME_PATTERN.matcher(name).matches(); + public static boolean isNameInvalid(@NonNull final CharSequence name) { + return !NAME_PATTERN.matcher(name).matches(); } public CompletionStage delete() { diff --git a/app/src/main/java/com/wireguard/android/model/TunnelManager.java b/app/src/main/java/com/wireguard/android/model/TunnelManager.java index 61468920..e4667c82 100644 --- a/app/src/main/java/com/wireguard/android/model/TunnelManager.java +++ b/app/src/main/java/com/wireguard/android/model/TunnelManager.java @@ -66,7 +66,7 @@ public final class TunnelManager extends BaseObservable { } public CompletionStage create(@NonNull final String name, final Config config) { - if (!Tunnel.isNameValid(name)) + if (Tunnel.isNameInvalid(name)) return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name")); if (tunnels.containsKey(name)) { final String message = "Tunnel " + name + " already exists"; @@ -190,7 +190,7 @@ public final class TunnelManager extends BaseObservable { } CompletionStage setTunnelName(final Tunnel tunnel, final String name) { - if (!Tunnel.isNameValid(name)) + if (Tunnel.isNameInvalid(name)) return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name")); if (tunnels.containsKey(name)) { final String message = "Tunnel " + name + " already exists"; diff --git a/app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java b/app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java index 0836025a..6e54fa56 100644 --- a/app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java +++ b/app/src/main/java/com/wireguard/android/preference/ToolsInstallerPreference.java @@ -30,10 +30,6 @@ public class ToolsInstallerPreference extends Preference { toolsInstaller = applicationComponent.getToolsInstaller(); } - public ToolsInstallerPreference(final Context context) { - this(context, null); - } - @Override public CharSequence getSummary() { return getContext().getString(state.messageResourceId); diff --git a/app/src/main/java/com/wireguard/android/util/AsyncWorker.java b/app/src/main/java/com/wireguard/android/util/AsyncWorker.java index 5f9f0a83..394b500f 100644 --- a/app/src/main/java/com/wireguard/android/util/AsyncWorker.java +++ b/app/src/main/java/com/wireguard/android/util/AsyncWorker.java @@ -22,7 +22,7 @@ public class AsyncWorker { private final Handler handler; @Inject - public AsyncWorker(final Executor executor, @ApplicationHandler final Handler handler) { + AsyncWorker(final Executor executor, @ApplicationHandler final Handler handler) { this.executor = executor; this.handler = handler; }