8000 Fix both timezone problem and saved date problem by neslihanturan · Pull Request #5019 · commons-app/apps-android-commons · GitHub
Skip to content

Fix both timezone problem and saved date problem #5019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ data class Contribution constructor(
val localUri: Uri? = null,
var dataLength: Long = 0,
var dateCreated: Date? = null,
var dateCreatedString: String? = null,
var dateModified: Date? = null,
var hasInvalidLocation : Int = 0,
var contentUri: Uri? = null,
Expand Down Expand Up @@ -67,7 +68,8 @@ data class Contribution constructor(
dateCreatedSource = "",
depictedItems = depictedItems,
wikidataPlace = from(item.place),
contentUri = item.contentUri
contentUri = item.contentUri,
dateCreatedString = item.fileCreatedDateString
)

/**
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/free/nrw/commons/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import fr.free.nrw.commons.upload.depicts.DepictsDao
* The database for accessing the respective DAOs
*
*/
@Database(entities = [Contribution::class, Depicts::class, UploadedStatus::class], version = 12, exportSchema = false)
@Database(entities = [Contribution::class, Depicts::class, UploadedStatus::class], version = 13, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun contributionDao(): ContributionDao
Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import fr.free.nrw.commons.upload.FileUtils;
Expand Down Expand Up @@ -124,13 +126,27 @@ private DateTimeWithSource getFileCreatedDateFromCP(Context context) {
private DateTimeWithSource getDateTimeFromExif() {
try {
ExifInterface exif = new ExifInterface(file.getAbsolutePath());
@SuppressLint("RestrictedApi") Long dateTime = exif.getDateTime();
String dateTimeSubString = exif.getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL);

dateTimeSubString = "2022:ss:33";
String year = dateTimeSubString.substring(0,4);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getAttribute may return null, so I think you will need to check that first.

(I would personally prefer checking the size than catching IndexOutOfBoundsException, but that's not a huge issue anyway.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a sample file without EXIF - trying to upload this gave a NullPointerException when I tested.

8000
Integer.parseInt(year);
String month = dateTimeSubString.substring(5,7);
Integer.parseInt(month);
String day = dateTimeSubString.substring(8,10);
Integer.parseInt(day);
String dateCreatedString = year+":"+month+":"+day;
@SuppressLint("RestrictedApi") Long dateTime = exif.getDateTimeOriginal();
if(dateTime != null){
Date date = new Date(dateTime);
return new DateTimeWithSource(date, DateTimeWithSource.EXIF_SOURCE);
return new DateTimeWithSource(date, dateCreatedString, DateTimeWithSource.EXIF_SOURCE);
}
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
return null;
}
Expand All @@ -149,6 +165,7 @@ public class DateTimeWithSource {
public static final String EXIF_SOURCE = "exif";

private final long epochDate;
private String dateString; // this does not includes timezone information
private final String source;

public DateTimeWithSource(long epochDate, String source) {
Expand All @@ -161,10 +178,20 @@ public DateTimeWithSource(Date date, String source) {
this.source = source;
}

public DateTimeWithSource(Date date, String dateString, String source) {
this.epochDate = date.getTime();
this.dateString = dateString;
this.source = source;
}

public long getEpochDate() {
return epochDate;
}

public String getDateString() {
return dateString;
}

public String getSource() {
return source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String createFrom(final Contribution contribution) {
.append(media.getAuthor()).append("]]\n");

final String templatizedCreatedDate = getTemplatizedCreatedDate(
contribution.getDateCreated(), contribution.getDateCreatedSource());
contribution.getDateCreatedString(), contribution.getDateCreated(), contribution.getDateCreatedSource());
if (!StringUtils.isBlank(templatizedCreatedDate)) {
buffer.append("|date=").append(templatizedCreatedDate);
}
Expand Down Expand Up @@ -90,12 +90,12 @@ public String createFrom(final Contribution contribution) {
* @param dateCreatedSource
* @return
*/
private String getTemplatizedCreatedDate(Date dateCreate 8E7A d, String dateCreatedSource) {
private String getTemplatizedCreatedDate(String dateCreatedString, Date dateCreated, String dateCreatedSource) {
if (dateCreated != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return String.format(Locale.ENGLISH,
isExif(dateCreatedSource) ? TEMPLATE_DATE_ACC_TO_EXIF : TEMPLATE_DATA_OTHER_SOURCE,
dateFormat.format(dateCreated)
isExif(dateCreatedSource) ? dateCreatedString: dateFormat.format(dateCreated)
) + "\n";
}
return "";
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/upload/UploadItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class UploadItem {
private boolean hasInvalidLocation;
private boolean isWLMUpload = false;
private String countryCode;
private String fileCreatedDateString; //according to EXIF data

/**
* Uri of uploadItem
Expand All @@ -40,7 +41,8 @@ public class UploadItem {
final Place place,
final long createdTimestamp,
final String createdTimestampSource,
final Uri contentUri) {
final Uri contentUri,
final String fileCreatedDateString) {
this.createdTimestampSource = createdTimestampSource;
uploadMediaDetails = new ArrayList<>(Collections.singletonList(new UploadMediaDetail()));
this.place = place;
Expand All @@ -50,6 +52,7 @@ public class UploadItem {
this.createdTimestamp = createdTimestamp;
this.contentUri = contentUri;
imageQuality = BehaviorSubject.createDefault(ImageUtils.IMAGE_WAIT);
this.fileCreatedDateString = fileCreatedDateString;
}

public String getCreatedTimestampSource() {
Expand Down Expand Up @@ -83,6 +86,8 @@ public int getImageQuality() {
*/
public Uri getContentUri() { return contentUri; }

public String getFileCreatedDateString() { return fileCreatedDateString; }

public void setImageQuality(final int imageQuality) {
this.imageQuality.onNext(imageQuality);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ private UploadItem createAndAddUploadItem(final UploadableFile uploadableFile,
.getFileCreatedDate(context);
long fileCreatedDate = -1;
String createdTimestampSource = "";
String fileCreatedDateString = "";
if (dateTimeWithSource != null) {
fileCreatedDate = dateTimeWithSource.getEpochDate();
fileCreatedDateString = dateTimeWithSource.getDateString();
createdTimestampSource = dateTimeWithSource.getSource();
}
Timber.d("File created date is %d", fileCreatedDate);
Expand All @@ -113,7 +115,8 @@ private UploadItem createAndAddUploadItem(final UploadableFile uploadableFile,
Uri.parse(uploadableFile.getFilePath()),
uploadableFile.getMimeType(context), imageCoordinates, place, fileCreatedDate,
createdTimestampSource,
uploadableFile.getContentUri());
uploadableFile.getContentUri(),
fileCreatedDateString);
if (place != null) {
uploadItem.getUploadMediaDetails().set(0, new UploadMediaDetail(place));
}
Expand Down