X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/35a384d05920c9422de033ab7edbc866cf244266..890bf7fa51135aa24e07ab8d02c002b22a66716a:/src/eu/alefzero/owncloud/files/services/FileUploader.java diff --git a/src/eu/alefzero/owncloud/files/services/FileUploader.java b/src/eu/alefzero/owncloud/files/services/FileUploader.java index a814fa8e..68ddafb7 100644 --- a/src/eu/alefzero/owncloud/files/services/FileUploader.java +++ b/src/eu/alefzero/owncloud/files/services/FileUploader.java @@ -2,19 +2,13 @@ package eu.alefzero.owncloud.files.services; import java.io.File; -import eu.alefzero.owncloud.AccountUtils; -import eu.alefzero.owncloud.R; -import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.owncloud.utils.OwnCloudVersion; -import eu.alefzero.webdav.OnUploadProgressListener; -import eu.alefzero.webdav.WebdavClient; import android.accounts.Account; import android.accounts.AccountManager; import android.app.Notification; import android.app.NotificationManager; +import android.app.PendingIntent; import android.app.Service; import android.content.Intent; -import android.net.Uri; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; @@ -25,9 +19,17 @@ import android.util.Log; import android.webkit.MimeTypeMap; import android.widget.RemoteViews; import android.widget.Toast; +import eu.alefzero.owncloud.R; +import eu.alefzero.owncloud.datamodel.FileDataStorageManager; +import eu.alefzero.owncloud.datamodel.OCFile; +import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener; +import eu.alefzero.webdav.WebdavClient; -public class FileUploader extends Service implements OnUploadProgressListener { +public class FileUploader extends Service implements OnDatatransferProgressListener { + public static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH"; + public static final String EXTRA_PARENT_DIR_ID = "PARENT_DIR_ID"; + public static final String KEY_LOCAL_FILE = "LOCAL_FILE"; public static final String KEY_REMOTE_FILE = "REMOTE_FILE"; public static final String KEY_ACCOUNT = "ACCOUNT"; @@ -40,14 +42,13 @@ public class FileUploader extends Service implements OnUploadProgressListener { private NotificationManager mNotificationManager; private Looper mServiceLooper; private ServiceHandler mServiceHandler; - private AccountManager mAccountManager; private Account mAccount; private String[] mLocalPaths, mRemotePaths; - private boolean mResult; private int mUploadType; private Notification mNotification; private int mTotalDataToSend, mSendData; - private int mCurrentIndexUpload; + private int mCurrentIndexUpload, mPreviousPercent; + private int mSuccessCounter; @Override public IBinder onBind(Intent arg0) { @@ -75,7 +76,6 @@ public class FileUploader extends Service implements OnUploadProgressListener { thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); - mAccountManager = AccountManager.get(this); } @Override @@ -115,26 +115,21 @@ public class FileUploader extends Service implements OnUploadProgressListener { } public void run() { - if (mResult) { - Toast.makeText(this, "Upload successfull", Toast.LENGTH_SHORT) - .show(); + String message; + if (mSuccessCounter == mLocalPaths.length) { + message = getString(R.string.uploader_upload_succeed); } else { - Toast.makeText(this, "No i kupa", Toast.LENGTH_SHORT).show(); + message = getString(R.string.uploader_upload_failed); + if (mLocalPaths.length > 1) + message += " (" + mSuccessCounter + " / " + mLocalPaths.length + getString(R.string.uploader_files_uploaded_suffix) + ")"; } + Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); } public void uploadFile() { - String baseUrl = mAccountManager.getUserData(mAccount, - AccountAuthenticator.KEY_OC_BASE_URL), ocVerStr = mAccountManager - .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION); - OwnCloudVersion ocVer = new OwnCloudVersion(ocVerStr); - String webdav_path = AccountUtils.getWebdavPath(ocVer); - Uri ocUri = Uri.parse(baseUrl + webdav_path); - String username = mAccount.name.substring(0, - mAccount.name.lastIndexOf('@')); - String password = mAccountManager.getPassword(mAccount); + FileDataStorageManager storageManager = new FileDataStorageManager(mAccount, getContentResolver()); - mTotalDataToSend = mSendData = 0; + mTotalDataToSend = mSendData = mPreviousPercent = 0; mNotification = new Notification( eu.alefzero.owncloud.R.drawable.icon, "Uploading...", @@ -143,13 +138,14 @@ public class FileUploader extends Service implements OnUploadProgressListener { mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout); mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, false); 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 an upload in the notification bar + mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); + mNotificationManager.notify(42, mNotification); - WebdavClient wc = new WebdavClient(ocUri); - wc.allowUnsignedCertificates(); - wc.setUploadListener(this); - wc.setCredentials(username, password); + WebdavClient wc = new WebdavClient(mAccount, getApplicationContext()); + wc.setDataTransferProgressListener(this); for (int i = 0; i < mLocalPaths.length; ++i) { File f = new File(mLocalPaths[i]); @@ -158,30 +154,55 @@ public class FileUploader extends Service implements OnUploadProgressListener { Log.d(TAG, "Will upload " + mTotalDataToSend + " bytes, with " + mLocalPaths.length + " files"); + mSuccessCounter = 0; + for (int i = 0; i < mLocalPaths.length; ++i) { - String mimeType = MimeTypeMap.getSingleton() - .getMimeTypeFromExtension( - mLocalPaths[i].substring(mLocalPaths[i] + + String mimeType; + try { + mimeType = MimeTypeMap.getSingleton() + .getMimeTypeFromExtension( + mLocalPaths[i].substring(mLocalPaths[i] .lastIndexOf('.') + 1)); - mResult = false; + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "Trying to find out MIME type of a file without extension: " + mLocalPaths[i]); + mimeType = "application/octet-stream"; + } + mCurrentIndexUpload = i; if (wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) { - mResult |= true; + mSuccessCounter++; + OCFile new_file = new OCFile(mRemotePaths[i]); + new_file.setMimetype(mimeType); + new_file.setFileLength(new File(mLocalPaths[i]).length()); + new_file.setModificationTimestamp(System.currentTimeMillis()); + new_file.setLastSyncDate(0); + new_file.setStoragePath(mLocalPaths[i]); + File f = new File(mRemotePaths[i]); + long parentDirId = storageManager.getFileByPath(f.getParent().endsWith("/")?f.getParent():f.getParent()+"/").getFileId(); + new_file.setParentId(parentDirId); + storageManager.saveFile(new_file); + + Intent end = new Intent(UPLOAD_FINISH_MESSAGE); + end.putExtra(EXTRA_PARENT_DIR_ID, parentDirId); + sendBroadcast(end); } + } - // notification.contentView.setProgressBar(R.id.status_progress, - // mLocalPaths.length-1, mLocalPaths.length-1, false); mNotificationManager.cancel(42); run(); } @Override - public void OnUploadProgress(long currentProgress) { - mSendData += currentProgress; - int percent = (int)(100*mSendData/mTotalDataToSend); - String text = String.format("%d%% Uploading %s file", percent, new File(mLocalPaths[mCurrentIndexUpload]).getName()); - mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, false); - mNotification.contentView.setTextViewText(R.id.status_text, text); - mNotificationManager.notify(42, mNotification); + public void transferProgress(long progressRate) { + mSendData += progressRate; + int percent = (int)(100*((double)mSendData)/((double)mTotalDataToSend)); + if (percent != mPreviousPercent) { + String text = String.format("%d%% Uploading %s file", percent, new File(mLocalPaths[mCurrentIndexUpload]).getName()); + mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, false); + mNotification.contentView.setTextViewText(R.id.status_text, text); + mNotificationManager.notify(42, mNotification); + } + mPreviousPercent = percent; } }