1cf22c207c5af0b1d5ff2fb3ddee03c1916511a5
[pub/Android/ownCloud.git] / src / eu / alefzero / owncloud / files / services / InstantUploadService.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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 eu.alefzero.owncloud.files.services;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25
26 import org.apache.commons.httpclient.HttpException;
27 import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
28
29 import eu.alefzero.owncloud.AccountUtils;
30 import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
31 import eu.alefzero.owncloud.utils.OwnCloudVersion;
32 import eu.alefzero.webdav.WebdavClient;
33
34 import android.accounts.Account;
35 import android.accounts.AccountManager;
36 import android.app.Service;
37 import android.content.Intent;
38 import android.net.Uri;
39 import android.os.IBinder;
40 import android.util.Log;
41
42 public class InstantUploadService extends Service {
43
44 public static String KEY_FILE_PATH = "KEY_FILEPATH";
45 public static String KEY_FILE_SIZE = "KEY_FILESIZE";
46 public static String KEY_MIME_TYPE = "KEY_MIMETYPE";
47 public static String KEY_DISPLAY_NAME = "KEY_FILENAME";
48 public static String KEY_ACCOUNT = "KEY_ACCOUNT";
49
50 private static String TAG = "InstantUploadService";
51 private static String INSTANT_UPLOAD_DIR = "/InstantUpload";
52 private UploaderRunnable mUploaderRunnable;
53
54 @Override
55 public IBinder onBind(Intent arg0) {
56 return null;
57 }
58
59 @Override
60 public int onStartCommand(Intent intent, int flags, int startId) {
61 if (intent == null ||
62 !intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_DISPLAY_NAME) ||
63 !intent.hasExtra(KEY_FILE_PATH) || !intent.hasExtra(KEY_FILE_SIZE) ||
64 !intent.hasExtra(KEY_MIME_TYPE)) {
65 Log.w(TAG, "Not all required information was provided, abording");
66 return Service.START_NOT_STICKY;
67 }
68
69 if (mUploaderRunnable == null) {
70 mUploaderRunnable = new UploaderRunnable();
71 }
72
73 String filename = intent.getStringExtra(KEY_DISPLAY_NAME);
74 String filepath = intent.getStringExtra(KEY_FILE_PATH);
75 String mimetype = intent.getStringExtra(KEY_MIME_TYPE);
76 Account account = intent.getParcelableExtra(KEY_ACCOUNT);
77 long filesize = intent.getLongExtra(KEY_FILE_SIZE, -1);
78
79 mUploaderRunnable.addElementToQueue(filename, filepath, mimetype, filesize, account);
80
81 // starting new thread for new download doesnt seems like a good idea
82 // maybe some thread pool or single background thread would be better
83 Log.d(TAG, "Starting instant upload thread");
84 new Thread(mUploaderRunnable).start();
85
86 return Service.START_STICKY;
87 }
88
89 private class UploaderRunnable implements Runnable {
90
91 Object mLock;
92 List<HashMap<String, Object>> mHashMapList;
93
94 public UploaderRunnable() {
95 mHashMapList = new LinkedList<HashMap<String, Object>>();
96 mLock = new Object();
97 }
98
99 public void addElementToQueue(String filename,
100 String filepath,
101 String mimetype,
102 long length,
103 Account account) {
104 HashMap<String, Object> new_map = new HashMap<String, Object>();
105 new_map.put(KEY_ACCOUNT, account);
106 new_map.put(KEY_DISPLAY_NAME, filename);
107 new_map.put(KEY_FILE_PATH, filepath);
108 new_map.put(KEY_MIME_TYPE, mimetype);
109 new_map.put(KEY_FILE_SIZE, length);
110
111 synchronized (mLock) {
112 mHashMapList.add(new_map);
113 }
114 }
115
116 private HashMap<String, Object> getFirstObject() {
117 synchronized (mLock) {
118 if (mHashMapList.size() == 0)
119 return null;
120 HashMap<String, Object> ret = mHashMapList.get(0);
121 mHashMapList.remove(0);
122 return ret;
123 }
124 }
125
126 public void run() {
127 HashMap<String, Object> working_map;
128 AccountManager am = AccountManager.get(getApplicationContext());
129
130 while ((working_map = getFirstObject()) != null) {
131 Account account = (Account) working_map.get(KEY_ACCOUNT);
132 String username = account.name.substring(0, account.name.lastIndexOf('@'));
133 String password = am.getPassword(account);
134 String filename = (String) working_map.get(KEY_DISPLAY_NAME);
135 String filepath = (String) working_map.get(KEY_FILE_PATH);
136 String mimetype = (String) working_map.get(KEY_MIME_TYPE);
137
138 String oc_base_url = am.getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL);
139 String oc_version = am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION);
140 OwnCloudVersion ocv = new OwnCloudVersion(oc_version);
141 String webdav_path = AccountUtils.getWebdavPath(ocv);
142 WebdavClient wdc = new WebdavClient(account, getApplicationContext());
143 wdc.allowSelfsignedCertificates();
144 wdc.setCredentials(username, password);
145
146 MkColMethod mkcol = new MkColMethod(oc_base_url+webdav_path+INSTANT_UPLOAD_DIR);
147 int status = 0;
148 try {
149 status = wdc.executeMethod(mkcol);
150 Log.e(TAG, "mkcol returned " + status);
151 wdc.putFile(filepath, INSTANT_UPLOAD_DIR + "/" + Uri.encode(filename), mimetype);
152 } catch (HttpException e) {
153 e.printStackTrace();
154 } catch (IOException e) {
155 e.printStackTrace();
156 }
157 }
158 }
159 }
160
161 }