From 6a76d7e399a8e0fdf86c70576b1374628cb960f8 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 22 Jun 2019 12:48:09 +0300 Subject: [PATCH] Uploads improvements & fixes --- .../activities/upload/UploadActivity.java | 36 ++++++++----------- .../activities/upload/UploadsCourse.java | 21 ++++++++--- .../activities/upload/UploadsHelper.java | 28 +++++++-------- .../mthmmy/utils/CrashReportingTree.java | 3 +- .../java/gr/thmmy/mthmmy/utils/TakePhoto.java | 6 ++-- 5 files changed, 47 insertions(+), 47 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 31f51b8e..d6fb274a 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 @@ -457,12 +457,8 @@ public class UploadActivity extends BaseActivity { fileIcon = "archive.gif"; textWatcher.setFileExtension(".zip"); - if (!hasModifiedFilename) { - String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE).format(new Date()); - String zipFilename = "mTHMMY_" + timeStamp + ".zip"; - uploadFilename.setText(zipFilename); - hasModifiedFilename = false; - } + if (!hasModifiedFilename) + setZipUploadFilename(); for (int fileIndex = 0; fileIndex < data.getClipData().getItemCount(); ++fileIndex) { Uri newFileUri = data.getClipData().getItemAt(fileIndex).getUri(); @@ -506,12 +502,8 @@ public class UploadActivity extends BaseActivity { fileIcon = "archive.gif"; textWatcher.setFileExtension(".zip"); - if (!hasModifiedFilename) { - String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE).format(new Date()); - String zipFilename = "mTHMMY_" + timeStamp + ".zip"; - uploadFilename.setText(zipFilename); - hasModifiedFilename = false; - } + if (!hasModifiedFilename) + setZipUploadFilename(); } addFileViewToList(filename); @@ -538,12 +530,8 @@ public class UploadActivity extends BaseActivity { fileIcon = "archive.gif"; textWatcher.setFileExtension(".zip"); - if (!hasModifiedFilename) { - String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE).format(new Date()); - String zipFilename = "mTHMMY_" + timeStamp + ".zip"; - uploadFilename.setText(zipFilename); - hasModifiedFilename = false; - } + if (!hasModifiedFilename) + setZipUploadFilename(); } UploadFile newFile = new UploadFile(true, TakePhoto.processResult(this, @@ -599,6 +587,13 @@ public class UploadActivity extends BaseActivity { } } + private void setZipUploadFilename(){ + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE).format(new Date()); + String zipFilename = "mTHMMY_" + timeStamp + ".zip"; + uploadFilename.setText(zipFilename); + hasModifiedFilename = false; + } + // Should only be called after making sure permissions are granted private void takePhoto() { // Create the File where the photo should go @@ -768,8 +763,7 @@ public class UploadActivity extends BaseActivity { } @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } + public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { @@ -951,7 +945,7 @@ public class UploadActivity extends BaseActivity { if(foundUploadsCourse != null){ uploadsCourse = foundUploadsCourse; semester = retrievedSemester; - Timber.i("Selected course: %s, semester: %s", uploadsCourse.getName(), semester); + Timber.d("Selected course: %s, semester: %s", uploadsCourse.getName(), semester); titleDescriptionBuilderButton.setEnabled(true); return; } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsCourse.java b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsCourse.java index 0fbedce8..6b4af081 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsCourse.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/upload/UploadsCourse.java @@ -1,16 +1,21 @@ package gr.thmmy.mthmmy.activities.upload; +import android.os.Bundle; + import java.util.HashMap; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import gr.thmmy.mthmmy.base.BaseApplication; import timber.log.Timber; -public class UploadsCourse { +class UploadsCourse { private String name; private String minifiedName; private String greeklishName; - public UploadsCourse(String fullName, String minifiedName, String greeklishName) { + private UploadsCourse(String fullName, String minifiedName, String greeklishName) { this.name = fullName; this.minifiedName = minifiedName; this.greeklishName = greeklishName; @@ -41,7 +46,6 @@ public class UploadsCourse { static UploadsCourse findCourse(String retrievedCourse, Map uploadsCourses){ retrievedCourse = normalizeGreekNumbers(retrievedCourse); - Timber.w("AAAAAAAA %s",retrievedCourse); UploadsCourse uploadsCourse = uploadsCourses.get(retrievedCourse); if(uploadsCourse != null) return uploadsCourse; @@ -54,13 +58,20 @@ public class UploadsCourse { if(foundKey==null){ Timber.w("Couldn't find course that matches %s", retrievedCourse); - //TODO: report to Firebase for a new Course + Bundle bundle = new Bundle(); + bundle.putString("COURSE_NAME", retrievedCourse); + BaseApplication.getInstance().logFirebaseAnalyticsEvent("UNSUPPORTED_UPLOADS_COURSE", bundle); } return uploadsCourses.get(foundKey); } private static String normalizeGreekNumbers(String stringWithGreekNumbers) { - return stringWithGreekNumbers.replaceAll("Ι", "I"); + StringBuilder normalizedStrBuilder = new StringBuilder(stringWithGreekNumbers); + Pattern pattern = Pattern.compile("(Ι+)(?:\\s|\\(|\\)|$)"); + Matcher matcher = pattern.matcher(stringWithGreekNumbers); + while (matcher.find()) + normalizedStrBuilder.replace(matcher.start(1), matcher.end(1), matcher.group(1).replaceAll("Ι", "I")); + return normalizedStrBuilder.toString(); } } 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 5bb385c2..92b71cb0 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 @@ -25,26 +25,24 @@ import gr.thmmy.mthmmy.utils.FileUtils; import timber.log.Timber; public class UploadsHelper { - private final static int BUFFER = 4096; - private static final String TEMP_FILES_DIRECTORY = "~tmp_mThmmy_uploads"; + private static final int BUFFER = 4096; + private static final String TEMP_FILES_DIRECTORY = "~tmp_mTHMMY_uploads"; @SuppressWarnings("ResultOfMethodCallIgnored") @Nullable static Uri createTempFile(Context context, Storage storage, Uri fileUri, String newFilename) { String oldFilename = FileUtils.filenameFromUri(context, fileUri); - String fileExtension = oldFilename.substring(oldFilename.indexOf(".")); + String fileExtension = oldFilename.substring(oldFilename.indexOf('.')); String destinationFilename = Environment.getExternalStorageDirectory().getPath() + File.separatorChar + TEMP_FILES_DIRECTORY + File.separatorChar + newFilename + fileExtension; File tempDirectory = new File(android.os.Environment.getExternalStorageDirectory().getPath() + File.separatorChar + TEMP_FILES_DIRECTORY); - if (!tempDirectory.exists()) { - if (!tempDirectory.mkdirs()) { - Timber.w("Temporary directory build returned false in %s", UploadActivity.class.getSimpleName()); - Toast.makeText(context, "Couldn't create temporary directory", Toast.LENGTH_SHORT).show(); - return null; - } + if (!tempDirectory.exists() && !tempDirectory.mkdirs()) { + Timber.w("Temporary directory build returned false in %s", UploadActivity.class.getSimpleName()); + Toast.makeText(context, "Couldn't create temporary directory", Toast.LENGTH_SHORT).show(); + return null; } InputStream inputStream; @@ -82,28 +80,26 @@ public class UploadsHelper { } @Nullable - public static File createZipFile(@NonNull String zipFilename) { + static File createZipFile(@NonNull String zipFilename) { // Create a zip file name File zipFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + - File.separator + "mThmmy"); + File.separator + "mTHMMY"); - if (!zipFolder.exists()) { - if (!zipFolder.mkdirs()) { + if (!zipFolder.exists() && !zipFolder.mkdirs()) { Timber.w("Zip folder build returned false in %s", UploadsHelper.class.getSimpleName()); return null; - } } return new File(zipFolder, zipFilename); } - public static void zip(Context context, Uri[] files, Uri zipFile) { + static void zip(Context context, Uri[] files, Uri zipFile) { try { BufferedInputStream origin; OutputStream dest = context.getContentResolver().openOutputStream(zipFile); assert dest != null; ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); - byte data[] = new byte[BUFFER]; + byte[] data = new byte[BUFFER]; for (Uri file : files) { InputStream inputStream = context.getContentResolver().openInputStream(file); diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java b/app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java index c4674134..8f85459a 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/CrashReportingTree.java @@ -27,8 +27,7 @@ public class CrashReportingTree extends DebugTree { Crashlytics.log(level + "/" + tag + ": " + message); - if(priority == Log.ERROR) - { + if(priority == Log.ERROR) { if (t!=null) Crashlytics.logException(t); else 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 1ac9c6a8..f1d7d990 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/TakePhoto.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/TakePhoto.java @@ -32,7 +32,7 @@ import timber.log.Timber; public class TakePhoto { private static final int DEFAULT_MIN_WIDTH_QUALITY = 400; - private static final String IMAGE_CONTENT_DESCRIPTION = "mThmmy uploads image"; + private static final String IMAGE_CONTENT_DESCRIPTION = "mTHMMY uploads image"; @Nullable public static Intent getIntent(Context context, @NonNull File photoFile) { @@ -81,10 +81,10 @@ public class TakePhoto { public static File createImageFile(Context context) { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.FRANCE).format(new Date()); - String imageFileName = "mThmmy_" + timeStamp + ".jpg"; + String imageFileName = "mTHMMY_" + timeStamp + ".jpg"; File imageFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + - File.separator + "mThmmy"); + File.separator + "mTHMMY"); if (!imageFolder.exists()) { if (!imageFolder.mkdirs()) {