mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +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
Binary file not shown.
35
res/layout/layout_upload_progress.xml
Normal file
35
res/layout/layout_upload_progress.xml
Normal 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
7
res/values-v9/styles.xml
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
<resources>
|
||||||
|
|
||||||
|
<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
|
||||||
|
|
||||||
|
<style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />
|
||||||
|
|
||||||
|
</resources>
|
||||||
|
|
@ -6,4 +6,13 @@
|
||||||
<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>
|
||||||
|
|
@ -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();
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
NotificationManager notificationsManager;
|
|
||||||
|
|
||||||
private Notification curNotification;
|
|
||||||
|
|
||||||
|
Notification curNotification;
|
||||||
|
public NotificationUpdateProgressListener(Notification curNotification) {
|
||||||
|
Log.d("Commons", "Fuckity");
|
||||||
|
this.curNotification = curNotification;
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
protected String doInBackground(String... params) {
|
public void onProgress(long transferred, long total) {
|
||||||
Uri imageUri = Uri.parse(params[0]);
|
double percent = transferred/total * 100;
|
||||||
String filename = params[1];
|
Log.d("Commons", "Uploaded " + percent + "% (" + transferred + " of " + total + ")");
|
||||||
String text = params[2];
|
curNotification.contentView.setProgressBar(R.id.uploadNotificationProgress, 100, (int)percent, false);
|
||||||
String comment = params[3];
|
notificationManager.notify(NOTIFICATION_DOWNLOAD_IN_PROGRESS, curNotification);
|
||||||
|
|
||||||
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
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public void onDestroy() {
|
||||||
return uploadBinder;
|
super.onDestroy();
|
||||||
|
Log.d("Commons", "ZOMG I AM BEING KILLED HALP!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public void onCreate() {
|
||||||
return Service.START_NOT_STICKY;
|
super.onCreate();
|
||||||
|
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doUpload(MWApi api, Uri imageUri, String filename, String description, String editSummary) {
|
@Override
|
||||||
UploadImageTask uploadImage = new UploadImageTask(api, this, (NotificationManager)getSystemService(NOTIFICATION_SERVICE));
|
protected void onHandleIntent(Intent intent) {
|
||||||
uploadImage.execute(imageUri.toString(), filename, description, editSummary);
|
MWApi api = ((CommonsApplication)this.getApplicationContext()).getApi();
|
||||||
|
InputStream file;
|
||||||
|
ApiResult result;
|
||||||
|
RemoteViews notificationView;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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