Bug fixed (and some refactoring): crash on local removal of a folder
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / FileDataStorageManager.java
index b10e525..3f0d69f 100644 (file)
@@ -33,7 +33,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 +40,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 +346,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 +486,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 +548,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 +576,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
                 }
             }
         }
@@ -787,6 +797,14 @@ public class FileDataStorageManager {
             }
             Log_OC.d(TAG, "Local file RENAMED : " + renamed);
             
+            // Notify MediaScanner about removed file
+            triggerMediaScan(file.getStoragePath());
+            
+            // Notify MediaScanner about new file/folder
+            triggerMediaScan(defaultSavePath + targetPath);
+            
+            Log_OC.d(TAG, "uri old: " + file.getStoragePath());
+            Log_OC.d(TAG, "uri new: " + defaultSavePath + targetPath);
         }
         
     }
@@ -1549,4 +1567,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);
+    }
+
 }