From: David A. Velasco Date: Mon, 23 Jul 2012 11:55:58 +0000 (+0200) Subject: Downloads through temporal file and better OCFile.isDownloading() and .isDown() imple... X-Git-Tag: oc-android-1.4.3~249 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/6f189bffe1aece71870676b3413c5ce7eb913308 Downloads through temporal file and better OCFile.isDownloading() and .isDown() implementation --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index c0082bfc..003f31f8 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -18,7 +18,7 @@ --> + android:versionName="0.1.175B" xmlns:android="http://schemas.android.com/apk/res/android"> diff --git a/src/eu/alefzero/owncloud/datamodel/OCFile.java b/src/eu/alefzero/owncloud/datamodel/OCFile.java index 32d36de3..7284604c 100644 --- a/src/eu/alefzero/owncloud/datamodel/OCFile.java +++ b/src/eu/alefzero/owncloud/datamodel/OCFile.java @@ -22,6 +22,8 @@ import java.io.File; import java.net.MalformedURLException; import java.net.URL; +import eu.alefzero.owncloud.files.services.FileDownloader; + import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; @@ -144,14 +146,12 @@ public class OCFile implements Parcelable, Comparable { /** * Use this to check if this file is available locally * - * TODO use a better condition not dependent upon mLenght being synchronized; to change when downloads are done through a temporal file - * * @return true if it is */ public boolean isDown() { if (mLocalPath != null && mLocalPath.length() > 0) { File file = new File(mLocalPath); - return (file.exists() && file.length() == mLength); + return (file.exists()); } return false; } @@ -159,14 +159,13 @@ public class OCFile implements Parcelable, Comparable { /** * Use this to check if this file is downloading * - * TODO use a better condition not dependent upon mLenght being synchronized; to change when downloads are done through a temporal file - * * @return true if it is in a download in progress */ public boolean isDownloading() { if (mLocalPath != null && mLocalPath.length() > 0) { - File file = new File(mLocalPath); - return (file.exists() && file.length() < mLength); + String savePath = FileDownloader.getSavePath(); + File file = new File(FileDownloader.getTemporalPath() + mLocalPath.substring(savePath.length())); + return (file.exists()); } return false; } diff --git a/src/eu/alefzero/owncloud/files/services/FileDownloader.java b/src/eu/alefzero/owncloud/files/services/FileDownloader.java index d08e7279..cb5d0c8d 100644 --- a/src/eu/alefzero/owncloud/files/services/FileDownloader.java +++ b/src/eu/alefzero/owncloud/files/services/FileDownloader.java @@ -60,6 +60,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis stopSelf(msg.arg1); } } + + public static final String getSavePath() { + File sdCard = Environment.getExternalStorageDirectory(); + return sdCard.getAbsolutePath() + "/owncloud/"; + } + + public static final String getTemporalPath() { + File sdCard = Environment.getExternalStorageDirectory(); + return sdCard.getAbsolutePath() + "/owncloud.tmp/"; + } @Override public void onCreate() { @@ -128,29 +138,40 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis mNotificationMngr.notify(1, mNotification); - File sdCard = Environment.getExternalStorageDirectory(); - File file = new File(sdCard.getAbsolutePath() + "/owncloud/" + mAccount.name + mFilePath); - file.getParentFile().mkdirs(); + // download in a temporal file + File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath); + tmpFile.getParentFile().mkdirs(); boolean download_result = false; - if (wdc.downloadFile(mRemotePath, file)) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); - getContentResolver().update( + File newFile = null; + if (wdc.downloadFile(mRemotePath, tmpFile)) { + newFile = new File(getSavePath() + mAccount.name + mFilePath); + newFile.getParentFile().mkdirs(); + boolean moved = tmpFile.renameTo(newFile); + + if (moved) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, newFile.getAbsolutePath()); + getContentResolver().update( ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta.FILE_NAME + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", new String[] { mFilePath.substring(mFilePath.lastIndexOf('/') + 1), - mAccount.name }); - download_result = true; + mAccount.name }); + download_result = true; + } + } + + if (!download_result) { + tmpFile.delete(); } mNotificationMngr.cancel(1); Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); end.putExtra(EXTRA_REMOTE_PATH, mRemotePath); - end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath()); + end.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath()); end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result); end.putExtra(ACCOUNT_NAME, mAccount.name); sendBroadcast(end); diff --git a/src/eu/alefzero/owncloud/ui/adapter/FileListListAdapter.java b/src/eu/alefzero/owncloud/ui/adapter/FileListListAdapter.java index 32cbb160..9c796038 100644 --- a/src/eu/alefzero/owncloud/ui/adapter/FileListListAdapter.java +++ b/src/eu/alefzero/owncloud/ui/adapter/FileListListAdapter.java @@ -111,12 +111,12 @@ public class FileListListAdapter implements ListAdapter { } ImageView downloaded = (ImageView) view.findViewById(R.id.imageView2); ImageView downloading = (ImageView) view.findViewById(R.id.imageView4); - if (file.isDown()) { - downloaded.setVisibility(View.VISIBLE); - downloading.setVisibility(View.INVISIBLE); - } else if (file.isDownloading()) { + if (file.isDownloading()) { downloaded.setVisibility(View.INVISIBLE); downloading.setVisibility(View.VISIBLE); + } else if (file.isDown()) { + downloaded.setVisibility(View.VISIBLE); + downloading.setVisibility(View.INVISIBLE); } else { downloaded.setVisibility(View.INVISIBLE); downloading.setVisibility(View.INVISIBLE);