Skip to content

Commit

Permalink
Remove depracated NetworkInfo API
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 authored and AndyScherzinger committed Nov 1, 2023
1 parent 24d2610 commit b95a165
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ package com.nextcloud.client.network

import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.NetworkInfo
import androidx.core.net.ConnectivityManagerCompat
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.operations.GetMethod
import com.owncloud.android.lib.common.utils.Log_OC
import org.apache.commons.httpclient.HttpStatus

internal class ConnectivityServiceImpl(
private val platformConnectivityManager: ConnectivityManager,
private val connectivityManager: ConnectivityManager,
private val accountManager: UserAccountManager,
private val clientFactory: ClientFactory,
private val requestBuilder: GetRequestBuilder,
Expand Down Expand Up @@ -57,6 +55,21 @@ internal class ConnectivityServiceImpl(
}
}

@Suppress("TooGenericExceptionCaught")
override fun getConnectivity(): Connectivity {
val network = connectivityManager.activeNetwork
val networkCapabilities = connectivityManager.getNetworkCapabilities(network)

return if (networkCapabilities != null) {
val isConnected = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
val isMetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED).not()
val isWifi = networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
Connectivity(isConnected, isMetered, isWifi, null)
} else {
Connectivity.DISCONNECTED
}
}

private fun isInternetWalledOnConnectedNonMeteredWifi(): Boolean {
val baseServerAddress = accountManager.user.server.toString()
return if (baseServerAddress.isEmpty()) {
Expand All @@ -79,54 +92,6 @@ internal class ConnectivityServiceImpl(
}
}

@Suppress("TooGenericExceptionCaught")
override fun getConnectivity(): Connectivity {
val networkInfo: NetworkInfo? = try {
platformConnectivityManager.activeNetworkInfo
} catch (t: Throwable) {
null // no network available or no information (permission denied?)
}
return if (networkInfo != null) {
val isConnected = networkInfo.isConnectedOrConnecting
// more detailed check
val isMetered: Boolean = isNetworkMetered
val isWifi = (networkInfo.type == ConnectivityManager.TYPE_WIFI) || hasNonCellularConnectivity()
Connectivity(isConnected, isMetered, isWifi, null)
} else {
Connectivity.DISCONNECTED
}
}

@Suppress("TooGenericExceptionCaught")
private val isNetworkMetered: Boolean
get() {
val network = platformConnectivityManager.activeNetwork
return try {
val networkCapabilities = platformConnectivityManager.getNetworkCapabilities(network)
if (networkCapabilities != null) {
!networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
} else {
ConnectivityManagerCompat.isActiveNetworkMetered(platformConnectivityManager)
}
} catch (e: RuntimeException) {
Log_OC.e(TAG, "Exception when checking network capabilities", e)
false
}
}

private fun hasNonCellularConnectivity(): Boolean {
for (networkInfo in platformConnectivityManager.allNetworkInfo) {
if (networkInfo.isConnectedOrConnecting && (
networkInfo.type == ConnectivityManager.TYPE_WIFI ||
networkInfo.type == ConnectivityManager.TYPE_ETHERNET
)
) {
return true
}
}
return false
}

companion object {
private const val TAG = "ConnectivityServiceImpl"
private const val CONNECTIVITY_CHECK_ROUTE = "/index.php/204"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
public class NetworkModule {

@Provides
ConnectivityService connectivityService(ConnectivityManager connectivityManager,
ConnectivityService connectivityService(Context context,
UserAccountManager accountManager,
ClientFactory clientFactory,
WalledCheckCache walledCheckCache) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
return new ConnectivityServiceImpl(connectivityManager,
accountManager,
clientFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import android.net.NetworkInfo
import com.nextcloud.client.account.Server
import com.nextcloud.client.account.User
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.logger.Logger
import com.nextcloud.common.PlainClient
import com.nextcloud.operations.GetMethod
import com.owncloud.android.lib.resources.status.OwnCloudVersion
Expand Down Expand Up @@ -102,9 +101,6 @@ class ConnectivityServiceTest {
@Mock
lateinit var networkCapabilities: NetworkCapabilities

@Mock
lateinit var logger: Logger

val baseServerUri = URI.create(SERVER_BASE_URL)
val newServer = Server(baseServerUri, OwnCloudVersion.nextcloud_20)
val legacyServer = Server(baseServerUri, OwnCloudVersion.nextcloud_16)
Expand All @@ -116,7 +112,8 @@ class ConnectivityServiceTest {

@Before
fun setUpMocks() {
MockitoAnnotations.initMocks(this)
MockitoAnnotations.openMocks(this)

connectivityService = ConnectivityServiceImpl(
platformConnectivityManager,
accountManager,
Expand Down

0 comments on commit b95a165

Please sign in to comment.