+ new CheckAvailableSpaceTask().execute();
+ }
+ }
+
+
+ /**
+ * Asynchronous task checking if there is space enough to copy all the files chosen
+ * to upload into the ownCloud local folder.
+ *
+ * Maybe an AsyncTask is not strictly necessary, but who really knows.
+ *
+ * @author David A. Velasco
+ */
+ private class CheckAvailableSpaceTask extends AsyncTask<Void, Void, Boolean> {
+
+ /**
+ * Updates the UI before trying the movement
+ */
+ @Override
+ protected void onPreExecute () {
+ /// progress dialog and disable 'Move' button
+ mCurrentDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false);
+ mCurrentDialog.show(getSupportFragmentManager(), WAIT_DIALOG_TAG);
+ }
+
+
+ /**
+ * Checks the available space
+ *
+ * @return 'True' if there is space enough.
+ */
+ @Override
+ protected Boolean doInBackground(Void... params) {
+ String[] checkedFilePaths = mFileListFragment.getCheckedFilePaths();
+ long total = 0;
+ for (int i=0; checkedFilePaths != null && i < checkedFilePaths.length ; i++) {
+ String localPath = checkedFilePaths[i];
+ File localFile = new File(localPath);
+ total += localFile.length();
+ }
+ return (FileStorageUtils.getUsableSpace(mAccountOnCreation.name) >= total);
+ }
+
+ /**
+ * Updates the activity UI after the check of space is done.
+ *
+ * If there is not space enough. shows a new dialog to query the user if wants to move the files instead
+ * of copy them.
+ *
+ * @param result 'True' when there is space enough to copy all the selected files.
+ */
+ @Override
+ protected void onPostExecute(Boolean result) {
+ mCurrentDialog.dismiss();
+ mCurrentDialog = null;
+
+ if (result) {
+ // return the list of selected files (success)
+ Intent data = new Intent();
+ data.putExtra(EXTRA_CHOSEN_FILES, mFileListFragment.getCheckedFilePaths());
+ setResult(RESULT_OK, data);
+ finish();
+
+ } else {
+ // show a dialog to query the user if wants to move the selected files to the ownCloud folder instead of copying
+ String[] args = {getString(R.string.app_name)};
+ ConfirmationDialogFragment dialog = ConfirmationDialogFragment.newInstance(R.string.upload_query_move_foreign_files, args, R.string.common_yes, -1, R.string.common_no);
+ dialog.setOnConfirmationListener(UploadFilesActivity.this);
+ dialog.show(getSupportFragmentManager(), QUERY_TO_MOVE_DIALOG_TAG);
+ }
+ }
+ }
+
+ @Override
+ public void onConfirmation(String callerTag) {
+ Log_OC.d(TAG, "Positive button in dialog was clicked; dialog tag is " + callerTag);
+ if (callerTag.equals(QUERY_TO_MOVE_DIALOG_TAG)) {
+ // return the list of selected files to the caller activity (success), signaling that they should be moved to the ownCloud folder, instead of copied