Use hash ok keys as actual key in thumbnails cache instead of custom transformed key
[pub/Android/ownCloud.git] / src / com / owncloud / android / providers / FileContentProvider.java
index 8bf258f..36bade5 100644 (file)
@@ -22,10 +22,11 @@ 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.utils.Log_OC;
+import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.lib.resources.shares.ShareType;
+import com.owncloud.android.ui.adapter.DiskLruImageCache;
 
 
 
@@ -42,6 +43,7 @@ import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.database.sqlite.SQLiteQueryBuilder;
+import android.graphics.Bitmap.CompressFormat;
 import android.net.Uri;
 import android.text.TextUtils;
 
@@ -55,6 +57,12 @@ import android.text.TextUtils;
 public class FileContentProvider extends ContentProvider {
 
     private DataBaseHelper mDbHelper;
+    private static final int DISK_CACHE_SIZE = 1024 * 1024 * 10; // 10MB
+    private static final CompressFormat mCompressFormat = CompressFormat.JPEG;
+    private static final int mCompressQuality = 70;
+
+    private final Object thumbnailDiskCacheLock = new Object();
+    private DiskLruImageCache mThumbnailCache;
 
     // Projection for filelist table
     private static HashMap<String, String> mFileProjectionMap;
@@ -93,12 +101,20 @@ public class FileContentProvider extends ContentProvider {
                 ProviderTableMeta.FILE_SHARE_BY_LINK);
         mFileProjectionMap.put(ProviderTableMeta.FILE_PUBLIC_LINK,
                 ProviderTableMeta.FILE_PUBLIC_LINK);
+        mFileProjectionMap.put(ProviderTableMeta.FILE_PERMISSIONS,
+                ProviderTableMeta.FILE_PERMISSIONS);
+        mFileProjectionMap.put(ProviderTableMeta.FILE_REMOTE_ID,
+                ProviderTableMeta.FILE_REMOTE_ID);
+        mFileProjectionMap.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL,
+                ProviderTableMeta.FILE_UPDATE_THUMBNAIL);
     }
 
     private static final int SINGLE_FILE = 1;
     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;
@@ -157,13 +173,14 @@ public class FileContentProvider extends ContentProvider {
         int count = 0;
         switch (mUriMatcher.match(uri)) {
         case SINGLE_FILE:
-            /*Cursor c = query(db, uri, null, where, whereArgs, null);
-            String remotePath = "(unexisting)";
+            Cursor c = query(db, uri, null, where, whereArgs, null);
+            String remoteId = "";
             if (c != null && c.moveToFirst()) {
-                remotePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
+                remoteId = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID));
+                removeFileFromCache(remoteId);
             }
-            Log_OC.d(TAG, "Removing FILE " + remotePath);
-            */
+            Log_OC.d(TAG, "Removing FILE " + remoteId);
+
             count = db.delete(ProviderTableMeta.FILE_TABLE_NAME,
                     ProviderTableMeta._ID
                             + "="
@@ -189,7 +206,7 @@ public class FileContentProvider extends ContentProvider {
             Cursor children = query(uri, null, null, null, null);
             if (children != null && children.moveToFirst())  {
                 long childId;
-                boolean isDir; 
+                boolean isDir;
                 //String remotePath; 
                 while (!children.isAfterLast()) {
                     childId = children.getLong(children.getColumnIndex(ProviderTableMeta._ID));
@@ -232,9 +249,30 @@ public class FileContentProvider extends ContentProvider {
         }
         return count;
     }
-    
 
-    // TODO: switch(uri)
+    /**
+     * Remove from cache the remoteId passed
+     * @param fileRemoteId: remote id of file passed
+     */
+    private void removeFileFromCache(String fileRemoteId){
+        Context context = getContext();
+        if (context != null) {
+            synchronized (thumbnailDiskCacheLock) {
+                try {
+                    mThumbnailCache = new DiskLruImageCache(context, "thumbnailCache", 
+                                        DISK_CACHE_SIZE, mCompressFormat, mCompressQuality);
+
+                    mThumbnailCache.removeKey(fileRemoteId);
+
+                } catch (Exception e) {
+                    Log_OC.d(TAG, "Thumbnail cache could not be opened ", e);
+                    mThumbnailCache = null;
+                }
+                thumbnailDiskCacheLock.notifyAll(); // Wake any waiting threads
+            }
+        }
+    }
+
     @Override
     public String getType(Uri uri) {
         switch (mUriMatcher.match(uri)) {
@@ -298,23 +336,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:
@@ -322,8 +360,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());
@@ -429,7 +479,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:
@@ -437,7 +487,7 @@ public class FileContentProvider extends ContentProvider {
         }
     }    
 
-    
+ /*   
     private int updateFolderSize(SQLiteDatabase db, String folderId) {
         int count = 0;
         String [] whereArgs = new String[] { folderId };
@@ -488,7 +538,7 @@ public class FileContentProvider extends ContentProvider {
         }
         return count;
     }
-
+*/
     
     @Override
     public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
@@ -540,7 +590,10 @@ public class FileContentProvider extends ContentProvider {
                     + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " INTEGER, "
                     + ProviderTableMeta.FILE_ETAG + " TEXT, " 
                     + ProviderTableMeta.FILE_SHARE_BY_LINK + " INTEGER, "
-                    + ProviderTableMeta.FILE_PUBLIC_LINK  + " TEXT );"
+                    + ProviderTableMeta.FILE_PUBLIC_LINK  + " TEXT, "
+                    + ProviderTableMeta.FILE_PERMISSIONS  + " TEXT null,"
+                    + ProviderTableMeta.FILE_REMOTE_ID  + " TEXT null,"
+                    + ProviderTableMeta.FILE_UPDATE_THUMBNAIL  + " INTEGER);" //boolean
                     );
             
             // Create table ocshares
@@ -629,7 +682,7 @@ public class FileContentProvider extends ContentProvider {
             }
             if (!upgraded)
                 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
-            
+
             if (oldVersion < 6 && newVersion >= 6) {
                 Log_OC.i("SQL", "Entering in the #5 ADD in onUpgrade");
                 db.beginTransaction();
@@ -641,7 +694,7 @@ public class FileContentProvider extends ContentProvider {
                     db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
                             " ADD COLUMN " + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
                             " DEFAULT NULL");
-                    
+
                     // Create table ocshares
                     db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
                             + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
@@ -659,6 +712,27 @@ public class FileContentProvider extends ContentProvider {
                             + ProviderTableMeta.OCSHARES_USER_ID + " INTEGER, "
                             + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER," 
                             + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );" );
+
+                    upgraded = true;
+                    db.setTransactionSuccessful();
+                } finally {
+                    db.endTransaction();
+                }
+            }
+            if (!upgraded)
+                Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
+
+            if (oldVersion < 7 && newVersion >= 7) {
+                Log_OC.i("SQL", "Entering in the #7 ADD in onUpgrade");
+                db.beginTransaction();
+                try {
+                    db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+                            " ADD COLUMN " + ProviderTableMeta.FILE_PERMISSIONS + " TEXT " +
+                            " DEFAULT NULL");
+
+                    db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+                            " ADD COLUMN " + ProviderTableMeta.FILE_REMOTE_ID + " TEXT " +
+                            " DEFAULT NULL");
                     
                     upgraded = true;
                     db.setTransactionSuccessful();
@@ -668,6 +742,23 @@ public class FileContentProvider extends ContentProvider {
             }
             if (!upgraded)
                 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
+
+            if (oldVersion < 8 && newVersion >= 8) {
+                Log_OC.i("SQL", "Entering in the #8 ADD in onUpgrade");
+                db.beginTransaction();
+                try {
+                    db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
+                            " ADD COLUMN " + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER " +
+                            " DEFAULT 0");
+
+                    upgraded = true;
+                    db.setTransactionSuccessful();
+                } finally {
+                    db.endTransaction();
+                }
+            }
+            if (!upgraded)
+                Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
         }
     }