Upload properly from the background. Push out notification

Also update java-mwapi to support progress callbacks
This commit is contained in:
YuviPanda 2012-10-13 03:41:13 +05:30
parent 5beb650063
commit 9175eaed95
6 changed files with 150 additions and 123 deletions

Binary file not shown.

View file

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<ImageView
android:id="@+id/uploadNotificationIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"
android:layout_marginRight="10dp" />
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/uploadNotificationTitle"
style="@style/NotificationTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Uploading image to commons"/>
<ProgressBar
android:id="@+id/uploadNotificationProgress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
>
</ProgressBar>
</LinearLayout>
</LinearLayout>

7
res/values-v9/styles.xml Normal file
View file

@ -0,0 +1,7 @@
<resources>
<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
<style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />
</resources>

View file

@ -5,5 +5,14 @@
<style name="NoTitle" parent="AppTheme"> <style name="NoTitle" parent="AppTheme">
<item name="android:windowNoTitle">true</item> <item name="android:windowNoTitle">true</item>
</style> </style>
<style name="NotificationText">
<item name="android:textColor">?android:attr/textColorPrimary</item>
</style>
<style name="NotificationTitle">
<item name="android:textColor">?android:attr/textColorPrimary</item>
<item name="android:textStyle">bold</item>
</style>
</resources> </resources>

View file

@ -4,7 +4,6 @@ import java.io.*;
import org.mediawiki.api.ApiResult; import org.mediawiki.api.ApiResult;
import org.mediawiki.api.MWApi; import org.mediawiki.api.MWApi;
import org.wikimedia.commons.UploadService.UploadBinder;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -49,8 +48,6 @@ public class ShareActivity extends Activity {
Intent intent = getIntent(); Intent intent = getIntent();
final Activity that = this;
if(intent.getAction().equals(Intent.ACTION_SEND)) { if(intent.getAction().equals(Intent.ACTION_SEND)) {
if(intent.getType().startsWith("image/")) { if(intent.getType().startsWith("image/")) {
ImageLoaderTask loader = new ImageLoaderTask(backgroundImageView); ImageLoaderTask loader = new ImageLoaderTask(backgroundImageView);
@ -60,31 +57,19 @@ public class ShareActivity extends Activity {
uploadButton.setOnClickListener(new View.OnClickListener() { uploadButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Log.d("Commons", "Starting upload, yo!"); Intent uploadIntent = new Intent(getApplicationContext(), UploadService.class);
ServiceConnection connection = new ServiceConnection() { uploadIntent.putExtra(UploadService.EXTRA_MEDIA_URI, imageUri);
uploadIntent.putExtra(UploadService.EXTRA_TARGET_FILENAME, titleEdit.getText().toString());
@Override uploadIntent.putExtra(UploadService.EXTRA_PAGE_CONTENT, descEdit.getText().toString());
public void onServiceDisconnected(ComponentName name) { uploadIntent.putExtra(UploadService.EXTRA_EDIT_SUMMARY, "Mobile upload from Wikimedia Commons Android app");
} startService(uploadIntent);
finish();
@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");
} }
}); });
} }
} }
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();

View file

@ -4,118 +4,109 @@ import java.io.*;
import org.mediawiki.api.*; import org.mediawiki.api.*;
import de.mastacode.http.ProgressListener;
import android.app.*; import android.app.*;
import android.content.*; import android.content.*;
import android.os.*; import android.os.*;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast; import android.widget.Toast;
import android.net.*; 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; public static final int NOTIFICATION_DOWNLOAD_IN_PROGRESS = 1;
class UploadImageTask extends AsyncTask<String, Integer, String> { private class NotificationUpdateProgressListener implements ProgressListener {
MWApi api;
Context context; Notification curNotification;
NotificationManager notificationsManager; 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; Bundle extras = intent.getExtras();
Uri mediaUri = (Uri)extras.getParcelable(EXTRA_MEDIA_URI);
@Override String filename = intent.getStringExtra(EXTRA_TARGET_FILENAME);
protected String doInBackground(String... params) { String pageContents = intent.getStringExtra(EXTRA_PAGE_CONTENT);
Uri imageUri = Uri.parse(params[0]); String editSummary = intent.getStringExtra(EXTRA_EDIT_SUMMARY);
String filename = params[1];
String text = params[2]; try {
String comment = params[3]; file = this.getContentResolver().openInputStream(mediaUri);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
InputStream file; notificationView = new RemoteViews(getPackageName(), R.layout.layout_upload_progress);
notificationView.setTextViewText(R.id.uploadNotificationTitle, "Uploading " + filename);
try {
file = context.getContentResolver().openInputStream(imageUri); Log.d("Commons", "Before execution!");
} catch (FileNotFoundException e) { Notification curNotification = new NotificationCompat.Builder(this).setAutoCancel(true)
throw new RuntimeException(e); .setSmallIcon(R.drawable.ic_launcher)
} .setAutoCancel(true)
.setContent(notificationView)
try { .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0))
ApiResult result = api.upload(filename, file, text, comment); .getNotification();
// Document document = (Document)result.getDocument(); notificationManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
// StringWriter wr = new StringWriter();
// try { Toast startingToast = Toast.makeText(this, R.string.uploading_started, Toast.LENGTH_LONG);
// Transformer trans = TransformerFactory.newInstance().newTransformer(); startingToast.show();
// trans.transform(new DOMSource(result.getDocument()), new StreamResult(wr)); Log.d("Commons", "Just before");
// String res = wr.toString(); try {
// return res; result = api.upload(filename, file, pageContents, editSummary, new NotificationUpdateProgressListener(curNotification));
// } catch (Exception e) { } catch (IOException e) {
// e.printStackTrace(); // Do error handling!
// //FUCK YOU YOU SON OF A LEMON PARTY Log.d("Commons", "Fuck");
// } e.printStackTrace();
throw new RuntimeException(e);
return result.getString("/api/upload/@result"); }
} catch (IOException e) {
e.printStackTrace(); Log.d("Commons", "After");
return "Failure"; notificationView.setTextViewText(R.id.uploadNotificationTitle, filename + " Uploaded!");
} notificationManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
}
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);
} }
} }