Browse Source

Merge pull request #1672 from nextcloud/detect-walled-garden

Detect walled garden
Andy Scherzinger 7 years ago
parent
commit
f043232af0

+ 2 - 2
build.gradle

@@ -191,8 +191,8 @@ android {
 dependencies {
     /// dependencies for app building
     implementation 'com.android.support:multidex:1.0.2'
-    implementation 'com.github.nextcloud:android-library:1.0.32'
-    versionDevImplementation 'com.github.nextcloud:android-library:1.0.32'
+    implementation 'com.github.nextcloud:android-library:1.0.33'
+    versionDevImplementation 'com.github.nextcloud:android-library:1.0.33'
     implementation "com.android.support:support-v4:${supportLibraryVersion}"
     implementation "com.android.support:design:${supportLibraryVersion}"
     implementation 'com.jakewharton:disklrucache:2.0.2'

+ 3 - 3
src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java

@@ -352,10 +352,10 @@ public class ThumbnailsCacheManager {
                         }
                     }
                 } else {
-                    if (ConnectivityUtils.isAppConnected(MainApp.getAppContext())) {
-                        previewImageFragment.setErrorPreviewMessage();
-                    } else {
+                    if (ConnectivityUtils.isInternetWalled(MainApp.getAppContext())) {
                         previewImageFragment.setNoConnectionErrorMessage();
+                    } else {
+                        previewImageFragment.setErrorPreviewMessage();
                     }
                 }
             }

+ 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());

+ 75 - 37
src/main/java/com/owncloud/android/utils/ConnectivityUtils.java

@@ -1,64 +1,102 @@
-/**
- * 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.accounts.Account;
+import android.accounts.AuthenticatorException;
+import android.accounts.OperationCanceledException;
 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 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;
+import com.owncloud.android.lib.common.OwnCloudClientFactory;
 import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.lib.resources.status.OwnCloudVersion;
+
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.io.IOException;
 
 public class ConnectivityUtils {
 
     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;
-    }
+    public static boolean isInternetWalled(Context context) {
+        if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
+            try {
+                Account account = AccountUtils.getCurrentOwnCloudAccount(context);
+                if (account != null) {
+                    OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
+                    OwnCloudVersion serverVersion = AccountUtils.getServerVersion(account);
 
-    public static boolean isAppConnected(Context context) {
-        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
-        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
-    }
+                    String url;
+                    if (serverVersion.compareTo(OwnCloudVersion.nextcloud_13) > 0) {
+                        url = ocAccount.getBaseUri() + "/index.php/204";
+                    } else {
+                        url = ocAccount.getBaseUri() + "/status.php";
+                    }
 
-    public static boolean isCharging(Context context) {
-        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
-        Intent batteryStatus = context.registerReceiver(null, ifilter);
+                    GetMethod get = new GetMethod(url);
+                    OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(account, context);
 
-        int status = 0;
-        if (batteryStatus != null) {
-            status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
+                    int status = client.executeMethod(get);
+                    
+                    if (serverVersion.compareTo(OwnCloudVersion.nextcloud_13) > 0) {
+                        return !(status == 204 && get.getResponseContentLength() == -1);
+                    } else {
+                        if (status == 200) {
+                            try {
+                                // try parsing json to verify response
+                                // check if json contains maintenance and it should be false
+
+                                String json = get.getResponseBodyAsString();
+                                return new JSONObject(json).getBoolean("maintenance");
+                            } catch (JSONException e) {
+                                return true;
+                            }
+                        } else {
+                            return true;
+                        }
+                    }
+                }
+            } catch (IOException e) {
+                Log_OC.e(TAG, "Error checking internet connection", e);
+            } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
+                Log_OC.e(TAG, "Account not found", e);
+            } catch (OperationCanceledException e) {
+                e.printStackTrace();
+            } catch (AuthenticatorException e) {
+                e.printStackTrace();
+            }
         }
-        return status == BatteryManager.BATTERY_STATUS_CHARGING ||
-                status == BatteryManager.BATTERY_STATUS_FULL;
-    }
 
+        return true;
+
+    }
 }

+ 16 - 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,12 @@ public class FilesSyncHelper {
             }
         }
 
-        uploadRequester.retryFailedUploads(
-                context,
-                null,
-                null
-        );
+        new Thread(() -> {
+            if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY) &&
+                    !ConnectivityUtils.isInternetWalled(context)) {
+                uploadRequester.retryFailedUploads(context, null, null);
+            }
+        }).start();
     }
 
     @RequiresApi(api = Build.VERSION_CODES.N)
@@ -270,15 +272,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) {