Răsfoiți Sursa

Remove uploads from persisted list when cancelled

David A. Velasco 9 ani în urmă
părinte
comite
5eeca847d1

+ 20 - 19
src/com/owncloud/android/datamodel/UploadsStorageManager.java

@@ -261,40 +261,42 @@ public class UploadsStorageManager extends Observable {
         notifyObservers();
     }
 
+
     /**
-     * Remove upload from upload list. Should be called when cleaning up upload
-     * list.
+     * Remove an upload from the uploads list, known its target account and remote path.
      *
-     * @param localPath
-     * @return true when one or more upload entries were removed
+     * @param upload            Upload instance to remove from persisted storage.
+     *
+     * @return true when the upload was stored and could be removed.
      */
-    public int removeUpload(String localPath) {
+    public int removeUpload(OCUpload upload) {
         int result = getDB().delete(
                 ProviderTableMeta.CONTENT_URI_UPLOADS,
-                ProviderTableMeta.UPLOADS_LOCAL_PATH + "=?",
-                new String[]{localPath}
+                ProviderTableMeta._ID + "=?" ,
+                new String[]{Long.toString(upload.getUploadId())}
         );
-        Log_OC.d(TAG, "delete returns with: " + result + " for file: " + localPath);
+        Log_OC.d(TAG, "delete returns " + result + " for upload " + upload);
         if (result > 0) {
             notifyObserversNow();
         }
         return result;
     }
 
+
     /**
-     * Remove upload from upload list. Should be called when cleaning up upload
-     * list.
+     * Remove an upload from the uploads list, known its target account and remote path.
      *
-     * @param id
+     * @param accountName       Name of the OC account target of the upload to remove.
+     * @param remotePath        Absolute path in the OC account target of the upload to remove.
      * @return true when one or more upload entries were removed
      */
-    public int removeUpload(long id) {
+    public int removeUpload(String accountName, String remotePath) {
         int result = getDB().delete(
                 ProviderTableMeta.CONTENT_URI_UPLOADS,
-                ProviderTableMeta._ID + "=?",
-                new String[]{String.valueOf(id)}
+                ProviderTableMeta.UPLOADS_ACCOUNT_NAME + "=? AND " + ProviderTableMeta.UPLOADS_REMOTE_PATH + "=?" ,
+                new String[]{accountName, remotePath}
         );
-        Log_OC.d(TAG, "delete returns with: " + result + " for file: " + id);
+        Log_OC.d(TAG, "delete returns " + result + " for file " + remotePath + " in " + accountName);
         if (result > 0) {
             notifyObserversNow();
         }
@@ -541,10 +543,9 @@ public class UploadsStorageManager extends Observable {
         // result: success or fail notification
         Log_OC.d(TAG, "updateDataseUploadResult uploadResult: " + uploadResult + " upload: " + upload);
         if (uploadResult.isCancelled()) {
-            updateUploadStatus(
-                    upload.getOCUploadId(),
-                    UploadStatus.UPLOAD_FAILED,
-                    UploadResult.CANCELLED
+            removeUpload(
+                    upload.getAccount().name,
+                    upload.getRemotePath()
             );
         } else {
 

+ 0 - 12
src/com/owncloud/android/files/FileOperationsHelper.java

@@ -541,18 +541,6 @@ public class FileOperationsHelper {
         }
     }
 
-    /**
-     * Remove upload from upload list.
-     */
-    public void removeUploadFromList(OCUpload upload) {
-        FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
-        if (uploaderBinder != null) {
-            uploaderBinder.remove(upload);
-        }  else {
-            Log_OC.w(TAG, "uploaderBinder not set. Cannot remove " + upload.getLocalPath());
-        }
-    }
-
     /**
      * Cancel the transference in downloads (files/folders) and file uploads
      * @param file OCFile

+ 7 - 32
src/com/owncloud/android/files/services/FileUploader.java

@@ -524,7 +524,7 @@ public class FileUploader extends Service
                         uploadKey = putResult.first;
                         requestedUploads.add(uploadKey);
                     } else {
-                        mUploadsStorageManager.removeUpload(id);
+                        mUploadsStorageManager.removeUpload(account.name, files[i].getRemotePath());
                     }
                     // else, file already in the queue of uploads; don't repeat the request
                 }
@@ -675,14 +675,12 @@ public class FileUploader extends Service
                 boolean pending = !upload.isUploadInProgress();
                 upload.cancel();
                 if (pending) {
-                    // need to update now table in mUploadsStorageManager, since the operation will not get
-                    // to be run by FileUploader#uploadFile
-                    OCUpload ocUpload =
-                            mUploadsStorageManager.getUploadByLocalPath(localPath)[0];
-                    // TODO bad idea, should search for account + remoteName, or uploadId
-                    ocUpload.setUploadStatus(UploadStatus.UPLOAD_FAILED);
-                    ocUpload.setLastResult(UploadResult.CANCELLED);
-                    mUploadsStorageManager.updateUploadStatus(ocUpload);
+                    // need to update now table in mUploadsStorageManager,
+                    // since the operation will not get to be run by FileUploader#uploadFile
+                    mUploadsStorageManager.removeUpload(
+                            accountName,
+                            remotePath
+                    );
                 }
             }
         }
@@ -705,31 +703,8 @@ public class FileUploader extends Service
             cancelUploadsForAccount(account);
         }
 
-        // TODO: Review: Method from FileUploadService with some changes because the merge with FileUploader
-        public void remove(Account account, OCFile file) {
-            Pair<UploadFileOperation, String> removeResult =
-                    mPendingUploads.remove(account.name, file.getRemotePath());
-            UploadFileOperation upload = removeResult.first;
-            //OCUpload upload = mPendingUploads.remove(buildRemoteName(account, file));
-            if (upload == null) {
-                Log_OC.e(TAG, "Could not delete upload " + file + " from mPendingUploads.");
-            }
-            int d = mUploadsStorageManager.removeUpload(upload.getStoragePath());
-            if (d == 0) {
-                Log_OC.e(TAG, "Could not delete upload " + file.getStoragePath() + " from database.");
-            }
-        }
-
-        public void remove(OCUpload upload) {
-            int d = mUploadsStorageManager.removeUpload(upload.getUploadId());
-            if (d == 0) {
-                Log_OC.e(TAG, "Could not delete upload " + upload.getRemotePath() + " from database.");
-            }
-        }
-
         // TODO: Review: Method from FileUploader with some changes because the merge with FileUploader
         // TODO Complete operation to retry the upload
-
         /**
          * Puts upload in upload list and tell FileUploader to upload items in list.
          */

+ 8 - 34
src/com/owncloud/android/ui/activity/UploadListActivity.java

@@ -2,6 +2,7 @@
  *   ownCloud Android client application
  *
  *   @author LukeOwncloud
+ *   @author David A. Velasco
  *   Copyright (C) 2015 ownCloud Inc.
  *
  *   This program is free software: you can redistribute it and/or modify
@@ -44,7 +45,7 @@ import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.fragment.UploadListFragment;
-import com.owncloud.android.ui.preview.PreviewImageActivity;
+import com.owncloud.android.utils.MimetypeIconUtil;
 
 import java.io.File;
 
@@ -66,7 +67,6 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        //Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
         setContentView(R.layout.upload_list_layout);
 
         // Navigation Drawer
@@ -75,7 +75,7 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
         // Add fragment with a transaction for setting a tag
         if(savedInstanceState == null) {
             createUploadListFragment();
-        }
+        } // else, the Fragment Manager makes the job on configuration changes
 
         // enable ActionBar app icon to behave as action to toggle nav drawer
         getSupportActionBar().setHomeButtonEnabled(true);
@@ -124,6 +124,7 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
     // ////////////////////////////////////////
     @Override
     public boolean onUploadItemClick(OCUpload file) {
+        /// TODO is this path still active?
         File f = new File(file.getLocalPath());
         if(!f.exists()) {
             Toast.makeText(this, "Cannot open. Local file does not exist.",
@@ -135,15 +136,15 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
     }
 
     /**
-     * Open file with app associates with its mimetype. If mimetype unknown, show list with all apps.  
+     * Open file with app associates with its MIME type. If MIME type unknown, show list with all apps.
      */
     private void openFileWithDefault(String localPath) {
         Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
         File file = new File(localPath);
-        String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
-        String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
-        if (mimetype == null)
+        String mimetype = MimetypeIconUtil.getBestMimeTypeByFilename(localPath);
+        if ("application/octet-stream".equals(mimetype)) {
             mimetype = "*/*";
+        }
         myIntent.setDataAndType(Uri.fromFile(file), mimetype);
         try {
             startActivity(myIntent);
@@ -158,37 +159,10 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
      * Same as openFileWithDefault() but user cannot save default app.
      * @param ocFile
      */
-    @SuppressWarnings("unused")
     private void openFileWithDefaultNoDefault(OCFile ocFile) {
         getFileOperationsHelper().openFile(ocFile);
     }
 
-    /**
-     * WARNING! This opens the local copy inside owncloud directory. If file not uploaded yet,
-     * there is none.
-     */
-    /*
-    @SuppressWarnings("unused")
-    private void openPreview(OCUpload file) {
-     // preview image
-        Intent showDetailsIntent = new Intent(this, PreviewImageActivity.class);
-        showDetailsIntent.putExtra(EXTRA_FILE, file.getOCFile());
-        showDetailsIntent.putExtra(EXTRA_ACCOUNT, getAccount());
-        startActivity(showDetailsIntent);  
-    }
-    */
-
-    /*
-    @SuppressWarnings("unused")
-    private void openDetails(OCUpload file) {
-        OCFile ocFile = file.getOCFile();
-        Intent showDetailsIntent = new Intent(this, FileDisplayActivity.class);
-        showDetailsIntent.putExtra(FileActivity.EXTRA_FILE, ocFile);
-        showDetailsIntent.putExtra(FileActivity.EXTRA_ACCOUNT, file.getAccount(this));
-        startActivity(showDetailsIntent);
-    }
-    */
-
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         boolean retval = true;

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

@@ -42,7 +42,6 @@ import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.datamodel.UploadsStorageManager;
 import com.owncloud.android.datamodel.UploadsStorageManager.UploadStatus;
 import com.owncloud.android.db.OCUpload;
-import com.owncloud.android.db.UploadResult;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.lib.common.network.OnDatatransferProgressListener;
 import com.owncloud.android.lib.common.utils.Log_OC;
@@ -392,6 +391,7 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
                         FileUploader.FileUploaderBinder uploaderBinder = mParentActivity.getFileUploaderBinder();
                         if (uploaderBinder != null) {
                             uploaderBinder.cancel(upload);
+                            refreshView();
                         }
                     }
                 });
@@ -401,7 +401,7 @@ public class ExpandableUploadListAdapter extends BaseExpandableListAdapter imple
                 rightButton.setOnClickListener(new OnClickListener() {
                     @Override
                     public void onClick(View v) {
-                        mParentActivity.getFileOperationsHelper().removeUploadFromList(upload);
+                        mUploadsStorageManager.removeUpload(upload);
                         refreshView();
                     }
                 });

+ 8 - 11
src/com/owncloud/android/ui/fragment/UploadListFragment.java

@@ -36,12 +36,10 @@ import android.widget.ListView;
 import android.widget.Toast;
 
 import com.owncloud.android.R;
-import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.db.OCUpload;
 import com.owncloud.android.files.services.FileUploader;
 import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.FileActivity;
-import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.adapter.ExpandableUploadListAdapter;
 
 /**
@@ -99,10 +97,7 @@ public class UploadListFragment extends ExpandableListFragment {
     public void onActivityCreated(Bundle savedInstanceState) {
         Log_OC.d(TAG, "onActivityCreated() start");
         super.onActivityCreated(savedInstanceState);
-//        mAdapter = new ExpandableUploadListAdapter((FileActivity)getActivity());
-//        setListAdapter(mAdapter);
-        //mAdapter.setFileActivity(((FileActivity) getActivity()));
-        
+
         registerForContextMenu(getListView());
         getListView().setOnCreateContextMenuListener(this);
     }
@@ -159,7 +154,8 @@ public class UploadListFragment extends ExpandableListFragment {
             }
         }
     }
-    
+
+    // TODO review if this path is really needed any time
     @Override
     public boolean onContextItemSelected (MenuItem item) {
         ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();  
@@ -173,13 +169,14 @@ public class UploadListFragment extends ExpandableListFragment {
                 uploaderBinder.cancel(uploadFile);
             }
             return true;
-        case R.id.action_remove_upload: {
-            ((FileActivity) getActivity()).getFileOperationsHelper().removeUploadFromList(uploadFile);
-            return true;
+//        case R.id.action_remove_upload: {
+//            ((FileActivity) getActivity()).getFileOperationsHelper().removeUploadFromList(uploadFile);
+//            return true;
 //        }case R.id.action_retry_upload: {
 //            ((FileActivity) getActivity()).getFileOperationsHelper().retryUpload(uploadFile);
 //            return true;
-        } case R.id.action_see_details: {
+//        }
+        case R.id.action_see_details: {
             Toast.makeText(getActivity(), "TO DO", Toast.LENGTH_SHORT).show();
             /*
             Intent showDetailsIntent = new Intent(getActivity(), FileDisplayActivity.class);