55import android .content .ContentResolver ;
66import android .content .Context ;
77import android .content .SharedPreferences ;
8+ import android .database .Cursor ;
89import android .graphics .BitmapRegionDecoder ;
910import android .net .Uri ;
1011import android .support .annotation .Nullable ;
1314import java .io .FileInputStream ;
1415import java .io .IOException ;
1516import java .util .ArrayList ;
17+ import java .util .Date ;
1618import java .util .List ;
1719import java .util .Map ;
1820
3739public 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 () {
0 commit comments