1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.owncloud
.files
.services
;
21 import java
.io
.IOException
;
22 import java
.util
.HashMap
;
23 import java
.util
.LinkedList
;
24 import java
.util
.List
;
26 import org
.apache
.commons
.httpclient
.HttpException
;
27 import org
.apache
.jackrabbit
.webdav
.client
.methods
.MkColMethod
;
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
;
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
;
42 public class InstantUploadService
extends Service
{
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";
50 private static String TAG
= "InstantUploadService";
51 private static String INSTANT_UPLOAD_DIR
= "/InstantUpload";
52 private UploaderRunnable mUploaderRunnable
;
55 public IBinder
onBind(Intent arg0
) {
60 public int onStartCommand(Intent intent
, int flags
, int startId
) {
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
;
69 if (mUploaderRunnable
== null
) {
70 mUploaderRunnable
= new UploaderRunnable();
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);
79 mUploaderRunnable
.addElementToQueue(filename
, filepath
, mimetype
, filesize
, account
);
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();
86 return Service
.START_STICKY
;
89 private class UploaderRunnable
implements Runnable
{
92 List
<HashMap
<String
, Object
>> mHashMapList
;
94 public UploaderRunnable() {
95 mHashMapList
= new LinkedList
<HashMap
<String
, Object
>>();
99 public void addElementToQueue(String filename
,
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
);
111 synchronized (mLock
) {
112 mHashMapList
.add(new_map
);
116 private HashMap
<String
, Object
> getFirstObject() {
117 synchronized (mLock
) {
118 if (mHashMapList
.size() == 0)
120 HashMap
<String
, Object
> ret
= mHashMapList
.get(0);
121 mHashMapList
.remove(0);
127 HashMap
<String
, Object
> working_map
;
128 AccountManager am
= AccountManager
.get(getApplicationContext());
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
);
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
);
146 MkColMethod mkcol
= new MkColMethod(oc_base_url
+webdav_path
+INSTANT_UPLOAD_DIR
);
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
) {
154 } catch (IOException e
) {