Copyright note fixes
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / InstantUploadService.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.files.services;
20
21 import java.util.HashMap;
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import com.owncloud.android.Log_OC;
26 import com.owncloud.android.utils.FileStorageUtils;
27 import com.owncloud.android.network.OwnCloudClientUtils;
28
29 import eu.alefzero.webdav.WebdavClient;
30
31 import android.accounts.Account;
32 import android.app.Service;
33 import android.content.Intent;
34 import android.os.IBinder;
35
36 public class InstantUploadService extends Service {
37
38 public static String KEY_FILE_PATH = "KEY_FILEPATH";
39 public static String KEY_FILE_SIZE = "KEY_FILESIZE";
40 public static String KEY_MIME_TYPE = "KEY_MIMETYPE";
41 public static String KEY_DISPLAY_NAME = "KEY_FILENAME";
42 public static String KEY_ACCOUNT = "KEY_ACCOUNT";
43
44 private static String TAG = "InstantUploadService";
45 // TODO make it configurable over the settings dialog
46 public static final String INSTANT_UPLOAD_DIR = "/InstantUpload";
47 private UploaderRunnable mUploaderRunnable;
48
49 @Override
50 public IBinder onBind(Intent arg0) {
51 return null;
52 }
53
54 @Override
55 public int onStartCommand(Intent intent, int flags, int startId) {
56 if (intent == null || !intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_DISPLAY_NAME)
57 || !intent.hasExtra(KEY_FILE_PATH) || !intent.hasExtra(KEY_FILE_SIZE)
58 || !intent.hasExtra(KEY_MIME_TYPE)) {
59 Log_OC.w(TAG, "Not all required information was provided, abording");
60 return Service.START_NOT_STICKY;
61 }
62
63 if (mUploaderRunnable == null) {
64 mUploaderRunnable = new UploaderRunnable();
65 }
66
67 String filename = intent.getStringExtra(KEY_DISPLAY_NAME);
68 String filepath = intent.getStringExtra(KEY_FILE_PATH);
69 String mimetype = intent.getStringExtra(KEY_MIME_TYPE);
70 Account account = intent.getParcelableExtra(KEY_ACCOUNT);
71 long filesize = intent.getLongExtra(KEY_FILE_SIZE, -1);
72
73 mUploaderRunnable.addElementToQueue(filename, filepath, mimetype, filesize, account);
74
75 // starting new thread for new download doesnt seems like a good idea
76 // maybe some thread pool or single background thread would be better
77 Log_OC.d(TAG, "Starting instant upload thread");
78 new Thread(mUploaderRunnable).start();
79
80 return Service.START_STICKY;
81 }
82
83 private class UploaderRunnable implements Runnable {
84
85 Object mLock;
86 List<HashMap<String, Object>> mHashMapList;
87
88 public UploaderRunnable() {
89 mHashMapList = new LinkedList<HashMap<String, Object>>();
90 mLock = new Object();
91 }
92
93 public void addElementToQueue(String filename, String filepath, String mimetype, long length, Account account) {
94 HashMap<String, Object> new_map = new HashMap<String, Object>();
95 new_map.put(KEY_ACCOUNT, account);
96 new_map.put(KEY_DISPLAY_NAME, filename);
97 new_map.put(KEY_FILE_PATH, filepath);
98 new_map.put(KEY_MIME_TYPE, mimetype);
99 new_map.put(KEY_FILE_SIZE, length);
100
101 synchronized (mLock) {
102 mHashMapList.add(new_map);
103 }
104 }
105
106 private HashMap<String, Object> getFirstObject() {
107 synchronized (mLock) {
108 if (mHashMapList.size() == 0)
109 return null;
110 HashMap<String, Object> ret = mHashMapList.get(0);
111 mHashMapList.remove(0);
112 return ret;
113 }
114 }
115
116 public void run() {
117 HashMap<String, Object> working_map;
118
119 while ((working_map = getFirstObject()) != null) {
120 Account account = (Account) working_map.get(KEY_ACCOUNT);
121 String filename = (String) working_map.get(KEY_DISPLAY_NAME);
122 String filepath = (String) working_map.get(KEY_FILE_PATH);
123 String mimetype = (String) working_map.get(KEY_MIME_TYPE);
124
125 WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(account, getApplicationContext());
126
127 wdc.createDirectory(INSTANT_UPLOAD_DIR); // fail could just mean that it already exists put will be tried anyway
128 try {
129 wdc.putFile(filepath, FileStorageUtils.getInstantUploadFilePath(filename), mimetype);
130 } catch (Exception e) {
131 // nothing to do; this service is deprecated, indeed
132 }
133 }
134 }
135 }
136
137 }