Replaced GPL v3 with GPL v2; PLEASE, note that GPL v2 still allows the code is used...
[pub/Android/ownCloud.git] / src / com / owncloud / android / Uploader.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 package com.owncloud.android;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Stack;
27 import java.util.Vector;
28
29 import com.owncloud.android.authenticator.AccountAuthenticator;
30 import com.owncloud.android.datamodel.DataStorageManager;
31 import com.owncloud.android.datamodel.FileDataStorageManager;
32 import com.owncloud.android.datamodel.OCFile;
33 import com.owncloud.android.files.services.FileUploader;
34 import com.owncloud.android.network.OwnCloudClientUtils;
35
36 import android.accounts.Account;
37 import android.accounts.AccountManager;
38 import android.app.AlertDialog;
39 import android.app.AlertDialog.Builder;
40 import android.app.Dialog;
41 import android.app.ListActivity;
42 import android.app.ProgressDialog;
43 import android.content.Context;
44 import android.content.DialogInterface;
45 import android.content.DialogInterface.OnCancelListener;
46 import android.content.DialogInterface.OnClickListener;
47 import android.content.Intent;
48 import android.database.Cursor;
49 import android.net.Uri;
50 import android.os.Bundle;
51 import android.os.Parcelable;
52 import android.provider.MediaStore.Images.Media;
53 import android.util.Log;
54 import android.view.View;
55 import android.view.Window;
56 import android.widget.AdapterView;
57 import android.widget.AdapterView.OnItemClickListener;
58 import android.widget.Button;
59 import android.widget.EditText;
60 import android.widget.SimpleAdapter;
61 import android.widget.Toast;
62
63 import com.owncloud.android.R;
64 import eu.alefzero.webdav.WebdavClient;
65
66 /**
67 * This can be used to upload things to an ownCloud instance.
68 *
69 * @author Bartek Przybylski
70 *
71 */
72 public class Uploader extends ListActivity implements OnItemClickListener, android.view.View.OnClickListener {
73 private static final String TAG = "ownCloudUploader";
74
75 private Account mAccount;
76 private AccountManager mAccountManager;
77 private Stack<String> mParents;
78 private ArrayList<Parcelable> mStreamsToUpload;
79 private boolean mCreateDir;
80 private String mUploadPath;
81 private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE };
82 private DataStorageManager mStorageManager;
83 private OCFile mFile;
84
85 private final static int DIALOG_NO_ACCOUNT = 0;
86 private final static int DIALOG_WAITING = 1;
87 private final static int DIALOG_NO_STREAM = 2;
88 private final static int DIALOG_MULTIPLE_ACCOUNT = 3;
89 //private final static int DIALOG_GET_DIRNAME = 4;
90
91 private final static int REQUEST_CODE_SETUP_ACCOUNT = 0;
92
93 @Override
94 protected void onCreate(Bundle savedInstanceState) {
95 super.onCreate(savedInstanceState);
96 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
97 mParents = new Stack<String>();
98 mParents.add("");
99 /*if (getIntent().hasExtra(Intent.EXTRA_STREAM)) {
100 prepareStreamsToUpload();*/
101 if (prepareStreamsToUpload()) {
102 mAccountManager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
103 Account[] accounts = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
104 if (accounts.length == 0) {
105 Log.i(TAG, "No ownCloud account is available");
106 showDialog(DIALOG_NO_ACCOUNT);
107 } else if (accounts.length > 1) {
108 Log.i(TAG, "More then one ownCloud is available");
109 showDialog(DIALOG_MULTIPLE_ACCOUNT);
110 } else {
111 mAccount = accounts[0];
112 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
113 populateDirectoryList();
114 }
115 } else {
116 showDialog(DIALOG_NO_STREAM);
117 }
118 }
119
120 @Override
121 protected Dialog onCreateDialog(final int id) {
122 final AlertDialog.Builder builder = new Builder(this);
123 switch (id) {
124 case DIALOG_WAITING:
125 ProgressDialog pDialog = new ProgressDialog(this);
126 pDialog.setIndeterminate(false);
127 pDialog.setCancelable(false);
128 pDialog.setMessage(getResources().getString(R.string.uploader_info_uploading));
129 return pDialog;
130 case DIALOG_NO_ACCOUNT:
131 builder.setIcon(android.R.drawable.ic_dialog_alert);
132 builder.setTitle(R.string.uploader_wrn_no_account_title);
133 builder.setMessage(String.format(getString(R.string.uploader_wrn_no_account_text), getString(R.string.app_name)));
134 builder.setCancelable(false);
135 builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() {
136 @Override
137 public void onClick(DialogInterface dialog, int which) {
138 if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
139 // using string value since in API7 this
140 // constatn is not defined
141 // in API7 < this constatant is defined in
142 // Settings.ADD_ACCOUNT_SETTINGS
143 // and Settings.EXTRA_AUTHORITIES
144 Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
145 intent.putExtra("authorities", new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
146 startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
147 } else {
148 // since in API7 there is no direct call for
149 // account setup, so we need to
150 // show our own AccountSetupAcricity, get
151 // desired results and setup
152 // everything for ourself
153 Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class);
154 startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
155 }
156 }
157 });
158 builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() {
159 @Override
160 public void onClick(DialogInterface dialog, int which) {
161 finish();
162 }
163 });
164 return builder.create();
165 /*case DIALOG_GET_DIRNAME:
166 final EditText dirName = new EditText(getBaseContext());
167 builder.setView(dirName);
168 builder.setTitle(R.string.uploader_info_dirname);
169 String pathToUpload;
170 if (mParents.empty()) {
171 pathToUpload = "/";
172 } else {
173 mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, mParents.peek()), null,
174 null, null, null);
175 mCursor.moveToFirst();
176 pathToUpload = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH))
177 + mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)).replace(" ", "%20"); // TODO don't make this ; use WebdavUtils.encode in the right moment
178 }
179 a a = new a(pathToUpload, dirName);
180 builder.setPositiveButton(R.string.common_ok, a);
181 builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
182 public void onClick(DialogInterface dialog, int which) {
183 dialog.cancel();
184 }
185 });
186 return builder.create();*/
187 case DIALOG_MULTIPLE_ACCOUNT:
188 CharSequence ac[] = new CharSequence[mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
189 for (int i = 0; i < ac.length; ++i) {
190 ac[i] = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[i].name;
191 }
192 builder.setTitle(R.string.common_choose_account);
193 builder.setItems(ac, new OnClickListener() {
194 @Override
195 public void onClick(DialogInterface dialog, int which) {
196 mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[which];
197 mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
198 populateDirectoryList();
199 }
200 });
201 builder.setCancelable(true);
202 builder.setOnCancelListener(new OnCancelListener() {
203 @Override
204 public void onCancel(DialogInterface dialog) {
205 dialog.cancel();
206 finish();
207 }
208 });
209 return builder.create();
210 case DIALOG_NO_STREAM:
211 builder.setIcon(android.R.drawable.ic_dialog_alert);
212 builder.setTitle(R.string.uploader_wrn_no_content_title);
213 builder.setMessage(R.string.uploader_wrn_no_content_text);
214 builder.setCancelable(false);
215 builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
216 @Override
217 public void onClick(DialogInterface dialog, int which) {
218 finish();
219 }
220 });
221 return builder.create();
222 default:
223 throw new IllegalArgumentException("Unknown dialog id: " + id);
224 }
225 }
226
227 class a implements OnClickListener {
228 String mPath;
229 EditText mDirname;
230
231 public a(String path, EditText dirname) {
232 mPath = path;
233 mDirname = dirname;
234 }
235
236 @Override
237 public void onClick(DialogInterface dialog, int which) {
238 Uploader.this.mUploadPath = mPath + mDirname.getText().toString();
239 Uploader.this.mCreateDir = true;
240 uploadFiles();
241 }
242 }
243
244 @Override
245 public void onBackPressed() {
246
247 if (mParents.size() <= 1) {
248 super.onBackPressed();
249 return;
250 } else {
251 mParents.pop();
252 populateDirectoryList();
253 }
254 }
255
256 @Override
257 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
258 // click on folder in the list
259 Log.d(TAG, "on item click");
260 Vector<OCFile> tmpfiles = mStorageManager.getDirectoryContent(mFile);
261 if (tmpfiles.size() <= 0) return;
262 // filter on dirtype
263 Vector<OCFile> files = new Vector<OCFile>();
264 for (OCFile f : tmpfiles)
265 if (f.isDirectory())
266 files.add(f);
267 if (files.size() < position) {
268 throw new IndexOutOfBoundsException("Incorrect item selected");
269 }
270 mParents.push(files.get(position).getFileName());
271 populateDirectoryList();
272 }
273
274 @Override
275 public void onClick(View v) {
276 // click on button
277 switch (v.getId()) {
278 case R.id.uploader_choose_folder:
279 mUploadPath = ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
280 for (String p : mParents)
281 mUploadPath += p + OCFile.PATH_SEPARATOR;
282 Log.d(TAG, "Uploading file to dir " + mUploadPath);
283
284 uploadFiles();
285
286 break;
287 /*case android.R.id.button1: // dynamic action for create aditional dir
288 showDialog(DIALOG_GET_DIRNAME);
289 break;*/
290 default:
291 throw new IllegalArgumentException("Wrong element clicked");
292 }
293 }
294
295 @Override
296 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
297 super.onActivityResult(requestCode, resultCode, data);
298 Log.i(TAG, "result received. req: " + requestCode + " res: " + resultCode);
299 if (requestCode == REQUEST_CODE_SETUP_ACCOUNT) {
300 dismissDialog(DIALOG_NO_ACCOUNT);
301 if (resultCode == RESULT_CANCELED) {
302 finish();
303 }
304 Account[] accounts = mAccountManager.getAccountsByType(AccountAuthenticator.AUTH_TOKEN_TYPE);
305 if (accounts.length == 0) {
306 showDialog(DIALOG_NO_ACCOUNT);
307 } else {
308 // there is no need for checking for is there more then one
309 // account at this point
310 // since account setup can set only one account at time
311 mAccount = accounts[0];
312 populateDirectoryList();
313 }
314 }
315 }
316
317 private void populateDirectoryList() {
318 setContentView(R.layout.uploader_layout);
319
320 String full_path = "";
321 for (String a : mParents)
322 full_path += a + "/";
323
324 Log.d(TAG, "Populating view with content of : " + full_path);
325
326 mFile = mStorageManager.getFileByPath(full_path);
327 if (mFile != null) {
328 Vector<OCFile> files = mStorageManager.getDirectoryContent(mFile);
329 List<HashMap<String, Object>> data = new LinkedList<HashMap<String,Object>>();
330 for (OCFile f : files) {
331 HashMap<String, Object> h = new HashMap<String, Object>();
332 if (f.isDirectory()) {
333 h.put("dirname", f.getFileName());
334 data.add(h);
335 }
336 }
337 SimpleAdapter sa = new SimpleAdapter(this,
338 data,
339 R.layout.uploader_list_item_layout,
340 new String[] {"dirname"},
341 new int[] {R.id.textView1});
342 setListAdapter(sa);
343 Button btn = (Button) findViewById(R.id.uploader_choose_folder);
344 btn.setOnClickListener(this);
345 getListView().setOnItemClickListener(this);
346 }
347 }
348
349 private boolean prepareStreamsToUpload() {
350 if (getIntent().getAction().equals(Intent.ACTION_SEND)) {
351 mStreamsToUpload = new ArrayList<Parcelable>();
352 mStreamsToUpload.add(getIntent().getParcelableExtra(Intent.EXTRA_STREAM));
353 } else if (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
354 mStreamsToUpload = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);
355 }
356 return (mStreamsToUpload != null && mStreamsToUpload.get(0) != null);
357 }
358
359 public void uploadFiles() {
360 try {
361 WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());
362
363 // create last directory in path if necessary
364 if (mCreateDir) {
365 wdc.createDirectory(mUploadPath);
366 }
367
368 String[] local = new String[mStreamsToUpload.size()], remote = new String[mStreamsToUpload.size()];
369
370 for (int i = 0; i < mStreamsToUpload.size(); ++i) {
371 Uri uri = (Uri) mStreamsToUpload.get(i);
372 if (uri.getScheme().equals("content")) {
373 Cursor c = getContentResolver().query((Uri) mStreamsToUpload.get(i),
374 CONTENT_PROJECTION,
375 null,
376 null,
377 null);
378
379 if (!c.moveToFirst())
380 continue;
381
382 final String display_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME)),
383 data = c.getString(c.getColumnIndex(Media.DATA));
384 local[i] = data;
385 remote[i] = mUploadPath + display_name;
386 } else if (uri.getScheme().equals("file")) {
387 final File file = new File(Uri.decode(uri.toString()).replace(uri.getScheme() + "://", ""));
388 local[i] = file.getAbsolutePath();
389 remote[i] = mUploadPath + file.getName();
390 }
391
392 }
393 Intent intent = new Intent(getApplicationContext(), FileUploader.class);
394 intent.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
395 intent.putExtra(FileUploader.KEY_LOCAL_FILE, local);
396 intent.putExtra(FileUploader.KEY_REMOTE_FILE, remote);
397 intent.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
398 startService(intent);
399 finish();
400
401 } catch (SecurityException e) {
402 String message = String.format(getString(R.string.uploader_error_forbidden_content), getString(R.string.app_name));
403 Toast.makeText(this, message, Toast.LENGTH_LONG).show();
404 }
405 }
406
407 }