diff --git a/src/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/com/owncloud/android/datamodel/FileDataStorageManager.java index d803064297..1f626d36bd 100644 --- a/src/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -193,6 +193,7 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions()); cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId()); cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail()); + cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading()); boolean sameRemotePath = fileExists(file.getRemotePath()); if (sameRemotePath || @@ -302,6 +303,7 @@ public class FileDataStorageManager { cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions()); cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId()); cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail()); + cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading()); boolean existsByPath = fileExists(file.getRemotePath()); if (existsByPath || fileExists(file.getFileId())) { @@ -877,6 +879,8 @@ public class FileDataStorageManager { file.setRemoteId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID))); file.setNeedsUpdateThumbnail(c.getInt( c.getColumnIndex(ProviderTableMeta.FILE_UPDATE_THUMBNAIL)) == 1 ? true : false); + file.setDownloading(c.getInt( + c.getColumnIndex(ProviderTableMeta.FILE_IS_DOWNLOADING)) == 1 ? true : false); } return file; @@ -1259,6 +1263,10 @@ public class FileDataStorageManager { ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail() ? 1 : 0 ); + cv.put( + ProviderTableMeta.FILE_IS_DOWNLOADING, + file.isDownloading() ? 1 : 0 + ); boolean existsByPath = fileExists(file.getRemotePath()); if (existsByPath || fileExists(file.getFileId())) { diff --git a/src/com/owncloud/android/datamodel/OCFile.java b/src/com/owncloud/android/datamodel/OCFile.java index 2ba0415aa9..e6fd41412c 100644 --- a/src/com/owncloud/android/datamodel/OCFile.java +++ b/src/com/owncloud/android/datamodel/OCFile.java @@ -70,6 +70,8 @@ public class OCFile implements Parcelable, Comparable { private boolean mNeedsUpdateThumbnail; + private boolean mIsDownloading; + /** * Create new {@link OCFile} with given path. @@ -112,6 +114,7 @@ public class OCFile implements Parcelable, Comparable { mPermissions = source.readString(); mRemoteId = source.readString(); mNeedsUpdateThumbnail = source.readInt() == 0; + mIsDownloading = source.readInt() == 0; } @@ -136,6 +139,7 @@ public class OCFile implements Parcelable, Comparable { dest.writeString(mPermissions); dest.writeString(mRemoteId); dest.writeInt(mNeedsUpdateThumbnail ? 1 : 0); + dest.writeInt(mIsDownloading ? 1 : 0); } /** @@ -348,6 +352,7 @@ public class OCFile implements Parcelable, Comparable { mPermissions = null; mRemoteId = null; mNeedsUpdateThumbnail = false; + mIsDownloading = false; } /** @@ -562,14 +567,16 @@ public class OCFile implements Parcelable, Comparable { this.mRemoteId = remoteId; } + public boolean isDownloading() { + return mIsDownloading; + } + + public void setDownloading(boolean isDownloading) { + this.mIsDownloading = isDownloading; + } + public boolean isSynchronizing() { // TODO real implementation return false; } - - public boolean isDownloading() { - // TODO real implementation - return false; - } - } diff --git a/src/com/owncloud/android/db/ProviderMeta.java b/src/com/owncloud/android/db/ProviderMeta.java index bc59869a09..b6bfe4a857 100644 --- a/src/com/owncloud/android/db/ProviderMeta.java +++ b/src/com/owncloud/android/db/ProviderMeta.java @@ -31,7 +31,7 @@ import com.owncloud.android.MainApp; public class ProviderMeta { public static final String DB_NAME = "filelist"; - public static final int DB_VERSION = 8; + public static final int DB_VERSION = 9; private ProviderMeta() { } @@ -71,6 +71,7 @@ public class ProviderMeta { public static final String FILE_PERMISSIONS = "permissions"; public static final String FILE_REMOTE_ID = "remote_id"; public static final String FILE_UPDATE_THUMBNAIL = "update_thumbnail"; + public static final String FILE_IS_DOWNLOADING= "is_downloading"; public static final String FILE_DEFAULT_SORT_ORDER = FILE_NAME + " collate nocase asc"; diff --git a/src/com/owncloud/android/files/services/FileDownloader.java b/src/com/owncloud/android/files/services/FileDownloader.java index ab9e3327ee..61483af8b6 100644 --- a/src/com/owncloud/android/files/services/FileDownloader.java +++ b/src/com/owncloud/android/files/services/FileDownloader.java @@ -169,6 +169,12 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis newDownload.addDatatransferProgressListener(this); newDownload.addDatatransferProgressListener((FileDownloaderBinder) mBinder); requestedDownloads.add(downloadKey); + + // Store file on db with state 'downloading' + FileDataStorageManager storageManager = new FileDataStorageManager(account, getContentResolver()); + file.setDownloading(true); + storageManager.saveFile(file); + sendBroadcastNewDownload(newDownload); } catch (IllegalArgumentException e) { @@ -377,6 +383,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis downloadResult = mCurrentDownload.execute(mDownloadClient); if (downloadResult.isSuccess()) { saveDownloadedFile(); + } else { + updateUnsuccessfulDownloadedFile(); } } catch (AccountsException e) { @@ -417,10 +425,20 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis file.setStoragePath(mCurrentDownload.getSavePath()); file.setFileLength((new File(mCurrentDownload.getSavePath()).length())); file.setRemoteId(mCurrentDownload.getFile().getRemoteId()); + file.setDownloading(false); mStorageManager.saveFile(file); mStorageManager.triggerMediaScan(file.getStoragePath()); } + /** + * Update the OC File after a unsuccessful download + */ + private void updateUnsuccessfulDownloadedFile() { + OCFile file = mStorageManager.getFileById(mCurrentDownload.getFile().getFileId()); + file.setDownloading(false); + mStorageManager.saveFile(file); + } + /** * Creates a status notification to show the download progress diff --git a/src/com/owncloud/android/providers/FileContentProvider.java b/src/com/owncloud/android/providers/FileContentProvider.java index 21a8e2c98e..4ea4812cab 100644 --- a/src/com/owncloud/android/providers/FileContentProvider.java +++ b/src/com/owncloud/android/providers/FileContentProvider.java @@ -97,6 +97,8 @@ public class FileContentProvider extends ContentProvider { ProviderTableMeta.FILE_REMOTE_ID); mFileProjectionMap.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ProviderTableMeta.FILE_UPDATE_THUMBNAIL); + mFileProjectionMap.put(ProviderTableMeta.FILE_IS_DOWNLOADING, + ProviderTableMeta.FILE_IS_DOWNLOADING); } private static final int SINGLE_FILE = 1; @@ -624,7 +626,8 @@ public class FileContentProvider extends ContentProvider { + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT, " + ProviderTableMeta.FILE_PERMISSIONS + " TEXT null," + ProviderTableMeta.FILE_REMOTE_ID + " TEXT null," - + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER);" //boolean + + ProviderTableMeta.FILE_UPDATE_THUMBNAIL + " INTEGER," //boolean + + ProviderTableMeta.FILE_IS_DOWNLOADING + " INTEGER);" //boolean ); // Create table ocshares @@ -795,7 +798,25 @@ public class FileContentProvider extends ContentProvider { } } if (!upgraded) - Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + + Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + + ", newVersion == " + newVersion); + + if (oldVersion < 9 && newVersion >= 9) { + Log_OC.i("SQL", "Entering in the #9 ADD in onUpgrade"); + db.beginTransaction(); + try { + db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME + + " ADD COLUMN " + ProviderTableMeta.FILE_IS_DOWNLOADING + " 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); } }