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>
This commit is contained in:
parent
7a4af834c2
commit
7ca2a0df18
@ -113,12 +113,12 @@ public abstract class BaseFragment extends Fragment implements OnSelectedTunnelC
|
|||||||
return;
|
return;
|
||||||
final String error = ErrorMessages.get(throwable);
|
final String error = ErrorMessages.get(throwable);
|
||||||
final int messageResId = checked ? R.string.error_up : R.string.error_down;
|
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();
|
final View view = getView();
|
||||||
if (view != null)
|
if (view != null)
|
||||||
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
|
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
|
||||||
else
|
else
|
||||||
Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
|
Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show();
|
||||||
Log.e(TAG, message, throwable);
|
Log.e(TAG, message, throwable);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -110,14 +110,14 @@ public class TunnelDetailFragment extends BaseFragment {
|
|||||||
|
|
||||||
private String formatBytes(final long bytes) {
|
private String formatBytes(final long bytes) {
|
||||||
if (bytes < 1024)
|
if (bytes < 1024)
|
||||||
return getContext().getString(R.string.transfer_bytes, bytes);
|
return requireContext().getString(R.string.transfer_bytes, bytes);
|
||||||
else if (bytes < 1024*1024)
|
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)
|
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)
|
else if (bytes < 1024*1024*1024*1024)
|
||||||
return getContext().getString(R.string.transfer_gibibytes, bytes/(1024.0*1024.0*1024.0));
|
return requireContext().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_tibibytes, bytes/(1024.0*1024.0*1024.0)/1024.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateStats() {
|
private void updateStats() {
|
||||||
@ -153,7 +153,7 @@ public class TunnelDetailFragment extends BaseFragment {
|
|||||||
peer.transferText.setVisibility(View.GONE);
|
peer.transferText.setVisibility(View.GONE);
|
||||||
continue;
|
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.transferLabel.setVisibility(View.VISIBLE);
|
||||||
peer.transferText.setVisibility(View.VISIBLE);
|
peer.transferText.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi
|
|||||||
if (throwable == null) {
|
if (throwable == null) {
|
||||||
message = getString(R.string.config_save_success, savedTunnel.getName());
|
message = getString(R.string.config_save_success, savedTunnel.getName());
|
||||||
Log.d(TAG, message);
|
Log.d(TAG, message);
|
||||||
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
|
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
|
||||||
onFinished();
|
onFinished();
|
||||||
} else {
|
} else {
|
||||||
final String error = ErrorMessages.get(throwable);
|
final String error = ErrorMessages.get(throwable);
|
||||||
@ -207,7 +207,7 @@ public class TunnelEditorFragment extends BaseFragment implements AppExclusionLi
|
|||||||
tunnel = newTunnel;
|
tunnel = newTunnel;
|
||||||
message = getString(R.string.tunnel_create_success, tunnel.getName());
|
message = getString(R.string.tunnel_create_success, tunnel.getName());
|
||||||
Log.d(TAG, message);
|
Log.d(TAG, message);
|
||||||
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
|
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
|
||||||
onFinished();
|
onFinished();
|
||||||
} else {
|
} else {
|
||||||
final String error = ErrorMessages.get(throwable);
|
final String error = ErrorMessages.get(throwable);
|
||||||
|
Loading…
Reference in New Issue
Block a user