ConnectivityServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.network;
  21. import android.net.ConnectivityManager;
  22. import android.net.NetworkInfo;
  23. import com.nextcloud.client.account.Server;
  24. import com.nextcloud.client.account.UserAccountManager;
  25. import com.nextcloud.client.logger.Logger;
  26. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  27. import org.apache.commons.httpclient.HttpClient;
  28. import org.apache.commons.httpclient.HttpStatus;
  29. import org.apache.commons.httpclient.methods.GetMethod;
  30. import org.json.JSONObject;
  31. import java.io.IOException;
  32. import androidx.core.net.ConnectivityManagerCompat;
  33. import kotlin.jvm.functions.Function1;
  34. class ConnectivityServiceImpl implements ConnectivityService {
  35. private final static String TAG = ConnectivityServiceImpl.class.getName();
  36. private final ConnectivityManager platformConnectivityManager;
  37. private final UserAccountManager accountManager;
  38. private final ClientFactory clientFactory;
  39. private final GetRequestBuilder requestBuilder;
  40. private final Logger logger;
  41. static class GetRequestBuilder implements Function1<String, GetMethod> {
  42. @Override
  43. public GetMethod invoke(String url) {
  44. return new GetMethod(url);
  45. }
  46. }
  47. ConnectivityServiceImpl(ConnectivityManager platformConnectivityManager,
  48. UserAccountManager accountManager,
  49. ClientFactory clientFactory,
  50. GetRequestBuilder requestBuilder,
  51. Logger logger) {
  52. this.platformConnectivityManager = platformConnectivityManager;
  53. this.accountManager = accountManager;
  54. this.clientFactory = clientFactory;
  55. this.requestBuilder = requestBuilder;
  56. this.logger = logger;
  57. }
  58. @Override
  59. public boolean isInternetWalled() {
  60. Connectivity c = getConnectivity();
  61. if (c.isConnected() && c.isWifi()) {
  62. GetMethod get = null;
  63. try {
  64. Server server = accountManager.getUser().getServer();
  65. String baseServerAddress = server.getUri().toString();
  66. if (baseServerAddress.isEmpty()) {
  67. return true;
  68. }
  69. String url;
  70. if (server.getVersion().compareTo(OwnCloudVersion.nextcloud_13) > 0) {
  71. url = baseServerAddress + "/index.php/204";
  72. } else {
  73. url = baseServerAddress + "/status.php";
  74. }
  75. get = requestBuilder.invoke(url);
  76. HttpClient client = clientFactory.createPlainClient();
  77. int status = client.executeMethod(get);
  78. if (server.getVersion().compareTo(OwnCloudVersion.nextcloud_13) > 0) {
  79. return !(status == HttpStatus.SC_NO_CONTENT &&
  80. (get.getResponseContentLength() == -1 || get.getResponseContentLength() == 0));
  81. } else {
  82. if (status == HttpStatus.SC_OK) {
  83. try {
  84. // try parsing json to verify response
  85. // check if json contains maintenance and it should be false
  86. String json = get.getResponseBodyAsString();
  87. return new JSONObject(json).getBoolean("maintenance");
  88. } catch (Exception e) {
  89. return true;
  90. }
  91. } else {
  92. return true;
  93. }
  94. }
  95. } catch (IOException e) {
  96. logger.e(TAG, "Error checking internet connection", e);
  97. } finally {
  98. if (get != null) {
  99. get.releaseConnection();
  100. }
  101. }
  102. } else {
  103. return !getConnectivity().isConnected();
  104. }
  105. return true;
  106. }
  107. @Override
  108. public Connectivity getConnectivity() {
  109. NetworkInfo networkInfo;
  110. try {
  111. networkInfo = platformConnectivityManager.getActiveNetworkInfo();
  112. } catch (Throwable t) {
  113. networkInfo = null; // no network available or no information (permission denied?)
  114. }
  115. if (networkInfo != null) {
  116. boolean isConnected = networkInfo.isConnectedOrConnecting();
  117. boolean isMetered = ConnectivityManagerCompat.isActiveNetworkMetered(platformConnectivityManager);
  118. boolean isWifi = networkInfo.getType() == ConnectivityManager.TYPE_WIFI || isAnyOtherNetworkWifi();
  119. return new Connectivity(isConnected, isMetered, isWifi, null);
  120. } else {
  121. return Connectivity.DISCONNECTED;
  122. }
  123. }
  124. private boolean isAnyOtherNetworkWifi() {
  125. for (NetworkInfo networkInfo : platformConnectivityManager.getAllNetworkInfo()) {
  126. if (networkInfo.isConnectedOrConnecting() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
  127. return true;
  128. }
  129. }
  130. return false;
  131. }
  132. }