Skip to content

Commit e4816af

Browse files
ashishkumar468maskaravivek
authored andcommitted
Bug fix issue #1854 (#1)
* updated ContributionsDao to save create date, which it was not doing currently [it was instead saving current date] * UploadItem accepts are dateCreated param * Added a function in UploadModel, getFileCreatedDate which tries to fetched the file creaction date from all possible content providers.
1 parent bd4504f commit e4816af

File tree

6 files changed

+55
-63
lines changed

6 files changed

+55
-63
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/Contribution.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,20 @@ public Contribution[] newArray(int i) {
4949
private Uri contentUri;
5050
private String source;
5151
private String editSummary;
52-
private Date timestamp;
5352
private int state;
5453
private long transferred;
5554
private String decimalCoords;
5655
private boolean isMultiple;
5756
private String wikiDataEntityId;
5857
private Uri contentProviderUri;
5958

60-
public Contribution(Uri contentUri, String filename, Uri localUri, String imageUrl, Date timestamp,
59+
public Contribution(Uri contentUri, String filename, Uri localUri, String imageUrl, Date dateCreated,
6160
int state, long dataLength, Date dateUploaded, long transferred,
6261
String source, String description, String creator, boolean isMultiple,
6362
int width, int height, String license) {
64-
super(localUri, imageUrl, filename, description, dataLength, timestamp, dateUploaded, creator);
63+
super(localUri, imageUrl, filename, description, dataLength, dateCreated, dateUploaded, creator);
6564
this.contentUri = contentUri;
6665
this.state = state;
67-
this.timestamp = timestamp;
6866
this.transferred = transferred;
6967
this.source = source;
7068
this.isMultiple = isMultiple;
@@ -78,14 +76,12 @@ public Contribution(Uri localUri, String imageUrl, String filename, String descr
7876
super(localUri, imageUrl, filename, description, dataLength, dateCreated, dateUploaded, creator);
7977
this.decimalCoords = decimalCoords;
8078
this.editSummary = editSummary;
81-
timestamp = new Date(System.currentTimeMillis());
8279
}
8380

8481
public Contribution(Parcel in) {
8582
super(in);
8683
contentUri = in.readParcelable(Uri.class.getClassLoader());
8784
source = in.readString();
88-
timestamp = (Date) in.readSerializable();
8985
state = in.readInt();
9086
transferred = in.readLong();
9187
isMultiple = in.readInt() == 1;
@@ -96,12 +92,13 @@ public void writeToParcel(Parcel parcel, int flags) {
9692
super.writeToParcel(parcel, flags);
9793
parcel.writeParcelable(contentUri, flags);
9894
parcel.writeString(source);
99-
parcel.writeSerializable(timestamp);
10095
parcel.writeInt(state);
10196
parcel.writeLong(transferred);
10297
parcel.writeInt(isMultiple ? 1 : 0);
10398
}
10499

100+
101+
105102
public boolean getMultiple() {
106103
return isMultiple;
107104
}
@@ -130,14 +127,6 @@ public void setContentUri(Uri contentUri) {
130127
this.contentUri = contentUri;
131128
}
132129

133-
public Date getTimestamp() {
134-
return timestamp;
135-
}
136-
137-
public void setTimestamp(Date timestamp) {
138-
this.timestamp = timestamp;
139-
}
140-
141130
public int getState() {
142131
return state;
143132
}
@@ -196,7 +185,7 @@ public void setImageUrl(String imageUrl) {
196185
}
197186

198187
public Contribution() {
199-
timestamp = new Date(System.currentTimeMillis());
188+
200189
}
201190

202191
public String getSource() {

app/src/main/java/fr/free/nrw/commons/contributions/ContributionDao.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ ContentValues toContentValues(Contribution contribution) {
9898
cv.put(Table.COLUMN_UPLOADED, contribution.getDateUploaded().getTime());
9999
}
100100
cv.put(Table.COLUMN_LENGTH, contribution.getDataLength());
101-
cv.put(Table.COLUMN_TIMESTAMP, contribution.getTimestamp().getTime());
101+
//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
102+
cv.put(Table.COLUMN_TIMESTAMP, contribution.getDateCreated()==null?System.currentTimeMillis():contribution.getDateCreated().getTime());
102103
cv.put(Table.COLUMN_STATE, contribution.getState());
103104
cv.put(Table.COLUMN_TRANSFERRED, contribution.getTransferred());
104105
cv.put(Table.COLUMN_SOURCE, contribution.getSource());

app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.DialogInterface;
77
import android.content.Intent;
88
import android.content.SharedPreferences;
9+
import android.database.Cursor;
910
import android.net.Uri;
1011
import android.os.Build;
1112
import android.os.Bundle;

app/src/main/java/fr/free/nrw/commons/upload/UploadController.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -85,44 +85,6 @@ public void cleanup() {
8585
}
8686
}
8787

88-
/**
89-
* Starts a new upload task.
90-
*
91-
* @param title the title of the contribution
92-
* @param mediaUri the media URI of the contribution
93-
* @param description the description of the contribution
94-
* @param mimeType the MIME type of the contribution
95-
* @param source the source of the contribution
96-
* @param decimalCoords the coordinates in decimal. (e.g. "37.51136|-77.602615")
97-
* @param wikiDataEntityId
98-
* @param onComplete the progress tracker
99-
*/
100-
public void startUpload(String title, Uri contentProviderUri, Uri mediaUri, String description, String mimeType, String source, String decimalCoords, String wikiDataEntityId, ContributionUploadProgress onComplete) {
101-
Contribution contribution;
102-
103-
Timber.d("Wikidata entity ID received from Share activity is %s", wikiDataEntityId);
104-
//TODO: Modify this to include coords
105-
Account currentAccount = sessionManager.getCurrentAccount();
106-
if (currentAccount == null) {
107-
Timber.d("Current account is null");
108-
ViewUtil.showLongToast(context, context.getString(R.string.user_not_logged_in));
109-
sessionManager.forceLogin(context);
110-
return;
111-
}
112-
contribution = new Contribution(mediaUri, null, title, description, -1,
113-
null, null, currentAccount.name,
114-
CommonsApplication.DEFAULT_EDIT_SUMMARY, decimalCoords);
115-
116-
117-
contribution.setTag("mimeType", mimeType);
118-
contribution.setSource(source);
119-
contribution.setWikiDataEntityId(wikiDataEntityId);
120-
contribution.setContentProviderUri(contentProviderUri);
121-
122-
//Calls the next overloaded method
123-
startUpload(contribution, onComplete);
124-
}
125-
12688
/**
12789
* Starts a new upload task.
12890
*

app/src/main/java/fr/free/nrw/commons/upload/UploadModel.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.ContentResolver;
66
import android.content.Context;
77
import android.content.SharedPreferences;
8+
import android.database.Cursor;
89
import android.graphics.BitmapRegionDecoder;
910
import android.net.Uri;
1011
import android.support.annotation.Nullable;
@@ -13,6 +14,7 @@
1314
import java.io.FileInputStream;
1415
import java.io.IOException;
1516
import java.util.ArrayList;
17+
import java.util.Date;
1618
import java.util.List;
1719
import java.util.Map;
1820

@@ -37,7 +39,7 @@
3739
public class UploadModel {
3840

3941
MediaWikiApi mwApi;
40-
private static UploadItem DUMMY = new UploadItem(Uri.EMPTY, "", "", GPSExtractor.DUMMY, "", null) {
42+
private static UploadItem DUMMY = new UploadItem(Uri.EMPTY, "", "", GPSExtractor.DUMMY, "", null,-1l) {
4143
@Override
4244
public boolean isDummy() {
4345
return true;
@@ -59,6 +61,7 @@ public boolean isDummy() {
5961

6062
@Inject
6163
SessionManager sessionManager;
64+
private Uri currentMediaUri;
6265

6366
@Inject
6467
UploadModel(@Named("licenses") List<String> licenses,
@@ -80,12 +83,16 @@ public boolean isDummy() {
8083
public void receive(List<Uri> mediaUri, String mimeType, String source) {
8184
currentStepIndex = 0;
8285
Observable<UploadItem> itemObservable = Observable.fromIterable(mediaUri)
83-
.map(this::cacheFileUpload)
86+
.map(media -> {
87+
currentMediaUri=media;
88+
return cacheFileUpload(media);
89+
})
8490
.map(filePath -> {
91+
long fileCreatedDate = getFileCreatedDate(currentMediaUri);
8592
Uri uri = Uri.fromFile(new File(filePath));
8693
FileProcessor fp = new FileProcessor(filePath, context.getContentResolver(), context);
8794
UploadItem item = new UploadItem(uri, mimeType, source, fp.processFileCoordinates(),
88-
FileUtils.getFileExt(filePath), null);
95+
FileUtils.getFileExt(filePath), null,fileCreatedDate);
8996
Single.zip(
9097
Single.fromCallable(() ->
9198
new FileInputStream(filePath))
@@ -110,11 +117,12 @@ public void receive(List<Uri> mediaUri, String mimeType, String source) {
110117
public void receiveDirect(Uri media, String mimeType, String source, String wikidataEntityIdPref, String title, String desc) {
111118
currentStepIndex = 0;
112119
items = new ArrayList<>();
120+
long fileCreatedDate = getFileCreatedDate(media);
113121
String filePath = this.cacheFileUpload(media);
114122
Uri uri = Uri.fromFile(new File(filePath));
115123
FileProcessor fp = new FileProcessor(filePath, context.getContentResolver(), context);
116124
UploadItem item = new UploadItem(uri, mimeType, source, fp.processFileCoordinates(),
117-
FileUtils.getFileExt(filePath), wikidataEntityIdPref);
125+
FileUtils.getFileExt(filePath), wikidataEntityIdPref,fileCreatedDate);
118126
item.title.setTitleText(title);
119127
item.descriptions.get(0).setDescriptionText(desc);
120128
//TODO figure out if default descriptions in other languages exist
@@ -135,6 +143,33 @@ public void receiveDirect(Uri media, String mimeType, String source, String wiki
135143
items.get(0).first = true;
136144
}
137145

146+
/**
147+
* Get file creation date from uri from all possible content providers
148+
* @param media
149+
* @return
150+
*/
151+
private long getFileCreatedDate(Uri media) {
152+
try {
153+
Cursor cursor = contentResolver.query(media, null, null, null, null);
154+
if (cursor == null) {
155+
return -1;//Could not fetch last_modified
156+
}
157+
//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
158+
int lastModifiedColumnIndex = cursor.getColumnIndex("last_modified");//If gallery is opened from in app
159+
if(lastModifiedColumnIndex==-1){
160+
lastModifiedColumnIndex=cursor.getColumnIndex("datetaken");
161+
}
162+
//If both the content providers do not give the data, lets leave it to Jesus
163+
if(lastModifiedColumnIndex==-1){
164+
return -1l;
165+
}
166+
cursor.moveToFirst();
167+
return cursor.getLong(lastModifiedColumnIndex);
168+
} catch (Exception e) {
169+
return -1;////Could not fetch last_modified
170+
}
171+
}
172+
138173
public boolean isPreviousAvailable() {
139174
return currentStepIndex > 0;
140175
}
@@ -266,6 +301,10 @@ public Observable<Contribution> buildContributions(List<String> categoryStringLi
266301
contribution.setTag("mimeType", item.mimeType);
267302
contribution.setSource(item.source);
268303
contribution.setContentProviderUri(item.mediaUri);
304+
if (item.createdTimestamp != -1l) {
305+
contribution.setDateCreated(new Date(item.createdTimestamp));
306+
//Set the date only if you have it, else the upload service is gonna try it the other way
307+
}
269308
return contribution;
270309
});
271310
}
@@ -322,9 +361,10 @@ static class UploadItem {
322361
public String wikidataEntityId;
323362
public boolean visited;
324363
public boolean error;
364+
public long createdTimestamp;
325365

326366
@SuppressLint("CheckResult")
327-
UploadItem(Uri mediaUri, String mimeType, String source, GPSExtractor gpsCoords, String fileExt, @Nullable String wikidataEntityId) {
367+
UploadItem(Uri mediaUri, String mimeType, String source, GPSExtractor gpsCoords, String fileExt, @Nullable String wikidataEntityId, long createdTimestamp) {
328368
title = new Title();
329369
descriptions = new ArrayList<>();
330370
descriptions.add(new Description());
@@ -336,6 +376,8 @@ static class UploadItem {
336376
this.gpsCoords = gpsCoords;
337377
this.fileExt = fileExt;
338378
imageQuality = BehaviorSubject.createDefault(ImageUtils.IMAGE_WAIT);
379+
this.createdTimestamp=createdTimestamp;
380+
// imageQuality.subscribe(iq->Timber.i("New value of imageQuality:"+ImageUtils.IMAGE_OK));
339381
}
340382

341383
public boolean isDummy() {

app/src/test/kotlin/fr/free/nrw/commons/contributions/ContributionDaoTest.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ class ContributionDaoTest {
279279
assertEquals("file", it.filename)
280280
assertEquals(localUri, it.localUri.toString())
281281
assertEquals("image", it.imageUrl)
282-
assertEquals(created, it.timestamp.time)
283282
assertEquals(created, it.dateCreated.time)
284283
assertEquals(STATE_QUEUED, it.state)
285284
assertEquals(222L, it.dataLength)
@@ -299,7 +298,6 @@ class ContributionDaoTest {
299298
fun createFromCursor_nullableTimestamps() {
300299
createCursor(0L, 0L, false, localUri).let { mc ->
301300
testObject.fromCursor(mc).let {
302-
assertNull(it.timestamp)
303301
assertNull(it.dateCreated)
304302
assertNull(it.dateUploaded)
305303
}
@@ -342,7 +340,6 @@ class ContributionDaoTest {
342340
source = SOURCE_CAMERA
343341
license = "007"
344342
multiple = isMultiple
345-
timestamp = Date(321L)
346343
width = 640
347344
height = 480 // VGA should be enough for anyone, right?
348345
}

0 commit comments

Comments
 (0)