浏览代码

Delay instant uploads when WiFi requirement is not met, and show as failed uploads in uploads list

David A. Velasco 9 年之前
父节点
当前提交
de1f34c493

+ 1 - 1
owncloud-android-library

@@ -1 +1 @@
-Subproject commit 4eb15976ea5bbf5a2b781bb89a8a31f2825b3925
+Subproject commit b0ac6b3fc94aa52a43927fae7b548e9123acf1bb

+ 59 - 19
src/com/owncloud/android/files/services/FileUploader.java

@@ -51,7 +51,6 @@ import com.owncloud.android.datamodel.UploadsStorageManager;
 import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus;
 import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus;
 import com.owncloud.android.db.OCUpload;
 import com.owncloud.android.db.OCUpload;
 import com.owncloud.android.db.PreferenceReader;
 import com.owncloud.android.db.PreferenceReader;
-import com.owncloud.android.db.UploadResult;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
@@ -66,6 +65,7 @@ import com.owncloud.android.notifications.NotificationDelayer;
 import com.owncloud.android.operations.UploadFileOperation;
 import com.owncloud.android.operations.UploadFileOperation;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.UploadListActivity;
 import com.owncloud.android.ui.activity.UploadListActivity;
+import com.owncloud.android.utils.ConnectivityUtils;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 import com.owncloud.android.utils.ErrorMessageAdapter;
 
 
 import java.util.AbstractList;
 import java.util.AbstractList;
@@ -260,16 +260,24 @@ public class FileUploader extends Service
     /**
     /**
      * Call to upload several new files
      * Call to upload several new files
      */
      */
-    public static void uploadNewFile(Context context, Account account, String[] localPaths, String[] remotePaths,
-                                     Integer behaviour, String mimeType, Boolean createRemoteFolder, int createdBy) {
+    public static void uploadNewFile(
+            Context context,
+            Account account,
+            String[] localPaths,
+            String[] remotePaths,
+            String[] mimeTypes,
+            Integer behaviour,
+            Boolean createRemoteFolder,
+            int createdBy
+    ) {
         Log_OC.d(TAG, "FileUploader.uploadNewFile()");
         Log_OC.d(TAG, "FileUploader.uploadNewFile()");
         Intent intent = new Intent(context, FileUploader.class);
         Intent intent = new Intent(context, FileUploader.class);
 
 
         intent.putExtra(FileUploader.KEY_ACCOUNT, account);
         intent.putExtra(FileUploader.KEY_ACCOUNT, account);
         intent.putExtra(FileUploader.KEY_LOCAL_FILE, localPaths);
         intent.putExtra(FileUploader.KEY_LOCAL_FILE, localPaths);
         intent.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
         intent.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
+        intent.putExtra(FileUploader.KEY_MIME_TYPE, mimeTypes);
         intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
         intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
-        intent.putExtra(FileUploader.KEY_MIME_TYPE, mimeType);
         intent.putExtra(FileUploader.KEY_CREATE_REMOTE_FOLDER, createRemoteFolder);
         intent.putExtra(FileUploader.KEY_CREATE_REMOTE_FOLDER, createRemoteFolder);
         intent.putExtra(FileUploader.KEY_CREATED_BY, createdBy);
         intent.putExtra(FileUploader.KEY_CREATED_BY, createdBy);
 
 
@@ -282,8 +290,16 @@ public class FileUploader extends Service
     public static void uploadNewFile(Context context, Account account, String localPath, String remotePath, int
     public static void uploadNewFile(Context context, Account account, String localPath, String remotePath, int
             behaviour, String mimeType, boolean createRemoteFile, int createdBy) {
             behaviour, String mimeType, boolean createRemoteFile, int createdBy) {
 
 
-        uploadNewFile(context, account, new String[]{localPath}, new String[]{remotePath}, behaviour, mimeType,
-                createRemoteFile, createdBy);
+        uploadNewFile(
+                context,
+                account,
+                new String[] {localPath},
+                new String[] {remotePath},
+                new String[] {mimeType},
+                behaviour,
+                createRemoteFile,
+                createdBy
+        );
     }
     }
 
 
     /**
     /**
@@ -904,19 +920,8 @@ public class FileUploader extends Service
                 return;
                 return;
             }
             }
 
 
-            /// Check that connectivity conditions are met
-            if (mCurrentUpload.isInstantPicture() &&
-                    PreferenceReader.instantPictureUploadViaWiFiOnly(this)) {
-
-                Log_OC.d(TAG, "Upload delayed until WiFi is available: " + mCurrentUpload.getRemotePath());
-                // TODO - update mUploadsStorageManager
-                return;
-            }
-            if (mCurrentUpload.isInstantVideo() &&
-                    PreferenceReader.instantVideoUploadViaWiFiOnly(this)) {
-
-                Log_OC.d(TAG, "Upload delayed until WiFi is available: " + mCurrentUpload.getRemotePath());
-                // TODO - update mUploadsStorageManager
+            /// Check that connectivity conditions are met and delayes the upload otherwise
+            if (delayForWifi()) {
                 return;
                 return;
             }
             }
 
 
@@ -979,6 +984,41 @@ public class FileUploader extends Service
 
 
     }
     }
 
 
+
+    /**
+     * Checks origin of current upload and network type to decide if should be delayed, according to
+     * current user preferences.
+     *
+     * @return      'True' if the upload was delayed until WiFi connectivity is available, 'false' otherwise.
+     */
+    private boolean delayForWifi() {
+
+        boolean delayInstantPicture = (
+                mCurrentUpload.isInstantPicture() &&
+                        PreferenceReader.instantPictureUploadViaWiFiOnly(this)
+        );
+        boolean delayInstantVideo = (mCurrentUpload.isInstantVideo() &&
+                PreferenceReader.instantVideoUploadViaWiFiOnly(this)
+        );
+        if ((delayInstantPicture || delayInstantVideo) &&
+                !ConnectivityUtils.isAppConnectedViaWiFi(this)) {
+
+            Log_OC.d(TAG, "Upload delayed until WiFi is available: " + mCurrentUpload.getRemotePath());
+            mPendingUploads.removePayload(
+                    mCurrentUpload.getAccount().name,
+                    mCurrentUpload.getRemotePath()
+            );
+            mUploadsStorageManager.updateDatabaseUploadResult(
+                    new RemoteOperationResult(ResultCode.DELAYED_FOR_WIFI),
+                    mCurrentUpload
+            );
+
+            return true;
+        }
+        return false;
+    }
+
+
     /**
     /**
      * Creates a status notification to show the upload progress
      * Creates a status notification to show the upload progress
      *
      *

+ 1 - 1
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -741,8 +741,8 @@ public class FileDisplayActivity extends HookActivity implements
                     getAccount(),
                     getAccount(),
                     filePaths,
                     filePaths,
                     remotePaths,
                     remotePaths,
-                    behaviour,
                     null,           // MIME type will be detected from file name
                     null,           // MIME type will be detected from file name
+                    behaviour,
                     false,          // do not create parent folder if not existent
                     false,          // do not create parent folder if not existent
                     UploadFileOperation.CREATED_BY_USER
                     UploadFileOperation.CREATED_BY_USER
             );
             );

+ 1 - 1
src/com/owncloud/android/ui/activity/Uploader.java

@@ -569,8 +569,8 @@ public class Uploader extends FileActivity
                         getAccount(),
                         getAccount(),
                         local.toArray(new String[local.size()]),
                         local.toArray(new String[local.size()]),
                         remote.toArray(new String[remote.size()]),
                         remote.toArray(new String[remote.size()]),
-                        FileUploader.LOCAL_BEHAVIOUR_FORGET,
                         null,       // MIME type will be detected from file name
                         null,       // MIME type will be detected from file name
+                        FileUploader.LOCAL_BEHAVIOUR_FORGET,
                         false,      // do not create parent folder if not existent
                         false,      // do not create parent folder if not existent
                         UploadFileOperation.CREATED_BY_USER
                         UploadFileOperation.CREATED_BY_USER
                 );
                 );

+ 40 - 0
src/com/owncloud/android/utils/ConnectivityUtils.java

@@ -0,0 +1,40 @@
+/**
+ * ownCloud Android client application
+ *
+ * @author David A. Velasco
+ * Copyright (C) 2016 ownCloud Inc.
+ * <p/>
+ * 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.
+ * <p/>
+ * 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.
+ * <p/>
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.owncloud.android.utils;
+
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+
+public class ConnectivityUtils {
+
+    public static boolean isAppConnectedViaWiFi(Context context) {
+        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+        return cm != null && cm.getActiveNetworkInfo() != null
+                && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
+                && cm.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED;
+    }
+
+    public static boolean isAppConnected(Context context) {
+        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
+    }
+
+}