Skip to content

Commit

Permalink
Merge branch 'multipleUploadsWithOverHaul' of https://github.com/mask…
Browse files Browse the repository at this point in the history
…aravivek/apps-android-commons into multipleUploadsWithOverHaul
  • Loading branch information
maskara committed Nov 6, 2018
2 parents 6ff0684 + 9d8cb93 commit 204cd81
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,20 @@ public Contribution[] newArray(int i) {
private Uri contentUri;
private String source;
private String editSummary;
private Date timestamp;
private int state;
private long transferred;
private String decimalCoords;
private boolean isMultiple;
private String wikiDataEntityId;
private Uri contentProviderUri;

public Contribution(Uri contentUri, String filename, Uri localUri, String imageUrl, Date timestamp,
public Contribution(Uri contentUri, String filename, Uri localUri, String imageUrl, Date dateCreated,
int state, long dataLength, Date dateUploaded, long transferred,
String source, String description, String creator, boolean isMultiple,
int width, int height, String license) {
super(localUri, imageUrl, filename, description, dataLength, timestamp, dateUploaded, creator);
super(localUri, imageUrl, filename, description, dataLength, dateCreated, dateUploaded, creator);
this.contentUri = contentUri;
this.state = state;
this.timestamp = timestamp;
this.transferred = transferred;
this.source = source;
this.isMultiple = isMultiple;
Expand All @@ -78,14 +76,12 @@ public Contribution(Uri localUri, String imageUrl, String filename, String descr
super(localUri, imageUrl, filename, description, dataLength, dateCreated, dateUploaded, creator);
this.decimalCoords = decimalCoords;
this.editSummary = editSummary;
timestamp = new Date(System.currentTimeMillis());
}

public Contribution(Parcel in) {
super(in);
contentUri = in.readParcelable(Uri.class.getClassLoader());
source = in.readString();
timestamp = (Date) in.readSerializable();
state = in.readInt();
transferred = in.readLong();
isMultiple = in.readInt() == 1;
Expand All @@ -96,12 +92,13 @@ public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeParcelable(contentUri, flags);
parcel.writeString(source);
parcel.writeSerializable(timestamp);
parcel.writeInt(state);
parcel.writeLong(transferred);
parcel.writeInt(isMultiple ? 1 : 0);
}



public boolean getMultiple() {
return isMultiple;
}
Expand Down Expand Up @@ -130,14 +127,6 @@ public void setContentUri(Uri contentUri) {
this.contentUri = contentUri;
}

public Date getTimestamp() {
return timestamp;
}

public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

public int getState() {
return state;
}
Expand Down Expand Up @@ -196,7 +185,7 @@ public void setImageUrl(String imageUrl) {
}

public Contribution() {
timestamp = new Date(System.currentTimeMillis());

}

public String getSource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ ContentValues toContentValues(Contribution contribution) {
cv.put(Table.COLUMN_UPLOADED, contribution.getDateUploaded().getTime());
}
cv.put(Table.COLUMN_LENGTH, contribution.getDataLength());
cv.put(Table.COLUMN_TIMESTAMP, contribution.getTimestamp().getTime());
//This was always meant to store the date created..If somehow date created is not fetched while actually saving the contribution, lets save today's date
cv.put(Table.COLUMN_TIMESTAMP, contribution.getDateCreated()==null?System.currentTimeMillis():contribution.getDateCreated().getTime());
cv.put(Table.COLUMN_STATE, contribution.getState());
cv.put(Table.COLUMN_TRANSFERRED, contribution.getTransferred());
cv.put(Table.COLUMN_SOURCE, contribution.getSource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand Down
54 changes: 47 additions & 7 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadModel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fr.free.nrw.commons.upload;

import android.accounts.Account;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.BitmapRegionDecoder;
import android.net.Uri;
import android.support.annotation.Nullable;
Expand All @@ -13,6 +13,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

Expand All @@ -25,7 +26,6 @@
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.utils.ImageUtils;
import fr.free.nrw.commons.utils.StringUtils;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.disposables.Disposable;
Expand All @@ -37,7 +37,7 @@
public class UploadModel {

MediaWikiApi mwApi;
private static UploadItem DUMMY = new UploadItem(Uri.EMPTY, "", "", GPSExtractor.DUMMY, "", null) {
private static UploadItem DUMMY = new UploadItem(Uri.EMPTY, "", "", GPSExtractor.DUMMY, "", null,-1l) {
@Override
public boolean isDummy() {
return true;
Expand All @@ -59,6 +59,7 @@ public boolean isDummy() {

@Inject
SessionManager sessionManager;
private Uri currentMediaUri;

@Inject
UploadModel(@Named("licenses") List<String> licenses,
Expand All @@ -80,12 +81,16 @@ public boolean isDummy() {
public void receive(List<Uri> mediaUri, String mimeType, String source, SimilarImageInterface similarImageInterface) {
currentStepIndex = 0;
Observable<UploadItem> itemObservable = Observable.fromIterable(mediaUri)
.map(this::cacheFileUpload)
.map(media -> {
currentMediaUri=media;
return cacheFileUpload(media);
})
.map(filePath -> {
long fileCreatedDate = getFileCreatedDate(currentMediaUri);
Uri uri = Uri.fromFile(new File(filePath));
FileProcessor fp = new FileProcessor(filePath, context.getContentResolver(), context);
UploadItem item = new UploadItem(uri, mimeType, source, fp.processFileCoordinates(similarImageInterface),
FileUtils.getFileExt(filePath), null);
FileUtils.getFileExt(filePath), null,fileCreatedDate);
Single.zip(
Single.fromCallable(() ->
new FileInputStream(filePath))
Expand All @@ -110,11 +115,12 @@ public void receive(List<Uri> mediaUri, String mimeType, String source, SimilarI
public void receiveDirect(Uri media, String mimeType, String source, String wikidataEntityIdPref, String title, String desc, SimilarImageInterface similarImageInterface) {
currentStepIndex = 0;
items = new ArrayList<>();
long fileCreatedDate = getFileCreatedDate(media);
String filePath = this.cacheFileUpload(media);
Uri uri = Uri.fromFile(new File(filePath));
FileProcessor fp = new FileProcessor(filePath, context.getContentResolver(), context);
UploadItem item = new UploadItem(uri, mimeType, source, fp.processFileCoordinates(similarImageInterface),
FileUtils.getFileExt(filePath), wikidataEntityIdPref);
FileUtils.getFileExt(filePath), wikidataEntityIdPref,fileCreatedDate);
item.title.setTitleText(title);
item.descriptions.get(0).setDescriptionText(desc);
//TODO figure out if default descriptions in other languages exist
Expand All @@ -135,6 +141,33 @@ public void receiveDirect(Uri media, String mimeType, String source, String wiki
items.get(0).first = true;
}

/**
* Get file creation date from uri from all possible content providers
* @param media
* @return
*/
private long getFileCreatedDate(Uri media) {
try {
Cursor cursor = contentResolver.query(media, null, null, null, null);
if (cursor == null) {
return -1;//Could not fetch last_modified
}
//Content provider contracts for opening gallery from the app and that by sharing from gallery from outside are different and we need to handle both the cases
int lastModifiedColumnIndex = cursor.getColumnIndex("last_modified");//If gallery is opened from in app
if(lastModifiedColumnIndex==-1){
lastModifiedColumnIndex=cursor.getColumnIndex("datetaken");
}
//If both the content providers do not give the data, lets leave it to Jesus
if(lastModifiedColumnIndex==-1){
return -1l;
}
cursor.moveToFirst();
return cursor.getLong(lastModifiedColumnIndex);
} catch (Exception e) {
return -1;////Could not fetch last_modified
}
}

public boolean isPreviousAvailable() {
return currentStepIndex > 0;
}
Expand Down Expand Up @@ -266,6 +299,10 @@ public Observable<Contribution> buildContributions(List<String> categoryStringLi
contribution.setTag("mimeType", item.mimeType);
contribution.setSource(item.source);
contribution.setContentProviderUri(item.mediaUri);
if (item.createdTimestamp != -1l) {
contribution.setDateCreated(new Date(item.createdTimestamp));
//Set the date only if you have it, else the upload service is gonna try it the other way
}
return contribution;
});
}
Expand Down Expand Up @@ -322,9 +359,10 @@ static class UploadItem {
public String wikidataEntityId;
public boolean visited;
public boolean error;
public long createdTimestamp;

@SuppressLint("CheckResult")
UploadItem(Uri mediaUri, String mimeType, String source, GPSExtractor gpsCoords, String fileExt, @Nullable String wikidataEntityId) {
UploadItem(Uri mediaUri, String mimeType, String source, GPSExtractor gpsCoords, String fileExt, @Nullable String wikidataEntityId, long createdTimestamp) {
title = new Title();
descriptions = new ArrayList<>();
descriptions.add(new Description());
Expand All @@ -336,6 +374,8 @@ static class UploadItem {
this.gpsCoords = gpsCoords;
this.fileExt = fileExt;
imageQuality = BehaviorSubject.createDefault(ImageUtils.IMAGE_WAIT);
this.createdTimestamp=createdTimestamp;
// imageQuality.subscribe(iq->Timber.i("New value of imageQuality:"+ImageUtils.IMAGE_OK));
}

public boolean isDummy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ class ContributionDaoTest {
assertEquals("file", it.filename)
assertEquals(localUri, it.localUri.toString())
assertEquals("image", it.imageUrl)
assertEquals(created, it.timestamp.time)
assertEquals(created, it.dateCreated.time)
assertEquals(STATE_QUEUED, it.state)
assertEquals(222L, it.dataLength)
Expand All @@ -299,7 +298,6 @@ class ContributionDaoTest {
fun createFromCursor_nullableTimestamps() {
createCursor(0L, 0L, false, localUri).let { mc ->
testObject.fromCursor(mc).let {
assertNull(it.timestamp)
assertNull(it.dateCreated)
assertNull(it.dateUploaded)
}
Expand Down Expand Up @@ -342,7 +340,6 @@ class ContributionDaoTest {
source = SOURCE_CAMERA
license = "007"
multiple = isMultiple
timestamp = Date(321L)
width = 640
height = 480 // VGA should be enough for anyone, right?
}
Expand Down

0 comments on commit 204cd81

Please sign in to comment.