X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/81970c8131abe99619d0705608ba91facb3441df..9ae0e1746ad60925dc18e19d2f63f5780ec75872:/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 3ee316fa..b0dd2b26 100644 --- a/src/eu/alefzero/owncloud/files/services/FileDownloader.java +++ b/src/eu/alefzero/owncloud/files/services/FileDownloader.java @@ -1,21 +1,19 @@ package eu.alefzero.owncloud.files.services; import java.io.File; -import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; import android.accounts.Account; import android.accounts.AccountManager; -import android.accounts.AuthenticatorException; -import android.accounts.OperationCanceledException; import android.app.Notification; import android.app.NotificationManager; 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; @@ -24,14 +22,11 @@ import android.os.Looper; import android.os.Message; import android.os.Process; import android.util.Log; -import android.util.LogPrinter; import android.widget.RemoteViews; -import android.widget.Toast; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener; -import eu.alefzero.owncloud.syncadapter.FileSyncService; import eu.alefzero.webdav.WebdavClient; public class FileDownloader extends Service implements OnDatatransferProgressListener { @@ -41,7 +36,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis 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"; + public static final String ACCOUNT_NAME = "ACCOUNT_NAME"; + private static final String TAG = "FileDownloader"; private NotificationManager mNotificationMngr; @@ -87,14 +83,16 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis } } - public static final String getSavePath() { + public static final String getSavePath(String accountName) { File sdCard = Environment.getExternalStorageDirectory(); - return sdCard.getAbsolutePath() + "/owncloud/"; + return sdCard.getAbsolutePath() + "/owncloud/" + Uri.encode(accountName, "@"); + // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B } - public static final String getTemporalPath() { + public static final String getTemporalPath(String accountName) { File sdCard = Environment.getExternalStorageDirectory(); - return sdCard.getAbsolutePath() + "/owncloud.tmp/"; + return sdCard.getAbsolutePath() + "/owncloud/tmp/" + Uri.encode(accountName, "@"); + // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B } @Override @@ -120,7 +118,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis !intent.hasExtra(EXTRA_REMOTE_PATH) ) { Log.e(TAG, "Not enough information provided in intent"); - return START_STICKY; + return START_NOT_STICKY; } mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT); mFilePath = intent.getStringExtra(EXTRA_FILE_PATH); @@ -135,7 +133,10 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis return START_NOT_STICKY; } - void downloadFile() { + /** + * Core download method: requests the file to download and stores it. + */ + private void downloadFile() { boolean downloadResult = false; /// prepare client object to send the request to the ownCloud server @@ -148,7 +149,6 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis AccountAuthenticator.AUTH_TOKEN_TYPE, true); } catch (Exception e) { Log.e(TAG, "Access to account credentials failed", e); - // TODO - check if that log prints the stack trace sendFinalBroadcast(downloadResult, null); return; } @@ -158,7 +158,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis /// download will be in a temporal file - File tmpFile = new File(getTemporalPath() + mAccount.name + mFilePath); + File tmpFile = new File(getTemporalPath(mAccount.name) + mFilePath); /// create status notification to show the download progress mNotification = new Notification(R.drawable.icon, getString(R.string.downloader_download_in_progress_ticker), System.currentTimeMillis()); @@ -167,8 +167,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, mTotalDownloadSize == -1); mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.downloader_download_in_progress_content), 0, tmpFile.getName())); mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon); - // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ; - // BUT an empty Intent is not a very elegant solution; something smart should happen when a user 'clicks' on a download in the notification bar + // TODO put something smart in the contentIntent below mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); mNotificationMngr.notify(R.string.downloader_download_in_progress_ticker, mNotification); @@ -177,36 +176,38 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis tmpFile.getParentFile().mkdirs(); mDownloadsInProgress.put(buildRemoteName(mAccount.name, mRemotePath), tmpFile.getAbsolutePath()); File newFile = null; - if (wdc.downloadFile(mRemotePath, tmpFile)) { - newFile = new File(getSavePath() + mAccount.name + mFilePath); - newFile.getParentFile().mkdirs(); - boolean moved = tmpFile.renameTo(newFile); + try { + 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 }); - downloadResult = true; + 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 }); + downloadResult = true; + } } + } finally { + mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath)); } - mDownloadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePath)); /// notify result mNotificationMngr.cancel(R.string.downloader_download_in_progress_ticker); - int tickerId = (downloadResult) ? R.string.downloader_download_succeed_ticker : R.string.downloader_download_failed_ticker; - int contentId = (downloadResult) ? R.string.downloader_download_succeed_content : R.string.downloader_download_failed_content; + int tickerId = (downloadResult) ? R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker; + int contentId = (downloadResult) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content; Notification finalNotification = new Notification(R.drawable.icon, getString(tickerId), System.currentTimeMillis()); finalNotification.flags |= Notification.FLAG_AUTO_CANCEL; - // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ; - // BUT an empty Intent is not a very elegant solution; something smart should happen when a user 'clicks' on a download in the notification bar + // TODO put something smart in the contentIntent below finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), tmpFile.getName()), finalNotification.contentIntent); mNotificationMngr.notify(tickerId, finalNotification); @@ -214,7 +215,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis sendFinalBroadcast(downloadResult, (downloadResult)?newFile.getAbsolutePath():null); } - + /** + * Callback method to update the progress bar in the status notification. + */ @Override public void transferProgress(long progressRate) { mCurrentDownloadSize += progressRate;