@ -5,32 +5,31 @@ import android.content.Context;
import android.content.Intent ;
import android.content.Intent ;
import android.content.SharedPreferences ;
import android.content.SharedPreferences ;
import android.content.pm.PackageManager ;
import android.content.pm.PackageManager ;
import android.database.Cursor ;
import android.graphics.drawable.Drawable ;
import android.graphics.drawable.Drawable ;
import android.net.Uri ;
import android.net.Uri ;
import android.os.AsyncTask ;
import android.os.AsyncTask ;
import android.os.Bundle ;
import android.os.Bundle ;
import android.os.Environment ;
import android.provider.OpenableColumns ;
import android.support.annotation.NonNull ;
import android.support.annotation.NonNull ;
import android.support.design.widget.FloatingActionButton ;
import android.support.design.widget.FloatingActionButton ;
import android.support.v4.content.FileProvider ;
import android.support.v4.content.FileProvider ;
import android.support.v7.content.res.AppCompatResources ;
import android.support.v7.content.res.AppCompatResources ;
import android.support.v7.preference.PreferenceManager ;
import android.support.v7.preference.PreferenceManager ;
import android.support.v7.widget.AppCompatButton ;
import android.support.v7.widget.AppCompatButton ;
import android.support.v7.widget.AppCompatTextView ;
import android.support.v7.widget.AppCompatImageButton ;
import android.util.Log ;
import android.text.Editable ;
import android.text.TextWatcher ;
import android.text.method.LinkMovementMethod ;
import android.view.LayoutInflater ;
import android.view.View ;
import android.view.View ;
import android.widget.AdapterView ;
import android.widget.AdapterView ;
import android.widget.ArrayAdapter ;
import android.widget.ArrayAdapter ;
import android.widget.EditText ;
import android.widget.EditText ;
import android.widget.LinearLayout ;
import android.widget.LinearLayout ;
import android.widget.PopupWindow ;
import android.widget.ProgressBar ;
import android.widget.ProgressBar ;
import android.widget.TextView ;
import android.widget.Toast ;
import android.widget.Toast ;
import com.snatik.storage.Storage ;
import net.gotev.uploadservice.MultipartUploadRequest ;
import net.gotev.uploadservice.MultipartUploadRequest ;
import net.gotev.uploadservice.ServerResponse ;
import net.gotev.uploadservice.ServerResponse ;
import net.gotev.uploadservice.UploadInfo ;
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 SELECT_FILE_METHOD_SELECTED = 1 ;
private static final int TAKE_PHOTO_METHOD_SELECTED = 2 ;
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 static final int MAX_FILE_SIZE_SUPPORTED = 45000000 ;
private ArrayList < UploadCategory > uploadRootCategories = new ArrayList < > ( ) ;
private ArrayList < UploadCategory > uploadRootCategories = new ArrayList < > ( ) ;
@ -100,16 +98,15 @@ public class UploadActivity extends BaseActivity {
private File photoFileSelected = null ;
private File photoFileSelected = null ;
private File photoFileCreated = null ;
private File photoFileCreated = null ;
private String fileIcon ;
private String fileIcon ;
private String generatorUploadFilename ;
//UI elements
//UI elements
private MaterialProgressBar progressBar ;
private MaterialProgressBar progressBar ;
private LinearLayout categoriesSpinners ;
private LinearLayout categoriesSpinners ;
private AppCompatSpinnerWithoutDefault rootCategorySpinner ;
private AppCompatSpinnerWithoutDefault rootCategorySpinner ;
private EditText uploadTitle ;
private EditText uploadTitle ;
private EditText uploadFilename ;
private EditText uploadDescription ;
private EditText uploadDescription ;
private AppCompatButton titleDescriptionBuilderButton ;
private AppCompatButton titleDescriptionBuilderButton ;
private AppCompatTextView filenameHolder ;
@Override
@Override
protected void onCreate ( Bundle savedInstanceState ) {
protected void onCreate ( Bundle savedInstanceState ) {
@ -215,9 +212,50 @@ public class UploadActivity extends BaseActivity {
uploadTitle = findViewById ( R . id . upload_title ) ;
uploadTitle = findViewById ( R . id . upload_title ) ;
uploadDescription = findViewById ( R . id . upload_description ) ;
uploadDescription = findViewById ( R . id . upload_description ) ;
filenameHolder = findViewById ( R . id . upload_filename ) ;
AppCompatImageButton uploadFilenameInfo = findViewById ( R . id . upload_filename_info ) ;
Drawable filenameDrawable = AppCompatResources . getDrawable ( this , R . drawable . ic_attach_file_white_24dp ) ;
uploadFilenameInfo . setOnClickListener ( view - > {
filenameHolder . setCompoundDrawablesRelativeWithIntrinsicBounds ( filenameDrawable , null , null , null ) ;
//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 ) ;
AppCompatButton selectFileButton = findViewById ( R . id . upload_select_file_button ) ;
Drawable selectStartDrawable = AppCompatResources . getDrawable ( this , R . drawable . ic_insert_drive_file_white_24dp ) ;
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 ( ) ;
Toast . makeText ( view . getContext ( ) , "Please choose category first" , Toast . LENGTH_SHORT ) . show ( ) ;
shouldReturn = true ;
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 ( ) ;
Toast . makeText ( view . getContext ( ) , "Your files are too powerful for thmmy. Reduce size or split!" , Toast . LENGTH_LONG ) . show ( ) ;
shouldReturn = true ;
shouldReturn = true ;
}
}
@ -284,35 +322,41 @@ public class UploadActivity extends BaseActivity {
uploadDescriptionText + = uploadedFromThmmyPromptHtml ;
uploadDescriptionText + = uploadedFromThmmyPromptHtml ;
}
}
//Checks if the generator was used
//Checks if a file renaming is necessary
if ( generatorUploadFilename ! = null ) {
{
//File should be uploaded with a certain name.
String editTextFilename = uploadFilename . getText ( ) . toString ( ) ;
switch ( uploadMethodSelected ) {
String selectedFileFilename = UploadsHelper . filenameFromUri ( this , fileUri ) ;
case SELECT_FILE_METHOD_SELECTED :
selectedFileFilename = selectedFileFilename . substring ( 0 , selectedFileFilename . indexOf ( "." ) ) ;
//Temporarily copies the file to a another location and renames it
fileUri = UploadsHelper . createTempFile ( this , storage , fileUri ,
if ( ! editTextFilename . equals ( selectedFileFilename ) ) {
generatorUploadFilename ) ;
//File should be uploaded with a different name
break ;
switch ( uploadMethodSelected ) {
case TAKE_PHOTO_METHOD_SELECTED :
case SELECT_FILE_METHOD_SELECTED :
//Renames the photo taken
//Temporarily copies the file to a another location and renames it
String photoPath = photoFileSelected . getPath ( ) ;
fileUri = UploadsHelper . createTempFile ( this , storage , fileUri ,
photoPath = photoPath . substring ( 0 , photoPath . lastIndexOf ( File . separator ) ) ;
editTextFilename ) ;
String destinationFilename = photoPath + File . separator + generatorUploadFilename + ".jpg" ;
break ;
case TAKE_PHOTO_METHOD_SELECTED :
if ( ! storage . rename ( photoFileSelected . getAbsolutePath ( ) , destinationFilename ) ) {
//Renames the photo taken
//Something went wrong, abort
String photoPath = photoFileSelected . getPath ( ) ;
Toast . makeText ( this , "Could not create temporary file for renaming" , Toast . LENGTH_SHORT ) . show ( ) ;
photoPath = photoPath . substring ( 0 , photoPath . lastIndexOf ( File . separator ) ) ;
progressBar . setVisibility ( View . GONE ) ;
String destinationFilename = photoPath + File . separator + editTextFilename + ".jpg" ;
return ;
}
if ( ! storage . rename ( photoFileSelected . getAbsolutePath ( ) , destinationFilename ) ) {
//Something went wrong, abort
Toast . makeText ( this , "Could not create temporary file for renaming" , Toast . LENGTH_SHORT ) . show ( ) ;
progressBar . setVisibility ( View . GONE ) ;
return ;
}
//Points photoFile and fileUri to the new copied and renamed file
//Points photoFile and fileUri to the new copied and renamed file
photoFileSelected = storage . getFile ( destinationFilename ) ;
photoFileSelected = storage . getFile ( destinationFilename ) ;
fileUri = FileProvider . getUriForFile ( this , getPackageName ( ) +
fileUri = FileProvider . getUriForFile ( this , getPackageName ( ) +
".provider" , photoFileSelected ) ;
".provider" , photoFileSelected ) ;
break ;
break ;
default :
default :
break ;
break ;
}
}
}
}
}
@ -346,17 +390,15 @@ public class UploadActivity extends BaseActivity {
UploadsHelper . deleteTempFiles ( ) ;
UploadsHelper . deleteTempFiles ( ) ;
BaseApplication . getInstance ( ) . logFirebaseAnalyticsEvent ( "file_upload" , null ) ;
BaseApplication . getInstance ( ) . logFirebaseAnalyticsEvent ( "file_upload" , null ) ;
if ( uploadMethodSelected = = TAKE_PHOTO_METHOD_SELECTED ) {
if ( uploadMethodSelected = = TAKE_PHOTO_METHOD_SELECTED ) {
TakePhoto . galleryAddPic ( context , photoFileSelected ) ;
TakePhoto . galleryAddPic ( context , photoFileSelected ) ;
}
}
uploadTitle . setText ( null ) ;
uploadTitle . setText ( null ) ;
uploadFilename . setText ( null ) ;
uploadDescription . setText ( null ) ;
uploadDescription . setText ( null ) ;
fileUri = null ;
fileUri = null ;
photoFileCreated = null ;
photoFileCreated = null ;
photoFileSelected = null ;
photoFileSelected = null ;
filenameHolder . setText ( null ) ;
filenameHolder . setVisibility ( View . GONE ) ;
progressBar . setVisibility ( View . GONE ) ;
progressBar . setVisibility ( View . GONE ) ;
}
}
@ -423,6 +465,21 @@ public class UploadActivity extends BaseActivity {
return ;
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
//Deletes any photo file previously created, as it is not going to be used
if ( photoFileCreated ! = null ) {
if ( photoFileCreated ! = null ) {
storage . deleteFile ( photoFileCreated . getAbsolutePath ( ) ) ;
storage . deleteFile ( photoFileCreated . getAbsolutePath ( ) ) ;
@ -435,8 +492,9 @@ 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 = UploadsHelper . filenameFromUri ( this , fileUri ) ;
filenameHolder . setText ( filename ) ;
if ( ! hasCustomFilename ) {
filenameHolder . setVisibility ( View . VISIBLE ) ;
uploadFilename . setText ( filename . substring ( 0 , filename . indexOf ( "." ) ) ) ;
}
filename = filename . toLowerCase ( ) ;
filename = filename . toLowerCase ( ) ;
if ( filename . endsWith ( ".jpg" ) ) {
if ( filename . endsWith ( ".jpg" ) ) {
@ -466,6 +524,21 @@ public class UploadActivity extends BaseActivity {
}
}
uploadMethodSelected = TAKE_PHOTO_METHOD_SELECTED ;
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 ) {
if ( photoFileSelected ! = null ) {
storage . deleteFile ( photoFileSelected . getAbsolutePath ( ) ) ;
storage . deleteFile ( photoFileSelected . getAbsolutePath ( ) ) ;
}
}
@ -473,16 +546,18 @@ public class UploadActivity extends BaseActivity {
fileUri = TakePhoto . processResult ( this , photoFileSelected ) ;
fileUri = TakePhoto . processResult ( this , photoFileSelected ) ;
filenameHolder . setText ( photoFileSelected . getName ( ) ) ;
if ( ! hasCustomFilename ) {
filenameHolder . setVisibility ( View . VISIBLE ) ;
uploadFilename . setText ( photoFileSelected . getName ( ) .
substring ( 0 , photoFileSelected . getName ( ) . indexOf ( "." ) ) ) ;
}
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 ) {
return ;
return ;
}
}
generatorUploadFilename = data . getStringExtra ( RESULT_FILENAME ) ;
uploadTitle . setText ( data . getStringExtra ( RESULT_TITLE ) ) ;
uploadTitle . setText ( data . getStringExtra ( RESULT_TITLE ) ) ;
uploadFilename . setText ( data . getStringExtra ( RESULT_FILENAME ) ) ;
uploadDescription . setText ( data . getStringExtra ( RESULT_DESCRIPTION ) ) ;
uploadDescription . setText ( data . getStringExtra ( RESULT_DESCRIPTION ) ) ;
} else {
} else {
super . onActivityResult ( requestCode , resultCode , data ) ;
super . onActivityResult ( requestCode , resultCode , data ) ;