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