Revert commit 6cdd830a7be28b77688efc3cd62857c1e71a1342
[pub/Android/ownCloud.git] / src / com / owncloud / android / providers / FileContentProvider.java
index 7463d20..fc03807 100644 (file)
@@ -22,9 +22,10 @@ import java.util.ArrayList;
 import java.util.HashMap;
 
 import com.owncloud.android.R;
-import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.db.ProviderMeta;
 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
+import com.owncloud.android.lib.resources.files.FileUtils;
+import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.utils.Log_OC;
 
 
@@ -99,6 +100,8 @@ public class FileContentProvider extends ContentProvider {
     private static final int DIRECTORY = 2;
     private static final int ROOT_DIRECTORY = 3;
     private static final int SHARES = 4;
+
+    private static final String TAG = FileContentProvider.class.getSimpleName();
     
     // Projection for ocshares table
     private static HashMap<String, String> mOCSharesProjectionMap;
@@ -139,20 +142,57 @@ public class FileContentProvider extends ContentProvider {
     
     @Override
     public int delete(Uri uri, String where, String[] whereArgs) {
-        //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
+        Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
         int count = 0;
         SQLiteDatabase db = mDbHelper.getWritableDatabase();
         db.beginTransaction();
+        
+        // Get parentId to notify the change
+        long parentId = getParentId(uri);
+        
+        // Delete action
         try {
             count = delete(db, uri, where, whereArgs);
             db.setTransactionSuccessful();
         } finally {
             db.endTransaction();
         }
+        Log_OC.d(TAG, "Uri " + uri);
         getContext().getContentResolver().notifyChange(uri, null);
+        
+        // Notify the change to the parent folder
+        notifyChangeToParentUri(parentId);
         return count;
     }
     
+    private long getParentId(Uri uri) {
+        long parentId = -1;
+        
+        if (mUriMatcher.match(uri) == SINGLE_FILE || mUriMatcher.match(uri) == DIRECTORY) {
+            String fileId = uri.toString().substring(uri.toString().lastIndexOf(FileUtils.PATH_SEPARATOR) + 1);
+            Uri selectFileUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, fileId);
+            String[] fileProjection = new String[] { ProviderTableMeta.FILE_PARENT };
+            Cursor fileCursor = query(selectFileUri, fileProjection, null, null, null);
+            
+            if (fileCursor != null  && fileCursor.moveToFirst()) {
+                parentId = fileCursor.getLong(fileCursor.getColumnIndex(ProviderTableMeta.FILE_PARENT));
+            }
+            fileCursor.close();
+        }
+        Log_OC.d(TAG, "getParentId = " + parentId);
+        return parentId;
+    }
+    
+    private void notifyChangeToParentUri(long parentId) {
+        if (parentId != -1) {
+            Uri parentUri = Uri.withAppendedPath(
+                    ProviderTableMeta.CONTENT_URI_DIR, 
+                    String.valueOf(parentId));
+            Log_OC.d(TAG, "ParentUri " + parentUri);
+            getContext().getContentResolver().notifyChange(parentUri, null);
+        }
+    }
+    
     private int delete(SQLiteDatabase db, Uri uri, String where, String[] whereArgs) {
         int count = 0;
         switch (mUriMatcher.match(uri)) {
@@ -224,12 +264,7 @@ public class FileContentProvider extends ContentProvider {
             count = db.delete(ProviderTableMeta.FILE_TABLE_NAME, where, whereArgs);
             break;
         case SHARES:
-            count = db.delete(ProviderTableMeta.OCSHARES_TABLE_NAME,
-                    ProviderTableMeta._ID
-                            + "="
-                            + uri.getPathSegments().get(1)
-                            + (!TextUtils.isEmpty(where) ? " AND (" + where
-                                    + ")" : ""), whereArgs);
+            count = db.delete(ProviderTableMeta.OCSHARES_TABLE_NAME, where, whereArgs);
             break;
         default:
             //Log_OC.e(TAG, "Unknown uri " + uri);
@@ -239,7 +274,6 @@ public class FileContentProvider extends ContentProvider {
     }
     
 
-    // TODO: switch(uri)
     @Override
     public String getType(Uri uri) {
         switch (mUriMatcher.match(uri)) {
@@ -256,9 +290,12 @@ public class FileContentProvider extends ContentProvider {
     @Override
     public Uri insert(Uri uri, ContentValues values) {
         //Log_OC.d(TAG, "Inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
+        Log_OC.d(TAG, "Uri " + uri);
         Uri newUri = null;
         SQLiteDatabase db = mDbHelper.getWritableDatabase();
         db.beginTransaction();
+        
+        // Insert action
         try {
             newUri = insert(db, uri, values);
             db.setTransactionSuccessful();
@@ -266,6 +303,12 @@ public class FileContentProvider extends ContentProvider {
             db.endTransaction();
         }
         getContext().getContentResolver().notifyChange(newUri, null);
+        
+        // Get parentId to notify the change
+        long parentId = getParentId(newUri);
+        // Notify the change to the parent folder
+        notifyChangeToParentUri(parentId);
+        
         return newUri;
     }
     
@@ -303,23 +346,23 @@ public class FileContentProvider extends ContentProvider {
             String[] projectionShare = new String[] {ProviderTableMeta._ID, ProviderTableMeta.OCSHARES_PATH, ProviderTableMeta.OCSHARES_ACCOUNT_OWNER };
             String whereShare = ProviderTableMeta.OCSHARES_PATH + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
             String[] whereArgsShare = new String[] {path, accountNameShare};
+            Uri insertedShareUri = null;
             Cursor doubleCheckShare = query(db, uri, projectionShare, whereShare, whereArgsShare, null);
             if (doubleCheckShare == null || !doubleCheckShare.moveToFirst()) {    // ugly patch; serious refactorization is needed to reduce work in FileDataStorageManager and bring it to FileContentProvider 
                 long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values);
                 if (rowId >0) {
-                    Uri insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
-                    return insertedShareUri;
+                    insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
                 } else {
                     throw new SQLException("ERROR " + uri);
 
                 }
             } else {
                 // file is already inserted; race condition, let's avoid a duplicated entry
-                Uri insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, doubleCheckShare.getLong(doubleCheckShare.getColumnIndex(ProviderTableMeta._ID)));
+                insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, doubleCheckShare.getLong(doubleCheckShare.getColumnIndex(ProviderTableMeta._ID)));
                 doubleCheckShare.close();
-
-                return insertedFileUri;
             }
+            updateFilesTableAccordingToShareInsertion(db, uri, values);
+            return insertedShareUri;
             
 
         default:
@@ -327,8 +370,20 @@ public class FileContentProvider extends ContentProvider {
         }
         
     }
-
     
+    private void updateFilesTableAccordingToShareInsertion(SQLiteDatabase db, Uri uri, ContentValues shareValues) {
+        ContentValues fileValues = new ContentValues();
+        fileValues.put(ProviderTableMeta.FILE_SHARE_BY_LINK, 
+                            ShareType.PUBLIC_LINK.getValue() == shareValues.getAsInteger(ProviderTableMeta.OCSHARES_SHARE_TYPE)? 1 : 0);
+        String whereShare = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
+        String[] whereArgsShare = new String[] {
+                shareValues.getAsString(ProviderTableMeta.OCSHARES_PATH), 
+                shareValues.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER)
+        };
+        db.update(ProviderTableMeta.FILE_TABLE_NAME, fileValues, whereShare, whereArgsShare);
+    }
+    
+
     @Override
     public boolean onCreate() {
         mDbHelper = new DataBaseHelper(getContext());
@@ -408,6 +463,7 @@ public class FileContentProvider extends ContentProvider {
         // DB case_sensitive
         db.execSQL("PRAGMA case_sensitive_like = true");
         Cursor c = sqlQuery.query(db, projection, selection, selectionArgs, null, null, order);
+        Log_OC.d(TAG, "setting notification URI: " + uri);
         c.setNotificationUri(getContext().getContentResolver(), uri);
         return c;
     }
@@ -415,7 +471,8 @@ public class FileContentProvider extends ContentProvider {
     @Override
     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
         
-        //Log_OC.d(TAG, "Updating " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
+        Log_OC.d(TAG, "Updating " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
+        Log_OC.d(TAG, "Uri " + uri);
         int count = 0;
         SQLiteDatabase db = mDbHelper.getWritableDatabase();
         db.beginTransaction();
@@ -426,6 +483,12 @@ public class FileContentProvider extends ContentProvider {
             db.endTransaction();
         }
         getContext().getContentResolver().notifyChange(uri, null);
+        
+        // Get parentId to notify the change
+        long parentId = getParentId(uri);
+        // Notify the change to the parent folder
+        notifyChangeToParentUri(parentId);
+        
         return count;
     }
     
@@ -434,7 +497,7 @@ public class FileContentProvider extends ContentProvider {
     private int update(SQLiteDatabase db, Uri uri, ContentValues values, String selection, String[] selectionArgs) {
         switch (mUriMatcher.match(uri)) {
             case DIRECTORY:
-                return updateFolderSize(db, selectionArgs[0]);
+                return  0; //updateFolderSize(db, selectionArgs[0]);
             case SHARES:
                 return db.update(ProviderTableMeta.OCSHARES_TABLE_NAME, values, selection, selectionArgs);
             default:
@@ -442,7 +505,7 @@ public class FileContentProvider extends ContentProvider {
         }
     }    
 
-    
+ /*   
     private int updateFolderSize(SQLiteDatabase db, String folderId) {
         int count = 0;
         String [] whereArgs = new String[] { folderId };
@@ -493,7 +556,7 @@ public class FileContentProvider extends ContentProvider {
         }
         return count;
     }
-
+*/
     
     @Override
     public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {