mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Upload properly from the background. Push out notification
Also update java-mwapi to support progress callbacks
This commit is contained in:
parent
5beb650063
commit
9175eaed95
6 changed files with 150 additions and 123 deletions
|
|
@ -4,7 +4,6 @@ import java.io.*;
|
|||
|
||||
import org.mediawiki.api.ApiResult;
|
||||
import org.mediawiki.api.MWApi;
|
||||
import org.wikimedia.commons.UploadService.UploadBinder;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
|
|
@ -49,8 +48,6 @@ public class ShareActivity extends Activity {
|
|||
|
||||
Intent intent = getIntent();
|
||||
|
||||
final Activity that = this;
|
||||
|
||||
if(intent.getAction().equals(Intent.ACTION_SEND)) {
|
||||
if(intent.getType().startsWith("image/")) {
|
||||
ImageLoaderTask loader = new ImageLoaderTask(backgroundImageView);
|
||||
|
|
@ -60,31 +57,19 @@ public class ShareActivity extends Activity {
|
|||
uploadButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Log.d("Commons", "Starting upload, yo!");
|
||||
ServiceConnection connection = new ServiceConnection() {
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
||||
Log.d("Commons", "Service connected!");
|
||||
UploadService uploadService = ((UploadService.UploadBinder)service).getService();
|
||||
uploadService.doUpload(app.getApi(), imageUri, titleEdit.getText().toString(),
|
||||
descEdit.getText().toString(), "Mobile Upload represent!" );
|
||||
that.finish();
|
||||
}
|
||||
};
|
||||
that.bindService(new Intent(that, UploadService.class), connection, Context.BIND_AUTO_CREATE);
|
||||
Log.d("Commons", "Service requeseted");
|
||||
Intent uploadIntent = new Intent(getApplicationContext(), UploadService.class);
|
||||
uploadIntent.putExtra(UploadService.EXTRA_MEDIA_URI, imageUri);
|
||||
uploadIntent.putExtra(UploadService.EXTRA_TARGET_FILENAME, titleEdit.getText().toString());
|
||||
uploadIntent.putExtra(UploadService.EXTRA_PAGE_CONTENT, descEdit.getText().toString());
|
||||
uploadIntent.putExtra(UploadService.EXTRA_EDIT_SUMMARY, "Mobile upload from Wikimedia Commons Android app");
|
||||
startService(uploadIntent);
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
|
|
|||
|
|
@ -4,118 +4,109 @@ import java.io.*;
|
|||
|
||||
import org.mediawiki.api.*;
|
||||
|
||||
import de.mastacode.http.ProgressListener;
|
||||
|
||||
import android.app.*;
|
||||
import android.content.*;
|
||||
import android.os.*;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
import android.widget.RemoteViews;
|
||||
import android.widget.Toast;
|
||||
import android.net.*;
|
||||
|
||||
public class UploadService extends Service {
|
||||
public class UploadService extends IntentService {
|
||||
|
||||
private static final String EXTRA_PREFIX = "org.wikimedia.commons.uploader";
|
||||
public static final String EXTRA_MEDIA_URI = EXTRA_PREFIX + ".media_uri";
|
||||
public static final String EXTRA_TARGET_FILENAME = EXTRA_PREFIX + ".filename";
|
||||
public static final String EXTRA_PAGE_CONTENT = EXTRA_PREFIX + ".content";
|
||||
public static final String EXTRA_EDIT_SUMMARY = EXTRA_PREFIX + ".summary";
|
||||
|
||||
private NotificationManager notificationManager;
|
||||
public UploadService(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public UploadService() {
|
||||
super("UploadService");
|
||||
}
|
||||
public static final int NOTIFICATION_DOWNLOAD_IN_PROGRESS = 1;
|
||||
|
||||
class UploadImageTask extends AsyncTask<String, Integer, String> {
|
||||
MWApi api;
|
||||
Context context;
|
||||
NotificationManager notificationsManager;
|
||||
private class NotificationUpdateProgressListener implements ProgressListener {
|
||||
|
||||
Notification curNotification;
|
||||
public NotificationUpdateProgressListener(Notification curNotification) {
|
||||
Log.d("Commons", "Fuckity");
|
||||
this.curNotification = curNotification;
|
||||
}
|
||||
@Override
|
||||
public void onProgress(long transferred, long total) {
|
||||
double percent = transferred/total * 100;
|
||||
Log.d("Commons", "Uploaded " + percent + "% (" + transferred + " of " + total + ")");
|
||||
curNotification.contentView.setProgressBar(R.id.uploadNotificationProgress, 100, (int)percent, false);
|
||||
notificationManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
Log.d("Commons", "ZOMG I AM BEING KILLED HALP!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
MWApi api = ((CommonsApplication)this.getApplicationContext()).getApi();
|
||||
InputStream file;
|
||||
ApiResult result;
|
||||
RemoteViews notificationView;
|
||||
|
||||
private Notification curNotification;
|
||||
|
||||
@Override
|
||||
protected String doInBackground(String... params) {
|
||||
Uri imageUri = Uri.parse(params[0]);
|
||||
String filename = params[1];
|
||||
String text = params[2];
|
||||
String comment = params[3];
|
||||
Bundle extras = intent.getExtras();
|
||||
Uri mediaUri = (Uri)extras.getParcelable(EXTRA_MEDIA_URI);
|
||||
String filename = intent.getStringExtra(EXTRA_TARGET_FILENAME);
|
||||
String pageContents = intent.getStringExtra(EXTRA_PAGE_CONTENT);
|
||||
String editSummary = intent.getStringExtra(EXTRA_EDIT_SUMMARY);
|
||||
|
||||
try {
|
||||
file = this.getContentResolver().openInputStream(mediaUri);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
InputStream file;
|
||||
|
||||
try {
|
||||
file = context.getContentResolver().openInputStream(imageUri);
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
ApiResult result = api.upload(filename, file, text, comment);
|
||||
|
||||
// Document document = (Document)result.getDocument();
|
||||
// StringWriter wr = new StringWriter();
|
||||
// try {
|
||||
// Transformer trans = TransformerFactory.newInstance().newTransformer();
|
||||
// trans.transform(new DOMSource(result.getDocument()), new StreamResult(wr));
|
||||
// String res = wr.toString();
|
||||
// return res;
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// //FUCK YOU YOU SON OF A LEMON PARTY
|
||||
// }
|
||||
|
||||
return result.getString("/api/upload/@result");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "Failure";
|
||||
}
|
||||
}
|
||||
|
||||
UploadImageTask(MWApi api, Context context, NotificationManager notificationManager) {
|
||||
this.api = api;
|
||||
this.context = context;
|
||||
this.notificationsManager = notificationManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String result) {
|
||||
Log.d("Commons", "Done!");
|
||||
super.onPostExecute(result);
|
||||
if(result.equals("Success")) {
|
||||
Toast successToast = Toast.makeText(context, R.string.uploading_success, Toast.LENGTH_LONG);
|
||||
successToast.show();
|
||||
} else {
|
||||
Toast failureToast = Toast.makeText(context, R.string.uploading_failed, Toast.LENGTH_LONG);
|
||||
failureToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
Toast startingToast = Toast.makeText(context, R.string.uploading_started, Toast.LENGTH_LONG);
|
||||
startingToast.show();
|
||||
Log.d("Commons", "Before execution!");
|
||||
// curNotification = new NotificationCompat.Builder(context).setAutoCancel(true)
|
||||
// .setContentTitle("Starting Upload!")
|
||||
// .setContentText("Uploading!")
|
||||
// .setSmallIcon(R.drawable.ic_launcher)
|
||||
// .setAutoCancel(true)
|
||||
// .getNotification();
|
||||
//
|
||||
// notificationsManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public class UploadBinder extends Binder {
|
||||
UploadService getService() {
|
||||
return UploadService.this;
|
||||
}
|
||||
}
|
||||
|
||||
final UploadBinder uploadBinder = new UploadBinder();
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return uploadBinder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
return Service.START_NOT_STICKY;
|
||||
}
|
||||
|
||||
public void doUpload(MWApi api, Uri imageUri, String filename, String description, String editSummary) {
|
||||
UploadImageTask uploadImage = new UploadImageTask(api, this, (NotificationManager)getSystemService(NOTIFICATION_SERVICE));
|
||||
uploadImage.execute(imageUri.toString(), filename, description, editSummary);
|
||||
notificationView = new RemoteViews(getPackageName(), R.layout.layout_upload_progress);
|
||||
notificationView.setTextViewText(R.id.uploadNotificationTitle, "Uploading " + filename);
|
||||
|
||||
Log.d("Commons", "Before execution!");
|
||||
Notification curNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.setAutoCancel(true)
|
||||
.setContent(notificationView)
|
||||
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0))
|
||||
.getNotification();
|
||||
|
||||
notificationManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
|
||||
|
||||
Toast startingToast = Toast.makeText(this, R.string.uploading_started, Toast.LENGTH_LONG);
|
||||
startingToast.show();
|
||||
Log.d("Commons", "Just before");
|
||||
try {
|
||||
result = api.upload(filename, file, pageContents, editSummary, new NotificationUpdateProgressListener(curNotification));
|
||||
} catch (IOException e) {
|
||||
// Do error handling!
|
||||
Log.d("Commons", "Fuck");
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Log.d("Commons", "After");
|
||||
notificationView.setTextViewText(R.id.uploadNotificationTitle, filename + " Uploaded!");
|
||||
notificationManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue