Selaa lähdekoodia

Save OCFile into DB when a file is prepare to be uploaded, before uploading (WIP)

masensio 9 vuotta sitten
vanhempi
commit
e89104b40e

+ 4 - 4
src/com/owncloud/android/datamodel/UploadsStorageManager.java

@@ -268,9 +268,9 @@ public class UploadsStorageManager extends Observable {
         if (c.moveToFirst()) {
             do {
                 long fileUploadId = c.getLong(c.getColumnIndex(ProviderTableMeta.UPLOADS_FILE_ID));
-                // getFile for this fileUploadId
-                OCFile file = getUploadFile(fileUploadId);
-                OCUpload upload = new OCUpload(file);
+//                // getFile for this fileUploadId
+//                OCFile file = getUploadFile(fileUploadId);
+                OCUpload upload = createOCUploadFromCursor(c);
                 if (upload == null) {
                     Log_OC.e(TAG, "Upload for file id = " + fileUploadId + "not found on DB");
                 } else {
@@ -369,7 +369,6 @@ public class UploadsStorageManager extends Observable {
             upload.setAccountName(c.getString(c.getColumnIndex(ProviderTableMeta.UPLOADS_ACCOUNT_NAME)));
             upload.setLocalAction(FileUploadService.LocalBehaviour.fromValue(
                     c.getInt(c.getColumnIndex((ProviderTableMeta.UPLOADS_LOCAL_BEHAVIOUR)))));
-            upload.setUploadId(c.getLong(c.getColumnIndex(ProviderTableMeta.UPLOADS_UPLOAD_TIME)));
             upload.setForceOverwrite(c.getInt(
                     c.getColumnIndex(ProviderTableMeta.UPLOADS_FORCE_OVERWRITE)) == 1 ? true : false);
             upload.setCreateRemoteFolder(c.getInt(
@@ -378,6 +377,7 @@ public class UploadsStorageManager extends Observable {
                     c.getColumnIndex(ProviderTableMeta.UPLOADS_IS_WHILE_CHARGING_ONLY)) == 1 ? true : false);
             upload.setUseWifiOnly(c.getInt(
                     c.getColumnIndex(ProviderTableMeta.UPLOADS_IS_WIFI_ONLY)) == 1 ? true : false);
+            upload.setUploadTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.UPLOADS_UPLOAD_TIMESTAMP)));
             upload.setLastResult(UploadResult.fromValue(
                     c.getInt(c.getColumnIndex(ProviderTableMeta.UPLOADS_LAST_RESULT))));
         }

+ 11 - 5
src/com/owncloud/android/db/OCUpload.java

@@ -28,6 +28,7 @@ import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.UploadsStorageManager;
 import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus;
 import com.owncloud.android.files.services.FileUploadService.LocalBehaviour;
+import com.owncloud.android.lib.common.utils.Log_OC;
 
 import java.util.Calendar;
 import java.util.GregorianCalendar;
@@ -54,7 +55,7 @@ public class OCUpload {
     /**
      * Date and time when this upload was first requested.
      */
-    private Calendar mUploadTime = new GregorianCalendar();
+    private Calendar mUploadTime = new GregorianCalendar();  //TODO needed??
     /**
      * Overwrite destination file?
      */
@@ -232,7 +233,7 @@ public class OCUpload {
     public void setAccountName(String accountName) {
         this.mAccountName = accountName;
     }
-    
+
     /**
      * Returns owncloud account as {@link Account} object.  
      */
@@ -268,9 +269,14 @@ public class OCUpload {
      * For debugging purposes only.
      */
     public String toFormattedString() {
-        String localPath = getLocalPath()!= null ? getLocalPath() : "";
-        return localPath+ " status:" + getUploadStatus() + " result:" +
-                (getLastResult() == null?"null" : getLastResult().getValue());
+        try {
+            String localPath = getLocalPath() != null ? getLocalPath() : "";
+            return localPath + " status:" + getUploadStatus() + " result:" +
+                    (getLastResult() == null ? "null" : getLastResult().getValue());
+        } catch (NullPointerException e){
+            Log_OC.d(TAG, "Exception " + e.toString() );
+            return (e.toString());
+        }
     }
 
     /**

+ 20 - 17
src/com/owncloud/android/files/services/FileUploadService.java

@@ -410,6 +410,7 @@ public class FileUploadService extends Service implements OnDatatransferProgress
                 Log_OC.e(TAG, "KEY_ACCOUNT no set or provided KEY_ACCOUNT does not exist");
                 return;
             }
+            mStorageManager = new FileDataStorageManager(account, getContentResolver());
 
             OCFile[] files = null;
             // if KEY_FILE given, use it
@@ -454,6 +455,8 @@ public class FileUploadService extends Service implements OnDatatransferProgress
                                 + " and localPaths[i]:" + localPaths[i]);
                         return;
                     }
+                    mStorageManager.saveFile(files[i]);
+                    files[i] = mStorageManager.getFileByLocalPath(files[i].getStoragePath());
                 }
             }
 
@@ -472,40 +475,40 @@ public class FileUploadService extends Service implements OnDatatransferProgress
             // save always persistently path of upload, so it can be retried if
             // failed.
             for (int i = 0; i < files.length; i++) {
-                OCUpload uploadObject = new OCUpload(files[i]);
-                uploadObject.setAccountName(account.name);
-                uploadObject.setForceOverwrite(forceOverwrite);
-                uploadObject.setCreateRemoteFolder(isCreateRemoteFolder);
-                uploadObject.setLocalAction(localAction);
-                uploadObject.setUseWifiOnly(isUseWifiOnly);
-                uploadObject.setWhileChargingOnly(isWhileChargingOnly);
-                uploadObject.setUploadTimestamp(uploadTimestamp);
-                uploadObject.setUploadStatus(UploadStatus.UPLOAD_LATER);
+                OCUpload upload = new OCUpload(files[i]);
+                upload.setAccountName(account.name);
+                upload.setForceOverwrite(forceOverwrite);
+                upload.setCreateRemoteFolder(isCreateRemoteFolder);
+                upload.setLocalAction(localAction);
+                upload.setUseWifiOnly(isUseWifiOnly);
+                upload.setWhileChargingOnly(isWhileChargingOnly);
+                upload.setUploadTimestamp(uploadTimestamp);
+                upload.setUploadStatus(UploadStatus.UPLOAD_LATER);
                 
-                String uploadKey = buildRemoteName(uploadObject);
-                OCUpload previous = mPendingUploads.putIfAbsent(uploadKey, uploadObject);
+                String uploadKey = buildRemoteName(upload);
+                OCUpload previous = mPendingUploads.putIfAbsent(uploadKey, upload);
                 
                 if(previous == null)
                 {
-                    Log_OC.d(TAG, "mPendingUploads added: " + uploadObject.toFormattedString());
+                    Log_OC.d(TAG, "mPendingUploads added: " + upload.toFormattedString());
 
                     // if upload was not queued before, we can add it to
                     // database because the user wants the file to be uploaded.
                     // however, it can happened that the user uploaded the same
                     // file before in which case there is an old db entry.
                     // delete that to be sure we have the latest one.
-                    if(mUploadsStorageManager.removeUpload(uploadObject.getLocalPath())>0) {
-                        Log_OC.w(TAG, "There was an old DB entry " + uploadObject.getLocalPath()
+                    if(mUploadsStorageManager.removeUpload(upload.getLocalPath())>0) {
+                        Log_OC.w(TAG, "There was an old DB entry " + upload.getLocalPath()
                                 + " which had to be removed in order to add new one.");
                     }
-                    boolean success = mUploadsStorageManager.storeUpload(uploadObject);
+                    boolean success = mUploadsStorageManager.storeUpload(upload);
                     if(!success) {
-                        Log_OC.e(TAG, "Could not add upload " + uploadObject.getLocalPath()
+                        Log_OC.e(TAG, "Could not add upload " + upload.getLocalPath()
                                 + " to database. This should not happen.");
                     } 
                 } else {
                     Log_OC.w(TAG, "FileUploadService got upload intent for file which is already queued: "
-                            + uploadObject.getRemotePath());
+                            + upload.getRemotePath());
                     // upload already pending. ignore.
                 }
             }

+ 0 - 1
src/com/owncloud/android/files/services/FileUploader.java

@@ -51,7 +51,6 @@ import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.authentication.AuthenticatorActivity;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
-//import com.owncloud.android.db.DbHandler;
 import com.owncloud.android.lib.common.OwnCloudAccount;
 import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;

+ 1 - 1
src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java

@@ -95,7 +95,7 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
     private UploadGroup[] mUploadGroups = null;
 
     public ExpandableUploadListAdapter(FileActivity parentActivity) {
-        Log_OC.d(TAG, "UploadListAdapter");
+        Log_OC.d(TAG, "ExpandableUploadListAdapter");
         mParentActivity = parentActivity;
         mUploadsStorageManager = new UploadsStorageManager(mParentActivity.getContentResolver());
         mUploadGroups = new UploadGroup[3];