mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 21:33:53 +01:00
* Fix update date template based on date source * Add javadocs
This commit is contained in:
parent
083a7a80f0
commit
546322ac35
4 changed files with 151 additions and 19 deletions
|
|
@ -6,7 +6,16 @@ import android.net.Uri;
|
|||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.drew.imaging.ImageMetadataReader;
|
||||
import com.drew.imaging.ImageProcessingException;
|
||||
import com.drew.metadata.Metadata;
|
||||
import com.drew.metadata.exif.ExifSubIFDDirectory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import fr.free.nrw.commons.upload.FileUtils;
|
||||
|
||||
|
|
@ -62,16 +71,32 @@ public class UploadableFile implements Parcelable {
|
|||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* First try to get the file creation date from EXIF else fall back to CP
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
@Nullable
|
||||
public DateTimeWithSource getFileCreatedDate(Context context) {
|
||||
DateTimeWithSource dateTimeFromExif = getDateTimeFromExif();
|
||||
if (dateTimeFromExif == null) {
|
||||
return getFileCreatedDateFromCP(context);
|
||||
} else {
|
||||
return dateTimeFromExif;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filePath creation date from uri from all possible content providers
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public long getFileCreatedDate(Context context) {
|
||||
private DateTimeWithSource getFileCreatedDateFromCP(Context context) {
|
||||
try {
|
||||
Cursor cursor = context.getContentResolver().query(contentUri, null, null, null, null);
|
||||
if (cursor == null) {
|
||||
return -1;//Could not fetch last_modified
|
||||
return null;//Could not fetch last_modified
|
||||
}
|
||||
//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
|
||||
int lastModifiedColumnIndex = cursor.getColumnIndex("last_modified");//If gallery is opened from in app
|
||||
|
|
@ -80,18 +105,69 @@ public class UploadableFile implements Parcelable {
|
|||
}
|
||||
//If both the content providers do not give the data, lets leave it to Jesus
|
||||
if (lastModifiedColumnIndex == -1) {
|
||||
return -1l;
|
||||
return null;
|
||||
}
|
||||
cursor.moveToFirst();
|
||||
return cursor.getLong(lastModifiedColumnIndex);
|
||||
return new DateTimeWithSource(cursor.getLong(lastModifiedColumnIndex), DateTimeWithSource.CP_SOURCE);
|
||||
} catch (Exception e) {
|
||||
return -1;////Could not fetch last_modified
|
||||
return null;////Could not fetch last_modified
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filePath creation date from uri from EXIF
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private DateTimeWithSource getDateTimeFromExif() {
|
||||
Metadata metadata;
|
||||
try {
|
||||
metadata = ImageMetadataReader.readMetadata(file);
|
||||
ExifSubIFDDirectory directory = metadata.getFirstDirectoryOfType(ExifSubIFDDirectory.class);
|
||||
if (directory!=null && directory.containsTag(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL)) {
|
||||
Date date = directory.getDate(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
|
||||
return new DateTimeWithSource(date, DateTimeWithSource.EXIF_SOURCE);
|
||||
}
|
||||
} catch (ImageProcessingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel parcel, int i) {
|
||||
parcel.writeParcelable(contentUri, 0);
|
||||
parcel.writeSerializable(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* This class contains the epochDate along with the source from which it was extracted
|
||||
*/
|
||||
public class DateTimeWithSource {
|
||||
public static final String CP_SOURCE = "contentProvider";
|
||||
public static final String EXIF_SOURCE = "exif";
|
||||
|
||||
private final long epochDate;
|
||||
private final String source;
|
||||
|
||||
public DateTimeWithSource(long epochDate, String source) {
|
||||
this.epochDate = epochDate;
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public DateTimeWithSource(Date date, String source) {
|
||||
this.epochDate = date.getTime();
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
public long getEpochDate() {
|
||||
return epochDate;
|
||||
}
|
||||
|
||||
public String getSource() {
|
||||
return source;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue