X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/a7385a7a5afb65fe65676a4807dbd0d148bff52e..d8de614d78c283d6d09849818c8043006e887256:/src/com/owncloud/android/datamodel/FileDataStorageManager.java diff --git a/src/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/com/owncloud/android/datamodel/FileDataStorageManager.java index 8b70d4a2..cd793b63 100644 --- a/src/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -41,27 +41,55 @@ import android.database.Cursor; import android.net.Uri; import android.os.RemoteException; -public class FileDataStorageManager implements DataStorageManager { +public class FileDataStorageManager { + + public static final int ROOT_PARENT_ID = 0; private ContentResolver mContentResolver; - private ContentProviderClient mContentProvider; + private ContentProviderClient mContentProviderClient; private Account mAccount; - private static String TAG = "FileDataStorageManager"; + private static String TAG = FileDataStorageManager.class.getSimpleName(); + public FileDataStorageManager(Account account, ContentResolver cr) { - mContentProvider = null; + mContentProviderClient = null; mContentResolver = cr; mAccount = account; } public FileDataStorageManager(Account account, ContentProviderClient cp) { - mContentProvider = cp; + mContentProviderClient = cp; mContentResolver = null; mAccount = account; } - @Override + + public void setAccount(Account account) { + mAccount = account; + } + + public Account getAccount() { + return mAccount; + } + + public void setContentResolver(ContentResolver cr) { + mContentResolver = cr; + } + + public ContentResolver getContentResolver() { + return mContentResolver; + } + + public void setContentProviderClient(ContentProviderClient cp) { + mContentProviderClient = cp; + } + + public ContentProviderClient getContentProviderClient() { + return mContentProviderClient; + } + + public OCFile getFileByPath(String path) { Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path); OCFile file = null; @@ -69,22 +97,13 @@ public class FileDataStorageManager implements DataStorageManager { file = createFileInstance(c); } c.close(); - if (file == null && OCFile.PATH_SEPARATOR.equals(path)) { + if (file == null && OCFile.ROOT_PATH.equals(path)) { return createRootDir(); // root should always exist } return file; } - private OCFile createRootDir() { - OCFile file = new OCFile(OCFile.PATH_SEPARATOR); - file.setMimetype("DIR"); - file.setParentId(DataStorageManager.ROOT_PARENT_ID); - saveFile(file); - return file; - } - - @Override public OCFile getFileById(long id) { Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id)); OCFile file = null; @@ -105,17 +124,42 @@ public class FileDataStorageManager implements DataStorageManager { return file; } - @Override public boolean fileExists(long id) { return fileExists(ProviderTableMeta._ID, String.valueOf(id)); } - @Override public boolean fileExists(String path) { return fileExists(ProviderTableMeta.FILE_PATH, path); } - @Override + + public Vector getFolderContent(OCFile f) { + if (f != null && f.isFolder() && f.getFileId() != -1) { + return getFolderContent(f.getFileId()); + + } else { + return new Vector(); + } + } + + + public Vector getFolderImages(OCFile folder) { + Vector ret = new Vector(); + if (folder != null) { + // TODO better implementation, filtering in the access to database (if possible) instead of here + Vector tmp = getFolderContent(folder); + OCFile current = null; + for (int i=0; i files) { Iterator filesIt = files.iterator(); @@ -207,19 +265,26 @@ public class FileDataStorageManager implements DataStorageManager { cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype()); cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); - if (file.getParentId() != 0) + if (file.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); - if (!file.isDirectory()) + if (!file.isFolder()) cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0); + cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag()); if (fileExists(file.getRemotePath())) { OCFile oldFile = getFileByPath(file.getRemotePath()); file.setFileId(oldFile.getFileId()); + + if (file.isFolder()) { + cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, oldFile.getFileLength()); + file.setFileLength(oldFile.getFileLength()); + } + operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). withValues(cv). withSelection( ProviderTableMeta._ID + "=?", @@ -230,9 +295,14 @@ public class FileDataStorageManager implements DataStorageManager { OCFile oldFile = getFileById(file.getFileId()); if (file.getStoragePath() == null && oldFile.getStoragePath() != null) file.setStoragePath(oldFile.getStoragePath()); - if (!file.isDirectory()); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); - + + if (!file.isFolder()) + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + else { + cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, oldFile.getFileLength()); + file.setFileLength(oldFile.getFileLength()); + } + operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). withValues(cv). withSelection( ProviderTableMeta._ID + "=?", @@ -251,7 +321,7 @@ public class FileDataStorageManager implements DataStorageManager { results = getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations); } else { - results = getContentProvider().applyBatch(operations); + results = getContentProviderClient().applyBatch(operations); } } catch (OperationApplicationException e) { @@ -274,178 +344,36 @@ public class FileDataStorageManager implements DataStorageManager { } for (OCFile aFile : files) { - if (aFile.isDirectory() && aFile.needsUpdatingWhileSaving()) - saveFiles(getDirectoryContent(aFile)); - } - - } - - public void setAccount(Account account) { - mAccount = account; - } - - public Account getAccount() { - return mAccount; - } - - public void setContentResolver(ContentResolver cr) { - mContentResolver = cr; - } - - public ContentResolver getContentResolver() { - return mContentResolver; - } - - public void setContentProvider(ContentProviderClient cp) { - mContentProvider = cp; - } - - public ContentProviderClient getContentProvider() { - return mContentProvider; - } - - @Override - public Vector getDirectoryContent(OCFile f) { - Vector ret = new Vector(); - if (f != null && f.isDirectory() && f.getFileId() != -1) { - - Uri req_uri = Uri.withAppendedPath( - ProviderTableMeta.CONTENT_URI_DIR, - String.valueOf(f.getFileId())); - Cursor c = null; - - if (getContentProvider() != null) { - try { - c = getContentProvider().query(req_uri, null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[] { mAccount.name }, null); - } catch (RemoteException e) { - Log_OC.e(TAG, e.getMessage()); - return ret; - } - } else { - c = getContentResolver().query(req_uri, null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[] { mAccount.name }, null); - } - - if (c.moveToFirst()) { - do { - OCFile child = createFileInstance(c); - ret.add(child); - } while (c.moveToNext()); - } - - c.close(); - - Collections.sort(ret); - + if (aFile.isFolder() && aFile.needsUpdatingWhileSaving()) + saveFiles(getFolderContent(aFile)); } - return ret; - } - private boolean fileExists(String cmp_key, String value) { - Cursor c; - if (getContentResolver() != null) { - c = getContentResolver() - .query(ProviderTableMeta.CONTENT_URI, - null, - cmp_key + "=? AND " - + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?", - new String[] { value, mAccount.name }, null); - } else { - try { - c = getContentProvider().query( - ProviderTableMeta.CONTENT_URI, - null, - cmp_key + "=? AND " - + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[] { value, mAccount.name }, null); - } catch (RemoteException e) { - Log_OC.e(TAG, - "Couldn't determine file existance, assuming non existance: " - + e.getMessage()); - return false; - } - } - boolean retval = c.moveToFirst(); - c.close(); - return retval; } - private Cursor getCursorForValue(String key, String value) { - Cursor c = null; - if (getContentResolver() != null) { - c = getContentResolver() - .query(ProviderTableMeta.CONTENT_URI, - null, - key + "=? AND " - + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?", - new String[] { value, mAccount.name }, null); - } else { - try { - c = getContentProvider().query( - ProviderTableMeta.CONTENT_URI, - null, - key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?", new String[] { value, mAccount.name }, - null); - } catch (RemoteException e) { - Log_OC.e(TAG, "Could not get file details: " + e.getMessage()); - c = null; - } - } - return c; - } - private OCFile createFileInstance(Cursor c) { - OCFile file = null; - if (c != null) { - file = new OCFile(c.getString(c - .getColumnIndex(ProviderTableMeta.FILE_PATH))); - file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID))); - file.setParentId(c.getLong(c - .getColumnIndex(ProviderTableMeta.FILE_PARENT))); - file.setMimetype(c.getString(c - .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))); - if (!file.isDirectory()) { - file.setStoragePath(c.getString(c - .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH))); - if (file.getStoragePath() == null) { - // try to find existing file and bind it with current account; - with the current update of SynchronizeFolderOperation, this won't be necessary anymore after a full synchronization of the account - File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)); - if (f.exists()) { - file.setStoragePath(f.getAbsolutePath()); - file.setLastSyncDateForData(f.lastModified()); - } - } - } - file.setFileLength(c.getLong(c - .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))); - file.setCreationTimestamp(c.getLong(c - .getColumnIndex(ProviderTableMeta.FILE_CREATION))); - file.setModificationTimestamp(c.getLong(c - .getColumnIndex(ProviderTableMeta.FILE_MODIFIED))); - file.setModificationTimestampAtLastSyncForData(c.getLong(c - .getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA))); - file.setLastSyncDateForProperties(c.getLong(c - .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE))); - file.setLastSyncDateForData(c.getLong(c. - getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA))); - file.setKeepInSync(c.getInt( - c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false); + /** + * Calculate and save the folderSize on DB + * @param id + */ + public void calculateFolderSize(long id) { + long folderSize = 0; + + Vector files = getFolderContent(id); + + for (OCFile f: files) + { + folderSize = folderSize + f.getFileLength(); } - return file; + + updateSize(id, folderSize); } - - @Override + + public void removeFile(OCFile file, boolean removeLocalCopy) { Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId()); - if (getContentProvider() != null) { + if (getContentProviderClient() != null) { try { - getContentProvider().delete(file_uri, + getContentProviderClient().delete(file_uri, ProviderTableMeta.FILE_ACCOUNT_OWNER+"=?", new String[]{mAccount.name}); } catch (RemoteException e) { @@ -459,27 +387,28 @@ public class FileDataStorageManager implements DataStorageManager { if (file.isDown() && removeLocalCopy) { new File(file.getStoragePath()).delete(); } - if (file.isDirectory() && removeLocalCopy) { + if (file.isFolder() && removeLocalCopy) { File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)); if (f.exists() && f.isDirectory() && (f.list() == null || f.list().length == 0)) { f.delete(); } } - updateSubtreeSize(file.getParentId()); + if (file.getFileLength() > 0) { + updateSizesToTheRoot(file.getParentId()); + } } - @Override - public void removeDirectory(OCFile dir, boolean removeDBData, boolean removeLocalContent) { + public void removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) { // TODO consider possible failures - if (dir != null && dir.isDirectory() && dir.getFileId() != -1) { - Vector children = getDirectoryContent(dir); + if (folder != null && folder.isFolder() && folder.getFileId() != -1) { + Vector children = getFolderContent(folder); if (children.size() > 0) { OCFile child = null; for (int i=0; i 0) { + updateSizesToTheRoot(folder.getParentId()); } } } @@ -504,32 +437,31 @@ public class FileDataStorageManager implements DataStorageManager { * TODO explore better (faster) implementations * TODO throw exceptions up ! */ - @Override - public void moveDirectory(OCFile dir, String newPath) { + public void moveFolder(OCFile folder, String newPath) { // TODO check newPath - if (dir != null && dir.isDirectory() && dir.fileExists() && !dir.getFileName().equals(OCFile.PATH_SEPARATOR)) { + 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 (getContentProvider() != null) { + if (getContentProviderClient() != null) { try { - c = getContentProvider().query(ProviderTableMeta.CONTENT_URI, + c = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI, null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ?", - new String[] { mAccount.name, dir.getRemotePath() + "%" }, 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, dir.getRemotePath() + "%" }, 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 operations = new ArrayList(c.getCount()); - int lengthOfOldPath = dir.getRemotePath().length(); + int lengthOfOldPath = folder.getRemotePath().length(); String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name); int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath; if (c.moveToFirst()) { @@ -555,38 +487,21 @@ public class FileDataStorageManager implements DataStorageManager { getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations); } else { - getContentProvider().applyBatch(operations); + getContentProviderClient().applyBatch(operations); } } catch (OperationApplicationException e) { - Log_OC.e(TAG, "Fail to update descendants of " + dir.getFileId() + " in database", 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 " + dir.getFileId() + " in database", e); + Log_OC.e(TAG, "Fail to update desendants of " + folder.getFileId() + " in database", e); } } } - @Override - public Vector getDirectoryImages(OCFile directory) { - Vector ret = new Vector(); - if (directory != null) { - // TODO better implementation, filtering in the access to database (if possible) instead of here - Vector tmp = getDirectoryContent(directory); - OCFile current = null; - for (int i=0; i getFilesbyParent(long parentId) { + + private Vector getFolderContent(long parentId) { Vector ret = new Vector(); @@ -595,9 +510,9 @@ public class FileDataStorageManager implements DataStorageManager { String.valueOf(parentId)); Cursor c = null; - if (getContentProvider() != null) { + if (getContentProviderClient() != null) { try { - c = getContentProvider().query(req_uri, null, + c = getContentProviderClient().query(req_uri, null, ProviderTableMeta.FILE_PARENT + "=?" , new String[] { String.valueOf(parentId)}, null); } catch (RemoteException e) { @@ -624,36 +539,131 @@ public class FileDataStorageManager implements DataStorageManager { return ret; } - /** - * Calculate and save the folderSize on DB - * @param id - */ - @Override - public void saveFolderSize(long id) { - long folderSize = 0; - - Vector files = getFilesbyParent(id); - - Log_OC.d(TAG, "Folder " + String.valueOf(id) + "--- Number of Files = " + String.valueOf(files.size())); - - for (OCFile f: files) - { - folderSize = folderSize + f.getFileLength(); - Log_OC.d(TAG, "Folder Size = " + String.valueOf(folderSize)); + + private OCFile createRootDir() { + OCFile file = new OCFile(OCFile.ROOT_PATH); + file.setMimetype("DIR"); + file.setParentId(FileDataStorageManager.ROOT_PARENT_ID); + saveFile(file); + return file; + } + + private boolean fileExists(String cmp_key, String value) { + Cursor c; + if (getContentResolver() != null) { + c = getContentResolver() + .query(ProviderTableMeta.CONTENT_URI, + null, + cmp_key + "=? AND " + + ProviderTableMeta.FILE_ACCOUNT_OWNER + + "=?", + new String[] { value, mAccount.name }, null); + } else { + try { + c = getContentProviderClient().query( + ProviderTableMeta.CONTENT_URI, + null, + cmp_key + "=? AND " + + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", + new String[] { value, mAccount.name }, null); + } catch (RemoteException e) { + Log_OC.e(TAG, + "Couldn't determine file existance, assuming non existance: " + + e.getMessage()); + return false; + } } - - updatefolderSize(id, folderSize); + boolean retval = c.moveToFirst(); + c.close(); + return retval; + } + + private Cursor getCursorForValue(String key, String value) { + Cursor c = null; + if (getContentResolver() != null) { + c = getContentResolver() + .query(ProviderTableMeta.CONTENT_URI, + null, + key + "=? AND " + + ProviderTableMeta.FILE_ACCOUNT_OWNER + + "=?", + new String[] { value, mAccount.name }, null); + } else { + try { + c = getContentProviderClient().query( + ProviderTableMeta.CONTENT_URI, + null, + key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + + "=?", new String[] { value, mAccount.name }, + null); + } catch (RemoteException e) { + Log_OC.e(TAG, "Could not get file details: " + e.getMessage()); + c = null; + } + } + return c; + } + + private OCFile createFileInstance(Cursor c) { + OCFile file = null; + if (c != null) { + file = new OCFile(c.getString(c + .getColumnIndex(ProviderTableMeta.FILE_PATH))); + file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID))); + file.setParentId(c.getLong(c + .getColumnIndex(ProviderTableMeta.FILE_PARENT))); + file.setMimetype(c.getString(c + .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))); + if (!file.isFolder()) { + file.setStoragePath(c.getString(c + .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH))); + if (file.getStoragePath() == null) { + // try to find existing file and bind it with current account; - with the current update of SynchronizeFolderOperation, this won't be necessary anymore after a full synchronization of the account + File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)); + if (f.exists()) { + file.setStoragePath(f.getAbsolutePath()); + file.setLastSyncDateForData(f.lastModified()); + } + } + } + file.setFileLength(c.getLong(c + .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))); + file.setCreationTimestamp(c.getLong(c + .getColumnIndex(ProviderTableMeta.FILE_CREATION))); + file.setModificationTimestamp(c.getLong(c + .getColumnIndex(ProviderTableMeta.FILE_MODIFIED))); + file.setModificationTimestampAtLastSyncForData(c.getLong(c + .getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA))); + file.setLastSyncDateForProperties(c.getLong(c + .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE))); + file.setLastSyncDateForData(c.getLong(c. + getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA))); + file.setKeepInSync(c.getInt( + c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false); + file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG))); + + } + return file; } /** - * Update the size value of a folder on DB + * Update the size value of an OCFile in DB */ - @Override - public int updatefolderSize(long id, long size) { + private int updateSize(long id, long size) { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, size); - int result = getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", - new String[] { String.valueOf(id) }); + int result = -1; + if (getContentResolver() != null) { + result = getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", + new String[] { String.valueOf(id) }); + } else { + try { + result = getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", + new String[] { String.valueOf(id) }); + } catch (RemoteException e) { + Log_OC.e(TAG,"Fail to update size column into database " + e.getMessage()); + } + } return result; } @@ -661,15 +671,14 @@ public class FileDataStorageManager implements DataStorageManager { * Update the size of a subtree of folder from a file to the root * @param parentId: parent of the file */ - private void updateSubtreeSize(long parentId) { + private void updateSizesToTheRoot(long parentId) { OCFile file; - while (parentId != 0) { + while (parentId != FileDataStorageManager.ROOT_PARENT_ID) { - Log_OC.d(TAG, "parent = " + parentId); // Update the size of the parent - saveFolderSize(parentId); + calculateFolderSize(parentId); // search the next parent file = getFileById(parentId);