package eu.alefzero.owncloud.files.services;\r
\r
import java.io.File;\r
-import java.io.IOException;\r
import java.util.Collections;\r
import java.util.HashMap;\r
import java.util.Map;\r
\r
import android.accounts.Account;\r
import android.accounts.AccountManager;\r
-import android.accounts.AuthenticatorException;\r
-import android.accounts.OperationCanceledException;\r
import android.app.Notification;\r
import android.app.NotificationManager;\r
import android.app.PendingIntent;\r
import android.os.Message;\r
import android.os.Process;\r
import android.util.Log;\r
-import android.util.LogPrinter;\r
import android.widget.RemoteViews;\r
-import android.widget.Toast;\r
import eu.alefzero.owncloud.R;\r
import eu.alefzero.owncloud.authenticator.AccountAuthenticator;\r
import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;\r
import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;\r
-import eu.alefzero.owncloud.syncadapter.FileSyncService;\r
import eu.alefzero.webdav.WebdavClient;\r
\r
public class FileDownloader extends Service implements OnDatatransferProgressListener {\r
!intent.hasExtra(EXTRA_REMOTE_PATH)\r
) {\r
Log.e(TAG, "Not enough information provided in intent");\r
- return START_STICKY;\r
+ return START_NOT_STICKY;\r
}\r
mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT);\r
mFilePath = intent.getStringExtra(EXTRA_FILE_PATH);\r
return START_NOT_STICKY;\r
}\r
\r
- void downloadFile() {\r
+ /**\r
+ * Core download method: requests the file to download and stores it.\r
+ */\r
+ private void downloadFile() {\r
boolean downloadResult = false;\r
\r
/// prepare client object to send the request to the ownCloud server\r
AccountAuthenticator.AUTH_TOKEN_TYPE, true);\r
} catch (Exception e) {\r
Log.e(TAG, "Access to account credentials failed", e);\r
- // TODO - check if that log prints the stack trace\r
sendFinalBroadcast(downloadResult, null);\r
return;\r
}\r
mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, mTotalDownloadSize == -1);\r
mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.downloader_download_in_progress_content), 0, tmpFile.getName()));\r
mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);\r
- // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ;\r
- // 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\r
+ // TODO put something smart in the contentIntent below\r
mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);\r
mNotificationMngr.notify(R.string.downloader_download_in_progress_ticker, mNotification);\r
\r
\r
/// notify result\r
mNotificationMngr.cancel(R.string.downloader_download_in_progress_ticker);\r
- int tickerId = (downloadResult) ? R.string.downloader_download_succeed_ticker : R.string.downloader_download_failed_ticker;\r
- int contentId = (downloadResult) ? R.string.downloader_download_succeed_content : R.string.downloader_download_failed_content;\r
+ int tickerId = (downloadResult) ? R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker;\r
+ int contentId = (downloadResult) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content;\r
Notification finalNotification = new Notification(R.drawable.icon, getString(tickerId), System.currentTimeMillis());\r
finalNotification.flags |= Notification.FLAG_AUTO_CANCEL;\r
- // dvelasco ; contentIntent MUST be assigned to avoid app crashes in versions previous to Android 4.x ;\r
- // 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\r
+ // TODO put something smart in the contentIntent below\r
finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);\r
finalNotification.setLatestEventInfo(getApplicationContext(), getString(tickerId), String.format(getString(contentId), tmpFile.getName()), finalNotification.contentIntent);\r
mNotificationMngr.notify(tickerId, finalNotification);\r
sendFinalBroadcast(downloadResult, (downloadResult)?newFile.getAbsolutePath():null);\r
}\r
\r
- \r
+ /**\r
+ * Callback method to update the progress bar in the status notification.\r
+ */\r
@Override\r
public void transferProgress(long progressRate) {\r
mCurrentDownloadSize += progressRate;\r
package eu.alefzero.owncloud.files.services;
import java.io.File;
-
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.HeadMethod;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
import android.accounts.Account;
-import android.accounts.AccountManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
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;
public static final int UPLOAD_MULTIPLE_FILES = 1;
private static final String TAG = "FileUploader";
+
private NotificationManager mNotificationManager;
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private String[] mLocalPaths, mRemotePaths;
private int mUploadType;
private Notification mNotification;
- private int mTotalDataToSend, mSendData;
+ private long mTotalDataToSend, mSendData;
private int mCurrentIndexUpload, mPreviousPercent;
private int mSuccessCounter;
+
+ /**
+ * Static map with the files being download and the path to the temporal file were are download
+ */
+ private static Map<String, String> mUploadsInProgress = Collections.synchronizedMap(new HashMap<String, String>());
+
+ /**
+ * Returns True when the file referred by 'remotePath' in the ownCloud account 'account' is downloading
+ */
+ public static boolean isUploading(Account account, String remotePath) {
+ return (mUploadsInProgress.get(buildRemoteName(account.name, remotePath)) != null);
+ }
+
+ /**
+ * Builds a key for mUplaodsInProgress from the accountName and remotePath
+ */
+ private static String buildRemoteName(String accountName, String remotePath) {
+ return accountName + remotePath;
+ }
+
+
+
@Override
public IBinder onBind(Intent arg0) {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!intent.hasExtra(KEY_ACCOUNT) && !intent.hasExtra(KEY_UPLOAD_TYPE)) {
- Log.e(TAG, "Not enought data in intent provided");
+ Log.e(TAG, "Not enough information provided in intent");
return Service.START_NOT_STICKY;
}
mAccount = intent.getParcelableExtra(KEY_ACCOUNT);
}
if (mLocalPaths.length != mRemotePaths.length) {
- Log.e(TAG, "Remote paths and local paths are not equal!");
+ Log.e(TAG, "Different number of remote paths and local paths!");
return Service.START_NOT_STICKY;
}
return Service.START_NOT_STICKY;
}
- public void run() {
- String message;
- if (mSuccessCounter == mLocalPaths.length) {
- message = getString(R.string.uploader_upload_succeed);
- } else {
- message = String.format(getString(R.string.uploader_upload_failed), mSuccessCounter, mLocalPaths.length);
- }
- Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
- }
-
+
+ /**
+ * Core upload method: sends the file(s) to upload
+ */
public void uploadFile() {
FileDataStorageManager storageManager = new FileDataStorageManager(mAccount, getContentResolver());
-
+
mTotalDataToSend = mSendData = mPreviousPercent = 0;
- mNotification = new Notification(
- eu.alefzero.owncloud.R.drawable.icon, "Uploading...",
- System.currentTimeMillis());
+ /// prepare client object to send the request to the ownCloud server
+ WebdavClient wc = new WebdavClient(mAccount, getApplicationContext());
+ wc.allowSelfsignedCertificates();
+ wc.setDataTransferProgressListener(this);
+
+ /// create status notification to show the upload progress
+ mNotification = new Notification(eu.alefzero.owncloud.R.drawable.icon, getString(R.string.uploader_upload_in_progress_ticker), System.currentTimeMillis());
mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
+ RemoteViews oldContentView = mNotification.contentView;
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(mAccount, getApplicationContext());
- wc.allowSelfsignedCertificates();
- wc.setDataTransferProgressListener(this);
+ mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotification);
+
+ /// perform the upload
+ File [] localFiles = new File[mLocalPaths.length];
for (int i = 0; i < mLocalPaths.length; ++i) {
- File f = new File(mLocalPaths[i]);
- mTotalDataToSend += f.length();
+ localFiles[i] = new File(mLocalPaths[i]);
+ mTotalDataToSend += localFiles[i].length();
}
-
Log.d(TAG, "Will upload " + mTotalDataToSend + " bytes, with " + mLocalPaths.length + " files");
-
mSuccessCounter = 0;
-
for (int i = 0; i < mLocalPaths.length; ++i) {
-
String mimeType = null;
try {
mimeType = MimeTypeMap.getSingleton()
}
if (mimeType == null)
mimeType = "application/octet-stream";
-
mCurrentIndexUpload = i;
mRemotePaths[i] = getAvailableRemotePath(wc, mRemotePaths[i]);
+ mUploadsInProgress.put(buildRemoteName(mAccount.name, mRemotePaths[i]), mLocalPaths[i]);
+ long parentDirId = -1;
if (mRemotePaths[i] != null && wc.putFile(mLocalPaths[i], mRemotePaths[i], mimeType)) {
mSuccessCounter++;
OCFile new_file = new OCFile(mRemotePaths[i]);
new_file.setMimetype(mimeType);
- new_file.setFileLength(new File(mLocalPaths[i]).length());
+ new_file.setFileLength(localFiles[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();
+ 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);
- end.putExtra(ACCOUNT_NAME, mAccount.name);
- sendBroadcast(end);
}
+ mUploadsInProgress.remove(buildRemoteName(mAccount.name, mRemotePaths[i]));
+
+ /// notify upload of EACH file to activities interested
+ Intent end = new Intent(UPLOAD_FINISH_MESSAGE);
+ end.putExtra(EXTRA_PARENT_DIR_ID, parentDirId);
+ end.putExtra(ACCOUNT_NAME, mAccount.name);
+ sendBroadcast(end);
+ }
+
+ /// notify final result
+ if (mSuccessCounter == mLocalPaths.length) { // success
+ //Notification finalNotification = new Notification(R.drawable.icon, getString(R.string.uploader_upload_succeeded_ticker), System.currentTimeMillis());
+ mNotification.flags ^= Notification.FLAG_ONGOING_EVENT; // remove the ongoing flag
+ mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
+ mNotification.contentView = oldContentView;
+ // TODO put something smart in the contentIntent below
+ mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
+ if (mLocalPaths.length == 1) {
+ mNotification.setLatestEventInfo( getApplicationContext(),
+ getString(R.string.uploader_upload_succeeded_ticker),
+ String.format(getString(R.string.uploader_upload_succeeded_content_single), localFiles[0].getName()),
+ mNotification.contentIntent);
+ } else {
+ mNotification.setLatestEventInfo( getApplicationContext(),
+ getString(R.string.uploader_upload_succeeded_ticker),
+ String.format(getString(R.string.uploader_upload_succeeded_content_multiple), mSuccessCounter),
+ mNotification.contentIntent);
+ }
+ mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotification); // NOT AN ERROR; uploader_upload_in_progress_ticker is the target, not a new notification
+ } else {
+ mNotificationManager.cancel(R.string.uploader_upload_in_progress_ticker);
+ Notification finalNotification = new Notification(R.drawable.icon, getString(R.string.uploader_upload_failed_ticker), System.currentTimeMillis());
+ finalNotification.flags |= Notification.FLAG_AUTO_CANCEL;
+ // TODO put something smart in the contentIntent below
+ finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
+ if (mLocalPaths.length == 1) {
+ finalNotification.setLatestEventInfo( getApplicationContext(),
+ getString(R.string.uploader_upload_failed_ticker),
+ String.format(getString(R.string.uploader_upload_failed_content_single), localFiles[0].getName()),
+ finalNotification.contentIntent);
+ } else {
+ finalNotification.setLatestEventInfo( getApplicationContext(),
+ getString(R.string.uploader_upload_failed_ticker),
+ String.format(getString(R.string.uploader_upload_failed_content_multiple), mSuccessCounter, mLocalPaths.length),
+ finalNotification.contentIntent);
+ }
+ mNotificationManager.notify(R.string.uploader_upload_failed_ticker, finalNotification);
}
- mNotificationManager.cancel(42);
- run();
+
}
/**
return remotePath + suffix;
}
}
-
+
+
+ /**
+ * Callback method to update the progress bar in the status notification.
+ */
@Override
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());
+ String text = String.format(getString(R.string.uploader_upload_in_progress_content), 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);
+ mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotification);
}
mPreviousPercent = percent;
}
boolean caughtException = false;\r
GetMethod get = new GetMethod(mUri.toString() + WebdavUtils.encodePath(remoteFilepath));\r
\r
- // get.setHeader("Host", mUri.getHost());\r
- // get.setHeader("User-Agent", "Android-ownCloud");\r
-\r
int status = -1;\r
try {\r
status = executeMethod(get);\r
* @param contentType MIME type of the file.\r
* @return 'True' then the upload was successfully completed\r
*/\r
- public boolean putFile(String localFile, String remoteTarget,\r
- String contentType) {\r
+ public boolean putFile(String localFile, String remoteTarget, String contentType) {\r
boolean result = false;\r
+ boolean caughtException = false;\r
+ int status = 0;\r
\r
try {\r
- Log.e("ASD", contentType + "");\r
File f = new File(localFile);\r
FileRequestEntity entity = new FileRequestEntity(f, contentType);\r
entity.setOnDatatransferProgressListener(mDataTransferListener);\r
- Log.e("ASD", f.exists() + " " + entity.getContentLength());\r
PutMethod put = new PutMethod(mUri.toString() + WebdavUtils.encodePath(remoteTarget));\r
put.setRequestEntity(entity);\r
- Log.d(TAG, "" + put.getURI().toString());\r
- int status = executeMethod(put, 0);\r
- Log.d(TAG, "PUT method return with status " + status);\r
-\r
- if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT) {\r
- result = true;\r
- Log.i(TAG, "Uploading, done");\r
- }\r
+ status = executeMethod(put);\r
\r
- } catch (final Exception e) {\r
- Log.i(TAG, "" + e.getMessage());\r
- result = false;\r
- }\r
+ result = (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT);\r
+ \r
+ Log.d(TAG, "PUT response for " + remoteTarget + " finished with HTTP status " + status);\r
+ \r
+ } catch (HttpException e) {\r
+ Log.e(TAG, "HTTP exception uploading " + localFile + " to " + remoteTarget, e);\r
+ caughtException = true;\r
\r
+ } catch (IOException e) {\r
+ Log.e(TAG, "I/O exception uploading " + localFile + " to " + remoteTarget, e);\r
+ caughtException = true;\r
+\r
+ } catch (Exception e) {\r
+ Log.e(TAG, "Unexpected exception uploading " + localFile + " to " + remoteTarget, e);\r
+ caughtException = true;\r
+ }\r
+ \r
+ if (!result && !caughtException) Log.e(TAG, "Upload of " + localFile + " to " + remoteTarget + " FAILED with HTTP status " + status);\r
+ \r
return result;\r
}\r
\r