Skip to content

Commit 98143aa

Browse files
[WIP] Fix both timezone problem and saved date problem (#5019)
* Fix both timezone problem and saved date problem * Fixaccidental test code and add comments * Add issue link to the comments * Fix format issue and null checks
1 parent cb3094b commit 98143aa

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ data class Contribution constructor(
3838
val localUri: Uri? = null,
3939
var dataLength: Long = 0,
4040
var dateCreated: Date? = null,
41+
var dateCreatedString: String? = null,
4142
var dateModified: Date? = null,
4243
var hasInvalidLocation : Int = 0,
4344
var contentUri: Uri? = null,
@@ -67,7 +68,8 @@ data class Contribution constructor(
6768
dateCreatedSource = "",
6869
depictedItems = depictedItems,
6970
wikidataPlace = from(item.place),
70-
contentUri = item.contentUri
71+
contentUri = item.contentUri,
72+
dateCreatedString = item.fileCreatedDateString
7173
)
7274

7375
/**

app/src/main/java/fr/free/nrw/commons/db/AppDatabase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import fr.free.nrw.commons.upload.depicts.DepictsDao
1414
* The database for accessing the respective DAOs
1515
*
1616
*/
17-
@Database(entities = [Contribution::class, Depicts::class, UploadedStatus::class], version = 12, exportSchema = false)
17+
@Database(entities = [Contribution::class, Depicts::class, UploadedStatus::class], version = 13, exportSchema = false)
1818
@TypeConverters(Converters::class)
1919
abstract class AppDatabase : RoomDatabase() {
2020
abstract fun contributionDao(): ContributionDao

app/src/main/java/fr/free/nrw/commons/filepicker/UploadableFile.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import java.io.File;
1414
import java.io.IOException;
15+
import java.text.ParseException;
16+
import java.text.SimpleDateFormat;
1517
import java.util.Date;
1618

1719
import fr.free.nrw.commons.upload.FileUtils;
@@ -124,13 +126,30 @@ private DateTimeWithSource getFileCreatedDateFromCP(Context context) {
124126
private DateTimeWithSource getDateTimeFromExif() {
125127
try {
126128
ExifInterface exif = new ExifInterface(file.getAbsolutePath());
127-
@SuppressLint("RestrictedApi") Long dateTime = exif.getDateTime();
128-
if(dateTime != null){
129-
Date date = new Date(dateTime);
130-
return new DateTimeWithSource(date, DateTimeWithSource.EXIF_SOURCE);
129+
// TAG_DATETIME returns the last edited date, we need TAG_DATETIME_ORIGINAL for creation date
130+
// See issue https://github.com/commons-app/apps-android-commons/issues/1971
131+
String dateTimeSubString = exif.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL);
132+
if (dateTimeSubString!=null) { //getAttribute may return null
133+
String year = dateTimeSubString.substring(0,4);
134+
String month = dateTimeSubString.substring(5,7);
135+
String day = dateTimeSubString.substring(8,10);
136+
// This date is stored as a string (not as a date), the rason is we don't want to include timezones
137+
String dateCreatedString = String.format("%04d-%02d-%02d", Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
138+
if (dateCreatedString.length() == 10) { //yyyy-MM-dd format of date is expected
139+
@SuppressLint("RestrictedApi") Long dateTime = exif.getDateTimeOriginal();
140+
if(dateTime != null){
141+
Date date = new Date(dateTime);
142+
return new DateTimeWithSource(date, dateCreatedString, DateTimeWithSource.EXIF_SOURCE);
143+
}
144+
}
131145
}
146+
132147
} catch (IOException e) {
133148
e.printStackTrace();
149+
} catch (NumberFormatException e) {
150+
e.printStackTrace();
151+
} catch (IndexOutOfBoundsException e) {
152+
e.printStackTrace();
134153
}
135154
return null;
136155
}
@@ -149,6 +168,7 @@ public class DateTimeWithSource {
149168
public static final String EXIF_SOURCE = "exif";
150169

151170
private final long epochDate;
171+
private String dateString; // this does not includes timezone information
152172
private final String source;
153173

154174
public DateTimeWithSource(long epochDate, String source) {
@@ -161,10 +181,20 @@ public DateTimeWithSource(Date date, String source) {
161181
this.source = source;
162182
}
163183

184+
public DateTimeWithSource(Date date, String dateString, String source) {
185+
this.epochDate = date.getTime();
186+
this.dateString = dateString;
187+
this.source = source;
188+
}
189+
164190
public long getEpochDate() {
165191
return epochDate;
166192
}
167193

194+
public String getDateString() {
195+
return dateString;
196+
}
197+
168198
public String getSource() {
169199
return source;
170200
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public String createFrom(final Contribution contribution) {
4848
.append(media.getAuthor()).append("]]\n");
4949

5050
final String templatizedCreatedDate = getTemplatizedCreatedDate(
51-
contribution.getDateCreated(), contribution.getDateCreatedSource());
51+
contribution.getDateCreatedString(), contribution.getDateCreated(), contribution.getDateCreatedSource());
5252
if (!StringUtils.isBlank(templatizedCreatedDate)) {
5353
buffer.append("|date=").append(templatizedCreatedDate);
5454
}
@@ -90,12 +90,12 @@ public String createFrom(final Contribution contribution) {
9090
* @param dateCreatedSource
9191
* @return
9292
*/
93-
private String getTemplatizedCreatedDate(Date dateCreated, String dateCreatedSource) {
93+
private String getTemplatizedCreatedDate(String dateCreatedString, Date dateCreated, String dateCreatedSource) {
9494
if (dateCreated != null) {
9595
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
9696
return String.format(Locale.ENGLISH,
9797
isExif(dateCreatedSource) ? TEMPLATE_DATE_ACC_TO_EXIF : TEMPLATE_DATA_OTHER_SOURCE,
98-
dateFormat.format(dateCreated)
98+
isExif(dateCreatedSource) ? dateCreatedString: dateFormat.format(dateCreated)
9999
) + "\n";
100100
}
101101
return "";

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class UploadItem {
2525
private boolean hasInvalidLocation;
2626
private boolean isWLMUpload = false;
2727
private String countryCode;
28+
private String fileCreatedDateString; //according to EXIF data
2829

2930
/**
3031
* Uri of uploadItem
@@ -40,7 +41,8 @@ public class UploadItem {
4041
final Place place,
4142
final long createdTimestamp,
4243
final String createdTimestampSource,
43-
final Uri contentUri) {
44+
final Uri contentUri,
45+
final String fileCreatedDateString) {
4446
this.createdTimestampSource = createdTimestampSource;
4547
uploadMediaDetails = new ArrayList<>(Collections.singletonList(new UploadMediaDetail()));
4648
this.place = place;
@@ -50,6 +52,7 @@ public class UploadItem {
5052
this.createdTimestamp = createdTimestamp;
5153
this.contentUri = contentUri;
5254
imageQuality = BehaviorSubject.createDefault(ImageUtils.IMAGE_WAIT);
55+
this.fileCreatedDateString = fileCreatedDateString;
5356
}
5457

5558
public String getCreatedTimestampSource() {
@@ -83,6 +86,8 @@ public int getImageQuality() {
8386
*/
8487
public Uri getContentUri() { return contentUri; }
8588

89+
public String getFileCreatedDateString() { return fileCreatedDateString; }
90+
8691
public void setImageQuality(final int imageQuality) {
8792
this.imageQuality.onNext(imageQuality);
8893
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ private UploadItem createAndAddUploadItem(final UploadableFile uploadableFile,
102102
.getFileCreatedDate(context);
103103
long fileCreatedDate = -1;
104104
String createdTimestampSource = "";
105+
String fileCreatedDateString = "";
105106
if (dateTimeWithSource != null) {
106107
fileCreatedDate = dateTimeWithSource.getEpochDate();
108+
fileCreatedDateString = dateTimeWithSource.getDateString();
107109
createdTimestampSource = dateTimeWithSource.getSource();
108110
}
109111
Timber.d("File created date is %d", fileCreatedDate);
@@ -113,7 +115,8 @@ private UploadItem createAndAddUploadItem(final UploadableFile uploadableFile,
113115
Uri.parse(uploadableFile.getFilePath()),
114116
uploadableFile.getMimeType(context), imageCoordinates, place, fileCreatedDate,
115117
createdTimestampSource,
116-
uploadableFile.getContentUri());
118+
uploadableFile.getContentUri(),
119+
fileCreatedDateString);
117120
if (place != null) {
118121
uploadItem.getUploadMediaDetails().set(0, new UploadMediaDetail(place));
119122
}

0 commit comments

Comments
 (0)