resized image is loaded instead of full sized image.
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / Uploader.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author Bartek Przybylski
5 * Copyright (C) 2012 Bartek Przybylski
6 * Copyright (C) 2015 ownCloud Inc.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 package com.owncloud.android.ui.activity;
23
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Stack;
30 import java.util.Vector;
31
32 import android.accounts.Account;
33 import android.accounts.AccountManager;
34 import android.app.AlertDialog;
35 import android.app.AlertDialog.Builder;
36 import android.app.Dialog;
37 import android.app.ProgressDialog;
38 import android.content.Context;
39 import android.content.DialogInterface;
40 import android.content.DialogInterface.OnCancelListener;
41 import android.content.DialogInterface.OnClickListener;
42 import android.content.Intent;
43 import android.content.SharedPreferences;
44 import android.content.res.Resources.NotFoundException;
45 import android.database.Cursor;
46 import android.net.Uri;
47 import android.os.Bundle;
48 import android.os.Parcelable;
49 import android.preference.PreferenceManager;
50 import android.provider.MediaStore.Audio;
51 import android.provider.MediaStore.Images;
52 import android.provider.MediaStore.Video;
53 import android.view.View;
54 import android.widget.AdapterView;
55 import android.widget.AdapterView.OnItemClickListener;
56 import android.widget.Button;
57 import android.widget.EditText;
58 import android.widget.ListView;
59 import android.widget.SimpleAdapter;
60 import android.widget.Toast;
61
62 import com.actionbarsherlock.app.ActionBar;
63 import com.actionbarsherlock.view.MenuItem;
64 import com.owncloud.android.MainApp;
65 import com.owncloud.android.R;
66 import com.owncloud.android.authentication.AccountAuthenticator;
67 import com.owncloud.android.datamodel.OCFile;
68 import com.owncloud.android.files.services.FileUploader;
69 import com.owncloud.android.lib.common.operations.RemoteOperation;
70 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
71 import com.owncloud.android.lib.common.utils.Log_OC;
72 import com.owncloud.android.operations.CreateFolderOperation;
73 import com.owncloud.android.ui.dialog.CreateFolderDialogFragment;
74 import com.owncloud.android.utils.DisplayUtils;
75 import com.owncloud.android.utils.ErrorMessageAdapter;
76
77
78 /**
79 * This can be used to upload things to an ownCloud instance.
80 */
81 public class Uploader extends FileActivity
82 implements OnItemClickListener, android.view.View.OnClickListener {
83
84 private static final String TAG = Uploader.class.getSimpleName();
85
86 private AccountManager mAccountManager;
87 private Stack<String> mParents;
88 private ArrayList<Parcelable> mStreamsToUpload;
89 private boolean mCreateDir;
90 private String mUploadPath;
91 private OCFile mFile;
92 private boolean mAccountSelected;
93
94 private final static int DIALOG_NO_ACCOUNT = 0;
95 private final static int DIALOG_WAITING = 1;
96 private final static int DIALOG_NO_STREAM = 2;
97 private final static int DIALOG_MULTIPLE_ACCOUNT = 3;
98
99 private final static int REQUEST_CODE_SETUP_ACCOUNT = 0;
100
101 private final static String KEY_PARENTS = "PARENTS";
102 private final static String KEY_FILE = "FILE";
103 private final static String KEY_ACCOUNT_SELECTED = "ACCOUNT_SELECTED";
104
105 @Override
106 protected void onCreate(Bundle savedInstanceState) {
107 prepareStreamsToUpload();
108
109 if (savedInstanceState == null) {
110 mParents = new Stack<String>();
111 mAccountSelected = false;
112 } else {
113 mParents = (Stack<String>) savedInstanceState.getSerializable(KEY_PARENTS);
114 mFile = savedInstanceState.getParcelable(KEY_FILE);
115 mAccountSelected = savedInstanceState.getBoolean(KEY_ACCOUNT_SELECTED);
116 }
117 super.onCreate(savedInstanceState);
118
119 ActionBar actionBar = getSupportActionBar();
120 actionBar.setIcon(DisplayUtils.getSeasonalIconId());
121
122 }
123
124 @Override
125 protected void setAccount(Account account, boolean savedAccount) {
126 if (somethingToUpload()) {
127 mAccountManager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
128 Account[] accounts = mAccountManager.getAccountsByType(MainApp.getAccountType());
129 if (accounts.length == 0) {
130 Log_OC.i(TAG, "No ownCloud account is available");
131 showDialog(DIALOG_NO_ACCOUNT);
132 } else if (accounts.length > 1 && !mAccountSelected) {
133 Log_OC.i(TAG, "More than one ownCloud is available");
134 showDialog(DIALOG_MULTIPLE_ACCOUNT);
135 } else {
136 if (!savedAccount) {
137 setAccount(accounts[0]);
138 }
139 }
140
141 } else {
142 showDialog(DIALOG_NO_STREAM);
143 }
144
145 super.setAccount(account, savedAccount);
146 }
147
148 @Override
149 protected void onAccountSet(boolean stateWasRecovered) {
150 super.onAccountSet(mAccountWasRestored);
151 initTargetFolder();
152 populateDirectoryList();
153 }
154
155 @Override
156 protected void onSaveInstanceState(Bundle outState) {
157 Log_OC.d(TAG, "onSaveInstanceState() start");
158 super.onSaveInstanceState(outState);
159 outState.putSerializable(KEY_PARENTS, mParents);
160 //outState.putParcelable(KEY_ACCOUNT, mAccount);
161 outState.putParcelable(KEY_FILE, mFile);
162 outState.putBoolean(KEY_ACCOUNT_SELECTED, mAccountSelected);
163
164 Log_OC.d(TAG, "onSaveInstanceState() end");
165 }
166
167 @Override
168 protected Dialog onCreateDialog(final int id) {
169 final AlertDialog.Builder builder = new Builder(this);
170 switch (id) {
171 case DIALOG_WAITING:
172 ProgressDialog pDialog = new ProgressDialog(this);
173 pDialog.setIndeterminate(false);
174 pDialog.setCancelable(false);
175 pDialog.setMessage(getResources().getString(R.string.uploader_info_uploading));
176 return pDialog;
177 case DIALOG_NO_ACCOUNT:
178 builder.setIcon(android.R.drawable.ic_dialog_alert);
179 builder.setTitle(R.string.uploader_wrn_no_account_title);
180 builder.setMessage(String.format(
181 getString(R.string.uploader_wrn_no_account_text), getString(R.string.app_name)));
182 builder.setCancelable(false);
183 builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() {
184 @Override
185 public void onClick(DialogInterface dialog, int which) {
186 if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
187 // using string value since in API7 this
188 // constatn is not defined
189 // in API7 < this constatant is defined in
190 // Settings.ADD_ACCOUNT_SETTINGS
191 // and Settings.EXTRA_AUTHORITIES
192 Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
193 intent.putExtra("authorities", new String[] { MainApp.getAuthTokenType() });
194 startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
195 } else {
196 // since in API7 there is no direct call for
197 // account setup, so we need to
198 // show our own AccountSetupAcricity, get
199 // desired results and setup
200 // everything for ourself
201 Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class);
202 startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
203 }
204 }
205 });
206 builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() {
207 @Override
208 public void onClick(DialogInterface dialog, int which) {
209 finish();
210 }
211 });
212 return builder.create();
213 case DIALOG_MULTIPLE_ACCOUNT:
214 CharSequence ac[] = new CharSequence[
215 mAccountManager.getAccountsByType(MainApp.getAccountType()).length];
216 for (int i = 0; i < ac.length; ++i) {
217 ac[i] = DisplayUtils.convertIdn(
218 mAccountManager.getAccountsByType(MainApp.getAccountType())[i].name, false);
219 }
220 builder.setTitle(R.string.common_choose_account);
221 builder.setItems(ac, new OnClickListener() {
222 @Override
223 public void onClick(DialogInterface dialog, int which) {
224 setAccount(mAccountManager.getAccountsByType(MainApp.getAccountType())[which]);
225 onAccountSet(mAccountWasRestored);
226 dialog.dismiss();
227 mAccountSelected = true;
228 }
229 });
230 builder.setCancelable(true);
231 builder.setOnCancelListener(new OnCancelListener() {
232 @Override
233 public void onCancel(DialogInterface dialog) {
234 dialog.cancel();
235 finish();
236 }
237 });
238 return builder.create();
239 case DIALOG_NO_STREAM:
240 builder.setIcon(android.R.drawable.ic_dialog_alert);
241 builder.setTitle(R.string.uploader_wrn_no_content_title);
242 builder.setMessage(R.string.uploader_wrn_no_content_text);
243 builder.setCancelable(false);
244 builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
245 @Override
246 public void onClick(DialogInterface dialog, int which) {
247 finish();
248 }
249 });
250 return builder.create();
251 default:
252 throw new IllegalArgumentException("Unknown dialog id: " + id);
253 }
254 }
255
256 class a implements OnClickListener {
257 String mPath;
258 EditText mDirname;
259
260 public a(String path, EditText dirname) {
261 mPath = path;
262 mDirname = dirname;
263 }
264
265 @Override
266 public void onClick(DialogInterface dialog, int which) {
267 Uploader.this.mUploadPath = mPath + mDirname.getText().toString();
268 Uploader.this.mCreateDir = true;
269 uploadFiles();
270 }
271 }
272
273 @Override
274 public void onBackPressed() {
275
276 if (mParents.size() <= 1) {
277 super.onBackPressed();
278 return;
279 } else {
280 mParents.pop();
281 populateDirectoryList();
282 }
283 }
284
285 @Override
286 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
287 // click on folder in the list
288 Log_OC.d(TAG, "on item click");
289 Vector<OCFile> tmpfiles = getStorageManager().getFolderContent(mFile);
290 if (tmpfiles.size() <= 0) return;
291 // filter on dirtype
292 Vector<OCFile> files = new Vector<OCFile>();
293 for (OCFile f : tmpfiles)
294 if (f.isFolder())
295 files.add(f);
296 if (files.size() < position) {
297 throw new IndexOutOfBoundsException("Incorrect item selected");
298 }
299 mParents.push(files.get(position).getFileName());
300 populateDirectoryList();
301 }
302
303 @Override
304 public void onClick(View v) {
305 // click on button
306 switch (v.getId()) {
307 case R.id.uploader_choose_folder:
308 mUploadPath = ""; // first element in mParents is root dir, represented by "";
309 // init mUploadPath with "/" results in a "//" prefix
310 for (String p : mParents)
311 mUploadPath += p + OCFile.PATH_SEPARATOR;
312 Log_OC.d(TAG, "Uploading file to dir " + mUploadPath);
313
314 uploadFiles();
315
316 break;
317
318 case R.id.uploader_new_folder:
319 CreateFolderDialogFragment dialog = CreateFolderDialogFragment.newInstance(mFile);
320 dialog.show(getSupportFragmentManager(), "createdirdialog");
321 break;
322
323
324 default:
325 throw new IllegalArgumentException("Wrong element clicked");
326 }
327 }
328
329 @Override
330 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
331 super.onActivityResult(requestCode, resultCode, data);
332 Log_OC.i(TAG, "result received. req: " + requestCode + " res: " + resultCode);
333 if (requestCode == REQUEST_CODE_SETUP_ACCOUNT) {
334 dismissDialog(DIALOG_NO_ACCOUNT);
335 if (resultCode == RESULT_CANCELED) {
336 finish();
337 }
338 Account[] accounts = mAccountManager.getAccountsByType(MainApp.getAuthTokenType());
339 if (accounts.length == 0) {
340 showDialog(DIALOG_NO_ACCOUNT);
341 } else {
342 // there is no need for checking for is there more then one
343 // account at this point
344 // since account setup can set only one account at time
345 setAccount(accounts[0]);
346 populateDirectoryList();
347 }
348 }
349 }
350
351 private void populateDirectoryList() {
352 setContentView(R.layout.uploader_layout);
353
354 ListView mListView = (ListView) findViewById(android.R.id.list);
355
356 String current_dir = mParents.peek();
357 if(current_dir.equals("")){
358 getSupportActionBar().setTitle(getString(R.string.default_display_name_for_root_folder));
359 }
360 else{
361 getSupportActionBar().setTitle(current_dir);
362 }
363 boolean notRoot = (mParents.size() > 1);
364 ActionBar actionBar = getSupportActionBar();
365 actionBar.setDisplayHomeAsUpEnabled(notRoot);
366 actionBar.setHomeButtonEnabled(notRoot);
367
368 String full_path = generatePath(mParents);
369
370 Log_OC.d(TAG, "Populating view with content of : " + full_path);
371
372 mFile = getStorageManager().getFileByPath(full_path);
373 if (mFile != null) {
374 Vector<OCFile> files = getStorageManager().getFolderContent(mFile);
375 List<HashMap<String, Object>> data = new LinkedList<HashMap<String,Object>>();
376 for (OCFile f : files) {
377 HashMap<String, Object> h = new HashMap<String, Object>();
378 if (f.isFolder()) {
379 h.put("dirname", f.getFileName());
380 data.add(h);
381 }
382 }
383 SimpleAdapter sa = new SimpleAdapter(this,
384 data,
385 R.layout.uploader_list_item_layout,
386 new String[] {"dirname"},
387 new int[] {R.id.textView1});
388
389 mListView.setAdapter(sa);
390 Button btnChooseFolder = (Button) findViewById(R.id.uploader_choose_folder);
391 btnChooseFolder.setOnClickListener(this);
392
393 Button btnNewFolder = (Button) findViewById(R.id.uploader_new_folder);
394 btnNewFolder.setOnClickListener(this);
395
396 mListView.setOnItemClickListener(this);
397 }
398 }
399
400 private String generatePath(Stack<String> dirs) {
401 String full_path = "";
402
403 for (String a : dirs)
404 full_path += a + "/";
405 return full_path;
406 }
407
408 private void prepareStreamsToUpload() {
409 if (getIntent().getAction().equals(Intent.ACTION_SEND)) {
410 mStreamsToUpload = new ArrayList<Parcelable>();
411 mStreamsToUpload.add(getIntent().getParcelableExtra(Intent.EXTRA_STREAM));
412 } else if (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
413 mStreamsToUpload = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);
414 }
415 }
416
417 private boolean somethingToUpload() {
418 return (mStreamsToUpload != null && mStreamsToUpload.get(0) != null);
419 }
420
421 public void uploadFiles() {
422 try {
423
424 ArrayList<String> local = new ArrayList<String>();
425 ArrayList<String> remote = new ArrayList<String>();
426
427 // this checks the mimeType
428 for (Parcelable mStream : mStreamsToUpload) {
429
430 Uri uri = (Uri) mStream;
431 if (uri !=null) {
432 if (uri.getScheme().equals("content")) {
433
434 String mimeType = getContentResolver().getType(uri);
435
436 if (mimeType.contains("image")) {
437 String[] CONTENT_PROJECTION = { Images.Media.DATA,
438 Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE,
439 Images.Media.SIZE};
440 Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null,
441 null, null);
442 c.moveToFirst();
443 int index = c.getColumnIndex(Images.Media.DATA);
444 String data = c.getString(index);
445 local.add(data);
446 remote.add(mUploadPath +
447 c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME)));
448
449 }
450 else if (mimeType.contains("video")) {
451 String[] CONTENT_PROJECTION = { Video.Media.DATA,
452 Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE,
453 Video.Media.SIZE, Video.Media.DATE_MODIFIED };
454 Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null,
455 null, null);
456 c.moveToFirst();
457 int index = c.getColumnIndex(Video.Media.DATA);
458 String data = c.getString(index);
459 local.add(data);
460 remote.add(mUploadPath +
461 c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME)));
462
463 }
464 else if (mimeType.contains("audio")) {
465 String[] CONTENT_PROJECTION = { Audio.Media.DATA,
466 Audio.Media.DISPLAY_NAME, Audio.Media.MIME_TYPE,
467 Audio.Media.SIZE };
468 Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null,
469 null, null);
470 c.moveToFirst();
471 int index = c.getColumnIndex(Audio.Media.DATA);
472 String data = c.getString(index);
473 local.add(data);
474 remote.add(mUploadPath +
475 c.getString(c.getColumnIndex(Audio.Media.DISPLAY_NAME)));
476
477 }
478 else {
479 String filePath = Uri.decode(uri.toString()).replace(uri.getScheme() +
480 "://", "");
481 // cut everything whats before mnt. It occurred to me that sometimes
482 // apps send their name into the URI
483 if (filePath.contains("mnt")) {
484 String splitedFilePath[] = filePath.split("/mnt");
485 filePath = splitedFilePath[1];
486 }
487 final File file = new File(filePath);
488 local.add(file.getAbsolutePath());
489 remote.add(mUploadPath + file.getName());
490 }
491
492 } else if (uri.getScheme().equals("file")) {
493 String filePath = Uri.decode(uri.toString()).replace(uri.getScheme() +
494 "://", "");
495 if (filePath.contains("mnt")) {
496 String splitedFilePath[] = filePath.split("/mnt");
497 filePath = splitedFilePath[1];
498 }
499 final File file = new File(filePath);
500 local.add(file.getAbsolutePath());
501 remote.add(mUploadPath + file.getName());
502 }
503 else {
504 throw new SecurityException();
505 }
506 }
507 else {
508 throw new SecurityException();
509 }
510
511 Intent intent = new Intent(getApplicationContext(), FileUploader.class);
512 intent.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
513 intent.putExtra(FileUploader.KEY_LOCAL_FILE, local.toArray(new String[local.size()]));
514 intent.putExtra(FileUploader.KEY_REMOTE_FILE,
515 remote.toArray(new String[remote.size()]));
516 intent.putExtra(FileUploader.KEY_ACCOUNT, getAccount());
517 startService(intent);
518
519 //Save the path to shared preferences
520 SharedPreferences.Editor appPrefs = PreferenceManager
521 .getDefaultSharedPreferences(getApplicationContext()).edit();
522 appPrefs.putString("last_upload_path", mUploadPath);
523 appPrefs.apply();
524
525 finish();
526 }
527
528 } catch (SecurityException e) {
529 String message = String.format(getString(R.string.uploader_error_forbidden_content),
530 getString(R.string.app_name));
531 Toast.makeText(this, message, Toast.LENGTH_LONG).show();
532 }
533 }
534
535 @Override
536 public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
537 super.onRemoteOperationFinish(operation, result);
538
539
540 if (operation instanceof CreateFolderOperation) {
541 onCreateFolderOperationFinish((CreateFolderOperation)operation, result);
542 }
543
544 }
545
546 /**
547 * Updates the view associated to the activity after the finish of an operation
548 * trying create a new folder
549 *
550 * @param operation Creation operation performed.
551 * @param result Result of the creation.
552 */
553 private void onCreateFolderOperationFinish(CreateFolderOperation operation,
554 RemoteOperationResult result) {
555 if (result.isSuccess()) {
556 dismissLoadingDialog();
557 populateDirectoryList();
558 } else {
559 dismissLoadingDialog();
560 try {
561 Toast msg = Toast.makeText(this,
562 ErrorMessageAdapter.getErrorCauseMessage(result, operation, getResources()),
563 Toast.LENGTH_LONG);
564 msg.show();
565
566 } catch (NotFoundException e) {
567 Log_OC.e(TAG, "Error while trying to show fail message " , e);
568 }
569 }
570 }
571
572
573 /**
574 * Loads the target folder initialize shown to the user.
575 *
576 * The target account has to be chosen before this method is called.
577 */
578 private void initTargetFolder() {
579 if (getStorageManager() == null) {
580 throw new IllegalStateException("Do not call this method before " +
581 "initializing mStorageManager");
582 }
583
584 SharedPreferences appPreferences = PreferenceManager
585 .getDefaultSharedPreferences(getApplicationContext());
586
587 String last_path = appPreferences.getString("last_upload_path", "");
588 // "/" equals root-directory
589 if(last_path.equals("/")) {
590 mParents.add("");
591 }
592 else{
593 String[] dir_names = last_path.split("/");
594 for (String dir : dir_names)
595 mParents.add(dir);
596 }
597 //Make sure that path still exists, if it doesn't pop the stack and try the previous path
598 while(!getStorageManager().fileExists(generatePath(mParents)) && mParents.size() > 1){
599 mParents.pop();
600 }
601 }
602
603
604 @Override
605 public boolean onOptionsItemSelected(MenuItem item) {
606 boolean retval = true;
607 switch (item.getItemId()) {
608 case android.R.id.home: {
609 if((mParents.size() > 1)) {
610 onBackPressed();
611 }
612 break;
613 }
614 default:
615 retval = super.onOptionsItemSelected(item);
616 }
617 return retval;
618 }
619
620
621 }