mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Refactored StartUploadActivity to it's own separate class
This commit is contained in:
parent
10b7a97f66
commit
b9f6c65f45
2 changed files with 99 additions and 46 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package org.wikimedia.commons;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.ComponentName;
|
||||
import android.content.ServiceConnection;
|
||||
import android.database.Cursor;
|
||||
|
|
@ -11,6 +12,7 @@ import android.text.TextWatcher;
|
|||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import org.wikimedia.commons.*;
|
||||
import org.wikimedia.commons.auth.AuthenticatedActivity;
|
||||
import org.wikimedia.commons.auth.WikiAccountAuthenticator;
|
||||
|
||||
|
|
@ -64,60 +66,20 @@ public class ShareActivity extends AuthenticatedActivity {
|
|||
};
|
||||
|
||||
|
||||
private class StartUploadTask extends AsyncTask<Void, Void, Contribution> {
|
||||
private class SingleStartUploadTask extends StartUploadTask {
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
Toast startingToast = Toast.makeText(getApplicationContext(), R.string.uploading_started, Toast.LENGTH_LONG);
|
||||
startingToast.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Contribution doInBackground(Void... voids) {
|
||||
String title = titleEdit.getText().toString();
|
||||
String description = descEdit.getText().toString();
|
||||
|
||||
Date dateCreated = null;
|
||||
|
||||
Long length = null;
|
||||
try {
|
||||
length = getContentResolver().openAssetFileDescriptor(mediaUri, "r").getLength();
|
||||
if(length == -1) {
|
||||
// Let us find out the long way!
|
||||
length = Utils.countBytes(getContentResolver().openInputStream(mediaUri));
|
||||
}
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
|
||||
|
||||
if(extension != null && !title.toLowerCase().endsWith(extension.toLowerCase())) {
|
||||
title += "." + extension;
|
||||
}
|
||||
|
||||
Log.d("Commons", "Title is " + title + " mimetype is " + mimeType);
|
||||
|
||||
if(mimeType.startsWith("image/")) {
|
||||
Cursor cursor = getContentResolver().query(mediaUri,
|
||||
new String[]{MediaStore.Images.ImageColumns.DATE_TAKEN}, null, null, null);
|
||||
if(cursor != null && cursor.getCount() != 0) {
|
||||
cursor.moveToFirst();
|
||||
dateCreated = new Date(cursor.getLong(0));
|
||||
} // FIXME: Alternate way of setting dateCreated if this data is not found
|
||||
}
|
||||
Contribution contribution = new Contribution(mediaUri, null, title, description, length, dateCreated, null, app.getCurrentAccount().name, CommonsApplication.DEFAULT_EDIT_SUMMARY);
|
||||
contribution.setSource(source);
|
||||
return contribution;
|
||||
private SingleStartUploadTask(Activity context, UploadService uploadService, String rawTitle, Uri mediaUri, String description, String mimeType, String source) {
|
||||
super(context, uploadService, rawTitle, mediaUri, description, mimeType, source);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Contribution contribution) {
|
||||
uploadService.queue(UploadService.ACTION_UPLOAD_FILE, contribution);
|
||||
super.onPostExecute(contribution);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
super.onBackPressed();
|
||||
|
|
@ -157,7 +119,7 @@ public class ShareActivity extends AuthenticatedActivity {
|
|||
uploadButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
StartUploadTask task = new StartUploadTask();
|
||||
StartUploadTask task = new SingleStartUploadTask(ShareActivity.this, uploadService, titleEdit.getText().toString(), mediaUri, descEdit.getText().toString(), mimeType, source);
|
||||
task.execute();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,91 @@
|
|||
package org.wikimedia.commons;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.widget.Toast;
|
||||
import org.wikimedia.commons.CommonsApplication;
|
||||
import org.wikimedia.commons.R;
|
||||
import org.wikimedia.commons.UploadService;
|
||||
import org.wikimedia.commons.Utils;
|
||||
import org.wikimedia.commons.contributions.Contribution;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
public class StartUploadTask extends AsyncTask<Void, Void, Contribution> {
|
||||
|
||||
private Activity context;
|
||||
private String rawTitle;
|
||||
private Uri mediaUri;
|
||||
private String description;
|
||||
private String mimeType;
|
||||
private String source;
|
||||
private UploadService uploadService;
|
||||
private CommonsApplication app;
|
||||
|
||||
public StartUploadTask(Activity context, UploadService uploadService, String rawTitle, Uri mediaUri, String description, String mimeType, String source) {
|
||||
this.context = context;
|
||||
this.rawTitle = rawTitle;
|
||||
this.mediaUri = mediaUri;
|
||||
this.description = description;
|
||||
this.mimeType = mimeType;
|
||||
this.source = source;
|
||||
this.uploadService = uploadService;
|
||||
|
||||
app = (CommonsApplication)context.getApplicationContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
Toast startingToast = Toast.makeText(context.getApplicationContext(), R.string.uploading_started, Toast.LENGTH_LONG);
|
||||
startingToast.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Contribution doInBackground(Void... voids) {
|
||||
String title = rawTitle;
|
||||
|
||||
Date dateCreated = null;
|
||||
|
||||
Long length = null;
|
||||
try {
|
||||
length = context.getContentResolver().openAssetFileDescriptor(mediaUri, "r").getLength();
|
||||
if(length == -1) {
|
||||
// Let us find out the long way!
|
||||
length = Utils.countBytes(context.getContentResolver().openInputStream(mediaUri));
|
||||
}
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
|
||||
|
||||
if(extension != null && !title.toLowerCase().endsWith(extension.toLowerCase())) {
|
||||
title += "." + extension;
|
||||
}
|
||||
|
||||
Log.d("Commons", "Title is " + title + " mimetype is " + mimeType);
|
||||
|
||||
if(mimeType.startsWith("image/")) {
|
||||
Cursor cursor = context.getContentResolver().query(mediaUri,
|
||||
new String[]{MediaStore.Images.ImageColumns.DATE_TAKEN}, null, null, null);
|
||||
if(cursor != null && cursor.getCount() != 0) {
|
||||
cursor.moveToFirst();
|
||||
dateCreated = new Date(cursor.getLong(0));
|
||||
} // FIXME: Alternate way of setting dateCreated if this data is not found
|
||||
}
|
||||
Contribution contribution = new Contribution(mediaUri, null, title, description, length, dateCreated, null, app.getCurrentAccount().name, CommonsApplication.DEFAULT_EDIT_SUMMARY);
|
||||
contribution.setSource(source);
|
||||
return contribution;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Contribution contribution) {
|
||||
uploadService.queue(UploadService.ACTION_UPLOAD_FILE, contribution);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue