2828import java .util .concurrent .Executors ;
2929
3030import androidx .annotation .NonNull ;
31+ import androidx .core .net .ConnectivityManagerCompat ;
3132import kotlin .jvm .functions .Function1 ;
3233
3334class ConnectivityServiceImpl implements ConnectivityService {
@@ -70,22 +71,6 @@ public void isNetworkAndServerAvailable(@NonNull GenericCallback<Boolean> callba
7071 });
7172 }
7273
73- /**
74- * Checks whether the device is currently connected to a network
75- * that has verified Internet access.
76- *
77- * <p>This method performs multiple levels of validation:
78- * <ul>
79- * <li>Ensures there is an active network connection.</li>
80- * <li>Retrieves and checks network capabilities.</li>
81- * <li>Verifies that the active network provides and has validated Internet access.</li>
82- * <li>Confirms that the network uses a supported transport type
83- * (Wi-Fi, Cellular, Ethernet, VPN, etc.).</li>
84- * </ul>
85- *
86- * @return {@code true} if the device is connected to the Internet via a valid transport type;
87- * {@code false} otherwise.
88- */
8974 @ Override
9075 public boolean isConnected () {
9176 Network nw = platformConnectivityManager .getActiveNetwork ();
@@ -98,10 +83,7 @@ public boolean isConnected() {
9883 return false ;
9984 }
10085
101- // Verify that the network both claims to provide Internet
102- // and has been validated (i.e., Internet is actually reachable).
103- if (actNw .hasCapability (NetworkCapabilities .NET_CAPABILITY_INTERNET ) && actNw .hasCapability (NetworkCapabilities .NET_CAPABILITY_VALIDATED )) {
104-
86+ if (actNw .hasCapability (NetworkCapabilities .NET_CAPABILITY_INTERNET )) {
10587 // Check if the active network uses one of the recognized transport types.
10688 if (actNw .hasTransport (NetworkCapabilities .TRANSPORT_WIFI ) ||
10789 actNw .hasTransport (NetworkCapabilities .TRANSPORT_CELLULAR ) ||
@@ -127,37 +109,31 @@ public boolean isInternetWalled() {
127109 final Boolean cachedValue = walledCheckCache .getValue ();
128110 if (cachedValue != null ) {
129111 return cachedValue ;
130- }
131-
132- final Server server = accountManager .getUser ().getServer ();
133- final String baseServerAddress = server .getUri ().toString ();
134-
135- if (!isConnected () || baseServerAddress .isEmpty ()) {
136- walledCheckCache .setValue (true );
137- return true ;
138- }
139-
140- final GetMethod get = requestBuilder .invoke (baseServerAddress + CONNECTIVITY_CHECK_ROUTE );
141- try {
142- final PlainClient client = clientFactory .createPlainClient ();
143- int status = get .execute (client );
144-
145- boolean isWalled = !(status == HttpStatus .SC_NO_CONTENT && get .getResponseContentLength () <= 0 );
146-
147- if (isWalled ) {
148- Log_OC .w (TAG , "isInternetWalled(): Failed to GET " + CONNECTIVITY_CHECK_ROUTE +
149- ", assuming connectivity is impaired" );
112+ } else {
113+ Server server = accountManager .getUser ().getServer ();
114+ String baseServerAddress = server .getUri ().toString ();
115+
116+ boolean result ;
117+ Connectivity c = getConnectivity ();
118+ if (c != null && c .isConnected () && c .isWifi () && !c .isMetered () && !baseServerAddress .isEmpty ()) {
119+ GetMethod get = requestBuilder .invoke (baseServerAddress + CONNECTIVITY_CHECK_ROUTE );
120+ PlainClient client = clientFactory .createPlainClient ();
121+
122+ int status = get .execute (client );
123+
124+ // Content-Length is not available when using chunked transfer encoding, so check for -1 as well
125+ result = !(status == HttpStatus .SC_NO_CONTENT && get .getResponseContentLength () <= 0 );
126+ get .releaseConnection ();
127+ if (result ) {
128+ Log_OC .w (TAG , "isInternetWalled(): Failed to GET " + CONNECTIVITY_CHECK_ROUTE + "," +
129+ " assuming connectivity is impaired" );
130+ }
131+ } else {
132+ result = (c != null && !c .isConnected ());
150133 }
151134
152- // Cache and return result
153- walledCheckCache .setValue (isWalled );
154- return isWalled ;
155- } catch (Exception e ) {
156- Log_OC .e (TAG , "Exception while checking internet walled state" , e );
157- walledCheckCache .setValue (true );
158- return true ;
159- } finally {
160- get .releaseConnection ();
135+ walledCheckCache .setValue (result );
136+ return result ;
161137 }
162138 }
163139
@@ -170,12 +146,25 @@ public Connectivity getConnectivity() {
170146
171147 NetworkCapabilities nc = platformConnectivityManager .getNetworkCapabilities (nw );
172148 boolean isConnected = isConnected ();
173- boolean isMetered = (nc != null ) && ! nc . hasCapability ( NetworkCapabilities . NET_CAPABILITY_NOT_METERED );
149+ boolean isMetered = isNetworkMetered (nc );
174150 boolean isWifi = (nc != null ) &&
175151 (nc .hasTransport (NetworkCapabilities .TRANSPORT_WIFI ) ||
176152 nc .hasTransport (NetworkCapabilities .TRANSPORT_ETHERNET ) ||
177153 nc .hasTransport (NetworkCapabilities .TRANSPORT_WIFI_AWARE ));
178154
179155 return new Connectivity (isConnected , isMetered , isWifi , null );
180156 }
157+
158+ private boolean isNetworkMetered (NetworkCapabilities nc ) {
159+ try {
160+ if (nc != null ) {
161+ return !nc .hasCapability (NetworkCapabilities .NET_CAPABILITY_NOT_RESTRICTED );
162+ } else {
163+ return ConnectivityManagerCompat .isActiveNetworkMetered (platformConnectivityManager );
164+ }
165+ } catch (RuntimeException e ) {
166+ Log_OC .e (TAG , "Exception when checking network capabilities" , e );
167+ return false ;
168+ }
169+ }
181170}
0 commit comments