mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Upload files in the background, untied to any Activity!
This commit is contained in:
parent
30021b26d6
commit
5beb650063
4 changed files with 144 additions and 80 deletions
|
|
@ -28,6 +28,7 @@
|
||||||
<data android:mimeType="image/*" />
|
<data android:mimeType="image/*" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
<service android:name="UploadService"></service>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
@ -18,5 +18,6 @@
|
||||||
<string name="uploading_message">More witty text here!</string>
|
<string name="uploading_message">More witty text here!</string>
|
||||||
<string name="uploading_success">Upload succeeded!</string>
|
<string name="uploading_success">Upload succeeded!</string>
|
||||||
<string name="uploading_failed">Upload failed!</string>
|
<string name="uploading_failed">Upload failed!</string>
|
||||||
|
<string name="uploading_started">Upload started!</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
@ -4,13 +4,17 @@ 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;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.IBinder;
|
||||||
import android.app.*;
|
import android.app.*;
|
||||||
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.ServiceConnection;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.*;
|
import android.view.*;
|
||||||
import android.widget.*;
|
import android.widget.*;
|
||||||
|
|
@ -27,84 +31,6 @@ public class ShareActivity extends Activity {
|
||||||
|
|
||||||
private Uri imageUri;
|
private Uri imageUri;
|
||||||
|
|
||||||
class UploadImageTask extends AsyncTask<String, Integer, String> {
|
|
||||||
MWApi api;
|
|
||||||
Activity context;
|
|
||||||
ProgressDialog dialog;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String doInBackground(String... params) {
|
|
||||||
Uri imageUri = Uri.parse(params[0]);
|
|
||||||
String filename = params[1];
|
|
||||||
String text = params[2];
|
|
||||||
String comment = params[3];
|
|
||||||
|
|
||||||
InputStream file;
|
|
||||||
|
|
||||||
try {
|
|
||||||
file = context.getContentResolver().openInputStream(imageUri);
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d("Commons", "isLoggedIn? " + api.isLoggedIn);
|
|
||||||
|
|
||||||
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
|
|
||||||
// }
|
|
||||||
|
|
||||||
// DOMImplementationLS domImplLS = (DOMImplementationLS) document
|
|
||||||
// .getImplementation();
|
|
||||||
// LSSerializer serializer = domImplLS.createLSSerializer();
|
|
||||||
// String str = serializer.writeToString(result.getDocument());
|
|
||||||
// return str;
|
|
||||||
return result.getString("/api/upload/@result");
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return "Failure";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
UploadImageTask(MWApi api, Activity context) {
|
|
||||||
this.api = api;
|
|
||||||
this.context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(String result) {
|
|
||||||
super.onPostExecute(result);
|
|
||||||
if(result.equals("Success")) {
|
|
||||||
dialog.hide();
|
|
||||||
Toast successToast = Toast.makeText(context, R.string.uploading_success, Toast.LENGTH_LONG);
|
|
||||||
successToast.show();
|
|
||||||
context.finish();
|
|
||||||
} else {
|
|
||||||
dialog.hide();
|
|
||||||
Toast failureToast = Toast.makeText(context, R.string.uploading_failed, Toast.LENGTH_LONG);
|
|
||||||
failureToast.show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPreExecute() {
|
|
||||||
super.onPreExecute();
|
|
||||||
dialog = new ProgressDialog(context);
|
|
||||||
dialog.setTitle(R.string.uploading_title);
|
|
||||||
dialog.setMessage(getString(R.string.uploading_message));
|
|
||||||
dialog.show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
@ -134,9 +60,24 @@ 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) {
|
||||||
UploadImageTask uploadImage = new UploadImageTask(app.getApi(), that);
|
|
||||||
Log.d("Commons", "Starting upload, yo!");
|
Log.d("Commons", "Starting upload, yo!");
|
||||||
uploadImage.execute(imageUri.toString(), titleEdit.getText().toString(), descEdit.getText().toString(), "Mobile Upload represent!");
|
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");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
121
src/org/wikimedia/commons/UploadService.java
Normal file
121
src/org/wikimedia/commons/UploadService.java
Normal file
|
|
@ -0,0 +1,121 @@
|
||||||
|
package org.wikimedia.commons;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
import org.mediawiki.api.*;
|
||||||
|
|
||||||
|
import android.app.*;
|
||||||
|
import android.content.*;
|
||||||
|
import android.os.*;
|
||||||
|
import android.support.v4.app.NotificationCompat;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.Toast;
|
||||||
|
import android.net.*;
|
||||||
|
|
||||||
|
public class UploadService extends Service {
|
||||||
|
|
||||||
|
public static final int NOTIFICATION_DOWNLOAD_IN_PROGRESS = 1;
|
||||||
|
|
||||||
|
class UploadImageTask extends AsyncTask<String, Integer, String> {
|
||||||
|
MWApi api;
|
||||||
|
Context context;
|
||||||
|
NotificationManager notificationsManager;
|
||||||
|
|
||||||
|
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];
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue