Browse Source

Add filename field, fixes

uploads
Apostolos Fanakis 6 years ago
parent
commit
05d79890d4
  1. 131
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java
  2. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsHelper.java
  3. 4
      app/src/main/res/drawable/ic_attach_file_white_24dp.xml
  4. 4
      app/src/main/res/drawable/ic_info_outline_warning_24dp.xml
  5. 43
      app/src/main/res/layout/activity_upload.xml
  6. 9
      app/src/main/res/layout/activity_upload_filename_info_popup.xml
  7. 4
      app/src/main/res/values/strings.xml

131
app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadActivity.java

@ -5,32 +5,31 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.OpenableColumns;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.FileProvider;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.util.Log;
import android.support.v7.widget.AppCompatImageButton;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.snatik.storage.Storage;
import net.gotev.uploadservice.MultipartUploadRequest;
import net.gotev.uploadservice.ServerResponse;
import net.gotev.uploadservice.UploadInfo;
@ -86,7 +85,6 @@ public class UploadActivity extends BaseActivity {
private static final int SELECT_FILE_METHOD_SELECTED = 1;
private static final int TAKE_PHOTO_METHOD_SELECTED = 2;
private static final String TEMPORARY_FILES_DIRECTORY = "~tmp_mThmmy_uploads";
private static final int MAX_FILE_SIZE_SUPPORTED = 45000000;
private ArrayList<UploadCategory> uploadRootCategories = new ArrayList<>();
@ -100,16 +98,15 @@ public class UploadActivity extends BaseActivity {
private File photoFileSelected = null;
private File photoFileCreated = null;
private String fileIcon;
private String generatorUploadFilename;
//UI elements
private MaterialProgressBar progressBar;
private LinearLayout categoriesSpinners;
private AppCompatSpinnerWithoutDefault rootCategorySpinner;
private EditText uploadTitle;
private EditText uploadFilename;
private EditText uploadDescription;
private AppCompatButton titleDescriptionBuilderButton;
private AppCompatTextView filenameHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -215,9 +212,50 @@ public class UploadActivity extends BaseActivity {
uploadTitle = findViewById(R.id.upload_title);
uploadDescription = findViewById(R.id.upload_description);
filenameHolder = findViewById(R.id.upload_filename);
Drawable filenameDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_attach_file_white_24dp);
filenameHolder.setCompoundDrawablesRelativeWithIntrinsicBounds(filenameDrawable, null, null, null);
AppCompatImageButton uploadFilenameInfo = findViewById(R.id.upload_filename_info);
uploadFilenameInfo.setOnClickListener(view -> {
//Inflates the popup menu content
LayoutInflater layoutInflater = (LayoutInflater) view.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (layoutInflater == null) {
return;
}
View popUpContent = layoutInflater.inflate(R.layout.activity_upload_filename_info_popup, null);
//Creates the PopupWindow
PopupWindow popUp = new PopupWindow(view.getContext());
popUp.setContentView(popUpContent);
popUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
popUp.setFocusable(true);
//((TextView) popUpContent.findViewById(R.id.upload_filename_info_text)).setTe
((TextView) popUpContent.findViewById(R.id.upload_filename_info_text)).
setMovementMethod(LinkMovementMethod.getInstance());
//Displays the popup
popUp.showAsDropDown(view);
});
uploadFilename = findViewById(R.id.upload_filename);
uploadFilename.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty() &&
!s.toString().matches("[0-9a-zA-Z~!@#$%^&()_+=\\-`\\[\\]{};',.]+")) {
uploadFilenameInfo.setImageResource(R.drawable.ic_info_outline_warning_24dp);
} else {
uploadFilenameInfo.setImageResource(R.drawable.ic_info_outline_white_24dp);
}
}
});
AppCompatButton selectFileButton = findViewById(R.id.upload_select_file_button);
Drawable selectStartDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_insert_drive_file_white_24dp);
@ -268,7 +306,7 @@ public class UploadActivity extends BaseActivity {
Toast.makeText(view.getContext(), "Please choose category first", Toast.LENGTH_SHORT).show();
shouldReturn = true;
}
if (UploadsHelper.sizeFromUri(this, fileUri) > MAX_FILE_SIZE_SUPPORTED) {
if (fileUri != null && UploadsHelper.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;
}
@ -284,20 +322,25 @@ public class UploadActivity extends BaseActivity {
uploadDescriptionText += uploadedFromThmmyPromptHtml;
}
//Checks if the generator was used
if (generatorUploadFilename != null) {
//File should be uploaded with a certain name.
//Checks if a file renaming is necessary
{
String editTextFilename = uploadFilename.getText().toString();
String selectedFileFilename = UploadsHelper.filenameFromUri(this, fileUri);
selectedFileFilename = selectedFileFilename.substring(0, selectedFileFilename.indexOf("."));
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,
generatorUploadFilename);
editTextFilename);
break;
case TAKE_PHOTO_METHOD_SELECTED:
//Renames the photo taken
String photoPath = photoFileSelected.getPath();
photoPath = photoPath.substring(0, photoPath.lastIndexOf(File.separator));
String destinationFilename = photoPath + File.separator + generatorUploadFilename + ".jpg";
String destinationFilename = photoPath + File.separator + editTextFilename + ".jpg";
if (!storage.rename(photoFileSelected.getAbsolutePath(), destinationFilename)) {
//Something went wrong, abort
@ -315,6 +358,7 @@ public class UploadActivity extends BaseActivity {
break;
}
}
}
try {
new MultipartUploadRequest(view.getContext(), uploadIndexUrl)
@ -346,17 +390,15 @@ public class UploadActivity extends BaseActivity {
UploadsHelper.deleteTempFiles();
BaseApplication.getInstance().logFirebaseAnalyticsEvent("file_upload", null);
if (uploadMethodSelected == TAKE_PHOTO_METHOD_SELECTED) {
TakePhoto.galleryAddPic(context, photoFileSelected);
}
uploadTitle.setText(null);
uploadFilename.setText(null);
uploadDescription.setText(null);
fileUri = null;
photoFileCreated = null;
photoFileSelected = null;
filenameHolder.setText(null);
filenameHolder.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
}
@ -423,6 +465,21 @@ public class UploadActivity extends BaseActivity {
return;
}
//Keeps the filename between different file selections if it has been modified
boolean hasCustomFilename = false;
{
String editTextFilename = uploadFilename.getText().toString();
String previousFilename = "";
if (fileUri != null) {
previousFilename = UploadsHelper.filenameFromUri(this, fileUri);
previousFilename = previousFilename.substring(0, previousFilename.indexOf("."));
}
if (!editTextFilename.isEmpty() && !editTextFilename.equals(previousFilename)) {
hasCustomFilename = true;
}
}
//Deletes any photo file previously created, as it is not going to be used
if (photoFileCreated != null) {
storage.deleteFile(photoFileCreated.getAbsolutePath());
@ -435,8 +492,9 @@ public class UploadActivity extends BaseActivity {
fileUri = data.getData();
if (fileUri != null) {
String filename = UploadsHelper.filenameFromUri(this, fileUri);
filenameHolder.setText(filename);
filenameHolder.setVisibility(View.VISIBLE);
if (!hasCustomFilename) {
uploadFilename.setText(filename.substring(0, filename.indexOf(".")));
}
filename = filename.toLowerCase();
if (filename.endsWith(".jpg")) {
@ -466,6 +524,21 @@ public class UploadActivity extends BaseActivity {
}
uploadMethodSelected = TAKE_PHOTO_METHOD_SELECTED;
//Keeps the filename between different file selections if it has been modified
boolean hasCustomFilename = false;
{
String editTextFilename = uploadFilename.getText().toString();
String previousFilename = "";
if (fileUri != null) {
previousFilename = UploadsHelper.filenameFromUri(this, fileUri);
previousFilename = previousFilename.substring(0, previousFilename.indexOf("."));
}
if (!editTextFilename.isEmpty() && !editTextFilename.equals(previousFilename)) {
hasCustomFilename = true;
}
}
if (photoFileSelected != null) {
storage.deleteFile(photoFileSelected.getAbsolutePath());
}
@ -473,16 +546,18 @@ public class UploadActivity extends BaseActivity {
fileUri = TakePhoto.processResult(this, photoFileSelected);
filenameHolder.setText(photoFileSelected.getName());
filenameHolder.setVisibility(View.VISIBLE);
if (!hasCustomFilename) {
uploadFilename.setText(photoFileSelected.getName().
substring(0, photoFileSelected.getName().indexOf(".")));
}
fileIcon = "jpg_image.gif";
} else if (requestCode == AFR_REQUEST_CODE_FIELDS_BUILDER) {
if (resultCode == Activity.RESULT_CANCELED) {
return;
}
generatorUploadFilename = data.getStringExtra(RESULT_FILENAME);
uploadTitle.setText(data.getStringExtra(RESULT_TITLE));
uploadFilename.setText(data.getStringExtra(RESULT_FILENAME));
uploadDescription.setText(data.getStringExtra(RESULT_DESCRIPTION));
} else {
super.onActivityResult(requestCode, resultCode, data);

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

@ -95,7 +95,7 @@ class UploadsHelper {
return filename;
}
static long sizeFromUri(Context context, Uri uri){
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));

4
app/src/main/res/drawable/ic_attach_file_white_24dp.xml

@ -1,4 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
<path android:fillColor="#FFFFFF" android:pathData="M16.5,6v11.5c0,2.21 -1.79,4 -4,4s-4,-1.79 -4,-4V5c0,-1.38 1.12,-2.5 2.5,-2.5s2.5,1.12 2.5,2.5v10.5c0,0.55 -0.45,1 -1,1s-1,-0.45 -1,-1V6H10v9.5c0,1.38 1.12,2.5 2.5,2.5s2.5,-1.12 2.5,-2.5V5c0,-2.21 -1.79,-4 -4,-4S7,2.79 7,5v12.5c0,3.04 2.46,5.5 5.5,5.5s5.5,-2.46 5.5,-5.5V6h-1.5z"/>
</vector>

4
app/src/main/res/drawable/ic_info_outline_warning_24dp.xml

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp">
<path android:fillColor="#FF9800" android:pathData="M11,17h2v-6h-2v6zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8zM11,9h2L13,7h-2v2z"/>
</vector>

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

@ -45,6 +45,7 @@
android:id="@+id/upload_spinners"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@color/background"
android:orientation="vertical">
@ -73,6 +74,36 @@
android:maxLength="500" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp"
android:layout_weight="1">
<EditText
android:id="@+id/upload_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/upload_filename"
android:inputType="text"
android:maxLength="500" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/upload_filename_info"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="12dp"
android:background="@color/background"
app:srcCompat="@drawable/ic_info_outline_white_24dp" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -97,18 +128,6 @@
android:textAlignment="center"
android:textColor="@color/accent" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/upload_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp"
android:drawablePadding="5dp"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@color/primary_text"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

9
app/src/main/res/layout/activity_upload_filename_info_popup.xml

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/upload_filename_info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:padding="8dp"
android:text="@string/upload_filename_info"
android:textColor="@color/primary_text" />

4
app/src/main/res/values/strings.xml

@ -114,12 +114,14 @@
<string name="upload_button">Upload</string>
<!--Upload Activity-->
<string name="upload_title_description_builder">Generate title and description</string>
<string name="upload_title_description_builder">Generate fields</string>
<string name="upload_title_hint">Title</string>
<string name="upload_filename">Upload as (filename)</string>
<string name="upload_description_hint">Description</string>
<string name="upload_select_file">Select file</string>
<string name="upload_take_photo">Take photo</string>
<string name="upload_spinners_hint">Select a category</string>
<string name="upload_filename_info">Please follow the filename rules described in <a href="https://www.thmmy.gr/smf/index.php?topic=34294.0">this topic</a>.</string>
<!--Upload Fields Builder Activity-->
<string name="upload_fields_builder_type_radio_buttons_title">Select type of upload</string>

Loading…
Cancel
Save