@@ -32,10 +33,10 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; -import android.content.IntentFilter; import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnClickListener; import android.content.Intent; +import android.content.IntentFilter; import android.content.res.Resources.NotFoundException; import android.net.Uri; import android.os.Bundle; @@ -77,13 +78,14 @@ import com.owncloud.android.operations.common.SyncOperation; import com.owncloud.android.syncadapter.FileSyncAdapter; import com.owncloud.android.ui.adapter.ReceiveExternalFilesAdapter; +import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask; import com.owncloud.android.ui.dialog.ConfirmationDialogFragment; import com.owncloud.android.ui.dialog.CreateFolderDialogFragment; -import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask; import com.owncloud.android.ui.errorhandling.ErrorMessageAdapter; import com.owncloud.android.ui.fragment.TaskRetainerFragment; import com.owncloud.android.ui.helpers.UriUploader; import com.owncloud.android.utils.DisplayUtils; +import com.owncloud.android.utils.Extras; import com.owncloud.android.utils.FileStorageUtils; import java.io.File; @@ -113,6 +115,7 @@ public class ReceiveExternalFilesActivity extends FileActivity private LocalBroadcastManager mLocalBroadcastManager; private SyncBroadcastReceiver mSyncBroadcastReceiver; + private UploadBroadcastReceiver mUploadBroadcastReceiver; private boolean mSyncInProgress = false; private boolean mAccountSelected; private boolean mAccountSelectionShowing; @@ -160,6 +163,12 @@ protected void onCreate(Bundle savedInstanceState) { mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); mLocalBroadcastManager.registerReceiver(mSyncBroadcastReceiver, syncIntentFilter); + // Listen for upload messages + IntentFilter uploadIntentFilter = new IntentFilter(FileUploader.getUploadFinishMessage()); + uploadIntentFilter.addAction(FileUploader.getUploadStartMessage()); + mUploadBroadcastReceiver = new UploadBroadcastReceiver(); + mLocalBroadcastManager.registerReceiver(mUploadBroadcastReceiver, uploadIntentFilter); + // Init Fragment without UI to retain AsyncTask across configuration changes FragmentManager fm = getSupportFragmentManager(); TaskRetainerFragment taskRetainerFragment = @@ -169,7 +178,6 @@ protected void onCreate(Bundle savedInstanceState) { fm.beginTransaction() .add(taskRetainerFragment, TaskRetainerFragment.FTAG_TASK_RETAINER_FRAGMENT).commit(); } // else, Fragment already created and retained across configuration change - } @Override @@ -485,6 +493,13 @@ private void prepareStreamsToUpload() { if (getIntent().getAction().equals(Intent.ACTION_SEND)) { mStreamsToUpload = new ArrayList<>(); mStreamsToUpload.add(getIntent().getParcelableExtra(Intent.EXTRA_STREAM)); + if (mStreamsToUpload.get(0) != null) { + String streamToUpload = mStreamsToUpload.get(0).toString(); + if (streamToUpload.contains("/data") && streamToUpload.contains(getPackageName()) && + !streamToUpload.contains(getCacheDir().getPath())) { + finish(); + } + } } else if (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) { mStreamsToUpload = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM); } @@ -821,25 +836,22 @@ public void afterTextChanged(Editable editable) { @Override public void onShow(DialogInterface dialog) { Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); - button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - String fileName = input.getText().toString(); - String error = null; - if (fileName.length() > MAX_FILENAME_LENGTH) { - error = String.format(getString(R.string.uploader_upload_text_dialog_filename_error_length_max), MAX_FILENAME_LENGTH); - } else if (fileName.length() == 0) { - error = getString(R.string.uploader_upload_text_dialog_filename_error_empty); - } else { - fileName += ".txt"; - Uri fileUri = savePlainTextToFile(fileName); - mStreamsToUpload.clear(); - mStreamsToUpload.add(fileUri); - uploadFiles(); - } - inputLayout.setErrorEnabled(error != null); - inputLayout.setError(error); + button.setOnClickListener(view -> { + String fileName = input.getText().toString(); + String error = null; + if (fileName.length() > MAX_FILENAME_LENGTH) { + error = String.format(getString(R.string.uploader_upload_text_dialog_filename_error_length_max), MAX_FILENAME_LENGTH); + } else if (fileName.length() == 0) { + error = getString(R.string.uploader_upload_text_dialog_filename_error_empty); + } else { + fileName += ".txt"; + Uri fileUri = savePlainTextToFile(fileName); + mStreamsToUpload.clear(); + mStreamsToUpload.add(fileUri); + uploadFiles(); } + inputLayout.setErrorEnabled(error != null); + inputLayout.setError(error); }); } }); @@ -861,6 +873,7 @@ private Uri savePlainTextToFile(String fileName) { outputStream.write(content.getBytes()); outputStream.close(); uri = Uri.fromFile(tmpFile); + } catch (IOException e) { Log_OC.w(TAG, "Failed to create temp file for uploading plain text: " + e.getMessage()); } @@ -889,4 +902,18 @@ private void setFileNameFromIntent(AlertDialog alertDialog, EditText input) { window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); } } + + private class UploadBroadcastReceiver extends BroadcastReceiver { + /** + * If the upload is text shared from other apps and was successfully uploaded -> delete cache + */ + @Override + public void onReceive(Context context, Intent intent) { + boolean success = intent.getBooleanExtra(Extras.EXTRA_UPLOAD_RESULT, false); + String localPath = intent.getStringExtra(Extras.EXTRA_OLD_FILE_PATH); + if (success && localPath.contains(getCacheDir().getPath())) { + FileStorageUtils.deleteDir(getCacheDir()); + } + } + } } diff --git a/src/com/owncloud/android/ui/activity/UploadListActivity.java b/src/com/owncloud/android/ui/activity/UploadListActivity.java index 41de3582cc2..03b52ba8456 100755 --- a/src/com/owncloud/android/ui/activity/UploadListActivity.java +++ b/src/com/owncloud/android/ui/activity/UploadListActivity.java @@ -200,7 +200,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } case R.id.action_retry_uploads: TransferRequester requester = new TransferRequester(); - requester.retryFailedUploads(this, null, null); + requester.retryFailedUploads(this, null, null, false); break; case R.id.action_clear_failed_uploads: @@ -248,7 +248,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { requester.retryFailedUploads( this, account, - UploadResult.CREDENTIAL_ERROR + UploadResult.CREDENTIAL_ERROR, + false ); } } @@ -272,7 +273,7 @@ public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationRe } else { // already updated -> just retry! TransferRequester requester = new TransferRequester(); - requester.retryFailedUploads(this, account, UploadResult.CREDENTIAL_ERROR); + requester.retryFailedUploads(this, account, UploadResult.CREDENTIAL_ERROR, false); } } else { diff --git a/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java b/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java index ded8d25e5a4..6032d64ccd6 100755 --- a/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/ExpandableUploadListAdapter.java @@ -388,7 +388,7 @@ public void onClick(View v) { File file = new File(upload.getLocalPath()); if (file.exists()) { TransferRequester requester = new TransferRequester(); - requester.retry(mParentActivity, upload); + requester.retry(mParentActivity, upload, false); refreshView(); } else { final String message = String.format( diff --git a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java index 5917627be29..8493a387ea4 100644 --- a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -266,6 +266,12 @@ public void onPrepareOptionsMenu(Menu menu) { item.setVisible(false); item.setEnabled(false); } + + item = menu.findItem(R.id.action_search); + if (item != null) { + item.setVisible(false); + item.setEnabled(false); + } } diff --git a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java index f8508ecf95a..b02b0e82dba 100644 --- a/src/com/owncloud/android/ui/preview/PreviewTextFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewTextFragment.java @@ -351,6 +351,12 @@ public void onPrepareOptionsMenu(Menu menu) { item.setVisible(false); item.setEnabled(false); } + + item = menu.findItem(R.id.action_search); + if (item != null) { + item.setVisible(false); + item.setEnabled(false); + } } /** diff --git a/src/com/owncloud/android/ui/preview/PreviewVideoFragment.java b/src/com/owncloud/android/ui/preview/PreviewVideoFragment.java index dda5516ead3..3781a3c3856 100644 --- a/src/com/owncloud/android/ui/preview/PreviewVideoFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewVideoFragment.java @@ -374,6 +374,12 @@ public void onPrepareOptionsMenu(Menu menu) { item.setVisible(false); item.setEnabled(false); } + + item = menu.findItem(R.id.action_search); + if (item != null) { + item.setVisible(false); + item.setEnabled(false); + } } /** diff --git a/src/com/owncloud/android/utils/FileStorageUtils.java b/src/com/owncloud/android/utils/FileStorageUtils.java index a8ad0db0acc..5a8c029612f 100644 --- a/src/com/owncloud/android/utils/FileStorageUtils.java +++ b/src/com/owncloud/android/utils/FileStorageUtils.java @@ -35,6 +35,7 @@ import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.files.RemoteFile; import java.io.File; @@ -50,6 +51,8 @@ * Static methods to help in access to local file system. */ public class FileStorageUtils { + private static final String TAG = FileStorageUtils.class.getSimpleName(); + public static final int SORT_NAME = 0; public static final int SORT_DATE = 1; public static final int SORT_SIZE = 2; @@ -348,4 +351,24 @@ public static String getMimeTypeFromName(String path) { return (result != null) ? result : ""; } + public static boolean deleteDir(File dir) { + if (dir != null && dir.isDirectory()) { + String[] children = dir.list(); + if (children != null) { + for (int i = 0; i < children.length; i++) { + boolean success = deleteDir(new File(dir, children[i])); + if (!success) { + Log_OC.w(TAG, "File NOT deleted " + children[i]); + return false; + } else { + Log_OC.d(TAG, "File deleted " + children[i]); + } + } + } else { + return false; + } + } + + return dir.delete(); + } }