-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add try/catch around trying to access the connectivityManager APIs. #160
Conversation
See https://issuetracker.google.com/issues/175055271 for the root Android bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I read in a book one time that a method should only do one thing, and that exception handling is also a thing. I think that means you might have cleaner code if you factored out the try
body to a method...but dealer's choice.
|
||
Network network = mock(Network.class); | ||
when(connectivityManager.getActiveNetwork()).thenReturn(network); | ||
when(connectivityManager.getNetworkCapabilities(network)).thenThrow(new SecurityException("bug")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻
ConnectivityManager connectivityManager = | ||
mock(ConnectivityManager.class); | ||
|
||
when(connectivityManager.getActiveNetworkInfo()).thenThrow(new SecurityException("bug")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏻
return new CurrentNetwork(NetworkState.TRANSPORT_CELLULAR, activeNetwork.getSubtypeName()); | ||
case ConnectivityManager.TYPE_WIFI: // Deprecated in API 28 | ||
return new CurrentNetwork(NetworkState.TRANSPORT_WIFI, activeNetwork.getSubtypeName()); | ||
case ConnectivityManager.TYPE_VPN: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Outside the scope of this specific change, but static imports on these would be nice for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think when I wrote this originally, I wanted to make sure it was clear which of these things were Android's, and which were ours, and keeping the class name prefixes made that a little more clear to me.
if (capabilities == null) { | ||
return NO_NETWORK; | ||
} | ||
String subType = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this change, but looks like the subType
should be moved into the if
scope block.
Heh. That method would have to be named something silly, though, wouldn't it? Like "doDetectCurrentNetwork"? or "detectCurrentNetworkUnlessAndroidIsBuggy"? :) |
I guess the other option (probably better) would be to move the exception handling up a level into the |
import static com.splunk.rum.ConnectionUtil.NO_NETWORK; | ||
import static com.splunk.rum.ConnectionUtil.UNKNOWN_NETWORK; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no idea why IDEA decided to reorder these imports. 🤷🏽
I think it's an improvement for sure. I would have gotten rid of the local variable there tho. |
oh yeah, that's totally a good change. I had missed altogether that it wasn't even necessary. |
See https://issuetracker.google.com/issues/175055271 for the root Android bug.