diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-03-08 22:23:51 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-03-09 10:12:50 +0530 |
commit | 7ca2a0df180e2c60c539e18e387eda62d7ef813d (patch) | |
tree | afb0a2b246f035b27887236175174444fcd25983 | |
parent | 7a4af834c213672c65c89b14c75229bf5f2ea4ab (diff) |
global: Use requireContext rather than getContext in fragments
requireContext provides helpful error messages when it's null as opposed to getContext
which simply throws a NullPointerException.
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
3 files changed, 10 insertions, 10 deletions
diff --git a/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java b/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java index 11af26e4..23bf44e7 100644 --- a/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/BaseFragment.java @@ -113,12 +113,12 @@ public abstract class BaseFragment extends Fragment implements OnSelectedTunnelC return; final String error = ErrorMessages.get(throwable); final int messageResId = checked ? R.string.error_up : R.string.error_down; - final String message = getContext().getString(messageResId, error); + final String message = requireContext().getString(messageResId, error); final View view = getView(); if (view != null) Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); else - Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show(); + Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show(); Log.e(TAG, message, throwable); }); } 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 89fe6c77..8d90fa7e 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java @@ -110,14 +110,14 @@ public class TunnelDetailFragment extends BaseFragment { private String formatBytes(final long bytes) { if (bytes < 1024) - return getContext().getString(R.string.transfer_bytes, bytes); + return requireContext().getString(R.string.transfer_bytes, bytes); else if (bytes < 1024*1024) - return getContext().getString(R.string.transfer_kibibytes, bytes/1024.0); + return requireContext().getString(R.string.transfer_kibibytes, bytes/1024.0); else if (bytes < 1024*1024*1024) - return getContext().getString(R.string.transfer_mibibytes, bytes/(1024.0*1024.0)); + return requireContext().getString(R.string.transfer_mibibytes, bytes/(1024.0*1024.0)); else if (bytes < 1024*1024*1024*1024) - return getContext().getString(R.string.transfer_gibibytes, bytes/(1024.0*1024.0*1024.0)); - return getContext().getString(R.string.transfer_tibibytes, bytes/(1024.0*1024.0*1024.0)/1024.0); + return requireContext().getString(R.string.transfer_gibibytes, bytes/(1024.0*1024.0*1024.0)); + return requireContext().getString(R.string.transfer_tibibytes, bytes/(1024.0*1024.0*1024.0)/1024.0); } private void updateStats() { @@ -153,7 +153,7 @@ public class TunnelDetailFragment extends BaseFragment { peer.transferText.setVisibility(View.GONE); continue; } - peer.transferText.setText(getContext().getString(R.string.transfer_rx_tx, formatBytes(rx), formatBytes(tx))); + peer.transferText.setText(requireContext().getString(R.string.transfer_rx_tx, formatBytes(rx), formatBytes(tx))); peer.transferLabel.setVisibility(View.VISIBLE); peer.transferText.setVisibility(View.VISIBLE); } 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 b342d5a8..92aeb52a 100644 --- a/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java +++ b/app/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.java @@ -61,7 +61,7 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi if (throwable == null) { message = getString(R.string.config_save_success, savedTunnel.getName()); Log.d(TAG, message); - Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show(); + Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show(); onFinished(); } else { final String error = ErrorMessages.get(throwable); @@ -207,7 +207,7 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi tunnel = newTunnel; message = getString(R.string.tunnel_create_success, tunnel.getName()); Log.d(TAG, message); - Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show(); + Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show(); onFinished(); } else { final String error = ErrorMessages.get(throwable); |