Get FileProcessor working by passing context, Uri, etc

This commit is contained in:
misaochan 2018-05-25 23:50:20 +10:00
parent af51ffad86
commit 89db8847bf
2 changed files with 55 additions and 35 deletions

View file

@ -1,12 +1,16 @@
package fr.free.nrw.commons.upload; package fr.free.nrw.commons.upload;
import android.app.Activity; import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import java.io.File; import java.io.File;
@ -27,38 +31,37 @@ import static fr.free.nrw.commons.upload.FileUtils.getSHA1;
public class FileProcessor { public class FileProcessor {
private Uri mediaUri; private Uri mediaUri;
private ContentResolver contentResolver;
private GPSExtractor imageObj;
private SharedPreferences prefs;
private Context context;
private String decimalCoords;
private boolean haveCheckedForOtherImages = false;
private String filePath;
private boolean useExtStorage;
public FileProcessor(Uri mediaUri) { FileProcessor(Uri mediaUri, ContentResolver contentResolver, SharedPreferences prefs, Context context) {
this.mediaUri = mediaUri; this.mediaUri = mediaUri;
this.contentResolver = contentResolver;
this.prefs = prefs;
this.context = context;
useExtStorage = prefs.getBoolean("useExternalStorage", true);
} }
/**
* Calls the async task that detects if image is fuzzy, too dark, etc
*/
private void detectUnwantedPictures() {
String imageMediaFilePath = FileUtils.getPath(this,mediaUri);
DetectUnwantedPicturesAsync detectUnwantedPicturesAsync
= new DetectUnwantedPicturesAsync(new WeakReference<Activity>(this), imageMediaFilePath);
detectUnwantedPicturesAsync.execute();
}
/** /**
* Gets file path from media URI. * 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. * 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 * @return file path of media
*/ */
@Nullable @Nullable
private String getPathOfMediaOrCopy() { String getPathOfMediaOrCopy() {
String filePath = FileUtils.getPath(getApplicationContext(), mediaUri); filePath = FileUtils.getPath(getApplicationContext(), mediaUri);
Timber.d("Filepath: " + filePath); Timber.d("Filepath: " + filePath);
if (filePath == null) { if (filePath == null) {
String copyPath = null; String copyPath = null;
try { try {
ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(mediaUri, "r"); ParcelFileDescriptor descriptor = contentResolver.openFileDescriptor(mediaUri, "r");
if (descriptor != null) { if (descriptor != null) {
boolean useExtStorage = prefs.getBoolean("useExternalStorage", true);
if (useExtStorage) { if (useExtStorage) {
copyPath = FileUtils.createCopyPath(descriptor); copyPath = FileUtils.createCopyPath(descriptor);
return copyPath; return copyPath;
@ -83,21 +86,19 @@ public class FileProcessor {
void getFileCoordinates(boolean gpsEnabled) { void getFileCoordinates(boolean gpsEnabled) {
Timber.d("Calling GPSExtractor"); Timber.d("Calling GPSExtractor");
try { try {
if (imageObj == null) {
ParcelFileDescriptor descriptor = getContentResolver().openFileDescriptor(mediaUri, "r"); ParcelFileDescriptor descriptor = contentResolver.openFileDescriptor(mediaUri, "r");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (descriptor != null) { if (descriptor != null) {
imageObj = new GPSExtractor(descriptor.getFileDescriptor(), this, prefs); imageObj = new GPSExtractor(descriptor.getFileDescriptor(), context, prefs);
} }
} else { } else {
String filePath = getPathOfMediaOrCopy(); String filePath = getPathOfMediaOrCopy();
if (filePath != null) { if (filePath != null) {
imageObj = new GPSExtractor(filePath, this, prefs); imageObj = new GPSExtractor(filePath, context, prefs);
}
} }
} }
if (imageObj != null) {
decimalCoords = imageObj.getCoords(gpsEnabled); decimalCoords = imageObj.getCoords(gpsEnabled);
if (decimalCoords == null || !imageObj.imageCoordsExists){ if (decimalCoords == null || !imageObj.imageCoordsExists){
//Find other photos taken around the same time which has gps coordinates //Find other photos taken around the same time which has gps coordinates
@ -107,19 +108,20 @@ public class FileProcessor {
else { else {
useImageCoords(); useImageCoords();
} }
}
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Timber.w("File not found: " + mediaUri, e); Timber.w("File not found: " + mediaUri, e);
} }
} }
private void findOtherImages(boolean gpsEnabled) { void findOtherImages(boolean gpsEnabled) {
Timber.d("filePath"+getPathOfMediaOrCopy()); Timber.d("filePath"+getPathOfMediaOrCopy());
String filePath = getPathOfMediaOrCopy();
long timeOfCreation = new File(filePath).lastModified();//Time when the original image was created 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(); File[] files = folder.listFiles();
Timber.d("folderTime Number:"+files.length); Timber.d("folderTime Number:"+files.length);
GPSExtractor tempImageObj;
for(File file : files){ for(File file : files){
if(file.lastModified()-timeOfCreation<=(120*1000) && file.lastModified()-timeOfCreation>=-(120*1000)){ if(file.lastModified()-timeOfCreation<=(120*1000) && file.lastModified()-timeOfCreation>=-(120*1000)){
@ -129,17 +131,17 @@ public class FileProcessor {
ParcelFileDescriptor descriptor ParcelFileDescriptor descriptor
= null; = null;
try { try {
descriptor = getContentResolver().openFileDescriptor(Uri.parse(file.getAbsolutePath()), "r"); descriptor = contentResolver.openFileDescriptor(Uri.parse(file.getAbsolutePath()), "r");
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (descriptor != null) { if (descriptor != null) {
tempImageObj = new GPSExtractor(descriptor.getFileDescriptor(),this, prefs); tempImageObj = new GPSExtractor(descriptor.getFileDescriptor(), context, prefs);
} }
} else { } else {
if (filePath != null) { if (filePath != null) {
tempImageObj = new GPSExtractor(file.getAbsolutePath(), this, prefs); tempImageObj = new GPSExtractor(file.getAbsolutePath(), context, prefs);
} }
} }
@ -149,7 +151,7 @@ public class FileProcessor {
// Current image has gps coordinates and it's not current gps locaiton // Current image has gps coordinates and it's not current gps locaiton
Timber.d("This fild has image coords:"+ file.getAbsolutePath()); Timber.d("This fild has image coords:"+ file.getAbsolutePath());
// Create a dialog fragment for the suggestion // Create a dialog fragment for the suggestion
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = (Fragment) context.getSupportFragmentManager();
SimilarImageDialogFragment newFragment = new SimilarImageDialogFragment(); SimilarImageDialogFragment newFragment = new SimilarImageDialogFragment();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("originalImagePath",filePath); args.putString("originalImagePath",filePath);

View file

@ -135,6 +135,7 @@ public class ShareActivity
private GPSExtractor imageObj; private GPSExtractor imageObj;
private GPSExtractor tempImageObj; private GPSExtractor tempImageObj;
private String decimalCoords; private String decimalCoords;
private FileProcessor fileObj;
private boolean useNewPermissions = false; private boolean useNewPermissions = false;
private boolean storagePermitted = false; private boolean storagePermitted = false;
@ -145,7 +146,7 @@ public class ShareActivity
private Snackbar snackbar; private Snackbar snackbar;
private boolean duplicateCheckPassed = false; private boolean duplicateCheckPassed = false;
private boolean haveCheckedForOtherImages = false;
private boolean isNearbyUpload = false; private boolean isNearbyUpload = false;
private Animator CurrentAnimator; private Animator CurrentAnimator;
@ -197,7 +198,7 @@ public class ShareActivity
* Gets file metadata for category suggestions, displays toast, caches categories found, calls uploadController * Gets file metadata for category suggestions, displays toast, caches categories found, calls uploadController
*/ */
private void uploadBegins() { private void uploadBegins() {
getFileCoordinates(locationPermitted); fileObj.getFileCoordinates(locationPermitted);
Toast startingToast = Toast.makeText(this, R.string.uploading_started, Toast.LENGTH_LONG); Toast startingToast = Toast.makeText(this, R.string.uploading_started, Toast.LENGTH_LONG);
startingToast.show(); startingToast.show();
@ -308,7 +309,13 @@ public class ShareActivity
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_PERM_ON_CREATE_LOCATION); REQUEST_PERM_ON_CREATE_LOCATION);
} }
FileProcessor fileObj = new FileProcessor(mediaUri);
ContentResolver contentResolver = this.getContentResolver();
fileObj = new FileProcessor(mediaUri, contentResolver, prefs, this);
String filePath = fileObj.getPathOfMediaOrCopy();
checkIfFileExists(); checkIfFileExists();
fileObj.getFileCoordinates(locationPermitted); fileObj.getFileCoordinates(locationPermitted);
@ -488,16 +495,27 @@ public class ShareActivity
imageObj = tempImageObj; imageObj = tempImageObj;
decimalCoords = imageObj.getCoords(false);// Not necessary to use gps as image already ha EXIF data decimalCoords = imageObj.getCoords(false);// Not necessary to use gps as image already ha EXIF data
Timber.d("EXIF from tempImageObj"); Timber.d("EXIF from tempImageObj");
useImageCoords(); fileObj.useImageCoords();
} }
@Override @Override
public void onNegativeResponse() { public void onNegativeResponse() {
Timber.d("EXIF from imageObj"); Timber.d("EXIF from imageObj");
useImageCoords(); fileObj.useImageCoords();
} }
/**
* Calls the async task that detects if image is fuzzy, too dark, etc
*/
private void detectUnwantedPictures() {
String imageMediaFilePath = FileUtils.getPath(this, mediaUri);
DetectUnwantedPicturesAsync detectUnwantedPicturesAsync
= new DetectUnwantedPicturesAsync(new WeakReference<Activity>(this), imageMediaFilePath);
detectUnwantedPicturesAsync.execute();
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();