1
1
package fr .free .nrw .commons .upload ;
2
2
3
- import android .accounts .Account ;
4
3
import android .annotation .SuppressLint ;
5
4
import android .content .ContentResolver ;
6
5
import android .content .Context ;
7
6
import android .content .SharedPreferences ;
7
+ import android .database .Cursor ;
8
8
import android .graphics .BitmapRegionDecoder ;
9
9
import android .net .Uri ;
10
10
import android .support .annotation .Nullable ;
13
13
import java .io .FileInputStream ;
14
14
import java .io .IOException ;
15
15
import java .util .ArrayList ;
16
+ import java .util .Date ;
16
17
import java .util .List ;
17
18
import java .util .Map ;
18
19
25
26
import fr .free .nrw .commons .mwapi .MediaWikiApi ;
26
27
import fr .free .nrw .commons .settings .Prefs ;
27
28
import fr .free .nrw .commons .utils .ImageUtils ;
28
- import fr .free .nrw .commons .utils .StringUtils ;
29
29
import io .reactivex .Observable ;
30
30
import io .reactivex .Single ;
31
31
import io .reactivex .disposables .Disposable ;
37
37
public class UploadModel {
38
38
39
39
MediaWikiApi mwApi ;
40
- private static UploadItem DUMMY = new UploadItem (Uri .EMPTY , "" , "" , GPSExtractor .DUMMY , "" , null ) {
40
+ private static UploadItem DUMMY = new UploadItem (Uri .EMPTY , "" , "" , GPSExtractor .DUMMY , "" , null ,- 1l ) {
41
41
@ Override
42
42
public boolean isDummy () {
43
43
return true ;
@@ -59,6 +59,7 @@ public boolean isDummy() {
59
59
60
60
@ Inject
61
61
SessionManager sessionManager ;
62
+ private Uri currentMediaUri ;
62
63
63
64
@ Inject
64
65
UploadModel (@ Named ("licenses" ) List <String > licenses ,
@@ -80,12 +81,16 @@ public boolean isDummy() {
80
81
public void receive (List <Uri > mediaUri , String mimeType , String source , SimilarImageInterface similarImageInterface ) {
81
82
currentStepIndex = 0 ;
82
83
Observable <UploadItem > itemObservable = Observable .fromIterable (mediaUri )
83
- .map (this ::cacheFileUpload )
84
+ .map (media -> {
85
+ currentMediaUri =media ;
86
+ return cacheFileUpload (media );
87
+ })
84
88
.map (filePath -> {
89
+ long fileCreatedDate = getFileCreatedDate (currentMediaUri );
85
90
Uri uri = Uri .fromFile (new File (filePath ));
86
91
FileProcessor fp = new FileProcessor (filePath , context .getContentResolver (), context );
87
92
UploadItem item = new UploadItem (uri , mimeType , source , fp .processFileCoordinates (similarImageInterface ),
88
- FileUtils .getFileExt (filePath ), null );
93
+ FileUtils .getFileExt (filePath ), null , fileCreatedDate );
89
94
Single .zip (
90
95
Single .fromCallable (() ->
91
96
new FileInputStream (filePath ))
@@ -110,11 +115,12 @@ public void receive(List<Uri> mediaUri, String mimeType, String source, SimilarI
110
115
public void receiveDirect (Uri media , String mimeType , String source , String wikidataEntityIdPref , String title , String desc , SimilarImageInterface similarImageInterface ) {
111
116
currentStepIndex = 0 ;
112
117
items = new ArrayList <>();
118
+ long fileCreatedDate = getFileCreatedDate (media );
113
119
String filePath = this .cacheFileUpload (media );
114
120
Uri uri = Uri .fromFile (new File (filePath ));
115
121
FileProcessor fp = new FileProcessor (filePath , context .getContentResolver (), context );
116
122
UploadItem item = new UploadItem (uri , mimeType , source , fp .processFileCoordinates (similarImageInterface ),
117
- FileUtils .getFileExt (filePath ), wikidataEntityIdPref );
123
+ FileUtils .getFileExt (filePath ), wikidataEntityIdPref , fileCreatedDate );
118
124
item .title .setTitleText (title );
119
125
item .descriptions .get (0 ).setDescriptionText (desc );
120
126
//TODO figure out if default descriptions in other languages exist
@@ -135,6 +141,33 @@ public void receiveDirect(Uri media, String mimeType, String source, String wiki
135
141
items .get (0 ).first = true ;
136
142
}
137
143
144
+ /**
145
+ * Get file creation date from uri from all possible content providers
146
+ * @param media
147
+ * @return
148
+ */
149
+ private long getFileCreatedDate (Uri media ) {
150
+ try {
151
+ Cursor cursor = contentResolver .query (media , null , null , null , null );
152
+ if (cursor == null ) {
153
+ return -1 ;//Could not fetch last_modified
154
+ }
155
+ //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
156
+ int lastModifiedColumnIndex = cursor .getColumnIndex ("last_modified" );//If gallery is opened from in app
157
+ if (lastModifiedColumnIndex ==-1 ){
158
+ lastModifiedColumnIndex =cursor .getColumnIndex ("datetaken" );
159
+ }
160
+ //If both the content providers do not give the data, lets leave it to Jesus
161
+ if (lastModifiedColumnIndex ==-1 ){
162
+ return -1l ;
163
+ }
164
+ cursor .moveToFirst ();
165
+ return cursor .getLong (lastModifiedColumnIndex );
166
+ } catch (Exception e ) {
167
+ return -1 ;////Could not fetch last_modified
168
+ }
169
+ }
170
+
138
171
public boolean isPreviousAvailable () {
139
172
return currentStepIndex > 0 ;
140
173
}
@@ -266,6 +299,10 @@ public Observable<Contribution> buildContributions(List<String> categoryStringLi
266
299
contribution .setTag ("mimeType" , item .mimeType );
267
300
contribution .setSource (item .source );
268
301
contribution .setContentProviderUri (item .mediaUri );
302
+ if (item .createdTimestamp != -1l ) {
303
+ contribution .setDateCreated (new Date (item .createdTimestamp ));
304
+ //Set the date only if you have it, else the upload service is gonna try it the other way
305
+ }
269
306
return contribution ;
270
307
});
271
308
}
@@ -322,9 +359,10 @@ static class UploadItem {
322
359
public String wikidataEntityId ;
323
360
public boolean visited ;
324
361
public boolean error ;
362
+ public long createdTimestamp ;
325
363
326
364
@ SuppressLint ("CheckResult" )
327
- UploadItem (Uri mediaUri , String mimeType , String source , GPSExtractor gpsCoords , String fileExt , @ Nullable String wikidataEntityId ) {
365
+ UploadItem (Uri mediaUri , String mimeType , String source , GPSExtractor gpsCoords , String fileExt , @ Nullable String wikidataEntityId , long createdTimestamp ) {
328
366
title = new Title ();
329
367
descriptions = new ArrayList <>();
330
368
descriptions .add (new Description ());
@@ -336,6 +374,8 @@ static class UploadItem {
336
374
this .gpsCoords = gpsCoords ;
337
375
this .fileExt = fileExt ;
338
376
imageQuality = BehaviorSubject .createDefault (ImageUtils .IMAGE_WAIT );
377
+ this .createdTimestamp =createdTimestamp ;
378
+ // imageQuality.subscribe(iq->Timber.i("New value of imageQuality:"+ImageUtils.IMAGE_OK));
339
379
}
340
380
341
381
public boolean isDummy () {
0 commit comments