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