Browse Source

Merge pull request #773 from owncloud/triggerMediaScan

Trigger media scan
jabarros 10 years ago
parent
commit
d58d6ca4d0

+ 0 - 2
.travis.yml

@@ -7,9 +7,7 @@ android:
     - android-19
 before_install:
   - rm pom.xml
-  - ./setup_env.sh gradle
 script:
-  - ./gradlew clean build
   - ./setup_env.sh ant
   - ant clean
   - ant debug

+ 1 - 1
owncloud-android-library

@@ -1 +1 @@
-Subproject commit 7ff0bc0d837edea90b075140f8caba308ce7d378
+Subproject commit 5bd0d7387712ce3f53869294761ac4d8537841cd

+ 52 - 111
src/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Vector;
 
 import com.owncloud.android.MainApp;
@@ -33,7 +34,6 @@ import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.lib.resources.files.FileUtils;
 import com.owncloud.android.utils.FileStorageUtils;
 
-
 import android.accounts.Account;
 import android.content.ContentProviderClient;
 import android.content.ContentProviderOperation;
@@ -41,6 +41,7 @@ import android.content.ContentProviderResult;
 import android.content.ContentResolver;
 import android.content.ContentUris;
 import android.content.ContentValues;
+import android.content.Intent;
 import android.content.OperationApplicationException;
 import android.database.Cursor;
 import android.net.Uri;
@@ -346,7 +347,9 @@ public class FileDataStorageManager {
                     ).withSelection(where, whereArgs).build());
                     
                     if (file.isDown()) {
-                        new File(file.getStoragePath()).delete();
+                        String path = file.getStoragePath();
+                        new File(path).delete();
+                        triggerMediaScan(path); // notify MediaScanner about removed file
                     }
                 }
             }
@@ -484,8 +487,12 @@ public class FileDataStorageManager {
                     }
                     success &= (deleted > 0); 
                 }
-                if (removeLocalCopy && file.isDown() && file.getStoragePath() != null && success) {
-                    success = new File(file.getStoragePath()).delete();
+                String localPath = file.getStoragePath();
+                if (removeLocalCopy && file.isDown() && localPath != null && success) {
+                    success = new File(localPath).delete();
+                    if (success) {
+                        triggerMediaScan(localPath);
+                    }
                     if (!removeDBData && success) {
                         // maybe unnecessary, but should be checked TODO remove if unnecessary
                         file.setStoragePath(null);
@@ -542,11 +549,13 @@ public class FileDataStorageManager {
                         success &= removeLocalFolder(file);
                     } else {
                         if (file.isDown()) {
+                            String path = file.getStoragePath();
                             File localFile = new File(file.getStoragePath());
                             success &= localFile.delete();
                             if (success) {
                                 file.setStoragePath(null);
                                 saveFile(file);
+                                triggerMediaScan(path); // notify MediaScanner about removed file
                             }
                         }
                     }
@@ -568,7 +577,9 @@ public class FileDataStorageManager {
                 if (localFile.isDirectory()) {
                     success &= removeLocalFolder(localFile);
                 } else {
+                    String path = localFile.getAbsolutePath();
                     success &= localFile.delete();
+                    triggerMediaScan(path); // notify MediaScanner about removed file
                 }
             }
         }
@@ -576,109 +587,20 @@ public class FileDataStorageManager {
         return success;
     }
 
+    
     /**
-     * Updates database for a folder that was moved to a different location.
+     * Updates database and file system for a file or folder that was moved to a different location.
      * 
      * TODO explore better (faster) implementations
      * TODO throw exceptions up !
      */
-    public void moveFolder(OCFile folder, String newPath) {
-        // TODO check newPath
-
-        if (    folder != null && folder.isFolder() && 
-                folder.fileExists() && !OCFile.ROOT_PATH.equals(folder.getFileName())
-            ) {
-            /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
-            Cursor c = null;
-            if (getContentProviderClient() != null) {
-                try {
-                    c = getContentProviderClient().query (
-                        ProviderTableMeta.CONTENT_URI, 
-                        null,
-                        ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + 
-                                ProviderTableMeta.FILE_PATH + " LIKE ? ",
-                        new String[] { mAccount.name, folder.getRemotePath() + "%"  }, 
-                        ProviderTableMeta.FILE_PATH + " ASC "
-                    );
-                } catch (RemoteException e) {
-                    Log_OC.e(TAG, e.getMessage());
-                }
-            } else {
-                c = getContentResolver().query (
-                    ProviderTableMeta.CONTENT_URI, 
-                    null,
-                    ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + 
-                            ProviderTableMeta.FILE_PATH + " LIKE ? ",
-                    new String[] { mAccount.name, folder.getRemotePath() + "%"  }, 
-                    ProviderTableMeta.FILE_PATH + " ASC "
-                );
-            }
-
-            /// 2. prepare a batch of update operations to change all the descendants
-            ArrayList<ContentProviderOperation> operations = 
-                    new ArrayList<ContentProviderOperation>(c.getCount());
-            int lengthOfOldPath = folder.getRemotePath().length();
-            String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
-            int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
-            if (c.moveToFirst()) {
-                do {
-                    ContentValues cv = new ContentValues(); // keep the constructor in the loop
-                    OCFile child = createFileInstance(c);
-                    cv.put(
-                        ProviderTableMeta.FILE_PATH, 
-                        newPath + child.getRemotePath().substring(lengthOfOldPath)
-                    );
-                    if (    child.getStoragePath() != null && 
-                            child.getStoragePath().startsWith(defaultSavePath)  ) {
-                        cv.put(
-                                ProviderTableMeta.FILE_STORAGE_PATH, 
-                                defaultSavePath + newPath + 
-                                    child.getStoragePath().substring(lengthOfOldStoragePath)
-                        );
-                    }
-                    operations.add(
-                            ContentProviderOperation.
-                            newUpdate(ProviderTableMeta.CONTENT_URI).
-                            withValues(cv).
-                            withSelection(  
-                                    ProviderTableMeta._ID + "=?", 
-                                    new String[] { String.valueOf(child.getFileId()) }
-                            ).
-                            build()
-                    );
-                } while (c.moveToNext());
-            }
-            c.close();
-
-            /// 3. apply updates in batch
-            try {
-                if (getContentResolver() != null) {
-                    getContentResolver().applyBatch(MainApp.getAuthority(), operations);
-
-                } else {
-                    getContentProviderClient().applyBatch(operations);
-                }
-
-            } catch (OperationApplicationException e) {
-                Log_OC.e(TAG, "Fail to update descendants of " + 
-                        folder.getFileId() + " in database", e);
-
-            } catch (RemoteException e) {
-                Log_OC.e(TAG, "Fail to update desendants of " + 
-                        folder.getFileId() + " in database", e);
-            }
-
-        }
-    }
-
-    
     public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) {
 
         if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
             
             OCFile targetParent = getFileByPath(targetParentPath);
             if (targetParent == null) {
-                // TODO panic
+                throw new IllegalStateException("Parent folder of the target path does not exist!!");
             }
             
             /// 1. get all the descendants of the moved element in a single QUERY
@@ -718,6 +640,8 @@ public class FileDataStorageManager {
             ArrayList<ContentProviderOperation> operations = 
                     new ArrayList<ContentProviderOperation>(c.getCount());
             String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
+            List<String> originalPathsToTriggerMediaScan = new ArrayList<String>();
+            List<String> newPathsToTriggerMediaScan = new ArrayList<String>();
             if (c.moveToFirst()) {
                 int lengthOfOldPath = file.getRemotePath().length();
                 int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
@@ -731,11 +655,14 @@ public class FileDataStorageManager {
                     if (child.getStoragePath() != null && 
                             child.getStoragePath().startsWith(defaultSavePath)) {
                         // update link to downloaded content - but local move is not done here!
-                        cv.put(
-                            ProviderTableMeta.FILE_STORAGE_PATH, 
-                            defaultSavePath + targetPath + 
-                                child.getStoragePath().substring(lengthOfOldStoragePath)
-                        );
+                        String targetLocalPath = defaultSavePath + targetPath + 
+                                child.getStoragePath().substring(lengthOfOldStoragePath);
+                        
+                        cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath);
+                        
+                        originalPathsToTriggerMediaScan.add(child.getStoragePath());
+                        newPathsToTriggerMediaScan.add(targetLocalPath);
+                        
                     }
                     if (child.getRemotePath().equals(file.getRemotePath())) {
                         cv.put(
@@ -766,27 +693,35 @@ public class FileDataStorageManager {
                 }
 
             } catch (Exception e) {
-                Log_OC.e(
-                    TAG, 
-                    "Fail to update " + file.getFileId() + " and descendants in database", 
-                    e
-                );
+                Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e);
             }
 
             /// 4. move in local file system 
-            String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
-            File localFile = new File(localPath);
+            String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
+            String targetLocalPath = defaultSavePath + targetPath;
+            File localFile = new File(originalLocalPath);
             boolean renamed = false;
             if (localFile.exists()) {
-                File targetFile = new File(defaultSavePath + targetPath);
+                File targetFile = new File(targetLocalPath);
                 File targetFolder = targetFile.getParentFile();
                 if (!targetFolder.exists()) {
                     targetFolder.mkdirs();
                 }
                 renamed = localFile.renameTo(targetFile);
             }
-            Log_OC.d(TAG, "Local file RENAMED : " + renamed);
-            
+
+            if (renamed) {
+                Iterator<String> it = originalPathsToTriggerMediaScan.iterator();
+                while (it.hasNext()) {
+                    // Notify MediaScanner about removed file
+                    triggerMediaScan(it.next());
+                }
+                it = newPathsToTriggerMediaScan.iterator();
+                while (it.hasNext()) {
+                    // Notify MediaScanner about new file/folder
+                    triggerMediaScan(it.next());
+                }
+            }
         }
         
     }
@@ -1549,4 +1484,10 @@ public class FileDataStorageManager {
         //}
     }
 
+    public void triggerMediaScan(String path) {
+        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
+        intent.setData(Uri.fromFile(new File(path)));
+        MainApp.getAppContext().sendBroadcast(intent);
+    }
+
 }

+ 0 - 1
src/com/owncloud/android/datamodel/OCFile.java

@@ -27,7 +27,6 @@ import com.owncloud.android.lib.common.utils.Log_OC;
 import java.io.File;
 
 import third_parties.daveKoeller.AlphanumComparator;
-
 public class OCFile implements Parcelable, Comparable<OCFile> {
 
     public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {

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

@@ -400,6 +400,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
         file.setFileLength((new File(mCurrentDownload.getSavePath()).length()));
         file.setRemoteId(mCurrentDownload.getFile().getRemoteId());
         mStorageManager.saveFile(file);
+        mStorageManager.triggerMediaScan(file.getStoragePath());
     }
 
 

+ 0 - 1
src/com/owncloud/android/operations/DetectAuthenticationMethodOperation.java

@@ -36,7 +36,6 @@ import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
 
 import android.content.Context;
 import android.net.Uri;
-import android.util.Log;
 
 /**
  * Operation to find out what authentication method requires

+ 0 - 2
src/com/owncloud/android/operations/DownloadFileOperation.java

@@ -150,13 +150,11 @@ public class DownloadFileOperation extends RemoteOperation {
             newFile = new File(getSavePath());
             newFile.getParentFile().mkdirs();
             moved = tmpFile.renameTo(newFile);
-        
             if (!moved)
                 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
         }
         Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " + result.getLogMessage());
         
-        
         return result;
     }
 

+ 12 - 19
src/com/owncloud/android/operations/RenameFileOperation.java

@@ -29,8 +29,6 @@ import com.owncloud.android.lib.resources.files.RenameRemoteFileOperation;
 import com.owncloud.android.operations.common.SyncOperation;
 import com.owncloud.android.utils.FileStorageUtils;
 
-import android.accounts.Account;
-
 
 /**
  * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
@@ -44,7 +42,6 @@ public class RenameFileOperation extends SyncOperation {
     
     private OCFile mFile;
     private String mRemotePath;
-    private Account mAccount;
     private String mNewName;
     private String mNewRemotePath;
 
@@ -57,9 +54,8 @@ public class RenameFileOperation extends SyncOperation {
      * @param account               OwnCloud account containing the remote file 
      * @param newName               New name to set as the name of file.
      */
-    public RenameFileOperation(String remotePath, Account account, String newName) {
+    public RenameFileOperation(String remotePath, String newName) {
         mRemotePath = remotePath;
-        mAccount = account;
         mNewName = newName;
         mNewRemotePath = null;
     }
@@ -103,7 +99,8 @@ public class RenameFileOperation extends SyncOperation {
 
             if (result.isSuccess()) {
                 if (mFile.isFolder()) {
-                    saveLocalDirectory();
+                    getStorageManager().moveLocalFile(mFile, mNewRemotePath, parent);
+                    //saveLocalDirectory();
 
                 } else {
                     saveLocalFile();
@@ -118,28 +115,24 @@ public class RenameFileOperation extends SyncOperation {
         return result;
     }
 
-    
-    private void saveLocalDirectory() {
-        getStorageManager().moveFolder(mFile, mNewRemotePath);
-        String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
-        File localDir = new File(localPath);
-        if (localDir.exists()) {
-            localDir.renameTo(new File(FileStorageUtils.getSavePath(mAccount.name) + mNewRemotePath));
-            // TODO - if renameTo fails, children files that are already down will result unlinked
-        }
-    }
-
     private void saveLocalFile() {
         mFile.setFileName(mNewName);
         
         // try to rename the local copy of the file
         if (mFile.isDown()) {
-            File f = new File(mFile.getStoragePath());
+            String oldPath = mFile.getStoragePath();
+            File f = new File(oldPath);
             String parentStoragePath = f.getParent();
             if (!parentStoragePath.endsWith(File.separator))
                 parentStoragePath += File.separator;
             if (f.renameTo(new File(parentStoragePath + mNewName))) {
-                mFile.setStoragePath(parentStoragePath + mNewName);
+                String newPath = parentStoragePath + mNewName;
+                mFile.setStoragePath(newPath);
+
+                // notify MediaScanner about removed file - TODO really works?
+                getStorageManager().triggerMediaScan(oldPath);
+                // notify to scan about new file
+                getStorageManager().triggerMediaScan(newPath);
             }
             // else - NOTHING: the link to the local file is kept although the local name can't be updated
             // TODO - study conditions when this could be a problem

+ 0 - 1
src/com/owncloud/android/providers/FileContentProvider.java

@@ -22,7 +22,6 @@ import java.util.ArrayList;
 import java.util.HashMap;
 
 import com.owncloud.android.R;
-import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.db.ProviderMeta;
 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
 import com.owncloud.android.lib.common.utils.Log_OC;

+ 1 - 1
src/com/owncloud/android/services/OperationsService.java

@@ -359,7 +359,7 @@ public class OperationsService extends Service {
                         // Rename file or folder
                         String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                         String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
-                        operation = new RenameFileOperation(remotePath, account, newName);
+                        operation = new RenameFileOperation(remotePath, newName);
                         
                     } else if (action.equals(ACTION_REMOVE)) {
                         // Remove file or folder

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

@@ -47,7 +47,6 @@ import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager.AsyncDrawable;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
-import com.owncloud.android.lib.common.utils.Log_OC;
 import com.owncloud.android.ui.activity.ComponentsGetter;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.FileStorageUtils;

+ 5 - 6
src/com/owncloud/android/ui/dialog/RemoveFileDialogFragment.java

@@ -26,15 +26,15 @@ package com.owncloud.android.ui.dialog;
  */
 import java.util.Vector;
 
+import android.app.Dialog;
+import android.os.Bundle;
+
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.ui.activity.ComponentsGetter;
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
 
-import android.app.Dialog;
-import android.os.Bundle;
-
 public class RemoveFileDialogFragment extends ConfirmationDialogFragment 
 implements ConfirmationDialogFragmentListener {
 
@@ -105,8 +105,7 @@ implements ConfirmationDialogFragmentListener {
     @Override
     public void onNeutral(String callerTag) {
         ComponentsGetter cg = (ComponentsGetter)getSherlockActivity();
-        cg.getFileOperationsHelper()
-            .removeFile(mTargetFile, true);
+        cg.getFileOperationsHelper().removeFile(mTargetFile, true);
         
         FileDataStorageManager storageManager = cg.getStorageManager();
         
@@ -141,4 +140,4 @@ implements ConfirmationDialogFragmentListener {
         // nothing to do here
     }
     
-}
+}

+ 8 - 12
src/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java

@@ -22,12 +22,6 @@ package com.owncloud.android.ui.dialog;
  * 
  *  Triggers the rename operation. 
  */
-import com.actionbarsherlock.app.SherlockDialogFragment;
-import com.owncloud.android.R;
-import com.owncloud.android.datamodel.OCFile;
-import com.owncloud.android.lib.resources.files.FileUtils;
-import com.owncloud.android.ui.activity.ComponentsGetter;
-
 import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.DialogInterface;
@@ -39,6 +33,12 @@ import android.widget.EditText;
 import android.widget.TextView;
 import android.widget.Toast;
 
+import com.actionbarsherlock.app.SherlockDialogFragment;
+import com.owncloud.android.R;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.lib.resources.files.FileUtils;
+import com.owncloud.android.ui.activity.ComponentsGetter;
+
 
 /**
  *  Dialog to input a new name for a file or folder to rename.  
@@ -125,12 +125,8 @@ extends SherlockDialogFragment implements DialogInterface.OnClickListener {
                         Toast.LENGTH_LONG).show();
                 return;
             }
-            
-            ((ComponentsGetter)getSherlockActivity()).
-                getFileOperationsHelper().renameFile(mTargetFile, newFileName);
-            
-            
+
+            ((ComponentsGetter)getSherlockActivity()).getFileOperationsHelper().renameFile(mTargetFile, newFileName);
         }
     }
-    
 }

+ 1 - 1
src/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@ -328,7 +328,7 @@ public class PreviewImageFragment extends FileFragment {
         finish();
     }
     
-    
+
     private class BitmapLoader extends AsyncTask<String, Void, Bitmap> {
 
         /**

+ 0 - 1
src/com/owncloud/android/utils/DisplayUtils.java

@@ -29,7 +29,6 @@ import java.util.Set;
 import android.annotation.TargetApi;
 import android.content.Context;
 import android.os.Build;
-import android.text.format.DateFormat;
 import android.text.format.DateUtils;
 
 import com.owncloud.android.MainApp;