Skip to content

Commit

Permalink
Merge pull request #367 from bugsnag/null-system-service
Browse files Browse the repository at this point in the history
Add Null check when accessing system service
  • Loading branch information
fractalwrench authored Aug 21, 2018
2 parents fe08a88 + 023f8ff commit b689975
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* Catch Exception within DefaultDelivery class [#361](https://github.com/bugsnag/bugsnag-android/pull/361)
* Add Null check when accessing system service [#367](https://github.com/bugsnag/bugsnag-android/pull/367)

## 4.6.0 (2018-08-02)

Expand Down
4 changes: 4 additions & 0 deletions sdk/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ private class ConnectivityChangeReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return;
}

NetworkInfo networkInfo = cm.getActiveNetworkInfo();
boolean retryReports = networkInfo != null && networkInfo.isConnectedOrConnecting();

Expand Down
9 changes: 7 additions & 2 deletions sdk/src/main/java/com/bugsnag/android/DefaultDelivery.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.annotation.Nullable;

import java.io.BufferedWriter;
import java.io.IOException;
Expand All @@ -15,9 +16,9 @@
class DefaultDelivery implements Delivery {

private static final int HTTP_REQUEST_FAILED = 0;
private final ConnectivityManager connectivityManager;
@Nullable private final ConnectivityManager connectivityManager;

DefaultDelivery(ConnectivityManager connectivityManager) {
DefaultDelivery(@Nullable ConnectivityManager connectivityManager) {
this.connectivityManager = connectivityManager;
}

Expand Down Expand Up @@ -89,6 +90,10 @@ int deliver(String urlString,
}

private void checkHasNetworkConnection() throws DeliveryFailureException {
if (connectivityManager == null) {
return; // unlikely case, allow delivery attempt without checking connection first
}

NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

// conserve device battery by avoiding radio use
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/main/java/com/bugsnag/android/DeviceData.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ private String getNetworkAccess() {
try {
ConnectivityManager cm =
(ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm == null) {
return null;
}

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
if (activeNetwork.getType() == 1) {
Expand Down

0 comments on commit b689975

Please sign in to comment.