Bläddra i källkod

check walled garden only on wifi

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 7 år sedan
förälder
incheckning
de51e9c004
1 ändrade filer med 24 tillägg och 4 borttagningar
  1. 24 4
      src/main/java/com/owncloud/android/utils/ConnectivityUtils.java

+ 24 - 4
src/main/java/com/owncloud/android/utils/ConnectivityUtils.java

@@ -27,9 +27,9 @@ import android.accounts.Account;
 import android.accounts.AuthenticatorException;
 import android.accounts.OperationCanceledException;
 import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
 
-import com.evernote.android.job.JobRequest;
-import com.evernote.android.job.util.Device;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudClient;
@@ -47,7 +47,7 @@ public class ConnectivityUtils {
     private final static String TAG = ConnectivityUtils.class.getName();
 
     public static boolean isInternetWalled(Context context) {
-        if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
+        if (isOnlineWithWifi(context)) {
             try {
                 Account account = AccountUtils.getCurrentOwnCloudAccount(context);
                 if (account != null) {
@@ -65,7 +65,7 @@ public class ConnectivityUtils {
                     OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(account, context);
 
                     int status = client.executeMethod(get);
-                    
+
                     if (serverVersion.compareTo(OwnCloudVersion.nextcloud_13) > 0) {
                         return !(status == 204 &&
                                 (get.getResponseContentLength() == -1 || get.getResponseContentLength() == 0));
@@ -94,9 +94,29 @@ public class ConnectivityUtils {
             } catch (AuthenticatorException e) {
                 Log_OC.e(TAG, e.getMessage());
             }
+        } else if (!isOffline(context)) {
+            return false;
         }
 
         return true;
+    }
+
+    public static boolean isOffline(Context context) {
+        try {
+            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+            return !cm.getActiveNetworkInfo().isConnectedOrConnecting();
+        } catch (NullPointerException exception) {
+            return false;
+        }
+    }
 
+    public static boolean isOnlineWithWifi(Context context) {
+        try {
+            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
+            return activeNetwork.isConnectedOrConnecting() && activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
+        } catch (NullPointerException exception) {
+            return false;
+        }
     }
 }