mirror of
https://github.com/nextcloud/android.git
synced 2024-11-27 09:39:25 +03:00
Downloads through temporal file and better OCFile.isDownloading() and .isDown() implementation
This commit is contained in:
parent
8ba2ca7b85
commit
6f189bffe1
4 changed files with 42 additions and 22 deletions
|
@ -18,7 +18,7 @@
|
|||
-->
|
||||
<manifest package="eu.alefzero.owncloud"
|
||||
android:versionCode="1"
|
||||
android:versionName="0.1.174B" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
android:versionName="0.1.175B" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
|
||||
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
|
||||
|
|
|
@ -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<OCFile> {
|
|||
/**
|
||||
* 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<OCFile> {
|
|||
/**
|
||||
* 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;
|
||||
}
|
||||
|
|
|
@ -61,6 +61,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
|
|||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
super.onCreate();
|
||||
|
@ -128,15 +138,21 @@ 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 "
|
||||
|
@ -144,13 +160,18 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
|
|||
new String[] {
|
||||
mFilePath.substring(mFilePath.lastIndexOf('/') + 1),
|
||||
mAccount.name });
|
||||
download_result = true;
|
||||
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);
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue