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