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