42033beb09b397a1a0081fc3e4b3e5eef42172db
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileUploader.java
1 package com.owncloud.android.files.services;
2
3 import java.io.File;
4 import java.util.AbstractList;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Iterator;
8 import java.util.Map;
9 import java.util.Vector;
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.ConcurrentMap;
12
13 import com.owncloud.android.authenticator.AccountAuthenticator;
14 import com.owncloud.android.datamodel.FileDataStorageManager;
15 import com.owncloud.android.datamodel.OCFile;
16 import com.owncloud.android.files.InstantUploadBroadcastReceiver;
17 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
18 import com.owncloud.android.operations.ChunkedUploadFileOperation;
19 import com.owncloud.android.operations.DownloadFileOperation;
20 import com.owncloud.android.operations.RemoteOperationResult;
21 import com.owncloud.android.operations.UploadFileOperation;
22 import com.owncloud.android.ui.activity.FileDetailActivity;
23 import com.owncloud.android.ui.fragment.FileDetailFragment;
24 import com.owncloud.android.utils.OwnCloudVersion;
25
26 import eu.alefzero.webdav.OnDatatransferProgressListener;
27
28 import com.owncloud.android.network.OwnCloudClientUtils;
29
30 import android.accounts.Account;
31 import android.accounts.AccountManager;
32 import android.app.Notification;
33 import android.app.NotificationManager;
34 import android.app.PendingIntent;
35 import android.app.Service;
36 import android.content.Intent;
37 import android.os.Binder;
38 import android.os.Handler;
39 import android.os.HandlerThread;
40 import android.os.IBinder;
41 import android.os.Looper;
42 import android.os.Message;
43 import android.os.Process;
44 import android.util.Log;
45 import android.widget.RemoteViews;
46
47 import com.owncloud.android.R;
48 import eu.alefzero.webdav.WebdavClient;
49
50 public class FileUploader extends Service implements OnDatatransferProgressListener {
51
52 public static final String UPLOAD_FINISH_MESSAGE = "UPLOAD_FINISH";
53 public static final String EXTRA_PARENT_DIR_ID = "PARENT_DIR_ID";
54 public static final String EXTRA_UPLOAD_RESULT = "RESULT";
55 public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
56 public static final String EXTRA_FILE_PATH = "FILE_PATH";
57
58 public static final String KEY_LOCAL_FILE = "LOCAL_FILE";
59 public static final String KEY_REMOTE_FILE = "REMOTE_FILE";
60 public static final String KEY_ACCOUNT = "ACCOUNT";
61 public static final String KEY_UPLOAD_TYPE = "UPLOAD_TYPE";
62 public static final String KEY_FORCE_OVERWRITE = "KEY_FORCE_OVERWRITE";
63 public static final String ACCOUNT_NAME = "ACCOUNT_NAME";
64 public static final String KEY_MIME_TYPE = "MIME_TYPE";
65 public static final String KEY_INSTANT_UPLOAD = "INSTANT_UPLOAD";
66
67 public static final int UPLOAD_SINGLE_FILE = 0;
68 public static final int UPLOAD_MULTIPLE_FILES = 1;
69
70 private static final String TAG = FileUploader.class.getSimpleName();
71
72 private Looper mServiceLooper;
73 private ServiceHandler mServiceHandler;
74 private IBinder mBinder;
75 private WebdavClient mUploadClient = null;
76 private Account mLastAccount = null, mLastAccountWhereInstantFolderWasCreated = null;
77 private FileDataStorageManager mStorageManager;
78
79 //private AbstractList<Account> mAccounts = new Vector<Account>();
80 //private AbstractList<UploadFileOperation> mUploads = new Vector<UploadFileOperation>();
81 //private int mCurrentIndexUpload;
82 private ConcurrentMap<String, UploadFileOperation> mPendingUploads = new ConcurrentHashMap<String, UploadFileOperation>();
83 private UploadFileOperation mCurrentUpload = null;
84
85 private NotificationManager mNotificationManager;
86 private Notification mNotification;
87 private int mLastPercent;
88 private RemoteViews mDefaultNotificationContentView;
89 /*private long mTotalDataToSend, mSendData;
90 private int mTotalFilesToSend, mPreviousPercent;
91 private int mSuccessCounter;*/
92
93
94 /**
95 * Builds a key for mPendingUploads from the account and file to upload
96 *
97 * @param account Account where the file to download is stored
98 * @param file File to download
99 */
100 private String buildRemoteName(Account account, OCFile file) {
101 return account.name + file.getRemotePath();
102 }
103
104 private String buildRemoteName(Account account, String remotePath) {
105 return account.name + remotePath;
106 }
107
108
109 /**
110 * Checks if an ownCloud server version should support chunked uploads.
111 *
112 * @param version OwnCloud version instance corresponding to an ownCloud server.
113 * @return 'True' if the ownCloud server with version supports chunked uploads.
114 */
115 private static boolean chunkedUploadIsSupported(OwnCloudVersion version) {
116 return (version != null && version.compareTo(OwnCloudVersion.owncloud_v4_5) >= 0);
117 }
118
119
120
121 /**
122 * Service initialization
123 */
124 @Override
125 public void onCreate() {
126 super.onCreate();
127 mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
128 HandlerThread thread = new HandlerThread("FileUploaderThread",
129 Process.THREAD_PRIORITY_BACKGROUND);
130 thread.start();
131 mServiceLooper = thread.getLooper();
132 mServiceHandler = new ServiceHandler(mServiceLooper, this);
133 mBinder = new FileUploaderBinder();
134 }
135
136
137 /**
138 * Entry point to add one or several files to the queue of uploads.
139 *
140 * New uploads are added calling to startService(), resulting in a call to this method. This ensures the service will keep on working
141 * although the caller activity goes away.
142 */
143 @Override
144 public int onStartCommand(Intent intent, int flags, int startId) {
145 if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_UPLOAD_TYPE)) {
146 Log.e(TAG, "Not enough information provided in intent");
147 return Service.START_NOT_STICKY;
148 }
149 int uploadType = intent.getIntExtra(KEY_UPLOAD_TYPE, -1);
150 if (uploadType == -1) {
151 Log.e(TAG, "Incorrect upload type provided");
152 return Service.START_NOT_STICKY;
153 }
154 Account account = intent.getParcelableExtra(KEY_ACCOUNT);
155
156 String[] localPaths, remotePaths, mimeTypes;
157 if (uploadType == UPLOAD_SINGLE_FILE) {
158 localPaths = new String[] { intent.getStringExtra(KEY_LOCAL_FILE) };
159 remotePaths = new String[] { intent
160 .getStringExtra(KEY_REMOTE_FILE) };
161 mimeTypes = new String[] { intent.getStringExtra(KEY_MIME_TYPE) };
162
163 } else { // mUploadType == UPLOAD_MULTIPLE_FILES
164 localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
165 remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
166 mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
167 }
168
169 if (localPaths == null) {
170 Log.e(TAG, "Incorrect array for local paths provided in upload intent");
171 return Service.START_NOT_STICKY;
172 }
173 if (remotePaths == null) {
174 Log.e(TAG, "Incorrect array for remote paths provided in upload intent");
175 return Service.START_NOT_STICKY;
176 }
177
178 if (localPaths.length != remotePaths.length) {
179 Log.e(TAG, "Different number of remote paths and local paths!");
180 return Service.START_NOT_STICKY;
181 }
182
183 boolean isInstant = intent.getBooleanExtra(KEY_INSTANT_UPLOAD, false);
184 boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
185
186 OwnCloudVersion ocv = new OwnCloudVersion(AccountManager.get(this).getUserData(account, AccountAuthenticator.KEY_OC_VERSION));
187 boolean chunked = FileUploader.chunkedUploadIsSupported(ocv);
188 AbstractList<String> requestedUploads = new Vector<String>();
189 String uploadKey = null;
190 UploadFileOperation newUpload = null;
191 try {
192 for (int i=0; i < localPaths.length; i++) {
193 uploadKey = buildRemoteName(account, remotePaths[i]);
194 if (chunked) {
195 newUpload = new ChunkedUploadFileOperation(account, localPaths[i], remotePaths[i], ((mimeTypes!=null)?mimeTypes[i]:(String)null), isInstant, forceOverwrite);
196 } else {
197 newUpload = new UploadFileOperation(account, localPaths[i], remotePaths[i], (mimeTypes!=null?mimeTypes[i]:(String)null), isInstant, forceOverwrite);
198 }
199 mPendingUploads.putIfAbsent(uploadKey, newUpload);
200 newUpload.addDatatransferProgressListener(this);
201 requestedUploads.add(uploadKey);
202 }
203
204 } catch (IllegalArgumentException e) {
205 Log.e(TAG, "Not enough information provided in intent: " + e.getMessage());
206 return START_NOT_STICKY;
207 }
208
209 if (requestedUploads.size() > 0) {
210 Message msg = mServiceHandler.obtainMessage();
211 msg.arg1 = startId;
212 msg.obj = requestedUploads;
213 mServiceHandler.sendMessage(msg);
214 }
215
216 return Service.START_NOT_STICKY;
217 }
218
219
220 /**
221 * Provides a binder object that clients can use to perform operations on the queue of uploads, excepting the addition of new files.
222 *
223 * Implemented to perform cancellation, pause and resume of existing uploads.
224 */
225 @Override
226 public IBinder onBind(Intent arg0) {
227 return mBinder;
228 }
229
230 /**
231 * Binder to let client components to perform operations on the queue of uploads.
232 *
233 * It provides by itself the available operations.
234 */
235 public class FileUploaderBinder extends Binder {
236
237 /**
238 * Cancels a pending or current upload of a remote file.
239 *
240 * @param account Owncloud account where the remote file will be stored.
241 * @param file A file in the queue of pending uploads
242 */
243 public void cancel(Account account, OCFile file) {
244 UploadFileOperation upload = null;
245 synchronized (mPendingUploads) {
246 upload = mPendingUploads.remove(buildRemoteName(account, file));
247 }
248 if (upload != null) {
249 // TODO upload.cancel();
250 }
251 }
252
253
254 /**
255 * Returns True when the file described by 'file' is being uploaded to the ownCloud account 'account' or waiting for it
256 *
257 * @param account Owncloud account where the remote file will be stored.
258 * @param file A file that could be in the queue of pending uploads
259 */
260 public boolean isUploading(Account account, OCFile file) {
261 synchronized (mPendingUploads) {
262 return (mPendingUploads.containsKey(buildRemoteName(account, file)));
263 }
264 }
265 }
266
267
268
269
270 /**
271 * Upload worker. Performs the pending uploads in the order they were requested.
272 *
273 * Created with the Looper of a new thread, started in {@link FileUploader#onCreate()}.
274 */
275 private static class ServiceHandler extends Handler {
276 // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
277 FileUploader mService;
278 public ServiceHandler(Looper looper, FileUploader service) {
279 super(looper);
280 if (service == null)
281 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
282 mService = service;
283 }
284
285 @Override
286 public void handleMessage(Message msg) {
287 @SuppressWarnings("unchecked")
288 AbstractList<String> requestedUploads = (AbstractList<String>) msg.obj;
289 if (msg.obj != null) {
290 Iterator<String> it = requestedUploads.iterator();
291 while (it.hasNext()) {
292 mService.uploadFile(it.next());
293 }
294 }
295 mService.stopSelf(msg.arg1);
296 }
297 }
298
299
300
301
302 /**
303 * Core upload method: sends the file(s) to upload
304 *
305 * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
306 */
307 public void uploadFile(String uploadKey) {
308
309 synchronized(mPendingUploads) {
310 mCurrentUpload = mPendingUploads.get(uploadKey);
311 }
312
313 if (mCurrentUpload != null) {
314
315 /// prepare upload statistics
316 /*mTotalDataToSend = mSendData = mPreviousPercent = 0;
317 Iterator<UploadFileOperation> it = mUploads.iterator();
318 while (it.hasNext()) {
319 mTotalDataToSend += new File(it.next().getLocalPath()).length();
320 }
321 mTotalFilesToSend = mUploads.size();
322 Log.d(TAG, "Will upload " + mTotalDataToSend + " bytes, with " + mUploads.size() + " files");*/
323
324
325 notifyUploadStart(mCurrentUpload);
326
327
328 /// prepare client object to send requests to the ownCloud server
329 if (mUploadClient == null || !mLastAccount.equals(mCurrentUpload.getAccount())) {
330 mLastAccount = mCurrentUpload.getAccount();
331 mStorageManager = new FileDataStorageManager(mLastAccount, getContentResolver());
332 mUploadClient = OwnCloudClientUtils.createOwnCloudClient(mLastAccount, getApplicationContext());
333 }
334
335 /// create remote folder for instant uploads, "if necessary" (would be great that HEAD to a folder worked as with files, we should check; but it's not WebDAV standard, anyway
336 if (mCurrentUpload.isInstant() && !mLastAccountWhereInstantFolderWasCreated.equals(mCurrentUpload.getAccount())) {
337 mLastAccountWhereInstantFolderWasCreated = mCurrentUpload.getAccount();
338 createRemoteFolderForInstantUploads(mUploadClient, mStorageManager);
339 }
340
341 /// perform the upload
342 RemoteOperationResult uploadResult = null;
343 long parentDirId = -1;
344 try {
345 File remote = new File(mCurrentUpload.getRemotePath());
346 parentDirId = mStorageManager.getFileByPath(remote.getParent().endsWith("/")?remote.getParent():remote.getParent()+"/").getFileId();
347 File local = new File(mCurrentUpload.getLocalPath());
348 long size = local.length();
349 uploadResult = mCurrentUpload.execute(mUploadClient);
350 if (uploadResult.isSuccess()) {
351 saveNewOCFile(mCurrentUpload, mStorageManager, parentDirId, size);
352 }
353
354 } finally {
355 synchronized(mPendingUploads) {
356 mPendingUploads.remove(uploadKey);
357 }
358 }
359
360 /// notify result
361 notifyUploadResult(uploadResult, mCurrentUpload);
362
363 sendFinalBroadcast(mCurrentUpload, uploadResult, parentDirId);
364
365 }
366
367 }
368
369 /**
370 * Create remote folder for instant uploads if necessary.
371 *
372 * @param client WebdavClient to the ownCloud server.
373 * @param storageManager Interface to the local database caching the data in the server.
374 * @return 'True' if the folder exists when the methods finishes.
375 */
376 private boolean createRemoteFolderForInstantUploads(WebdavClient client, FileDataStorageManager storageManager) {
377 boolean result = true;
378 OCFile instantUploadDir = storageManager.getFileByPath(InstantUploadBroadcastReceiver.INSTANT_UPLOAD_DIR);
379 if (instantUploadDir == null) {
380 result = client.createDirectory(InstantUploadBroadcastReceiver.INSTANT_UPLOAD_DIR); // fail could just mean that it already exists, but local database is not synchronized; the upload will be started anyway
381 OCFile newDir = new OCFile(InstantUploadBroadcastReceiver.INSTANT_UPLOAD_DIR);
382 newDir.setMimetype("DIR");
383 newDir.setParentId(storageManager.getFileByPath(OCFile.PATH_SEPARATOR).getFileId());
384 storageManager.saveFile(newDir);
385 }
386 return result;
387 }
388
389 /**
390 * Saves a new OC File after a successful upload.
391 *
392 * @param upload Upload operation completed.
393 * @param storageManager Interface to the database where the new OCFile has to be stored.
394 * @param parentDirId Id of the parent OCFile.
395 * @param size Size of the file.
396 */
397 private void saveNewOCFile(UploadFileOperation upload, FileDataStorageManager storageManager, long parentDirId, long size) {
398 OCFile newFile = new OCFile(upload.getRemotePath());
399 newFile.setMimetype(upload.getMimeType());
400 newFile.setFileLength(size);
401 newFile.setModificationTimestamp(System.currentTimeMillis());
402 newFile.setLastSyncDate(0);
403 newFile.setStoragePath(upload.getLocalPath());
404 newFile.setParentId(parentDirId);
405 if (upload.getForceOverwrite())
406 newFile.setKeepInSync(true);
407 storageManager.saveFile(newFile);
408 }
409
410 /**
411 * Creates a status notification to show the upload progress
412 *
413 * @param upload Upload operation starting.
414 */
415 private void notifyUploadStart(UploadFileOperation upload) {
416 /// create status notification with a progress bar
417 mLastPercent = 0;
418 mNotification = new Notification(R.drawable.icon, getString(R.string.uploader_upload_in_progress_ticker), System.currentTimeMillis());
419 mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
420 mDefaultNotificationContentView = mNotification.contentView;
421 mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout);
422 mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, false);
423 mNotification.contentView.setTextViewText(R.id.status_text, String.format(getString(R.string.uploader_upload_in_progress_content), 0, new File(upload.getLocalPath()).getName()));
424 mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon);
425
426 /// includes a pending intent in the notification showing the details view of the file
427 /* TODO
428 Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
429 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, upload.getFile());
430 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, upload.getAccount());
431 showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
432 mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, showDetailsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
433 */
434
435 mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotification);
436 }
437
438
439 /**
440 * Callback method to update the progress bar in the status notification
441 */
442 @Override
443 public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String fileName) {
444 int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
445 if (percent != mLastPercent) {
446 mNotification.contentView.setProgressBar(R.id.status_progress, 100, percent, false);
447 String text = String.format(getString(R.string.uploader_upload_in_progress_content), percent, fileName);
448 mNotification.contentView.setTextViewText(R.id.status_text, text);
449 mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotification);
450 }
451 mLastPercent = percent;
452 }
453
454
455 /**
456 * Callback method to update the progress bar in the status notification (old version)
457 */
458 @Override
459 public void onTransferProgress(long progressRate) {
460 // NOTHING TO DO HERE ANYMORE
461 }
462
463
464 /**
465 * Updates the status notification with the result of an upload operation.
466 *
467 * @param uploadResult Result of the upload operation.
468 * @param upload Finished upload operation
469 */
470 private void notifyUploadResult(RemoteOperationResult uploadResult, UploadFileOperation upload) {
471 if (uploadResult.isCancelled()) {
472 /// cancelled operation -> silent removal of progress notification
473 mNotificationManager.cancel(R.string.uploader_upload_in_progress_ticker);
474
475 } else if (uploadResult.isSuccess()) {
476 /// success -> silent update of progress notification to success message
477 mNotification.flags ^= Notification.FLAG_ONGOING_EVENT; // remove the ongoing flag
478 mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
479 mNotification.contentView = mDefaultNotificationContentView;
480
481 /// includes a pending intent in the notification showing the details view of the file
482 /*
483 Intent showDetailsIntent = new Intent(this, FileDetailActivity.class);
484 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_FILE, upload.getFile());
485 showDetailsIntent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, upload.getAccount());
486 showDetailsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
487 mNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, showDetailsIntent, PendingIntent.FLAG_UPDATE_CURRENT);
488 */
489
490 mNotification.setLatestEventInfo( getApplicationContext(),
491 getString(R.string.uploader_upload_succeeded_ticker),
492 String.format(getString(R.string.uploader_upload_succeeded_content_single), (new File(upload.getLocalPath())).getName()),
493 mNotification.contentIntent);
494
495 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
496
497 /* Notification about multiple uploads: pending of update
498 mNotification.setLatestEventInfo( getApplicationContext(),
499 getString(R.string.uploader_upload_succeeded_ticker),
500 String.format(getString(R.string.uploader_upload_succeeded_content_multiple), mSuccessCounter),
501 mNotification.contentIntent);
502 */
503
504 } else {
505 /// fail -> explicit failure notification
506 mNotificationManager.cancel(R.string.uploader_upload_in_progress_ticker);
507 Notification finalNotification = new Notification(R.drawable.icon, getString(R.string.uploader_upload_failed_ticker), System.currentTimeMillis());
508 finalNotification.flags |= Notification.FLAG_AUTO_CANCEL;
509 // TODO put something smart in the contentIntent below
510 finalNotification.contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
511 finalNotification.setLatestEventInfo( getApplicationContext(),
512 getString(R.string.uploader_upload_failed_ticker),
513 String.format(getString(R.string.uploader_upload_failed_content_single), (new File(upload.getLocalPath())).getName()),
514 finalNotification.contentIntent);
515
516 mNotificationManager.notify(R.string.uploader_upload_failed_ticker, finalNotification);
517
518 /* Notification about multiple uploads failure: pending of update
519 finalNotification.setLatestEventInfo( getApplicationContext(),
520 getString(R.string.uploader_upload_failed_ticker),
521 String.format(getString(R.string.uploader_upload_failed_content_multiple), mSuccessCounter, mTotalFilesToSend),
522 finalNotification.contentIntent);
523 } */
524 }
525
526 }
527
528
529 /**
530 * Sends a broadcast in order to the interested activities can update their view
531 *
532 * @param upload Finished upload operation
533 * @param uploadResult Result of the upload operation
534 */
535 private void sendFinalBroadcast(UploadFileOperation upload, RemoteOperationResult uploadResult, long parentDirId) {
536 Intent end = new Intent(UPLOAD_FINISH_MESSAGE);
537 end.putExtra(EXTRA_REMOTE_PATH, upload.getRemotePath());
538 end.putExtra(EXTRA_FILE_PATH, upload.getLocalPath());
539 end.putExtra(ACCOUNT_NAME, upload.getAccount().name);
540 end.putExtra(EXTRA_UPLOAD_RESULT, uploadResult.isSuccess());
541 end.putExtra(EXTRA_PARENT_DIR_ID, parentDirId);
542 sendBroadcast(end);
543 }
544
545
546 }