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