X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/164417ec61f1b96823e57a518b79caa611aa0f53..890bf7fa51135aa24e07ab8d02c002b22a66716a:/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 a5ee28f8..d6d7780a 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,20 +20,18 @@ import android.os.Message; import android.os.Process; import android.util.Log; import android.widget.RemoteViews; -import eu.alefzero.owncloud.AccountUtils; 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.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_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"; private static final String TAG = "FileDownloader"; @@ -43,6 +40,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis private ServiceHandler mServiceHandler; private Account mAccount; private String mFilePath; + private String mRemotePath; private int mLastPercent; private long mTotalDownloadSize; private long mCurrentDownlodSize; @@ -85,6 +83,7 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis } mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT); mFilePath = intent.getStringExtra(EXTRA_FILE_PATH); + mRemotePath = intent.getStringExtra(EXTRA_REMOTE_PATH); Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; mServiceHandler.sendMessage(msg); @@ -96,13 +95,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 = ""; @@ -124,20 +119,23 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout); mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, mTotalDownloadSize == -1); 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 + mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); 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+""); - if (wdc.downloadFile(mFilePath, file)) { + String message; + if (wdc.downloadFile(mRemotePath, file)) { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); getContentResolver().update( @@ -148,9 +146,14 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis new String[] { mFilePath.substring(mFilePath.lastIndexOf('/') + 1), mAccount.name }); + message = DOWNLOAD_FINISH_MESSAGE; + } else { + message = BAD_DOWNLOAD_MESSAGE; } + mNotificationMngr.cancel(1); - Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); + Intent end = new Intent(message); + end.putExtra(EXTRA_FILE_PATH, file.getAbsolutePath()); sendBroadcast(end); }