Skip to content

When uploading several files at once the date is missing #1854 #1979

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -77,6 +77,7 @@ public class CommonsApplication extends Application {
public static final String FEEDBACK_EMAIL_SUBJECT = "Commons Android App (%s) Feedback";

public static final String NOTIFICATION_CHANNEL_ID_ALL = "CommonsNotificationAll";
public static final CharSequence NOTIFICATION_CHANNEL_TITLE = "Commons App";

/**
* Constants End
Expand Down
29 changes: 29 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.ContextCompat;
Expand Down Expand Up @@ -190,4 +192,31 @@ public static Bitmap getScreenShot(View view) {
return bitmap;
}

/**
* returns absolute path from uri
* @param uri
* @return
*/
public static String getImageFileAbsolutePath(Context context, Uri uri) {
String path = null, image_id = null;

Cursor cursor1 = context.getContentResolver().query(uri, null, null, null, null);
if (cursor1 != null) {
cursor1.moveToFirst();
image_id = cursor1.getString(0);
image_id = image_id.substring(image_id.lastIndexOf(":") + 1);
cursor1.close();
}

Cursor cursor = context.getContentResolver()
.query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,
MediaStore.Images.Media._ID + " = ? ", new String[] { image_id }, null);
if (cursor != null) {
cursor.moveToFirst();
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}
return path;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,46 @@ public Contribution[] newArray(int i) {
private Uri contentUri;
private String source;
private String editSummary;
private Date timestamp;
private int state;
private long transferred;
private String decimalCoords;
private boolean isMultiple;
private String wikiDataEntityId;
private Uri contentProviderUri;

public Contribution(){

}

public Contribution(Uri contentUri, String filename, Uri localUri, String imageUrl, Date timestamp,
int state, long dataLength, Date dateUploaded, long transferred,
String source, String description, String creator, boolean isMultiple,
int width, int height, String license) {
super(localUri, imageUrl, filename, description, dataLength, timestamp, dateUploaded, creator);
this.contentUri = contentUri;
this.state = state;
this.timestamp = timestamp;
this.dateCreated = timestamp;
this.transferred = transferred;
this.source = source;
this.isMultiple = isMultiple;
this.width = width;
this.height = height;
this.license = license;
this.dateUploaded=dateUploaded;
}

public Contribution(Uri localUri, String imageUrl, String filename, String description, long dataLength,
Date dateCreated, Date dateUploaded, String creator, String editSummary, String decimalCoords) {
super(localUri, imageUrl, filename, description, dataLength, dateCreated, dateUploaded, creator);
this.decimalCoords = decimalCoords;
this.editSummary = editSummary;
timestamp = new Date(System.currentTimeMillis());
}

public Contribution(Parcel in) {
super(in);
contentUri = in.readParcelable(Uri.class.getClassLoader());
source = in.readString();
timestamp = (Date) in.readSerializable();
dateCreated = (Date) in.readSerializable();
state = in.readInt();
transferred = in.readLong();
isMultiple = in.readInt() == 1;
Expand All @@ -87,7 +90,7 @@ public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeParcelable(contentUri, flags);
parcel.writeString(source);
parcel.writeSerializable(timestamp);
parcel.writeSerializable(dateCreated);
parcel.writeInt(state);
parcel.writeLong(transferred);
parcel.writeInt(isMultiple ? 1 : 0);
Expand Down Expand Up @@ -121,14 +124,6 @@ public void setContentUri(Uri contentUri) {
this.contentUri = contentUri;
}

public Date getTimestamp() {
return timestamp;
}

public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}

public int getState() {
return state;
}
Expand Down Expand Up @@ -183,10 +178,6 @@ public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

public Contribution() {
timestamp = new Date(System.currentTimeMillis());
}

public String getSource() {
return source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ ContentValues toContentValues(Contribution contribution) {
cv.put(Table.COLUMN_UPLOADED, contribution.getDateUploaded().getTime());
}
cv.put(Table.COLUMN_LENGTH, contribution.getDataLength());
cv.put(Table.COLUMN_TIMESTAMP, contribution.getTimestamp().getTime());
cv.put(Table.COLUMN_TIMESTAMP, contribution.getDateCreated().getTime());//This is the date created column [Previously it used to store uploaded date giving wrong created date in case of retries]
cv.put(Table.COLUMN_STATE, contribution.getState());
cv.put(Table.COLUMN_TRANSFERRED, contribution.getTransferred());
cv.put(Table.COLUMN_SOURCE, contribution.getSource());
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/upload/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ private static boolean isMediaDocument(Uri uri) {
* Check if the URI is owned by the current app.
*/
public static boolean isSelfOwned(Context context, Uri uri) {
if(null == uri.getAuthority()){
return false;
}
return uri.getAuthority().equals(context.getPackageName() + ".provider");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.DataSetObserver;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -30,8 +31,11 @@
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.single.BasePermissionListener;

import fr.free.nrw.commons.Utils;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
Expand Down Expand Up @@ -385,16 +389,25 @@ private void prepareMultipleUploadList() {
photosList = new ArrayList<>();
ArrayList<Uri> urisList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (int i = 0; i < urisList.size(); i++) {
Contribution up = new Contribution();
Uri uri = urisList.get(i);
// Use temporarily saved file Uri instead
uri = ContributionUtils.saveFileBeingUploadedTemporarily(this, uri);
up.setLocalUri(uri);
Uri contentUri = urisList.get(i);
Uri absoluteUri=contentUri = Uri.parse(Utils.getImageFileAbsolutePath(this,contentUri));
File file=new File(absoluteUri.toString());
Contribution up=new Contribution();
up.setDateCreated(new Date(file.lastModified()));//Set the date created
up.setLocalUri(absoluteUri);
up.setTag("mimeType", intent.getType());
up.setTag("sequence", i);
up.setSource(Contribution.SOURCE_EXTERNAL);
//Lets add the image dimensions as well..does not hurt to add it.
try {
ExifInterface exifInterface=new ExifInterface(absoluteUri.toString());
up.setHeight(Integer.parseInt(exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH)));
up.setWidth(Integer.parseInt(exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH)));
} catch (Exception e) {
e.printStackTrace();
}
up.setMultiple(true);
String imageGpsCoordinates = extractImageGpsData(uri);
String imageGpsCoordinates = extractImageGpsData(contentUri);
if (imageGpsCoordinates != null) {
Timber.d("GPS data for image found!");
up.setDecimalCoords(imageGpsCoordinates);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.facebook.drawee.view.SimpleDraweeView;
import com.github.chrisbanes.photoview.PhotoView;

import fr.free.nrw.commons.Utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -125,7 +126,6 @@ public class ShareActivity
private String mimeType;
private CategorizationFragment categorizationFragment;
private Uri mediaUri;
private Uri contentProviderUri;
private Contribution contribution;
private GPSExtractor gpsObj;
private String decimalCoords;
Expand Down Expand Up @@ -214,7 +214,7 @@ private void uploadBegins() {
Timber.d("Cache the categories found");
}

uploadController.startUpload(title, contentProviderUri, mediaUri, description, mimeType, source, decimalCoords, wikiDataEntityId, c -> {
uploadController.startUpload(title,mediaUri, description, mimeType, source, decimalCoords, wikiDataEntityId, c -> {
ShareActivity.this.contribution = c;
showPostUpload();
});
Expand Down Expand Up @@ -354,9 +354,8 @@ private void receiveImageIntent() {

if (Intent.ACTION_SEND.equals(intent.getAction())) {
mediaUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
contentProviderUri = mediaUri;
mediaUri = ContributionUtils.saveFileBeingUploadedTemporarily(this, mediaUri);

String imageFilePath = Utils.getImageFileAbsolutePath(this, mediaUri);
mediaUri=Uri.parse(imageFilePath);
if (intent.hasExtra(UploadService.EXTRA_SOURCE)) {
source = intent.getStringExtra(UploadService.EXTRA_SOURCE);
} else {
Expand Down
45 changes: 7 additions & 38 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadController.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,29 @@ public void cleanup() {
* @param wikiDataEntityId
* @param onComplete the progress tracker
*/
public void startUpload(String title, Uri contentProviderUri, Uri mediaUri, String description, String mimeType, String source, String decimalCoords, String wikiDataEntityId, ContributionUploadProgress onComplete) {
public void startUpload(String title, Uri mediaUri, String description, String mimeType, String source, String decimalCoords, String wikiDataEntityId, ContributionUploadProgress onComplete) {
Contribution contribution;


//TODO: Modify this to include coords
contribution = new Contribution(mediaUri, null, title, description, -1,
null, null, sessionManager.getCurrentAccount().name,
CommonsApplication.DEFAULT_EDIT_SUMMARY, decimalCoords);


contribution.setTag("mimeType", mimeType);
contribution.setSource(source);

Timber.d("Wikidata entity ID received from Share activity is %s", wikiDataEntityId);
//TODO: Modify this to include coords
Account currentAccount = sessionManager.getCurrentAccount();
if(currentAccount == null) {
Timber.d("Current account is null");
ViewUtil.showLongToast(context, context.getString(R.string.user_not_logged_in));
sessionManager.forceLogin(context);
return;
}
String fileAbsolutePath = mediaUri.toString();
File file=new File(fileAbsolutePath);
long lastModified = file.lastModified();
Date lastModifiedDate=new Date(lastModified);
contribution = new Contribution(mediaUri, null, title, description, -1,
null, null, currentAccount.name,
lastModifiedDate, new Date(System.currentTimeMillis()), currentAccount.name,
CommonsApplication.DEFAULT_EDIT_SUMMARY, decimalCoords);


contribution.setTag("mimeType", mimeType);
contribution.setSource(source);
contribution.setWikiDataEntityId(wikiDataEntityId);
contribution.setContentProviderUri(contentProviderUri);

contribution.setContentProviderUri(mediaUri);
//Calls the next overloaded method
startUpload(contribution, onComplete);
}
Expand Down Expand Up @@ -188,36 +179,14 @@ protected Contribution doInBackground(Void... voids /* stare into you */) {
}

String mimeType = (String) contribution.getTag("mimeType");
Boolean imagePrefix = false;

if (mimeType == null || TextUtils.isEmpty(mimeType) || mimeType.endsWith("*")) {
mimeType = contentResolver.getType(contribution.getLocalUri());
}

if (mimeType != null) {
contribution.setTag("mimeType", mimeType);
imagePrefix = mimeType.startsWith("image/");
Timber.d("MimeType is: %s", mimeType);
}

if (imagePrefix && contribution.getDateCreated() == null) {
Timber.d("local uri " + contribution.getLocalUri());
Cursor cursor = contentResolver.query(contribution.getLocalUri(),
new String[]{MediaStore.Images.ImageColumns.DATE_TAKEN}, null, null, null);
if (cursor != null && cursor.getCount() != 0 && cursor.getColumnCount() != 0) {
cursor.moveToFirst();
Date dateCreated = new Date(cursor.getLong(0));
Date epochStart = new Date(0);
if (dateCreated.equals(epochStart) || dateCreated.before(epochStart)) {
// If date is incorrect (1st second of unix time) then set it to the current date
dateCreated = new Date();
}
contribution.setDateCreated(dateCreated);
cursor.close();
} else {
contribution.setDateCreated(new Date());
}
}
return contribution;
}

Expand Down
31 changes: 26 additions & 5 deletions app/src/main/java/fr/free/nrw/commons/upload/UploadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.webkit.MimeTypeMap;
Expand Down Expand Up @@ -185,16 +187,30 @@ public int onStartCommand(Intent intent, int flags, int startId) {

@SuppressLint("StringFormatInvalid")
private NotificationCompat.Builder getNotificationBuilder(Contribution contribution, String channelId) {
return new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, channelId).setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setAutoCancel(true)
.setContentTitle(getString(R.string.upload_progress_notification_title_start, contribution.getDisplayTitle()))
.setContentText(getResources().getQuantityString(R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
.setContentTitle(getString(R.string.upload_progress_notification_title_start,
contribution.getDisplayTitle()))
.setContentText(getResources().getQuantityString(
R.plurals.uploads_pending_notification_indicator, toUpload, toUpload))
.setOngoing(true)
.setProgress(100, 0, true)
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, ContributionsActivity.class), 0))
.setTicker(getString(R.string.upload_progress_notification_title_in_progress, contribution.getDisplayTitle()));
.setContentIntent(PendingIntent.getActivity(this, 0,
new Intent(this, ContributionsActivity.class), 0))
.setTicker(getString(R.string.upload_progress_notification_title_in_progress,
contribution.getDisplayTitle()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (null == notificationManager.getNotificationChannel(channelId)) {
notificationManager.createNotificationChannel(new NotificationChannel(channelId,
CommonsApplication.NOTIFICATION_CHANNEL_TITLE,
NotificationManager.IMPORTANCE_DEFAULT));
}
builder.setChannelId(channelId);
}
return builder;
}

private void uploadContribution(Contribution contribution) {
Expand All @@ -205,6 +221,11 @@ private void uploadContribution(Contribution contribution) {

try {
File file1 = new File(contribution.getLocalUri().getPath());
if(!file1.canRead()){
Timber.d("file does not have read access");
Toast.makeText(this, R.string.file_doesnot_have_read_access, Toast.LENGTH_LONG).show();
return;
}
fileInputStream = new FileInputStream(file1);
tempFileInputStream = new FileInputStream(file1);
if (contentInfoUtil == null) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,5 @@
<string name="log_collection_started">Log collection started. Please RESTART the app, perform action that you wish to log, and then tap \'Send log file\' again</string>
<string name="no_uploads">Welcome to Commons!\n
Upload your first media by touching the camera or gallery icon above.</string>
<string name="file_doesnot_have_read_access">This file doesnot have read access, Please try uploading another file.</string>
</resources>
Loading