|
@ -2,10 +2,11 @@ package gr.thmmy.mthmmy.activities.upload; |
|
|
|
|
|
|
|
|
import android.app.Activity; |
|
|
import android.app.Activity; |
|
|
import android.content.Context; |
|
|
import android.content.Context; |
|
|
import android.content.DialogInterface; |
|
|
|
|
|
import android.content.Intent; |
|
|
import android.content.Intent; |
|
|
import android.content.SharedPreferences; |
|
|
import android.content.SharedPreferences; |
|
|
|
|
|
import android.content.pm.ResolveInfo; |
|
|
import android.database.Cursor; |
|
|
import android.database.Cursor; |
|
|
|
|
|
import android.graphics.Bitmap; |
|
|
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; |
|
@ -42,8 +43,12 @@ import java.io.File; |
|
|
import java.io.FileOutputStream; |
|
|
import java.io.FileOutputStream; |
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStream; |
|
|
import java.io.InputStream; |
|
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
import java.util.Arrays; |
|
|
import java.util.Arrays; |
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Locale; |
|
|
|
|
|
|
|
|
import gr.thmmy.mthmmy.R; |
|
|
import gr.thmmy.mthmmy.R; |
|
|
import gr.thmmy.mthmmy.base.BaseActivity; |
|
|
import gr.thmmy.mthmmy.base.BaseActivity; |
|
@ -134,46 +139,62 @@ public class UploadActivity extends BaseActivity { |
|
|
rootCategorySpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener(uploadRootCategories)); |
|
|
rootCategorySpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener(uploadRootCategories)); |
|
|
|
|
|
|
|
|
titleDescriptionBuilderButton = findViewById(R.id.upload_title_description_builder); |
|
|
titleDescriptionBuilderButton = findViewById(R.id.upload_title_description_builder); |
|
|
titleDescriptionBuilderButton.setOnClickListener(new View.OnClickListener() { |
|
|
titleDescriptionBuilderButton.setOnClickListener(view -> { |
|
|
@Override |
|
|
if (categorySelected.equals("-1")) { |
|
|
public void onClick(View view) { |
|
|
Toast.makeText(view.getContext(), "Please choose category first", Toast.LENGTH_SHORT).show(); |
|
|
if (categorySelected.equals("-1")) { |
|
|
return; |
|
|
Toast.makeText(view.getContext(), "Please choose category first", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int numberOfSpinners = categoriesSpinners.getChildCount(); |
|
|
int numberOfSpinners = categoriesSpinners.getChildCount(); |
|
|
|
|
|
|
|
|
if (numberOfSpinners < 3) { |
|
|
if (numberOfSpinners < 3) { |
|
|
|
|
|
Toast.makeText(view.getContext(), "Please choose a course category", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String maybeSemester = "", maybeCourse = ""; |
|
|
|
|
|
|
|
|
|
|
|
if (numberOfSpinners == 5) { |
|
|
|
|
|
if (((AppCompatSpinnerWithoutDefault) categoriesSpinners.getChildAt(numberOfSpinners - 1)). |
|
|
|
|
|
getSelectedItemPosition() == -1) { |
|
|
|
|
|
maybeSemester = (String) ((AppCompatSpinnerWithoutDefault) |
|
|
|
|
|
categoriesSpinners.getChildAt(numberOfSpinners - 4)).getSelectedItem(); |
|
|
|
|
|
maybeCourse = (String) ((AppCompatSpinnerWithoutDefault) |
|
|
|
|
|
categoriesSpinners.getChildAt(numberOfSpinners - 2)).getSelectedItem(); |
|
|
|
|
|
} else { |
|
|
Toast.makeText(view.getContext(), "Please choose a course category", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(view.getContext(), "Please choose a course category", Toast.LENGTH_SHORT).show(); |
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
} else if (numberOfSpinners == 4) { |
|
|
String maybeSemester = (String) ((AppCompatSpinnerWithoutDefault) |
|
|
maybeSemester = (String) ((AppCompatSpinnerWithoutDefault) |
|
|
|
|
|
categoriesSpinners.getChildAt(numberOfSpinners - 3)).getSelectedItem(); |
|
|
|
|
|
maybeCourse = (String) ((AppCompatSpinnerWithoutDefault) |
|
|
|
|
|
categoriesSpinners.getChildAt(numberOfSpinners - 1)).getSelectedItem(); |
|
|
|
|
|
} else { |
|
|
|
|
|
maybeSemester = (String) ((AppCompatSpinnerWithoutDefault) |
|
|
categoriesSpinners.getChildAt(numberOfSpinners - 2)).getSelectedItem(); |
|
|
categoriesSpinners.getChildAt(numberOfSpinners - 2)).getSelectedItem(); |
|
|
String maybeCourse = (String) ((AppCompatSpinnerWithoutDefault) |
|
|
maybeCourse = (String) ((AppCompatSpinnerWithoutDefault) |
|
|
categoriesSpinners.getChildAt(numberOfSpinners - 1)).getSelectedItem(); |
|
|
categoriesSpinners.getChildAt(numberOfSpinners - 1)).getSelectedItem(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (!maybeSemester.contains("εξάμηνο") && !maybeSemester.contains("Εξάμηνο")) { |
|
|
if (!maybeSemester.contains("εξάμηνο") && !maybeSemester.contains("Εξάμηνο")) { |
|
|
Toast.makeText(view.getContext(), "Please choose a course category", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(view.getContext(), "Please choose a course category", Toast.LENGTH_SHORT).show(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
if (maybeCourse == null) { |
|
|
if (maybeCourse == null) { |
|
|
Toast.makeText(view.getContext(), "Please choose a course", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(view.getContext(), "Please choose a course", Toast.LENGTH_SHORT).show(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//Fixes course and semester
|
|
|
//Fixes course and semester
|
|
|
maybeCourse = maybeCourse.replaceAll("-", "").replace("(ΝΠΣ)", "").trim(); |
|
|
maybeCourse = maybeCourse.replaceAll("-", "").replace("(ΝΠΣ)", "").trim(); |
|
|
maybeSemester = maybeSemester.replaceAll("-", "").trim().substring(0, 1); |
|
|
maybeSemester = maybeSemester.replaceAll("-", "").trim().substring(0, 1); |
|
|
|
|
|
|
|
|
Intent intent = new Intent(UploadActivity.this, UploadFieldsBuilderActivity.class); |
|
|
Intent intent = new Intent(UploadActivity.this, UploadFieldsBuilderActivity.class); |
|
|
Bundle extras = new Bundle(); |
|
|
Bundle builderExtras = new Bundle(); |
|
|
extras.putString(BUNDLE_UPLOAD_FIELD_BUILDER_COURSE, maybeCourse); |
|
|
builderExtras.putString(BUNDLE_UPLOAD_FIELD_BUILDER_COURSE, maybeCourse); |
|
|
extras.putString(BUNDLE_UPLOAD_FIELD_BUILDER_SEMESTER, maybeSemester); |
|
|
builderExtras.putString(BUNDLE_UPLOAD_FIELD_BUILDER_SEMESTER, maybeSemester); |
|
|
intent.putExtras(extras); |
|
|
intent.putExtras(builderExtras); |
|
|
startActivityForResult(intent, REQUEST_CODE_FIELDS_BUILDER); |
|
|
startActivityForResult(intent, REQUEST_CODE_FIELDS_BUILDER); |
|
|
} |
|
|
|
|
|
}); |
|
|
}); |
|
|
titleDescriptionBuilderButton.setEnabled(false); |
|
|
titleDescriptionBuilderButton.setEnabled(false); |
|
|
|
|
|
|
|
@ -183,128 +204,132 @@ public class UploadActivity extends BaseActivity { |
|
|
selectFileButton = findViewById(R.id.upload_select_file_button); |
|
|
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); |
|
|
selectFileButton.setCompoundDrawablesRelativeWithIntrinsicBounds(selectStartDrawable, null, null, null); |
|
|
selectFileButton.setCompoundDrawablesRelativeWithIntrinsicBounds(selectStartDrawable, null, null, null); |
|
|
selectFileButton.setOnClickListener(new View.OnClickListener() { |
|
|
selectFileButton.setOnClickListener(v -> { |
|
|
@Override |
|
|
final CharSequence[] options = {"Take photo", "Choose file", |
|
|
public void onClick(View v) { |
|
|
"Cancel"}; |
|
|
final CharSequence[] options = {"Take photo", "Choose file", |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(UploadActivity.this); |
|
|
"Cancel"}; |
|
|
builder.setTitle("Upload file"); |
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(UploadActivity.this); |
|
|
builder.setItems(options, (dialog, item) -> { |
|
|
builder.setTitle("Upload file"); |
|
|
if (options[item].equals("Take photo")) { |
|
|
builder.setItems(options, new DialogInterface.OnClickListener() { |
|
|
/*Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
|
|
@Override |
|
|
startActivityForResult(intent, REQUEST_CODE_CAMERA);*/ |
|
|
public void onClick(DialogInterface dialog, int item) { |
|
|
|
|
|
if (options[item].equals("Take photo")) { |
|
|
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
|
|
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
|
|
takePhotoIntent.putExtra("return-data", true); |
|
|
startActivityForResult(intent, REQUEST_CODE_CAMERA); |
|
|
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(UploadsHelper.getTempFile(this))); |
|
|
} else if (options[item].equals("Choose file")) { |
|
|
|
|
|
String[] mimeTypes = {"image/jpeg", "text/html", "image/png", "image/jpg", "image/gif", |
|
|
Intent targetedIntent = new Intent(takePhotoIntent); |
|
|
"application/pdf", "application/rar", "application/x-tar", "application/zip", |
|
|
List<ResolveInfo> resInfo = this.getPackageManager().queryIntentActivities(takePhotoIntent, 0); |
|
|
"application/msword", "image/vnd.djvu", "application/gz", "application/tar.gz"}; |
|
|
for (ResolveInfo resolveInfo : resInfo) { |
|
|
|
|
|
String packageName = resolveInfo.activityInfo.packageName; |
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT) |
|
|
targetedIntent.setPackage(packageName); |
|
|
//.setType("*/*")
|
|
|
|
|
|
.setType("image/jpeg") |
|
|
|
|
|
.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes); |
|
|
|
|
|
|
|
|
|
|
|
startActivityForResult(intent, REQUEST_CODE_CHOOSE_FILE); |
|
|
|
|
|
} else if (options[item].equals("Cancel")) { |
|
|
|
|
|
dialog.dismiss(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
startActivityForResult(takePhotoIntent, REQUEST_CODE_CAMERA); |
|
|
builder.show(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
findViewById(R.id.upload_upload_button).setOnClickListener(new View.OnClickListener() { |
|
|
} else if (options[item].equals("Choose file")) { |
|
|
@Override |
|
|
String[] mimeTypes = {"image/jpeg", "text/html", "image/png", "image/jpg", "image/gif", |
|
|
public void onClick(View view) { |
|
|
"application/pdf", "application/rar", "application/x-tar", "application/zip", |
|
|
String uploadTitleText = uploadTitle.getText().toString(); |
|
|
"application/msword", "image/vnd.djvu", "application/gz", "application/tar.gz"}; |
|
|
String uploadDescriptionText = uploadDescription.getText().toString(); |
|
|
|
|
|
|
|
|
|
|
|
if (uploadTitleText.equals("")) { |
|
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT) |
|
|
uploadTitle.setError("Required"); |
|
|
//.setType("*/*")
|
|
|
} |
|
|
.setType("image/jpeg") |
|
|
if (fileUri == null) { |
|
|
.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes); |
|
|
selectFileButton.setError("Required"); |
|
|
|
|
|
} |
|
|
|
|
|
if (categorySelected.equals("-1")) { |
|
|
|
|
|
Toast.makeText(view.getContext(), "Please choose category first", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (categorySelected.equals("-1") || uploadTitleText.equals("") || fileUri == null) { |
|
|
startActivityForResult(intent, REQUEST_CODE_CHOOSE_FILE); |
|
|
return; |
|
|
} else if (options[item].equals("Cancel")) { |
|
|
|
|
|
dialog.dismiss(); |
|
|
} |
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
builder.show(); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
String tmpDescriptionText = uploadDescriptionText; |
|
|
findViewById(R.id.upload_upload_button).setOnClickListener(view -> { |
|
|
|
|
|
String uploadTitleText = uploadTitle.getText().toString(); |
|
|
|
|
|
String uploadDescriptionText = uploadDescription.getText().toString(); |
|
|
|
|
|
|
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(view.getContext()); |
|
|
if (uploadTitleText.equals("")) { |
|
|
if (sharedPrefs.getBoolean(UPLOADING_APP_SIGNATURE_ENABLE_KEY, true)) { |
|
|
uploadTitle.setError("Required"); |
|
|
tmpDescriptionText += uploadedFrommThmmyPromptHtml; |
|
|
} |
|
|
} |
|
|
if (fileUri == null) { |
|
|
|
|
|
selectFileButton.setError("Required"); |
|
|
|
|
|
} |
|
|
|
|
|
if (categorySelected.equals("-1")) { |
|
|
|
|
|
Toast.makeText(view.getContext(), "Please choose category first", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
String tempFilePath = null; |
|
|
if (categorySelected.equals("-1") || uploadTitleText.equals("") || fileUri == null) { |
|
|
if (uploadFilename != null) { |
|
|
return; |
|
|
tempFilePath = createTempFile(uploadFilename); |
|
|
} |
|
|
if (tempFilePath == null) { |
|
|
|
|
|
//Something went wrong, abort
|
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
String tmpDescriptionText = uploadDescriptionText; |
|
|
final String finalTempFilePath = tempFilePath; |
|
|
|
|
|
new MultipartUploadRequest(view.getContext(), uploadIndexUrl) |
|
|
|
|
|
.setUtf8Charset() |
|
|
|
|
|
.addParameter("tp-dluploadtitle", uploadTitleText) |
|
|
|
|
|
.addParameter("tp-dluploadcat", categorySelected) |
|
|
|
|
|
.addParameter("tp-dluploadtext", tmpDescriptionText) |
|
|
|
|
|
.addFileToUpload(tempFilePath == null |
|
|
|
|
|
? fileUri.toString() |
|
|
|
|
|
: tempFilePath |
|
|
|
|
|
, "tp-dluploadfile") |
|
|
|
|
|
.addParameter("tp_dluploadicon", fileIcon) |
|
|
|
|
|
.addParameter("tp-uploaduser", uploaderProfileIndex) |
|
|
|
|
|
.setNotificationConfig(new UploadNotificationConfig()) |
|
|
|
|
|
.setMaxRetries(2) |
|
|
|
|
|
.setDelegate(new UploadStatusDelegate() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onProgress(Context context, UploadInfo uploadInfo) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(view.getContext()); |
|
|
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, |
|
|
if (sharedPrefs.getBoolean(UPLOADING_APP_SIGNATURE_ENABLE_KEY, true)) { |
|
|
Exception exception) { |
|
|
tmpDescriptionText += uploadedFrommThmmyPromptHtml; |
|
|
Toast.makeText(context, "Upload failed", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
if (finalTempFilePath != null) { |
|
|
|
|
|
if (!deleteTempFile(finalTempFilePath)) { |
|
|
String tempFilePath = null; |
|
|
Toast.makeText(context, "Failed to delete temp file", Toast.LENGTH_SHORT).show(); |
|
|
if (uploadFilename != null) { |
|
|
} |
|
|
tempFilePath = UploadsHelper.createTempFile(this, fileUri, uploadFilename); |
|
|
|
|
|
if (tempFilePath == null) { |
|
|
|
|
|
//Something went wrong, abort
|
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
final String finalTempFilePath = tempFilePath; |
|
|
|
|
|
new MultipartUploadRequest(view.getContext(), uploadIndexUrl) |
|
|
|
|
|
.setUtf8Charset() |
|
|
|
|
|
.addParameter("tp-dluploadtitle", uploadTitleText) |
|
|
|
|
|
.addParameter("tp-dluploadcat", categorySelected) |
|
|
|
|
|
.addParameter("tp-dluploadtext", tmpDescriptionText) |
|
|
|
|
|
.addFileToUpload(tempFilePath == null |
|
|
|
|
|
? fileUri.toString() |
|
|
|
|
|
: tempFilePath |
|
|
|
|
|
, "tp-dluploadfile") |
|
|
|
|
|
.addParameter("tp_dluploadicon", fileIcon) |
|
|
|
|
|
.addParameter("tp-uploaduser", uploaderProfileIndex) |
|
|
|
|
|
.setNotificationConfig(new UploadNotificationConfig()) |
|
|
|
|
|
.setMaxRetries(2) |
|
|
|
|
|
.setDelegate(new UploadStatusDelegate() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onProgress(Context context, UploadInfo uploadInfo) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, |
|
|
|
|
|
Exception exception) { |
|
|
|
|
|
Toast.makeText(context, "Upload failed", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
if (finalTempFilePath != null) { |
|
|
|
|
|
if (!UploadsHelper.deleteTempFile(finalTempFilePath)) { |
|
|
|
|
|
Toast.makeText(context, "Failed to delete temporary file", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { |
|
|
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { |
|
|
if (finalTempFilePath != null) { |
|
|
if (finalTempFilePath != null) { |
|
|
if (!deleteTempFile(finalTempFilePath)) { |
|
|
if (!UploadsHelper.deleteTempFile(finalTempFilePath)) { |
|
|
Toast.makeText(context, "Failed to delete temp file", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(context, "Failed to delete temporary file", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onCancelled(Context context, UploadInfo uploadInfo) { |
|
|
public void onCancelled(Context context, UploadInfo uploadInfo) { |
|
|
if (finalTempFilePath != null) { |
|
|
if (finalTempFilePath != null) { |
|
|
if (!deleteTempFile(finalTempFilePath)) { |
|
|
if (!UploadsHelper.deleteTempFile(finalTempFilePath)) { |
|
|
Toast.makeText(context, "Failed to delete temp file", Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(context, "Failed to delete temporary file", Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
} |
|
|
.startUpload(); |
|
|
}) |
|
|
} catch (Exception exception) { |
|
|
.startUpload(); |
|
|
Timber.e(exception, "AndroidUploadService: %s", exception.getMessage()); |
|
|
} catch (Exception exception) { |
|
|
} |
|
|
Timber.e(exception, "AndroidUploadService: %s", exception.getMessage()); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
@ -326,7 +351,7 @@ public class UploadActivity extends BaseActivity { |
|
|
if (bundleCategory != null) { |
|
|
if (bundleCategory != null) { |
|
|
int bundleSelectionIndex = -1, currentIndex = 0; |
|
|
int bundleSelectionIndex = -1, currentIndex = 0; |
|
|
for (UploadCategory category : uploadRootCategories) { |
|
|
for (UploadCategory category : uploadRootCategories) { |
|
|
if (category.getCategoryTitle().contains(bundleCategory.get(0))) { //TODO fix .contains, always false
|
|
|
if (bundleCategory.get(0).contains(category.getCategoryTitle())) { |
|
|
bundleSelectionIndex = currentIndex; |
|
|
bundleSelectionIndex = currentIndex; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
@ -335,6 +360,7 @@ public class UploadActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
if (bundleSelectionIndex != -1) { |
|
|
if (bundleSelectionIndex != -1) { |
|
|
rootCategorySpinner.setSelection(bundleSelectionIndex, true); |
|
|
rootCategorySpinner.setSelection(bundleSelectionIndex, true); |
|
|
|
|
|
bundleCategory.remove(0); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -373,7 +399,7 @@ public class UploadActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
fileUri = data.getData(); |
|
|
fileUri = data.getData(); |
|
|
if (fileUri != null) { |
|
|
if (fileUri != null) { |
|
|
String filename = filenameFromUri(fileUri); |
|
|
String filename = UploadsHelper.filenameFromUri(this, fileUri); |
|
|
selectFileButton.setText(filename); |
|
|
selectFileButton.setText(filename); |
|
|
|
|
|
|
|
|
filename = filename.toLowerCase(); |
|
|
filename = filename.toLowerCase(); |
|
@ -396,11 +422,37 @@ public class UploadActivity extends BaseActivity { |
|
|
fileIcon = "blank.gif"; |
|
|
fileIcon = "blank.gif"; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} else if (requestCode == REQUEST_CODE_CAMERA && data != null) { |
|
|
} else if (requestCode == REQUEST_CODE_CAMERA) { |
|
|
if (resultCode == Activity.RESULT_CANCELED) { |
|
|
if (resultCode == Activity.RESULT_CANCELED) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
//TODO
|
|
|
|
|
|
|
|
|
Bitmap bitmap; |
|
|
|
|
|
File cacheImageFile = UploadsHelper.getTempFile(this); |
|
|
|
|
|
if (resultCode == Activity.RESULT_OK) { |
|
|
|
|
|
fileUri = Uri.fromFile(cacheImageFile); |
|
|
|
|
|
fileIcon = "jpg_image.gif"; |
|
|
|
|
|
|
|
|
|
|
|
bitmap = UploadsHelper.getImageResized(this, fileUri); |
|
|
|
|
|
int rotation = UploadsHelper.getRotation(this, fileUri); |
|
|
|
|
|
bitmap = UploadsHelper.rotate(bitmap, rotation); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
FileOutputStream out = new FileOutputStream(cacheImageFile); |
|
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); |
|
|
|
|
|
out.flush(); |
|
|
|
|
|
out.close(); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String newFilename = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE). |
|
|
|
|
|
format(new Date()); |
|
|
|
|
|
fileUri = Uri.parse(UploadsHelper.createTempFile(this, fileUri, newFilename)); |
|
|
|
|
|
|
|
|
|
|
|
newFilename += ".jpg"; |
|
|
|
|
|
selectFileButton.setText(newFilename); |
|
|
|
|
|
} |
|
|
} else if (requestCode == REQUEST_CODE_FIELDS_BUILDER) { |
|
|
} else if (requestCode == REQUEST_CODE_FIELDS_BUILDER) { |
|
|
if (resultCode == Activity.RESULT_CANCELED) { |
|
|
if (resultCode == Activity.RESULT_CANCELED) { |
|
|
return; |
|
|
return; |
|
@ -414,82 +466,6 @@ public class UploadActivity extends BaseActivity { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
|
|
private String filenameFromUri(Uri uri) { |
|
|
|
|
|
String filename = null; |
|
|
|
|
|
if (uri.getScheme().equals("content")) { |
|
|
|
|
|
try (Cursor cursor = 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; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
private String createTempFile(String newFilename) { |
|
|
|
|
|
String oldFilename = filenameFromUri(fileUri); |
|
|
|
|
|
String fileExtension = oldFilename.substring(oldFilename.indexOf(".")); |
|
|
|
|
|
String destinationFilename = android.os.Environment.getExternalStorageDirectory().getPath() + |
|
|
|
|
|
File.separatorChar + "~tmp_mThmmy_uploads" + File.separatorChar + newFilename + fileExtension; |
|
|
|
|
|
|
|
|
|
|
|
File tempDirectory = new File(android.os.Environment.getExternalStorageDirectory().getPath() + |
|
|
|
|
|
File.separatorChar + "~tmp_mThmmy_uploads"); |
|
|
|
|
|
|
|
|
|
|
|
if (!tempDirectory.exists()) { |
|
|
|
|
|
if (!tempDirectory.mkdirs()) { |
|
|
|
|
|
Timber.w("Temporary directory build returned false in %s", UploadActivity.class.getSimpleName()); |
|
|
|
|
|
Toast.makeText(this, "Couldn't create temporary directory", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
InputStream inputStream; |
|
|
|
|
|
BufferedInputStream bufferedInputStream = null; |
|
|
|
|
|
BufferedOutputStream bufferedOutputStream = null; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
inputStream = getContentResolver().openInputStream(fileUri); |
|
|
|
|
|
if (inputStream == null) { |
|
|
|
|
|
Timber.w("Input stream was null, %s", UploadActivity.class.getSimpleName()); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bufferedInputStream = new BufferedInputStream(inputStream); |
|
|
|
|
|
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destinationFilename, false)); |
|
|
|
|
|
byte[] buf = new byte[1024]; |
|
|
|
|
|
bufferedInputStream.read(buf); |
|
|
|
|
|
do { |
|
|
|
|
|
bufferedOutputStream.write(buf); |
|
|
|
|
|
} while (bufferedInputStream.read(buf) != -1); |
|
|
|
|
|
} catch (IOException exception) { |
|
|
|
|
|
exception.printStackTrace(); |
|
|
|
|
|
} finally { |
|
|
|
|
|
try { |
|
|
|
|
|
if (bufferedInputStream != null) bufferedInputStream.close(); |
|
|
|
|
|
if (bufferedOutputStream != null) bufferedOutputStream.close(); |
|
|
|
|
|
} catch (IOException exception) { |
|
|
|
|
|
exception.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return destinationFilename; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean deleteTempFile(String destinationFilename) { |
|
|
|
|
|
File file = new File(destinationFilename); |
|
|
|
|
|
return file.delete(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener { |
|
|
private class CustomOnItemSelectedListener implements AdapterView.OnItemSelectedListener { |
|
|
private ArrayList<UploadCategory> parentCategories, childCategories; |
|
|
private ArrayList<UploadCategory> parentCategories, childCategories; |
|
|
|
|
|
|
|
@ -534,10 +510,12 @@ public class UploadActivity extends BaseActivity { |
|
|
categoriesSpinners.addView(subSpinner); |
|
|
categoriesSpinners.addView(subSpinner); |
|
|
|
|
|
|
|
|
//Sets bundle selection
|
|
|
//Sets bundle selection
|
|
|
if (bundleCategory != null && viewIndex < bundleCategory.size()) { |
|
|
if (bundleCategory != null && !bundleCategory.isEmpty()) { |
|
|
int bundleSelectionIndex = -1, currentIndex = 0; |
|
|
int bundleSelectionIndex = -1, currentIndex = 0; |
|
|
for (UploadCategory category : parentCategories) { |
|
|
|
|
|
if (category.getCategoryTitle().contains(bundleCategory.get(viewIndex))) { |
|
|
for (UploadCategory category : childCategories) { |
|
|
|
|
|
if (bundleCategory.get(0).contains(category.getCategoryTitle() |
|
|
|
|
|
.replace("-", "").trim())) { |
|
|
bundleSelectionIndex = currentIndex; |
|
|
bundleSelectionIndex = currentIndex; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
@ -546,6 +524,7 @@ public class UploadActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
if (bundleSelectionIndex != -1) { |
|
|
if (bundleSelectionIndex != -1) { |
|
|
subSpinner.setSelection(bundleSelectionIndex, true); |
|
|
subSpinner.setSelection(bundleSelectionIndex, true); |
|
|
|
|
|
bundleCategory.remove(0); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -627,8 +606,9 @@ public class UploadActivity extends BaseActivity { |
|
|
//Sets bundle selection
|
|
|
//Sets bundle selection
|
|
|
if (bundleCategory != null) { |
|
|
if (bundleCategory != null) { |
|
|
int bundleSelectionIndex = -1, currentIndex = 0; |
|
|
int bundleSelectionIndex = -1, currentIndex = 0; |
|
|
|
|
|
|
|
|
for (UploadCategory category : uploadRootCategories) { |
|
|
for (UploadCategory category : uploadRootCategories) { |
|
|
if (category.getCategoryTitle().contains(bundleCategory.get(0))) { //TODO fix .contains, always false
|
|
|
if (bundleCategory.get(0).contains(category.getCategoryTitle())) { |
|
|
bundleSelectionIndex = currentIndex; |
|
|
bundleSelectionIndex = currentIndex; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
@ -637,6 +617,7 @@ public class UploadActivity extends BaseActivity { |
|
|
|
|
|
|
|
|
if (bundleSelectionIndex != -1) { |
|
|
if (bundleSelectionIndex != -1) { |
|
|
rootCategorySpinner.setSelection(bundleSelectionIndex, true); |
|
|
rootCategorySpinner.setSelection(bundleSelectionIndex, true); |
|
|
|
|
|
bundleCategory.remove(0); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|