X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/41f2cf116916d7fd8e2adbe3b513713fed02eefa..7f75f13f0c47b5cc4f5fe6a743a6fb8a9281fef7:/src/eu/alefzero/owncloud/files/services/FileDownloader.java diff --git a/src/eu/alefzero/owncloud/files/services/FileDownloader.java b/src/eu/alefzero/owncloud/files/services/FileDownloader.java index 734359ed..cb5d0c8d 100644 --- a/src/eu/alefzero/owncloud/files/services/FileDownloader.java +++ b/src/eu/alefzero/owncloud/files/services/FileDownloader.java @@ -11,7 +11,6 @@ import android.app.PendingIntent; import android.app.Service; import android.content.ContentValues; import android.content.Intent; -import android.net.Uri; import android.os.Environment; import android.os.Handler; import android.os.HandlerThread; @@ -21,23 +20,22 @@ import android.os.Message; import android.os.Process; import android.util.Log; import android.widget.RemoteViews; -import eu.alefzero.owncloud.AccountUtils; +import android.widget.Toast; import eu.alefzero.owncloud.R; -import eu.alefzero.owncloud.R.drawable; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener; -import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; -import eu.alefzero.owncloud.utils.OwnCloudVersion; +import eu.alefzero.owncloud.syncadapter.FileSyncService; import eu.alefzero.webdav.WebdavClient; public class FileDownloader extends Service implements OnDatatransferProgressListener { public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH"; - public static final String BAD_DOWNLOAD_MESSAGE = "BAD_DOWNLOAD"; + public static final String EXTRA_DOWNLOAD_RESULT = "RESULT"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; public static final String EXTRA_FILE_PATH = "FILE_PATH"; public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH"; public static final String EXTRA_FILE_SIZE = "FILE_SIZE"; + public static final String ACCOUNT_NAME = "ACCOUNT_NAME"; private static final String TAG = "FileDownloader"; private NotificationManager mNotificationMngr; @@ -62,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() { @@ -100,13 +108,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis void downloadFile() { AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE); - String oc_base_url = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL); - OwnCloudVersion ocv = new OwnCloudVersion(am - .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION)); - String webdav_path = AccountUtils.getWebdavPath(ocv); - Uri oc_url = Uri.parse(oc_base_url+webdav_path); - WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path)); + + WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext()); String username = mAccount.name.split("@")[0]; String password = ""; @@ -134,38 +138,50 @@ 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); - try { - file.getParentFile().mkdirs(); - file.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - - Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString()); - Log.e(TAG, mFilePath+""); - String message; - if (wdc.downloadFile(mRemotePath, file)) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); - getContentResolver().update( + // download in a temporal file + File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath); + tmpFile.getParentFile().mkdirs(); + + boolean download_result = false; + 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 }); - message = DOWNLOAD_FINISH_MESSAGE; - } else { - message = BAD_DOWNLOAD_MESSAGE; + mAccount.name }); + download_result = true; + } + } + + if (!download_result) { + tmpFile.delete(); } mNotificationMngr.cancel(1); - Intent end = new Intent(message); - end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath()); + Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); + end.putExtra(EXTRA_REMOTE_PATH, mRemotePath); + end.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath()); + end.putExtra(EXTRA_DOWNLOAD_RESULT, download_result); + end.putExtra(ACCOUNT_NAME, mAccount.name); sendBroadcast(end); + + if (download_result) { + Toast.makeText(this, R.string.downloader_download_succeed , Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(this, R.string.downloader_download_failed , Toast.LENGTH_SHORT).show(); + } + } @Override @@ -180,5 +196,6 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis mLastPercent = percent; } - + + }