Browse Source

Filename field fixes, Temp file fix, Add grey, disabled look to filename extension

uploads
Apostolos Fanakis 6 years ago
parent
commit
102a239fe1
  1. 71
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java
  2. 36
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java
  3. 36
      app/src/main/java/gr/thmmy/mthmmy/utils/FileUtils.java
  4. 2
      app/src/main/java/gr/thmmy/mthmmy/utils/TakePhoto.java
  5. 2
      app/src/main/res/layout/activity_upload.xml

71
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.AppCompatButton;
import android.support.v7.widget.AppCompatImageButton; import android.support.v7.widget.AppCompatImageButton;
import android.text.Editable; import android.text.Editable;
import android.text.Spannable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.util.Log; import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
@ -240,15 +241,24 @@ public class UploadActivity extends BaseActivity {
uploadFilename = findViewById(R.id.upload_filename); uploadFilename = findViewById(R.id.upload_filename);
uploadFilename.addTextChangedListener(new TextWatcher() { uploadFilename.addTextChangedListener(new TextWatcher() {
String filenameText, fileExtension; String oldFilename, fileExtension;
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
filenameText = s.toString(); oldFilename = s.toString();
if (fileUri != null) { if (fileUri != null) {
fileExtension = FileUtils.getFileExtension(UploadsHelper. String uriExtension = FileUtils.getFileExtension(FileUtils.
filenameFromUri(getApplicationContext(), fileUri)); 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 { } else {
fileExtension = null; fileExtension = null;
} }
@ -260,22 +270,26 @@ public class UploadActivity extends BaseActivity {
@Override @Override
public void afterTextChanged(Editable s) { 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) { if (fileExtension == null) {
return; return;
} }
if (!s.toString().endsWith(fileExtension)) { if (!s.toString().endsWith(fileExtension)) {
uploadFilename.setText(filenameText); uploadFilename.setText(oldFilename);
return; return;
} }
String filenameWithoutExtension = FileUtils.getFilenameWithoutExtension(s.toString()); s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.secondary_text)),
if (filenameWithoutExtension != null && !filenameWithoutExtension.isEmpty() && s.length() - fileExtension.length(), s.length(),
!filenameWithoutExtension.matches("[0-9a-zA-Z~!@#$%^&()_+=\\-`\\[\\]{};',.]+")) { Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
uploadFilenameInfo.setImageResource(R.drawable.ic_info_outline_warning_24dp);
} else {
uploadFilenameInfo.setImageResource(R.drawable.ic_info_outline_white_24dp);
}
} }
}); });
@ -311,6 +325,7 @@ public class UploadActivity extends BaseActivity {
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
String uploadTitleText = uploadTitle.getText().toString(); String uploadTitleText = uploadTitle.getText().toString();
String editTextFilename = uploadFilename.getText().toString();
String uploadDescriptionText = uploadDescription.getText().toString(); String uploadDescriptionText = uploadDescription.getText().toString();
//Checks if all required fields are filled //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(); Toast.makeText(view.getContext(), "Please choose category first", Toast.LENGTH_SHORT).show();
shouldReturn = true; 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(); Toast.makeText(view.getContext(), "Your files are too powerful for thmmy. Reduce size or split!", Toast.LENGTH_LONG).show();
shouldReturn = true; shouldReturn = true;
} }
if (!editTextFilename.matches("(.+\\.)+.+")){
uploadFilename.setError("Invalid filename");
shouldReturn = true;
}
if (shouldReturn) { if (shouldReturn) {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
return; return;
@ -345,16 +364,16 @@ public class UploadActivity extends BaseActivity {
} }
//Checks if a file renaming is necessary //Checks if a file renaming is necessary
Uri tempFileUri = null;
{ {
String editTextFilename = uploadFilename.getText().toString(); String selectedFileFilename = FileUtils.filenameFromUri(this, fileUri);
String selectedFileFilename = UploadsHelper.filenameFromUri(this, fileUri);
if (!editTextFilename.equals(selectedFileFilename)) { if (!editTextFilename.equals(selectedFileFilename)) {
//File should be uploaded with a different name //File should be uploaded with a different name
switch (uploadMethodSelected) { switch (uploadMethodSelected) {
case SELECT_FILE_METHOD_SELECTED: case SELECT_FILE_METHOD_SELECTED:
//Temporarily copies the file to a another location and renames it //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)); FileUtils.getFilenameWithoutExtension(editTextFilename));
break; break;
case TAKE_PHOTO_METHOD_SELECTED: case TAKE_PHOTO_METHOD_SELECTED:
@ -388,7 +407,10 @@ public class UploadActivity extends BaseActivity {
.addParameter("tp-dluploadtitle", uploadTitleText) .addParameter("tp-dluploadtitle", uploadTitleText)
.addParameter("tp-dluploadcat", categorySelected) .addParameter("tp-dluploadcat", categorySelected)
.addParameter("tp-dluploadtext", uploadDescriptionText) .addParameter("tp-dluploadtext", uploadDescriptionText)
.addFileToUpload(fileUri.toString(), "tp-dluploadfile") .addFileToUpload(tempFileUri == null
? fileUri.toString()
: tempFileUri.toString()
, "tp-dluploadfile")
.addParameter("tp_dluploadicon", fileIcon) .addParameter("tp_dluploadicon", fileIcon)
.addParameter("tp-uploaduser", uploaderProfileIndex) .addParameter("tp-uploaduser", uploaderProfileIndex)
.setNotificationConfig(new UploadNotificationConfig()) .setNotificationConfig(new UploadNotificationConfig())
@ -495,7 +517,7 @@ public class UploadActivity extends BaseActivity {
String previousFilename = ""; String previousFilename = "";
if (fileUri != null) { if (fileUri != null) {
previousFilename = FileUtils. previousFilename = FileUtils.
getFilenameWithoutExtension(UploadsHelper.filenameFromUri(this, fileUri)); getFilenameWithoutExtension(FileUtils.filenameFromUri(this, fileUri));
} }
if (editTextFilename != null && !editTextFilename.isEmpty() && if (editTextFilename != null && !editTextFilename.isEmpty() &&
@ -515,9 +537,13 @@ public class UploadActivity extends BaseActivity {
fileUri = data.getData(); fileUri = data.getData();
if (fileUri != null) { if (fileUri != null) {
String filename = UploadsHelper.filenameFromUri(this, fileUri); String filename = FileUtils.filenameFromUri(this, fileUri);
if (!hasCustomFilename) { if (!hasCustomFilename) {
uploadFilename.setText(filename); 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(); filename = filename.toLowerCase();
@ -556,7 +582,7 @@ public class UploadActivity extends BaseActivity {
String previousFilename = ""; String previousFilename = "";
if (fileUri != null) { if (fileUri != null) {
previousFilename = FileUtils. previousFilename = FileUtils.
getFilenameWithoutExtension(UploadsHelper.filenameFromUri(this, fileUri)); getFilenameWithoutExtension(FileUtils.filenameFromUri(this, fileUri));
} }
if (editTextFilename != null && !editTextFilename.isEmpty() && if (editTextFilename != null && !editTextFilename.isEmpty() &&
@ -574,7 +600,12 @@ public class UploadActivity extends BaseActivity {
if (!hasCustomFilename) { if (!hasCustomFilename) {
uploadFilename.setText(photoFileSelected.getName()); 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"; fileIcon = "jpg_image.gif";
} else if (requestCode == AFR_REQUEST_CODE_FIELDS_BUILDER) { } else if (requestCode == AFR_REQUEST_CODE_FIELDS_BUILDER) {
if (resultCode == Activity.RESULT_CANCELED) { if (resultCode == Activity.RESULT_CANCELED) {

36
app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java

@ -1,11 +1,8 @@
package gr.thmmy.mthmmy.activities.upload; package gr.thmmy.mthmmy.activities.upload;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.provider.OpenableColumns;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider; import android.support.v4.content.FileProvider;
import android.widget.Toast; import android.widget.Toast;
@ -19,13 +16,14 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import gr.thmmy.mthmmy.utils.FileUtils;
import timber.log.Timber; import timber.log.Timber;
class UploadsHelper { class UploadsHelper {
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
@Nullable @Nullable
static Uri createTempFile(Context context, Storage storage, Uri fileUri, String newFilename) { 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 fileExtension = oldFilename.substring(oldFilename.indexOf("."));
String destinationFilename = Environment.getExternalStorageDirectory().getPath() + String destinationFilename = Environment.getExternalStorageDirectory().getPath() +
File.separatorChar + "~tmp_mThmmy_uploads" + File.separatorChar + newFilename + fileExtension; File.separatorChar + "~tmp_mThmmy_uploads" + File.separatorChar + newFilename + fileExtension;
@ -74,36 +72,6 @@ class UploadsHelper {
".provider", storage.getFile(destinationFilename)); ".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") @SuppressWarnings("ResultOfMethodCallIgnored")
static void deleteTempFiles() { static void deleteTempFiles() {
File tempFilesDirectory = new File(Environment.getExternalStorageDirectory().getPath() + File tempFilesDirectory = new File(Environment.getExternalStorageDirectory().getPath() +

36
app/src/main/java/gr/thmmy/mthmmy/utils/FileUtils.java

@ -1,5 +1,9 @@
package gr.thmmy.mthmmy.utils; 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.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
@ -45,7 +49,37 @@ public class FileUtils {
String fileExtension = getFileExtension(filename); String fileExtension = getFileExtension(filename);
return fileExtension == null return fileExtension == null
? null ? filename
: filename.substring(0, filename.indexOf(fileExtension)); : 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;
}
} }

2
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.BitmapFactory;
import android.graphics.Matrix; import android.graphics.Matrix;
import android.media.ExifInterface; import android.media.ExifInterface;
import android.media.MediaScannerConnection;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider; import android.support.v4.content.FileProvider;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import java.io.File; import java.io.File;

2
app/src/main/res/layout/activity_upload.xml

@ -91,7 +91,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/upload_filename" android:hint="@string/upload_filename"
android:inputType="text" android:inputType="textNoSuggestions"
android:maxLength="500" /> android:maxLength="500" />
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>

Loading…
Cancel
Save