From 102a239fe118d5f58b2197cb6063fc43c067c4bb Mon Sep 17 00:00:00 2001 From: Apostolof Date: Tue, 4 Sep 2018 19:23:36 +0300 Subject: [PATCH] Filename field fixes, Temp file fix, Add grey, disabled look to filename extension --- .../activities/upload/UploadActivity.java | 71 +++++++++++++------ .../activities/upload/UploadsHelper.java | 36 +--------- .../java/gr/thmmy/mthmmy/utils/FileUtils.java | 36 +++++++++- .../java/gr/thmmy/mthmmy/utils/TakePhoto.java | 2 - app/src/main/res/layout/activity_upload.xml | 2 +- 5 files changed, 89 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java index c0c97916..b5bb5b87 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java @@ -17,9 +17,10 @@ import android.support.v7.preference.PreferenceManager; import android.support.v7.widget.AppCompatButton; import android.support.v7.widget.AppCompatImageButton; import android.text.Editable; +import android.text.Spannable; import android.text.TextWatcher; import android.text.method.LinkMovementMethod; -import android.util.Log; +import android.text.style.ForegroundColorSpan; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; @@ -240,15 +241,24 @@ public class UploadActivity extends BaseActivity { uploadFilename = findViewById(R.id.upload_filename); uploadFilename.addTextChangedListener(new TextWatcher() { - String filenameText, fileExtension; + String oldFilename, fileExtension; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { - filenameText = s.toString(); + oldFilename = s.toString(); if (fileUri != null) { - fileExtension = FileUtils.getFileExtension(UploadsHelper. + String uriExtension = FileUtils.getFileExtension(FileUtils. filenameFromUri(getApplicationContext(), fileUri)); + + if (fileExtension == null || !fileExtension.equals(uriExtension)) { + //New file selected + //Changes the extension of the saved filename instance + oldFilename = FileUtils.getFilenameWithoutExtension(oldFilename) + + uriExtension; + } + + fileExtension = uriExtension; } else { fileExtension = null; } @@ -260,22 +270,26 @@ public class UploadActivity extends BaseActivity { @Override public void afterTextChanged(Editable s) { + String filenameWithoutExtension = FileUtils.getFilenameWithoutExtension(s.toString()); + if (filenameWithoutExtension != null && !filenameWithoutExtension.isEmpty() && + !filenameWithoutExtension.matches("[0-9a-zA-Z~!@#$%^&()_+=\\-`\\[\\]{};',.]+")) { + uploadFilenameInfo.setImageResource(R.drawable.ic_info_outline_warning_24dp); + } else { + uploadFilenameInfo.setImageResource(R.drawable.ic_info_outline_white_24dp); + } + if (fileExtension == null) { return; } if (!s.toString().endsWith(fileExtension)) { - uploadFilename.setText(filenameText); + uploadFilename.setText(oldFilename); return; } - String filenameWithoutExtension = FileUtils.getFilenameWithoutExtension(s.toString()); - if (filenameWithoutExtension != null && !filenameWithoutExtension.isEmpty() && - !filenameWithoutExtension.matches("[0-9a-zA-Z~!@#$%^&()_+=\\-`\\[\\]{};',.]+")) { - uploadFilenameInfo.setImageResource(R.drawable.ic_info_outline_warning_24dp); - } else { - uploadFilenameInfo.setImageResource(R.drawable.ic_info_outline_white_24dp); - } + s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.secondary_text)), + s.length() - fileExtension.length(), s.length(), + Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } }); @@ -311,6 +325,7 @@ public class UploadActivity extends BaseActivity { progressBar.setVisibility(View.VISIBLE); String uploadTitleText = uploadTitle.getText().toString(); + String editTextFilename = uploadFilename.getText().toString(); String uploadDescriptionText = uploadDescription.getText().toString(); //Checks if all required fields are filled @@ -328,10 +343,14 @@ public class UploadActivity extends BaseActivity { Toast.makeText(view.getContext(), "Please choose category first", Toast.LENGTH_SHORT).show(); shouldReturn = true; } - if (fileUri != null && UploadsHelper.sizeFromUri(this, fileUri) > MAX_FILE_SIZE_SUPPORTED) { + if (fileUri != null && FileUtils.sizeFromUri(this, fileUri) > MAX_FILE_SIZE_SUPPORTED) { Toast.makeText(view.getContext(), "Your files are too powerful for thmmy. Reduce size or split!", Toast.LENGTH_LONG).show(); shouldReturn = true; } + if (!editTextFilename.matches("(.+\\.)+.+")){ + uploadFilename.setError("Invalid filename"); + shouldReturn = true; + } if (shouldReturn) { progressBar.setVisibility(View.GONE); return; @@ -345,16 +364,16 @@ public class UploadActivity extends BaseActivity { } //Checks if a file renaming is necessary + Uri tempFileUri = null; { - String editTextFilename = uploadFilename.getText().toString(); - String selectedFileFilename = UploadsHelper.filenameFromUri(this, fileUri); + String selectedFileFilename = FileUtils.filenameFromUri(this, fileUri); if (!editTextFilename.equals(selectedFileFilename)) { //File should be uploaded with a different name switch (uploadMethodSelected) { case SELECT_FILE_METHOD_SELECTED: //Temporarily copies the file to a another location and renames it - fileUri = UploadsHelper.createTempFile(this, storage, fileUri, + tempFileUri = UploadsHelper.createTempFile(this, storage, fileUri, FileUtils.getFilenameWithoutExtension(editTextFilename)); break; case TAKE_PHOTO_METHOD_SELECTED: @@ -388,7 +407,10 @@ public class UploadActivity extends BaseActivity { .addParameter("tp-dluploadtitle", uploadTitleText) .addParameter("tp-dluploadcat", categorySelected) .addParameter("tp-dluploadtext", uploadDescriptionText) - .addFileToUpload(fileUri.toString(), "tp-dluploadfile") + .addFileToUpload(tempFileUri == null + ? fileUri.toString() + : tempFileUri.toString() + , "tp-dluploadfile") .addParameter("tp_dluploadicon", fileIcon) .addParameter("tp-uploaduser", uploaderProfileIndex) .setNotificationConfig(new UploadNotificationConfig()) @@ -495,7 +517,7 @@ public class UploadActivity extends BaseActivity { String previousFilename = ""; if (fileUri != null) { previousFilename = FileUtils. - getFilenameWithoutExtension(UploadsHelper.filenameFromUri(this, fileUri)); + getFilenameWithoutExtension(FileUtils.filenameFromUri(this, fileUri)); } if (editTextFilename != null && !editTextFilename.isEmpty() && @@ -515,9 +537,13 @@ public class UploadActivity extends BaseActivity { fileUri = data.getData(); if (fileUri != null) { - String filename = UploadsHelper.filenameFromUri(this, fileUri); + String filename = FileUtils.filenameFromUri(this, fileUri); if (!hasCustomFilename) { uploadFilename.setText(filename); + } else { + //Causes the EditText watcher to detect the fileUri change in case the extension changed + String badHack = "stringThatIntentionallyInvalidatesFilename"; + uploadFilename.setText(badHack); } filename = filename.toLowerCase(); @@ -556,7 +582,7 @@ public class UploadActivity extends BaseActivity { String previousFilename = ""; if (fileUri != null) { previousFilename = FileUtils. - getFilenameWithoutExtension(UploadsHelper.filenameFromUri(this, fileUri)); + getFilenameWithoutExtension(FileUtils.filenameFromUri(this, fileUri)); } if (editTextFilename != null && !editTextFilename.isEmpty() && @@ -574,7 +600,12 @@ public class UploadActivity extends BaseActivity { if (!hasCustomFilename) { uploadFilename.setText(photoFileSelected.getName()); + } else { + //Causes the EditText watcher to detect the fileUri change in case the extension changed + String badHack = "stringThatIntentionallyInvalidatesFilename"; + uploadFilename.setText(badHack); } + fileIcon = "jpg_image.gif"; } else if (requestCode == AFR_REQUEST_CODE_FIELDS_BUILDER) { if (resultCode == Activity.RESULT_CANCELED) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java index 5ad0f8fe..a61fff6d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java @@ -1,11 +1,8 @@ package gr.thmmy.mthmmy.activities.upload; import android.content.Context; -import android.database.Cursor; import android.net.Uri; import android.os.Environment; -import android.provider.OpenableColumns; -import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.content.FileProvider; import android.widget.Toast; @@ -19,13 +16,14 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import gr.thmmy.mthmmy.utils.FileUtils; import timber.log.Timber; class UploadsHelper { @SuppressWarnings("ResultOfMethodCallIgnored") @Nullable static Uri createTempFile(Context context, Storage storage, Uri fileUri, String newFilename) { - String oldFilename = filenameFromUri(context, fileUri); + String oldFilename = FileUtils.filenameFromUri(context, fileUri); String fileExtension = oldFilename.substring(oldFilename.indexOf(".")); String destinationFilename = Environment.getExternalStorageDirectory().getPath() + File.separatorChar + "~tmp_mThmmy_uploads" + File.separatorChar + newFilename + fileExtension; @@ -74,36 +72,6 @@ class UploadsHelper { ".provider", storage.getFile(destinationFilename)); } - @NonNull - static String filenameFromUri(Context context, Uri uri) { - String filename = null; - if (uri.getScheme().equals("content")) { - try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) { - if (cursor != null && cursor.moveToFirst()) { - filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); - } - } - } - if (filename == null) { - filename = uri.getPath(); - int cut = filename.lastIndexOf('/'); - if (cut != -1) { - filename = filename.substring(cut + 1); - } - } - - return filename; - } - - static long sizeFromUri(Context context, @NonNull Uri uri){ - try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) { - if (cursor != null && cursor.moveToFirst()) { - return cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE)); - } - } - return -1; - } - @SuppressWarnings("ResultOfMethodCallIgnored") static void deleteTempFiles() { File tempFilesDirectory = new File(Environment.getExternalStorageDirectory().getPath() + diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/FileUtils.java b/app/src/main/java/gr/thmmy/mthmmy/utils/FileUtils.java index a4d0653d..933df627 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/FileUtils.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/FileUtils.java @@ -1,5 +1,9 @@ package gr.thmmy.mthmmy.utils; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.provider.OpenableColumns; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.webkit.MimeTypeMap; @@ -45,7 +49,37 @@ public class FileUtils { String fileExtension = getFileExtension(filename); return fileExtension == null - ? null + ? filename : filename.substring(0, filename.indexOf(fileExtension)); } + + @NonNull + public static String filenameFromUri(Context context, Uri uri) { + String filename = null; + if (uri.getScheme().equals("content")) { + try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) { + if (cursor != null && cursor.moveToFirst()) { + filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); + } + } + } + if (filename == null) { + filename = uri.getPath(); + int cut = filename.lastIndexOf('/'); + if (cut != -1) { + filename = filename.substring(cut + 1); + } + } + + return filename; + } + + public static long sizeFromUri(Context context, @NonNull Uri uri){ + try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) { + if (cursor != null && cursor.moveToFirst()) { + return cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE)); + } + } + return -1; + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/TakePhoto.java b/app/src/main/java/gr/thmmy/mthmmy/utils/TakePhoto.java index 967b523f..05adbb9f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/TakePhoto.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/TakePhoto.java @@ -11,14 +11,12 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.media.ExifInterface; -import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.content.FileProvider; -import android.util.Log; import android.widget.Toast; import java.io.File; diff --git a/app/src/main/res/layout/activity_upload.xml b/app/src/main/res/layout/activity_upload.xml index 21dfeb54..1a12fecc 100644 --- a/app/src/main/res/layout/activity_upload.xml +++ b/app/src/main/res/layout/activity_upload.xml @@ -91,7 +91,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/upload_filename" - android:inputType="text" + android:inputType="textNoSuggestions" android:maxLength="500" />