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