Skip to content

Commit

Permalink
enable prePopulateCategoriesAndDepictionsBy for current user location
Browse files Browse the repository at this point in the history
  • Loading branch information
RitikaPahwa4444 committed Jun 25, 2023
1 parent a8e0c54 commit 7208018
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ public Observable<UploadItem> preProcessImage(UploadableFile uploadableFile, Pla
* @param uploadItem
* @return
*/
public Single<Integer> getImageQuality(UploadItem uploadItem) {
return uploadModel.getImageQuality(uploadItem);
public Single<Integer> getImageQuality(UploadItem uploadItem, LatLng location) {
return uploadModel.getImageQuality(uploadItem, location);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/fr/free/nrw/commons/upload/FileProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ class FileProcessor @Inject constructor(
// Redact EXIF data as indicated in preferences.
redactExifTags(exifInterface, getExifTagsToRedact())
Timber.d("Calling GPSExtractor")
val originalImageCoordinates = ImageCoordinates(exifInterface)
val originalImageCoordinates = ImageCoordinates(exifInterface, location)
if (originalImageCoordinates.decimalCoords == null) {
if (location == null) {
//Find other photos taken around the same time which has gps coordinates
findOtherImages(
File(filePath),
similarImageInterface
)
}
} else {
prePopulateCategoriesAndDepictionsBy(originalImageCoordinates)
}
Expand Down Expand Up @@ -166,11 +164,11 @@ class FileProcessor @Inject constructor(

private fun readImageCoordinates(file: File) =
try {
ImageCoordinates(contentResolver.openInputStream(Uri.fromFile(file))!!)
ImageCoordinates(contentResolver.openInputStream(Uri.fromFile(file))!!, null)
} catch (e: IOException) {
Timber.e(e)
try {
ImageCoordinates(file.absolutePath)
ImageCoordinates(file.absolutePath, null)
} catch (ex: IOException) {
Timber.e(ex)
null
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/fr/free/nrw/commons/upload/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import androidx.exifinterface.media.ExifInterface;

import fr.free.nrw.commons.location.LatLng;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -64,11 +65,11 @@ static String getSHA1(InputStream is) {
/**
* Get Geolocation of filePath from input filePath path
*/
static String getGeolocationOfFile(String filePath) {
static String getGeolocationOfFile(String filePath, LatLng location) {

try {
ExifInterface exifInterface = new ExifInterface(filePath);
ImageCoordinates imageObj = new ImageCoordinates(exifInterface);
ImageCoordinates imageObj = new ImageCoordinates(exifInterface, location);
if (imageObj.getDecimalCoords() != null) { // If image has geolocation information in its EXIF
return imageObj.getDecimalCoords();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.free.nrw.commons.upload;

import android.content.Context;
import fr.free.nrw.commons.location.LatLng;
import io.reactivex.Observable;
import java.io.BufferedInputStream;
import java.io.File;
Expand Down Expand Up @@ -37,8 +38,8 @@ public FileInputStream getFileInputStream(String filePath) throws FileNotFoundEx
return FileUtils.getFileInputStream(filePath);
}

public String getGeolocationOfFile(String filePath) {
return FileUtils.getGeolocationOfFile(filePath);
public String getGeolocationOfFile(String filePath, LatLng location) {
return FileUtils.getGeolocationOfFile(filePath, location);
}


Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/fr/free/nrw/commons/upload/ImageCoordinates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import java.io.InputStream
/**
* Extracts geolocation to be passed to API for category suggestions. If a picture with geolocation
* is uploaded, extract latitude and longitude from EXIF data of image.
* Otherwise, if current user location is available while using the in-app camera,
* use it to set image coordinates
*/
class ImageCoordinates internal constructor(exif: ExifInterface?) {
class ImageCoordinates internal constructor(exif: ExifInterface?, location: LatLng?) {
var decLatitude = 0.0
var decLongitude = 0.0
var imageCoordsExists = false
Expand All @@ -26,13 +28,13 @@ class ImageCoordinates internal constructor(exif: ExifInterface?) {
/**
* Construct from a stream.
*/
internal constructor(stream: InputStream) : this(ExifInterface(stream))
internal constructor(stream: InputStream, location: LatLng?) : this(ExifInterface(stream), location)
/**
* Construct from the file path of the image.
* @param path file path of the image
*/
@Throws(IOException::class)
internal constructor(path: String) : this(ExifInterface(path))
internal constructor(path: String, location: LatLng?) : this(ExifInterface(path), location)

init {
//If image has no EXIF data and user has enabled GPS setting, get user's location
Expand Down Expand Up @@ -61,6 +63,14 @@ class ImageCoordinates internal constructor(exif: ExifInterface?) {
//If image has EXIF data, extract image coords
imageCoordsExists = true
Timber.d("EXIF data has location info")
} else if (location != null) {
decLatitude = location.latitude
decLongitude = location.longitude
if (!(decLatitude == 0.0 && decLongitude == 0.0)) {
decimalCoords = "$decLatitude|$decLongitude"
imageCoordsExists = true
Timber.d("Image coordinates recorded while using in-app camera")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static fr.free.nrw.commons.utils.ImageUtils.IMAGE_OK;

import android.content.Context;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.media.MediaClient;
import fr.free.nrw.commons.nearby.Place;
import fr.free.nrw.commons.utils.ImageUtils;
Expand Down Expand Up @@ -46,7 +47,7 @@ public ImageProcessingService(FileUtilsWrapper fileUtilsWrapper,
* Check image quality before upload - checks duplicate image - checks dark image - checks
* geolocation for image - check for valid title
*/
Single<Integer> validateImage(UploadItem uploadItem) {
Single<Integer> validateImage(UploadItem uploadItem, LatLng location) {
int currentImageQuality = uploadItem.getImageQuality();
Timber.d("Current image quality is %d", currentImageQuality);
if (currentImageQuality == ImageUtils.IMAGE_KEEP) {
Expand All @@ -57,7 +58,7 @@ Single<Integer> validateImage(UploadItem uploadItem) {

return Single.zip(
checkDuplicateImage(filePath),
checkImageGeoLocation(uploadItem.getPlace(), filePath),
checkImageGeoLocation(uploadItem.getPlace(), filePath, location),
checkDarkImage(filePath),
validateItemTitle(uploadItem),
checkFBMD(filePath),
Expand Down Expand Up @@ -148,13 +149,13 @@ private Single<Integer> checkDarkImage(String filePath) {
* @param filePath file to be checked
* @return IMAGE_GEOLOCATION_DIFFERENT or IMAGE_OK
*/
private Single<Integer> checkImageGeoLocation(Place place, String filePath) {
private Single<Integer> checkImageGeoLocation(Place place, String filePath, LatLng location) {
Timber.d("Checking for image geolocation %s", filePath);
if (place == null || StringUtils.isBlank(place.getWikiDataEntityId())) {
return Single.just(ImageUtils.IMAGE_OK);
}
return Single.fromCallable(() -> filePath)
.map(fileUtilsWrapper::getGeolocationOfFile)
.flatMap(path -> Single.just(fileUtilsWrapper.getGeolocationOfFile(path, location)))
.flatMap(geoLocation -> {
if (StringUtils.isBlank(geoLocation)) {
return Single.just(ImageUtils.IMAGE_OK);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public Observable<UploadItem> preProcessImage(final UploadableFile uploadableFil
createAndAddUploadItem(uploadableFile, place, similarImageInterface, location));
}

public Single<Integer> getImageQuality(final UploadItem uploadItem) {
return imageProcessingService.validateImage(uploadItem);
public Single<Integer> getImageQuality(final UploadItem uploadItem, LatLng location) {
return imageProcessingService.validateImage(uploadItem, location);
}

private UploadItem createAndAddUploadItem(final UploadableFile uploadableFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void verifyImageQuality(int uploadItemIndex, LatLng location) {
view.showProgress(true);
compositeDisposable.add(
repository
.getImageQuality(uploadItem)
.getImageQuality(uploadItem, location)
.observeOn(mainThreadScheduler)
.subscribe(imageResult -> {
view.showProgress(false);
Expand All @@ -208,7 +208,7 @@ public void verifyImageQuality(int uploadItemIndex, LatLng location) {
view.showProgress(true);
compositeDisposable.add(
repository
.getImageQuality(uploadItem)
.getImageQuality(uploadItem, location)
.observeOn(mainThreadScheduler)
.subscribe(imageResult -> {
view.showProgress(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class u {
internal var readEXIF: EXIFReader?=null
@Mock
internal var mediaClient: MediaClient? = null
@Mock
internal var location: LatLng? = null

@InjectMocks
var imageProcessingService: ImageProcessingService? = null
Expand Down Expand Up @@ -62,7 +64,7 @@ class u {
`when`(fileUtilsWrapper!!.getSHA1(any(FileInputStream::class.java)))
.thenReturn("fileSha")

`when`(fileUtilsWrapper!!.getGeolocationOfFile(ArgumentMatchers.anyString()))
`when`(fileUtilsWrapper!!.getGeolocationOfFile(ArgumentMatchers.anyString(), location))
.thenReturn("latLng")

`when`(imageUtilsWrapper?.checkIfImageIsTooDark(ArgumentMatchers.anyString()))
Expand All @@ -88,45 +90,45 @@ class u {
@Test
fun validateImageForKeepImage() {
`when`(uploadItem.imageQuality).thenReturn(ImageUtils.IMAGE_KEEP)
val validateImage = imageProcessingService!!.validateImage(uploadItem)
val validateImage = imageProcessingService!!.validateImage(uploadItem, location)
assertEquals(ImageUtils.IMAGE_OK, validateImage.blockingGet())
}

@Test
fun validateImageForDuplicateImage() {
`when`(mediaClient!!.checkFileExistsUsingSha(ArgumentMatchers.anyString()))
.thenReturn(Single.just(true))
val validateImage = imageProcessingService!!.validateImage(uploadItem)
val validateImage = imageProcessingService!!.validateImage(uploadItem, location)
assertEquals(ImageUtils.IMAGE_DUPLICATE, validateImage.blockingGet())
}

@Test
fun validateImageForOkImage() {
val validateImage = imageProcessingService!!.validateImage(uploadItem)
val validateImage = imageProcessingService!!.validateImage(uploadItem, location)
assertEquals(ImageUtils.IMAGE_OK, validateImage.blockingGet())
}

@Test
fun validateImageForDarkImage() {
`when`(imageUtilsWrapper?.checkIfImageIsTooDark(ArgumentMatchers.anyString()))
.thenReturn(Single.just(ImageUtils.IMAGE_DARK))
val validateImage = imageProcessingService!!.validateImage(uploadItem)
val validateImage = imageProcessingService!!.validateImage(uploadItem, location)
assertEquals(ImageUtils.IMAGE_DARK, validateImage.blockingGet())
}

@Test
fun validateImageForWrongGeoLocation() {
`when`(imageUtilsWrapper!!.checkImageGeolocationIsDifferent(ArgumentMatchers.anyString(), any(LatLng::class.java)))
.thenReturn(Single.just(ImageUtils.IMAGE_GEOLOCATION_DIFFERENT))
val validateImage = imageProcessingService!!.validateImage(uploadItem)
val validateImage = imageProcessingService!!.validateImage(uploadItem, location)
assertEquals(ImageUtils.IMAGE_GEOLOCATION_DIFFERENT, validateImage.blockingGet())
}

@Test
fun validateImageForFileNameExistsWithCheckTitleOn() {
`when`(mediaClient?.checkPageExistsUsingTitle(ArgumentMatchers.anyString()))
.thenReturn(Single.just(true))
val validateImage = imageProcessingService!!.validateImage(uploadItem)
val validateImage = imageProcessingService!!.validateImage(uploadItem, location)
assertEquals(ImageUtils.FILE_NAME_EXISTS, validateImage.blockingGet())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class UploadMediaPresenterTest {
@Test
fun verifyImageQualityTest() {
whenever(repository.uploads).thenReturn(listOf(uploadItem))
whenever(repository.getImageQuality(uploadItem))
whenever(repository.getImageQuality(uploadItem, location))
.thenReturn(testSingleImageResult)
whenever(uploadItem.imageQuality).thenReturn(0)
whenever(uploadItem.gpsCoords)
Expand All @@ -130,7 +130,7 @@ class UploadMediaPresenterTest {
@Test
fun `verify ImageQuality Test while coordinates equals to null`() {
whenever(repository.uploads).thenReturn(listOf(uploadItem))
whenever(repository.getImageQuality(uploadItem))
whenever(repository.getImageQuality(uploadItem, location))
.thenReturn(testSingleImageResult)
whenever(uploadItem.imageQuality).thenReturn(0)
whenever(uploadItem.gpsCoords)
Expand Down Expand Up @@ -171,7 +171,7 @@ class UploadMediaPresenterTest {
val uploadMediaDetailList: ArrayList<UploadMediaDetail> = ArrayList()
uploadMediaDetailList.add(uploadMediaDetail)
uploadItem.setMediaDetails(uploadMediaDetailList)
Mockito.`when`(repository.getImageQuality(uploadItem)).then {
Mockito.`when`(repository.getImageQuality(uploadItem, location)).then {
verify(view).showProgress(true)
testScheduler.triggerActions()
verify(view).showProgress(true)
Expand All @@ -187,7 +187,7 @@ class UploadMediaPresenterTest {
uploadMediaDetail.captionText = "added caption"
uploadMediaDetail.languageCode = "eo"
uploadItem.setMediaDetails(Collections.singletonList(uploadMediaDetail))
Mockito.`when`(repository.getImageQuality(uploadItem)).then {
Mockito.`when`(repository.getImageQuality(uploadItem, location)).then {
verify(view).showProgress(true)
testScheduler.triggerActions()
verify(view).showProgress(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ class UploadRepositoryUnitTest {
@Test
fun testGetImageQuality() {
assertEquals(
repository.getImageQuality(uploadItem),
uploadModel.getImageQuality(uploadItem)
repository.getImageQuality(uploadItem, location),
uploadModel.getImageQuality(uploadItem, location)
)
}

Expand Down

0 comments on commit 7208018

Please sign in to comment.