Эх сурвалжийг харах

Detect walled garden

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 жил өмнө
parent
commit
d190b21cae

+ 6 - 0
src/main/java/com/owncloud/android/operations/UploadFileOperation.java

@@ -45,6 +45,7 @@ import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation;
 import com.owncloud.android.lib.resources.files.RemoteFile;
 import com.owncloud.android.lib.resources.files.UploadRemoteFileOperation;
 import com.owncloud.android.operations.common.SyncOperation;
+import com.owncloud.android.utils.ConnectivityUtils;
 import com.owncloud.android.utils.FileStorageUtils;
 import com.owncloud.android.utils.MimeType;
 import com.owncloud.android.utils.MimeTypeUtil;
@@ -341,6 +342,11 @@ public class UploadFileOperation extends SyncOperation {
 
         try {
 
+            if (Device.getNetworkType(mContext).equals(JobRequest.NetworkType.ANY) ||
+                    ConnectivityUtils.isInternetWalled(mContext)) {
+                return new RemoteOperationResult(ResultCode.NO_NETWORK_CONNECTION);
+            }
+
             /// Check that connectivity conditions are met and delays the upload otherwise
             if (mOnWifiOnly && !Device.getNetworkType(mContext).equals(JobRequest.NetworkType.UNMETERED)) {
                 Log_OC.d(TAG, "Upload delayed until WiFi is available: " + getRemotePath());

+ 39 - 41
src/main/java/com/owncloud/android/utils/ConnectivityUtils.java

@@ -1,64 +1,62 @@
-/**
- * ownCloud Android client application
+/*
+ * Nextcloud Android client application
  *
- * @author David A. Velasco
- * Copyright (C) 2016 ownCloud Inc.
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ * Copyright (C) 2017 Nextcloud GmbH.
  *
  * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2,
- * as published by the Free Software Foundation.
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * GNU Affero General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
+ * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Inspired by: https://stackoverflow.com/questions/6493517/detect-if-android-device-has-internet-connection
  */
 
 package com.owncloud.android.utils;
 
 import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.net.ConnectivityManager;
-import android.net.NetworkInfo;
-import android.os.BatteryManager;
-import android.support.v4.net.ConnectivityManagerCompat;
+import android.util.Log;
 
-import com.owncloud.android.lib.common.utils.Log_OC;
+import com.evernote.android.job.JobRequest;
+import com.evernote.android.job.util.Device;
 
-public class ConnectivityUtils {
+import org.apache.commons.httpclient.util.HttpURLConnection;
 
-    private final static String TAG = ConnectivityUtils.class.getName();
-
-    public static boolean isAppConnectedViaUnmeteredWiFi(Context context) {
-        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
-        boolean result =
-                cm != null && cm.getActiveNetworkInfo() != null
-                        && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
-                        && cm.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED
-                        && !ConnectivityManagerCompat.isActiveNetworkMetered(cm);
-        Log_OC.d(TAG, "is AppConnectedViaWifi returns " + result);
-        return result;
-    }
+import java.io.IOException;
+import java.net.URL;
 
-    public static boolean isAppConnected(Context context) {
-        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
-        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
-    }
+public class ConnectivityUtils {
 
-    public static boolean isCharging(Context context) {
-        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
-        Intent batteryStatus = context.registerReceiver(null, ifilter);
+    private final static String TAG = ConnectivityUtils.class.getName();
 
-        int status = 0;
-        if (batteryStatus != null) {
-            status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
+    public static boolean isInternetWalled(Context context) {
+        if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
+            try {
+                HttpURLConnection urlc = (HttpURLConnection)
+                        (new URL("http://clients3.google.com/generate_204")
+                                .openConnection());
+                urlc.setRequestProperty("User-Agent", "Android");
+                urlc.setRequestProperty("Connection", "close");
+                urlc.setConnectTimeout(1500);
+                urlc.connect();
+                return !(urlc.getResponseCode() == 204 &&
+                        urlc.getContentLength() == 0);
+            } catch (IOException e) {
+                Log.e(TAG, "Error checking internet connection", e);
+            }
+        } else {
+            Log.d(TAG, "No network available!");
         }
-        return status == BatteryManager.BATTERY_STATUS_CHARGING ||
-                status == BatteryManager.BATTERY_STATUS_FULL;
-    }
+        return true;
 
+    }
 }

+ 18 - 14
src/main/java/com/owncloud/android/utils/FilesSyncHelper.java

@@ -35,6 +35,7 @@ import android.text.TextUtils;
 import android.util.Log;
 
 import com.evernote.android.job.JobRequest;
+import com.evernote.android.job.util.Device;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
@@ -153,7 +154,7 @@ public class FilesSyncHelper {
 
         for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) {
             if ((syncedFolder.isEnabled()) && ((MediaFolderType.CUSTOM != syncedFolder.getType()) || !skipCustom)) {
-                    insertAllDBEntriesForSyncedFolder(syncedFolder);
+                insertAllDBEntriesForSyncedFolder(syncedFolder);
             }
         }
     }
@@ -228,11 +229,14 @@ public class FilesSyncHelper {
             }
         }
 
-        uploadRequester.retryFailedUploads(
-                context,
-                null,
-                null
-        );
+        if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY) ||
+                !ConnectivityUtils.isInternetWalled(context)) {
+            uploadRequester.retryFailedUploads(
+                    context,
+                    null,
+                    null
+            );
+        }
     }
 
     @RequiresApi(api = Build.VERSION_CODES.N)
@@ -270,15 +274,15 @@ public class FilesSyncHelper {
             }
         }
 
-            if (hasImageFolders || hasVideoFolders) {
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
-                    scheduleJobOnN(hasImageFolders, hasVideoFolders, force);
-                }
-            } else {
-                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
-                    cancelJobOnN();
-                }
+        if (hasImageFolders || hasVideoFolders) {
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+                scheduleJobOnN(hasImageFolders, hasVideoFolders, force);
             }
+        } else {
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+                cancelJobOnN();
+            }
+        }
     }
 
     public static void scheduleFilesSyncIfNeeded(Context context) {