ConnectivityUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Inspired by: https://stackoverflow.com/questions/6493517/detect-if-android-device-has-internet-connection
  22. */
  23. package com.owncloud.android.utils;
  24. import android.accounts.Account;
  25. import android.accounts.AuthenticatorException;
  26. import android.accounts.OperationCanceledException;
  27. import android.content.Context;
  28. import android.net.ConnectivityManager;
  29. import android.net.NetworkInfo;
  30. import com.evernote.android.job.JobRequest;
  31. import com.evernote.android.job.util.Device;
  32. import com.owncloud.android.authentication.AccountUtils;
  33. import com.owncloud.android.lib.common.OwnCloudAccount;
  34. import com.owncloud.android.lib.common.OwnCloudClient;
  35. import com.owncloud.android.lib.common.OwnCloudClientFactory;
  36. import com.owncloud.android.lib.common.utils.Log_OC;
  37. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  38. import org.apache.commons.httpclient.methods.GetMethod;
  39. import org.json.JSONObject;
  40. import java.io.IOException;
  41. public class ConnectivityUtils {
  42. private final static String TAG = ConnectivityUtils.class.getName();
  43. public static boolean isInternetWalled(Context context) {
  44. if (isOnlineWithWifi(context)) {
  45. try {
  46. Account account = AccountUtils.getCurrentOwnCloudAccount(context);
  47. if (account != null) {
  48. OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
  49. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(account);
  50. String url;
  51. if (serverVersion.compareTo(OwnCloudVersion.nextcloud_13) > 0) {
  52. url = ocAccount.getBaseUri() + "/index.php/204";
  53. } else {
  54. url = ocAccount.getBaseUri() + "/status.php";
  55. }
  56. GetMethod get = new GetMethod(url);
  57. OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(account, context);
  58. int status = client.executeMethod(get);
  59. if (serverVersion.compareTo(OwnCloudVersion.nextcloud_13) > 0) {
  60. return !(status == 204 &&
  61. (get.getResponseContentLength() == -1 || get.getResponseContentLength() == 0));
  62. } else {
  63. if (status == 200) {
  64. try {
  65. // try parsing json to verify response
  66. // check if json contains maintenance and it should be false
  67. String json = get.getResponseBodyAsString();
  68. return new JSONObject(json).getBoolean("maintenance");
  69. } catch (Exception e) {
  70. return true;
  71. }
  72. } else {
  73. return true;
  74. }
  75. }
  76. }
  77. } catch (IOException e) {
  78. Log_OC.e(TAG, "Error checking internet connection", e);
  79. } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
  80. Log_OC.e(TAG, "Account not found", e);
  81. } catch (OperationCanceledException | AuthenticatorException e) {
  82. Log_OC.e(TAG, e.getMessage());
  83. }
  84. } else if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
  85. return false;
  86. }
  87. return true;
  88. }
  89. private static boolean isOnlineWithWifi(Context context) {
  90. try {
  91. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  92. NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  93. if (activeNetwork.isConnectedOrConnecting()) {
  94. switch (activeNetwork.getType()) {
  95. case ConnectivityManager.TYPE_VPN:
  96. // check if any other network is wifi
  97. for (NetworkInfo networkInfo : cm.getAllNetworkInfo()) {
  98. if (networkInfo.isConnectedOrConnecting() &&
  99. networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
  100. return true;
  101. }
  102. }
  103. return false;
  104. case ConnectivityManager.TYPE_WIFI:
  105. return true;
  106. default:
  107. return false;
  108. }
  109. } else {
  110. return false;
  111. }
  112. } catch (NullPointerException exception) {
  113. return false;
  114. }
  115. }
  116. }