From c9ae7176c0743844e1a6c2d8bee3979b90a8afbc Mon Sep 17 00:00:00 2001 From: misaochan Date: Mon, 4 Jun 2018 19:40:49 +1000 Subject: [PATCH] Optimize formatting and imports --- .../nrw/commons/upload/FileProcessor.java | 91 ++++++++++--------- .../nrw/commons/upload/ShareActivity.java | 22 +++-- 2 files changed, 59 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/upload/FileProcessor.java b/app/src/main/java/fr/free/nrw/commons/upload/FileProcessor.java index 3fc3e55b7..2845b8d1f 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/FileProcessor.java +++ b/app/src/main/java/fr/free/nrw/commons/upload/FileProcessor.java @@ -33,18 +33,7 @@ import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; /** * Processing of the image file that is about to be uploaded via ShareActivity is done here */ -public class FileProcessor implements SimilarImageDialogFragment.onResponse{ - - private Uri mediaUri; - private ContentResolver contentResolver; - private GPSExtractor imageObj; - private Context context; - private String decimalCoords; - private boolean haveCheckedForOtherImages = false; - private String filePath; - private boolean useExtStorage; - private boolean cacheFound; - private GPSExtractor tempImageObj; +public class FileProcessor implements SimilarImageDialogFragment.onResponse { @Inject CacheController cacheController; @@ -55,6 +44,16 @@ public class FileProcessor implements SimilarImageDialogFragment.onResponse{ @Inject @Named("default_preferences") SharedPreferences prefs; + private Uri mediaUri; + private ContentResolver contentResolver; + private GPSExtractor imageObj; + private Context context; + private String decimalCoords; + private boolean haveCheckedForOtherImages = false; + private String filePath; + private boolean useExtStorage; + private boolean cacheFound; + private GPSExtractor tempImageObj; FileProcessor(Uri mediaUri, ContentResolver contentResolver, Context context) { this.mediaUri = mediaUri; @@ -67,6 +66,7 @@ public class FileProcessor implements SimilarImageDialogFragment.onResponse{ /** * Gets file path from media URI. * In older devices getPath() may fail depending on the source URI, creating and using a copy of the file seems to work instead. + * * @return file path of media */ @Nullable @@ -97,32 +97,32 @@ public class FileProcessor implements SimilarImageDialogFragment.onResponse{ /** * Processes file coordinates, either from EXIF data or user location + * * @param gpsEnabled if true use GPS */ GPSExtractor processFileCoordinates(boolean gpsEnabled) { Timber.d("Calling GPSExtractor"); try { - ParcelFileDescriptor descriptor = contentResolver.openFileDescriptor(mediaUri, "r"); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - if (descriptor != null) { - imageObj = new GPSExtractor(descriptor.getFileDescriptor(), context, prefs); - } - } else { - String filePath = getPathOfMediaOrCopy(); - if (filePath != null) { - imageObj = new GPSExtractor(filePath, context, prefs); - } + ParcelFileDescriptor descriptor = contentResolver.openFileDescriptor(mediaUri, "r"); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + if (descriptor != null) { + imageObj = new GPSExtractor(descriptor.getFileDescriptor(), context, prefs); + } + } else { + String filePath = getPathOfMediaOrCopy(); + if (filePath != null) { + imageObj = new GPSExtractor(filePath, context, prefs); + } } - decimalCoords = imageObj.getCoords(gpsEnabled); - if (decimalCoords == null || !imageObj.imageCoordsExists){ - //Find other photos taken around the same time which has gps coordinates - if(!haveCheckedForOtherImages) - findOtherImages(gpsEnabled);// Do not do repeat the process - } - else { - useImageCoords(); - } + decimalCoords = imageObj.getCoords(gpsEnabled); + if (decimalCoords == null || !imageObj.imageCoordsExists) { + //Find other photos taken around the same time which has gps coordinates + if (!haveCheckedForOtherImages) + findOtherImages(gpsEnabled);// Do not do repeat the process + } else { + useImageCoords(); + } } catch (FileNotFoundException e) { Timber.w("File not found: " + mediaUri, e); @@ -136,21 +136,22 @@ public class FileProcessor implements SimilarImageDialogFragment.onResponse{ /** * Find other images around the same location that were taken within the last 20 sec + * * @param gpsEnabled True if GPS is enabled */ private void findOtherImages(boolean gpsEnabled) { - Timber.d("filePath"+getPathOfMediaOrCopy()); + Timber.d("filePath" + getPathOfMediaOrCopy()); long timeOfCreation = new File(filePath).lastModified();//Time when the original image was created - File folder = new File(filePath.substring(0,filePath.lastIndexOf('/'))); + File folder = new File(filePath.substring(0, filePath.lastIndexOf('/'))); File[] files = folder.listFiles(); - Timber.d("folderTime Number:"+files.length); + Timber.d("folderTime Number:" + files.length); - for(File file : files){ - if(file.lastModified()-timeOfCreation<=(120*1000) && file.lastModified()-timeOfCreation>=-(120*1000)){ + for (File file : files) { + if (file.lastModified() - timeOfCreation <= (120 * 1000) && file.lastModified() - timeOfCreation >= -(120 * 1000)) { //Make sure the photos were taken within 20seconds - Timber.d("fild date:"+file.lastModified()+ " time of creation"+timeOfCreation); + Timber.d("fild date:" + file.lastModified() + " time of creation" + timeOfCreation); tempImageObj = null;//Temporary GPSExtractor to extract coords from these photos ParcelFileDescriptor descriptor = null; try { @@ -168,17 +169,17 @@ public class FileProcessor implements SimilarImageDialogFragment.onResponse{ } } - if(tempImageObj!=null){ - Timber.d("not null fild EXIF"+tempImageObj.imageCoordsExists +" coords"+tempImageObj.getCoords(gpsEnabled)); - if(tempImageObj.getCoords(gpsEnabled)!=null && tempImageObj.imageCoordsExists){ + if (tempImageObj != null) { + Timber.d("not null fild EXIF" + tempImageObj.imageCoordsExists + " coords" + tempImageObj.getCoords(gpsEnabled)); + if (tempImageObj.getCoords(gpsEnabled) != null && tempImageObj.imageCoordsExists) { // Current image has gps coordinates and it's not current gps locaiton - Timber.d("This file has image coords:"+ file.getAbsolutePath()); + Timber.d("This file has image coords:" + file.getAbsolutePath()); SimilarImageDialogFragment newFragment = new SimilarImageDialogFragment(); Bundle args = new Bundle(); - args.putString("originalImagePath",filePath); - args.putString("possibleImagePath",file.getAbsolutePath()); + args.putString("originalImagePath", filePath); + args.putString("possibleImagePath", file.getAbsolutePath()); newFragment.setArguments(args); - newFragment.show(((AppCompatActivity)context).getSupportFragmentManager(), "dialog"); + newFragment.show(((AppCompatActivity) context).getSupportFragmentManager(), "dialog"); break; } } @@ -195,7 +196,7 @@ public class FileProcessor implements SimilarImageDialogFragment.onResponse{ public void useImageCoords() { if (decimalCoords != null) { Timber.d("Decimal coords of image: %s", decimalCoords); - Timber.d("is EXIF data present:"+imageObj.imageCoordsExists+" from findOther image"); + Timber.d("is EXIF data present:" + imageObj.imageCoordsExists + " from findOther image"); // Only set cache for this point if image has coords if (imageObj.imageCoordsExists) { diff --git a/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java b/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java index 02cb1ac27..3ae9bae96 100644 --- a/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java @@ -166,6 +166,7 @@ public class ShareActivity /** * Checks whether storage permissions need to be requested. * Permissions are needed if the file is not owned by this application, (e.g. shared from the Gallery) + * * @return true if file is not owned by this application and permission hasn't been granted beforehand */ @RequiresApi(16) @@ -211,6 +212,7 @@ public class ShareActivity /** * Send categories to modifications queue after they are selected + * * @param categories categories selected */ @Override @@ -339,9 +341,9 @@ public class ShareActivity * Function to display the zoom and map FAB */ private void showFABMenu() { - isFABOpen=true; + isFABOpen = true; - if( gpsObj != null && gpsObj.imageCoordsExists) + if (gpsObj != null && gpsObj.imageCoordsExists) mapButton.setVisibility(View.VISIBLE); zoomInButton.setVisibility(View.VISIBLE); @@ -353,8 +355,8 @@ public class ShareActivity /** * Function to close the zoom and map FAB */ - private void closeFABMenu(){ - isFABOpen=false; + private void closeFABMenu() { + isFABOpen = false; mainFab.animate().rotationBy(-180); mapButton.animate().translationY(0); zoomInButton.animate().translationY(0).setListener(new Animator.AnimatorListener() { @@ -364,7 +366,7 @@ public class ShareActivity @Override public void onAnimationEnd(Animator animator) { - if(!isFABOpen){ + if (!isFABOpen) { mapButton.setVisibility(View.GONE); zoomInButton.setVisibility(View.GONE); } @@ -382,6 +384,7 @@ public class ShareActivity /** * Checks if upload was initiated via Nearby + * * @return true if upload was initiated via Nearby */ protected boolean isNearbyUpload() { @@ -390,8 +393,9 @@ public class ShareActivity /** * Handles BOTH snackbar permission request (for location) and submit button permission request (for storage) - * @param requestCode type of request - * @param permissions permissions requested + * + * @param requestCode type of request + * @param permissions permissions requested * @param grantResults grant results */ @Override @@ -422,7 +426,7 @@ public class ShareActivity } /** - * Displays Snackbar to ask for location permissions + * Displays Snackbar to ask for location permissions */ private Snackbar requestPermissionUsingSnackBar(String rationale, final String[] perms, final int code) { Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), rationale, @@ -453,7 +457,7 @@ public class ShareActivity //image is not a duplicate, so now check if its a unwanted picture or not fileObj.detectUnwantedPictures(); } - },mwApi); + }, mwApi); fileAsyncTask.execute(); } catch (IOException e) { Timber.e(e, "IO Exception: ");