Skip to content

Commit

Permalink
chore: fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
izaaz committed Aug 21, 2024
1 parent e366839 commit d6a0466
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AndroidNetworkConnectivityChecker(private val context: Context, private va
val capabilities = cm.getNetworkCapabilities(network) ?: return false

return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
} else {
@SuppressLint("MissingPermission")
val networkInfo = cm.activeNetworkInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class AndroidNetworkListener(private val context: Context, private val logger: L
// startListening() checks API level
// ACCESS_NETWORK_STATE permission should be added manually by users to enable this feature
private fun setupNetworkCallback() {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkRequest =
NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
Expand All @@ -66,7 +67,10 @@ class AndroidNetworkListener(private val context: Context, private val logger: L
}
}

connectivityManager.registerNetworkCallback(networkRequest, networkCallbackForHigherApiLevels!!)
connectivityManager.registerNetworkCallback(
networkRequest,
networkCallbackForHigherApiLevels!!
)
}

private fun setupBroadcastReceiver() {
Expand All @@ -78,7 +82,8 @@ class AndroidNetworkListener(private val context: Context, private val logger: L
intent: Intent,
) {
if (ConnectivityManager.CONNECTIVITY_ACTION == intent.action) {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = connectivityManager.activeNetworkInfo
val isConnected = activeNetwork?.isConnectedOrConnecting == true

Expand All @@ -96,26 +101,30 @@ class AndroidNetworkListener(private val context: Context, private val logger: L
}

fun stopListening() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
networkCallbackForHigherApiLevels?.let { connectivityManager.unregisterNetworkCallback(it) }
} else {
networkCallbackForLowerApiLevels?.let { context.unregisterReceiver(it) }
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val connectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
networkCallbackForHigherApiLevels?.let {
connectivityManager.unregisterNetworkCallback(
it
)
}
} catch (e: IllegalArgumentException) {
// callback was already unregistered.
} catch (e: IllegalStateException) {
// shutdown process is in progress and certain operations are not allowed.
} catch (throwable: Throwable) {
// We've seen issues where we see exceptions being thrown by connectivity manager
// which crashes an app. Its safe to ignore these exceptions since we try our best
// to mark a device as offline
// Github Issues:
// https://github.com/amplitude/Amplitude-Kotlin/issues/220
// https://github.com/amplitude/Amplitude-Kotlin/issues/197
logger.warn("Error stopping network listener: ${throwable.message}")
} else {
networkCallbackForLowerApiLevels?.let { context.unregisterReceiver(it) }
}
} catch (e: IllegalArgumentException) {
// callback was already unregistered.
} catch (e: IllegalStateException) {
// shutdown process is in progress and certain operations are not allowed.
} catch (throwable: Throwable) {
// We've seen issues where we see exceptions being thrown by connectivity manager
// which crashes an app. Its safe to ignore these exceptions since we try our best
// to mark a device as offline
// Github Issues:
// https://github.com/amplitude/Amplitude-Kotlin/issues/220
// https://github.com/amplitude/Amplitude-Kotlin/issues/197
logger.warn("Error stopping network listener: ${throwable.message}")
}

}
}

0 comments on commit d6a0466

Please sign in to comment.